Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends the --no-build option to serverless offline start #501

Closed
wants to merge 3 commits into from
Closed
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
199 changes: 106 additions & 93 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const packageModules = require('./lib/packageModules');
const lib = require('./lib');

class ServerlessWebpack {

static get lib() {
return lib;
}
Expand All @@ -28,10 +27,10 @@ class ServerlessWebpack {

if (
(_.has(this.serverless, 'service.custom.webpack') &&
_.isString(this.serverless.service.custom.webpack) &&
_.endsWith(this.serverless.service.custom.webpack, '.ts')) ||
_.isString(this.serverless.service.custom.webpack) &&
_.endsWith(this.serverless.service.custom.webpack, '.ts')) ||
(_.has(this.serverless, 'service.custom.webpack.webpackConfig') &&
_.endsWith(this.serverless.service.custom.webpack.webpackConfig, '.ts'))
_.endsWith(this.serverless.service.custom.webpack.webpackConfig, '.ts'))
) {
require('ts-node/register');
}
Expand All @@ -48,15 +47,13 @@ class ServerlessWebpack {
prepareLocalInvoke,
runPluginSupport,
prepareOfflineInvoke,
prepareStepOfflineInvoke
prepareStepOfflineInvoke,
);

this.commands = {
webpack: {
usage: 'Bundle with Webpack',
lifecycleEvents: [
'webpack'
],
lifecycleEvents: ['webpack'],
options: {
out: {
usage: 'Path to output directory',
Expand All @@ -66,127 +63,143 @@ class ServerlessWebpack {
commands: {
validate: {
type: 'entrypoint',
lifecycleEvents: [
'validate',
],
lifecycleEvents: ['validate'],
},
compile: {
type: 'entrypoint',
lifecycleEvents: [
'compile',
],
lifecycleEvents: ['compile'],
commands: {
watch: {
type: 'entrypoint',
lifecycleEvents: [
'compile'
]
}
}
lifecycleEvents: ['compile'],
},
},
},
package: {
type: 'entrypoint',
lifecycleEvents: [
'packExternalModules',
'packageModules'
],
lifecycleEvents: ['packExternalModules', 'packageModules'],
},
},
},
};

this.hooks = {
'before:package:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(() => this.serverless.pluginManager.spawn('webpack:package')),

'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:invoke:local:invoke': () => BbPromise.bind(this)
.then(() => {
lib.webpack.isLocal = true;
// --no-build override
if (this.options.build === false) {
this.skipCompile = true;
}

return this.serverless.pluginManager.spawn('webpack:validate');
})
.then(() => this.skipCompile ? BbPromise.resolve() : this.serverless.pluginManager.spawn('webpack:compile'))
.then(this.prepareLocalInvoke),
'before:package:createDeploymentArtifacts': () =>
BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(() => this.serverless.pluginManager.spawn('webpack:package')),

'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:invoke:local:invoke': () =>
BbPromise.bind(this)
.then(() => {
lib.webpack.isLocal = true;
// --no-build override
if (this.options.build === false) {
this.skipCompile = true;
}

'after:invoke:local:invoke': () => BbPromise.bind(this)
.then(() => {
return this.serverless.pluginManager.spawn('webpack:validate');
})
.then(() =>
this.skipCompile
? BbPromise.resolve()
: this.serverless.pluginManager.spawn('webpack:compile'),
)
.then(this.prepareLocalInvoke),

'after:invoke:local:invoke': () =>
BbPromise.bind(this).then(() => {
if (this.options.watch && !this.isWatching) {
return this.watch('invoke:local');
}
return BbPromise.resolve();
}),

'before:run:run': () => BbPromise.bind(this)
.then(() => _.set(this.serverless, 'service.package.individually', false))
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(this.packExternalModules)
.then(this.prepareRun),

'after:run:run': () => BbPromise.bind(this)
.then(() => {
'before:run:run': () =>
BbPromise.bind(this)
.then(() =>
_.set(this.serverless, 'service.package.individually', false),
)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(this.packExternalModules)
.then(this.prepareRun),

'after:run:run': () =>
BbPromise.bind(this).then(() => {
if (this.options.watch && !this.isWatching) {
return this.watch(this.watchRun.bind(this));
}
return BbPromise.resolve();
}),

'webpack:webpack': () => BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(() => this.serverless.pluginManager.spawn('webpack:package')),
'webpack:webpack': () =>
BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(() => this.serverless.pluginManager.spawn('webpack:package')),

/*
* Internal webpack events (can be hooked by plugins)
*/
'webpack:validate:validate': () => BbPromise.bind(this)
.then(this.validate),
'webpack:validate:validate': () =>
BbPromise.bind(this).then(this.validate),

'webpack:compile:compile': () => BbPromise.bind(this)
.then(this.compile),
'webpack:compile:compile': () => BbPromise.bind(this).then(this.compile),

'webpack:compile:watch:compile': () => BbPromise.resolve(),

'webpack:package:packExternalModules': () => BbPromise.bind(this)
.then(this.packExternalModules),

'webpack:package:packageModules': () => BbPromise.bind(this)
.then(this.packageModules),

'before:offline:start': () => BbPromise.bind(this)
.tap(() => {
lib.webpack.isLocal = true;
})
.then(this.prepareOfflineInvoke)
.then(this.wpwatch),

'before:offline:start:init': () => BbPromise.bind(this)
.tap(() => {
lib.webpack.isLocal = true;
})
.then(this.prepareOfflineInvoke)
.then(this.wpwatch),

'before:step-functions-offline:start': () => BbPromise.bind(this)
.tap(() => {
lib.webpack.isLocal = true;
})
.then(this.prepareStepOfflineInvoke)
.then(() => this.serverless.pluginManager.spawn('webpack:compile')),
'webpack:package:packExternalModules': () =>
BbPromise.bind(this).then(this.packExternalModules),

'webpack:package:packageModules': () =>
BbPromise.bind(this).then(this.packageModules),

'before:offline:start': () =>
BbPromise.bind(this)
.tap(() => {
lib.webpack.isLocal = true;
// --no-build override
if (this.options.build === false) {
this.skipCompile = true;
}
})
.then(this.prepareOfflineInvoke)
.then(() =>
this.skipCompile ? BbPromise.resolve() : this.wpwatch(),
),

'before:offline:start:init': () =>
BbPromise.bind(this)
.tap(() => {
lib.webpack.isLocal = true;
// --no-build override
if (this.options.build === false) {
this.skipCompile = true;
}
})
.then(this.prepareOfflineInvoke)
.then(() =>
this.skipCompile ? BbPromise.resolve() : this.wpwatch(),
),

'before:step-functions-offline:start': () =>
BbPromise.bind(this)
.tap(() => {
lib.webpack.isLocal = true;
})
.then(this.prepareStepOfflineInvoke)
.then(() => this.serverless.pluginManager.spawn('webpack:compile')),
};
}
}
Expand Down
Loading