Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const prepareOfflineInvoke = require('./lib/prepareOfflineInvoke');
const prepareStepOfflineInvoke = require('./lib/prepareStepOfflineInvoke');
const packExternalModules = require('./lib/packExternalModules');
const packageModules = require('./lib/packageModules');
const { isNodeRuntime } = require('./lib/utils');
const lib = require('./lib');

class ServerlessWebpack {
Expand Down Expand Up @@ -104,11 +105,16 @@ class ServerlessWebpack {

'after:package:createDeploymentArtifacts': () => BbPromise.bind(this).then(this.cleanup),

'before:deploy:function:packageFunction': () =>
BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(() => this.serverless.pluginManager.spawn('webpack:package')),
'before:deploy:function:packageFunction': () => {
const runtime = this.serverless.service.getFunction(this.options.function).runtime;

if (isNodeRuntime(runtime)) {
return BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(() => this.serverless.pluginManager.spawn('webpack:package'));
}
},

'before:invoke:local:invoke': () =>
BbPromise.bind(this)
Expand Down
3 changes: 3 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('ServerlessWebpack', () => {
};

sandbox.stub(serverless.pluginManager, 'spawn').returns(BbPromise.resolve());
sandbox.stub(serverless.service, 'getFunction').returns({ runtime: 'nodejs12.x' });
});

afterEach(() => {
Expand Down Expand Up @@ -222,6 +223,8 @@ describe('ServerlessWebpack', () => {
name: 'before:deploy:function:packageFunction',
test: () => {
it('should spawn validate, compile and package', () => {
slsw.options.function = functionName;

return expect(slsw.hooks['before:deploy:function:packageFunction']()).to.be.fulfilled.then(() => {
expect(slsw.serverless.pluginManager.spawn).to.have.been.calledThrice;
expect(slsw.serverless.pluginManager.spawn.firstCall).to.have.been.calledWithExactly(
Expand Down
11 changes: 8 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

Expand All @@ -125,5 +129,6 @@ module.exports = {
spawnProcess,
safeJsonParse,
splitLines,
getAllNodeFunctions
getAllNodeFunctions,
isNodeRuntime
};
9 changes: 5 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 @@ -120,7 +120,7 @@ module.exports = {
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 Expand Up @@ -227,6 +227,7 @@ module.exports = {
const func = this.serverless.service.getFunction(funcName);
const handler = getHandlerFileAndFunctionName(func);
const handlerFile = path.relative('.', getHandlerFile(handler));

return {
handlerFile,
funcName,
Expand Down Expand Up @@ -271,8 +272,8 @@ module.exports = {
// Webpack config can be a Promise, If it's a Promise wait for resolved config object.
if (this.webpackConfig && _.isFunction(this.webpackConfig.then)) {
return BbPromise.resolve(this.webpackConfig.then(config => processConfig(config)));
} else {
return processConfig(this.webpackConfig);
}

return processConfig(this.webpackConfig);
}
};