Skip to content

Commit

Permalink
Do not run webpack on a single non-node function
Browse files Browse the repository at this point in the history
  • Loading branch information
j0k3r committed Jun 14, 2021
1 parent 1498206 commit 817dc53
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
15 changes: 12 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,20 @@ 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/);

if (func.image && func.image.uri) {
return false;
}

return isNodeRuntime(func.runtime || this.serverless.service.provider.runtime || 'nodejs');
});
}

Expand All @@ -125,5 +133,6 @@ module.exports = {
spawnProcess,
safeJsonParse,
splitLines,
getAllNodeFunctions
getAllNodeFunctions,
isNodeRuntime
};
13 changes: 9 additions & 4 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions tests/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 817dc53

Please sign in to comment.