-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: bundle function when using
serverless invoke local
BREAKING CHANGE: changes `invoke local` behaviour
- Loading branch information
Showing
2 changed files
with
119 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use strict' | ||
|
||
const os = require('os') | ||
const path = require('path') | ||
|
||
const get = require('lodash/get') | ||
const Bb = require('bluebird') | ||
|
||
const bundle = require('./bundle') | ||
const configure = require('./configure') | ||
const errors = require('./errors') | ||
const validate = require('./validate') | ||
|
||
class BrowserifierPlugin { | ||
constructor (serverless, options) { | ||
this._s = serverless | ||
this._o = options | ||
this._b = { | ||
debugOn: Boolean(process.env.SLS_DEBUG) || Boolean(process.env.SLS_BROWSERIFIER_DEBUG), | ||
isDisabled: get(this._s, 'service.custom.browserify.disable', false), | ||
servicePath: path.join(this._s.config.servicePath || os.tmpdir(), '.serverless'), | ||
runtimeIsNode: get(this._s, 'service.provider.runtime', '').includes('nodejs'), | ||
packageIndividually: get(this._s, 'service.package.individually', false), | ||
globalBrowserifyConfig: {}, | ||
functionConfigCache: {}, | ||
localInvoke: false | ||
} | ||
this.hooks = { | ||
'initialize': this.initialise.bind(this), | ||
// handles `sls deploy` | ||
'before:package:createDeploymentArtifacts': this.prepareAndBundleAll.bind(this), | ||
// handles `sls deploy function` | ||
'before:package:function:package': this.prepareAndBundleFunction.bind(this, false), | ||
// handles `sls invoke local` | ||
'before:invoke:local:invoke': this.prepareAndBundleFunction.bind(this, true) | ||
} | ||
} | ||
|
||
initialise () { | ||
if (this._s.processedInput && this._s.processedInput.options) { | ||
this._o = this._s.processedInput.options | ||
} | ||
} | ||
|
||
prepareAndBundleAll () { | ||
return Bb.bind(this) | ||
.then(this.prepareAllFunctions) | ||
.then(this.bundleAllFunctions) | ||
.catch(this.handleSkip) | ||
.tapCatch(this.warnFailure) | ||
} | ||
|
||
prepareAndBundleFunction (localInvoke) { | ||
this._b.localInvoke = localInvoke | ||
return Bb.bind(this) | ||
.then(this.prepareFunction) | ||
.then(this.bundleFunction) | ||
.catch(this.handleSkip) | ||
.tapCatch(this.warnFailure) | ||
} | ||
|
||
prepareAllFunctions () { | ||
return Bb.bind(this) | ||
.then(this._validate) | ||
.then(this._computeGlobalConfig) | ||
.then(() => { | ||
const functionList = this.getAllFunctions() | ||
this._s.cli.log(`Browserifier: Preparing ${functionList.length} function(s)...`) | ||
return Bb.map(functionList, name => this._bootstrap(name).reflect()) | ||
}) | ||
.then(results => { | ||
return results | ||
.filter(inspection => inspection.isRejected()) | ||
.forEach(inspection => this.handleSkip(inspection.reason())) | ||
}) | ||
} | ||
|
||
prepareFunction () { | ||
return Bb.bind(this) | ||
.then(this._validate) | ||
.then(this._computeGlobalConfig) | ||
.then(() => this._bootstrap(this._o.function)) | ||
} | ||
|
||
bundleAllFunctions () { | ||
return Bb.bind(this) | ||
.then(this._validate) | ||
.then(() => Bb.map(this.getAllFunctions(), name => this._bundle(name))) | ||
} | ||
|
||
bundleFunction () { | ||
return Bb.bind(this) | ||
.then(this._validate) | ||
.then(() => this._bundle(this._o.function)) | ||
} | ||
|
||
getAllFunctions () { | ||
return this._s.service.getAllFunctions() | ||
} | ||
|
||
handleSkip (err) { | ||
if (err instanceof errors.SkipError) { | ||
this._s.cli.log(`Browserifier: ${err.message}`) | ||
} else { | ||
throw err | ||
} | ||
} | ||
|
||
warnFailure () { | ||
this._s.cli.log('Browserifier: Unexpected failure detected!') | ||
} | ||
|
||
static getName () { | ||
return 'com.digitalmaas.BrowserifierPlugin' | ||
} | ||
} | ||
|
||
Object.assign(BrowserifierPlugin.prototype, configure, bundle, validate) | ||
module.exports = BrowserifierPlugin |