From b87a65c2c1f154b24ba6a8aecd1e2845e4f0aad4 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 14 Jun 2021 11:08:48 +0200 Subject: [PATCH] Do not run webpack on a single non-node function --- lib/utils.js | 11 ++++++++--- lib/validate.js | 13 +++++++++---- tests/validate.test.js | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 47c4449f0..935e868d9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -108,12 +108,16 @@ function splitLines(str) { return _.split(str, /\r?\n/); } +function isNodeRuntime(runtime) { + return runtime.match(/node/); +} + function getAllNodeFunctions() { const functions = this.serverless.service.getAllFunctions(); return _.filter(functions, funcName => { const func = this.serverless.service.getFunction(funcName); - const runtime = func.runtime || this.serverless.service.provider.runtime || 'nodejs'; - return runtime.match(/node/); + + return isNodeRuntime(func.runtime || this.serverless.service.provider.runtime || 'nodejs'); }); } @@ -125,5 +129,6 @@ module.exports = { spawnProcess, safeJsonParse, splitLines, - getAllNodeFunctions + getAllNodeFunctions, + isNodeRuntime }; diff --git a/lib/validate.js b/lib/validate.js index 32f8c890b..af5afa12e 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -7,7 +7,7 @@ const glob = require('glob'); const lib = require('./index'); const _ = require('lodash'); const Configuration = require('./Configuration'); -const { getAllNodeFunctions } = require('./utils'); +const { getAllNodeFunctions, isNodeRuntime } = require('./utils'); /** * For automatic entry detection we sort the found files to solve ambiguities. @@ -113,14 +113,19 @@ module.exports = { const functions = getAllNodeFunctions.call(this); if (this.options.function) { const serverlessFunction = this.serverless.service.getFunction(this.options.function); - const entry = getEntryForFunction.call(this, this.options.function, serverlessFunction); - _.merge(entries, entry); + const runtime = serverlessFunction.runtime || this.serverless.service.provider.runtime; + + // only package that lonely function if it's a node function + if (isNodeRuntime(runtime)) { + const entry = getEntryForFunction.call(this, this.options.function, serverlessFunction); + _.merge(entries, entry); + } } else { _.forEach(functions, (func, index) => { const loadedFunc = this.serverless.service.getFunction(func); const runtime = loadedFunc.runtime || this.serverless.service.provider.runtime || 'nodejs'; - if (runtime.match(/node/)) { + if (isNodeRuntime(runtime)) { // runtimes can be 'nodejsX.Y' (AWS, Azure) or 'google-nodejs' (Google Cloud) const entry = getEntryForFunction.call(this, functions[index], loadedFunc); _.merge(entries, entry); diff --git a/tests/validate.test.js b/tests/validate.test.js index 7eca04b6f..f7eea7aa4 100644 --- a/tests/validate.test.js +++ b/tests/validate.test.js @@ -596,6 +596,31 @@ describe('validate', () => { }); }); + it('should ignore the requested function if `options.function` is defined and the function is not a node one', () => { + const testOutPath = 'test'; + const testFunction = 'func5'; + const testConfig = { + entry: 'test', + context: 'testcontext', + output: { + path: testOutPath + } + }; + _.set(module.serverless.service, 'custom.webpack.config', testConfig); + module.serverless.service.functions = testFunctionsConfig; + module.options.function = testFunction; + globSyncStub.callsFake(filename => [_.replace(filename, '*', 'js')]); + return expect(module.validate()).to.be.fulfilled.then(() => { + const lib = require('../lib/index'); + const expectedLibEntries = {}; + + expect(lib.entries).to.deep.equal(expectedLibEntries); + expect(globSyncStub).to.not.have.been.called; + expect(serverless.cli.log).to.not.have.been.called; + return null; + }); + }); + it('should ignore non-node runtimes', () => { const testOutPath = 'test'; const testFunctionsConfig = {