Skip to content

Commit

Permalink
fix: fallback to sls when runtime is not node, at service and fn level
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioramos committed Apr 9, 2021
1 parent 6aaf974 commit eb25809
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 104 deletions.
34 changes: 27 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const pkgUp = require('pkg-up');

const zipService = require('serverless/lib/plugins/package/lib/zipService');
const packageService = require('serverless/lib/plugins/package/lib/packageService');
const NativePkg = require('serverless/lib/plugins/package/package');

const EXTENSIONS = ['.tsx', '.ts', '.node', '.mjs', '.cjs', '.js'];
const NODE_BUILTINS = [
Expand Down Expand Up @@ -117,6 +118,8 @@ module.exports = class {
this.files = [];
this.syms = [];

this.nativeService = new NativePkg(serverless, options);

if (options.package) {
this.packagePath = options.package;
} else if (serverless.service.package.path) {
Expand Down Expand Up @@ -260,11 +263,31 @@ module.exports = class {
}

// from: https://github.com/serverless/serverless/blob/f93b27bf684d9a14b1e67ec554a7719ca3628135/lib/plugins/package/lib/zipService.js#L65-L116
async zipFiles(files, zipFileName, prefix, filesToChmodPlusX) {
async zipFiles(files, zipFileName, prefix, filesToChmodPlusX, ...args) {
if (files.length === 0) {
throw new this.serverless.classes.Error('No files to package');
}

const { service } = this.serverless;
const fn = (() => {
try {
return service.getFunction(zipFileName.replace(/\.zip$/, ''));
} catch {
return {};
}
})();

const runtime = fn.runtime || service.provider.runtime;
if (runtime && !/^nodejs/.test(runtime)) {
return this.nativeService.zipFiles(
files,
zipFileName,
prefix,
filesToChmodPlusX,
...args,
);
}

const zip = archiver.create('zip');
// Create artifact in temp path and move it to the package path (if any) later
const artifactFilePath = resolve(
Expand Down Expand Up @@ -397,15 +420,12 @@ module.exports = class {

async resolveFilePathsFunction(fnName, ...args) {
const { service } = this.serverless;
const { package: pkg = {}, handler, runtime } = service.getFunction(fnName);
const { package: pkg = {}, handler, ...fn } = service.getFunction(fnName);
const { include = [], exclude = [] } = pkg;

const runtime = fn.runtime || service.provider.runtime;
if (runtime && !/^nodejs/.test(runtime)) {
return packageService.resolveFilePathsFunction.call(
this,
fnName,
...args,
);
return this.nativeService.resolveFilePathsFunction(fnName, ...args);
}

const { excludes, includes } = await Parallel({
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/js-nm-individually/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "serverless-plugin-tree-shake-ws-all",
"name": "js-nm-individually",
"private": true,
"devDependencies": {
"@zeit/ncc": "^0.22.3",
Expand Down
20 changes: 10 additions & 10 deletions test/__fixtures__/js-nm-individually/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,16 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"js-nm-individually@workspace:.":
version: 0.0.0-use.local
resolution: "js-nm-individually@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
languageName: unknown
linkType: soft

"js-string-escape@npm:^1.0.1":
version: 1.0.1
resolution: "js-string-escape@npm:1.0.1"
Expand Down Expand Up @@ -8297,16 +8307,6 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"serverless-plugin-tree-shake-ws-all@workspace:.":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake-ws-all@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
languageName: unknown
linkType: soft

"serverless-plugin-tree-shake@workspace:plugin":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake@workspace:plugin"
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/js-nm-single/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "serverless-plugin-tree-shake-ws-all",
"name": "js-nm-single",
"private": true,
"devDependencies": {
"@zeit/ncc": "^0.22.3",
Expand Down
20 changes: 10 additions & 10 deletions test/__fixtures__/js-nm-single/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,16 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"js-nm-single@workspace:.":
version: 0.0.0-use.local
resolution: "js-nm-single@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
languageName: unknown
linkType: soft

"js-string-escape@npm:^1.0.1":
version: 1.0.1
resolution: "js-string-escape@npm:1.0.1"
Expand Down Expand Up @@ -8297,16 +8307,6 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"serverless-plugin-tree-shake-ws-all@workspace:.":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake-ws-all@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
languageName: unknown
linkType: soft

"serverless-plugin-tree-shake@workspace:plugin":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake@workspace:plugin"
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/js-pnp-individually/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "serverless-plugin-tree-shake-ws-all",
"name": "js-pnp-individually",
"private": true,
"devDependencies": {
"@zeit/ncc": "^0.22.3",
Expand Down
20 changes: 10 additions & 10 deletions test/__fixtures__/js-pnp-individually/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,16 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"js-pnp-individually@workspace:.":
version: 0.0.0-use.local
resolution: "js-pnp-individually@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
languageName: unknown
linkType: soft

"js-string-escape@npm:^1.0.1":
version: 1.0.1
resolution: "js-string-escape@npm:1.0.1"
Expand Down Expand Up @@ -8297,16 +8307,6 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"serverless-plugin-tree-shake-ws-all@workspace:.":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake-ws-all@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
languageName: unknown
linkType: soft

"serverless-plugin-tree-shake@workspace:plugin":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake@workspace:plugin"
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/js-pnp-single/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "serverless-plugin-tree-shake-ws-all",
"name": "js-pnp-single",
"private": true,
"devDependencies": {
"@zeit/ncc": "^0.22.3",
Expand Down
20 changes: 10 additions & 10 deletions test/__fixtures__/js-pnp-single/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,16 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"js-pnp-single@workspace:.":
version: 0.0.0-use.local
resolution: "js-pnp-single@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
languageName: unknown
linkType: soft

"js-string-escape@npm:^1.0.1":
version: 1.0.1
resolution: "js-string-escape@npm:1.0.1"
Expand Down Expand Up @@ -8297,16 +8307,6 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"serverless-plugin-tree-shake-ws-all@workspace:.":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake-ws-all@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
languageName: unknown
linkType: soft

"serverless-plugin-tree-shake@workspace:plugin":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake@workspace:plugin"
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/ts-nm-individually/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "serverless-plugin-tree-shake-ts-ws-individual",
"name": "ts-nm-individually",
"private": true,
"devDependencies": {
"@zeit/ncc": "^0.22.3",
Expand Down
24 changes: 12 additions & 12 deletions test/__fixtures__/ts-nm-individually/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8547,18 +8547,6 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"serverless-plugin-tree-shake-ts-ws-individual@workspace:.":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake-ts-ws-individual@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
serverless-plugin-typescript: ^1.1.9
typescript: ^4.1.3
languageName: unknown
linkType: soft

"serverless-plugin-tree-shake@workspace:plugin":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake@workspace:plugin"
Expand Down Expand Up @@ -9715,6 +9703,18 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"ts-nm-individually@workspace:.":
version: 0.0.0-use.local
resolution: "ts-nm-individually@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
serverless-plugin-typescript: ^1.1.9
typescript: ^4.1.3
languageName: unknown
linkType: soft

"tslib@npm:^1.9.0":
version: 1.13.0
resolution: "tslib@npm:1.13.0"
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/ts-nm-single/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "serverless-plugin-tree-shake-ts-ws-all",
"name": "ts-nm-single",
"private": true,
"devDependencies": {
"@zeit/ncc": "^0.22.3",
Expand Down
24 changes: 12 additions & 12 deletions test/__fixtures__/ts-nm-single/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8547,18 +8547,6 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"serverless-plugin-tree-shake-ts-ws-all@workspace:.":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake-ts-ws-all@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
serverless-plugin-typescript: ^1.1.9
typescript: ^4.1.3
languageName: unknown
linkType: soft

"serverless-plugin-tree-shake@workspace:plugin":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake@workspace:plugin"
Expand Down Expand Up @@ -9715,6 +9703,18 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"ts-nm-single@workspace:.":
version: 0.0.0-use.local
resolution: "ts-nm-single@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
serverless-plugin-typescript: ^1.1.9
typescript: ^4.1.3
languageName: unknown
linkType: soft

"tslib@npm:^1.9.0":
version: 1.13.0
resolution: "tslib@npm:1.13.0"
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/ts-pnp-individually/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "serverless-plugin-tree-shake-ts-ws-individual",
"name": "ts-pnp-individually",
"private": true,
"devDependencies": {
"@zeit/ncc": "^0.22.3",
Expand Down
24 changes: 12 additions & 12 deletions test/__fixtures__/ts-pnp-individually/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8547,18 +8547,6 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"serverless-plugin-tree-shake-ts-ws-individual@workspace:.":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake-ts-ws-individual@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
serverless-plugin-typescript: ^1.1.9
typescript: ^4.1.3
languageName: unknown
linkType: soft

"serverless-plugin-tree-shake@workspace:plugin":
version: 0.0.0-use.local
resolution: "serverless-plugin-tree-shake@workspace:plugin"
Expand Down Expand Up @@ -9715,6 +9703,18 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"ts-pnp-individually@workspace:.":
version: 0.0.0-use.local
resolution: "ts-pnp-individually@workspace:."
dependencies:
"@zeit/ncc": ^0.22.3
serverless: ^2.18.0
serverless-plugin-ncc: ^0.4.0
serverless-plugin-typescript: ^1.1.9
typescript: ^4.1.3
languageName: unknown
linkType: soft

"tslib@npm:^1.9.0":
version: 1.13.0
resolution: "tslib@npm:1.13.0"
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/ts-pnp-single/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "serverless-plugin-tree-shake-ts-ws-all",
"name": "ts-pnp-single",
"private": true,
"devDependencies": {
"@zeit/ncc": "^0.22.3",
Expand Down
Loading

0 comments on commit eb25809

Please sign in to comment.