|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const os = require('os') |
| 4 | +const path = require('path') |
| 5 | + |
| 6 | +const get = require('lodash/get') |
| 7 | +const Bb = require('bluebird') |
| 8 | + |
| 9 | +const bundle = require('./bundle') |
| 10 | +const configure = require('./configure') |
| 11 | +const errors = require('./errors') |
| 12 | +const validate = require('./validate') |
| 13 | + |
| 14 | +class BrowserifierPlugin { |
| 15 | + constructor (serverless, options) { |
| 16 | + this._s = serverless |
| 17 | + this._o = options |
| 18 | + this._b = { |
| 19 | + debugOn: Boolean(process.env.SLS_DEBUG) || Boolean(process.env.SLS_BROWSERIFIER_DEBUG), |
| 20 | + isDisabled: get(this._s, 'service.custom.browserify.disable', false), |
| 21 | + servicePath: path.join(this._s.config.servicePath || os.tmpdir(), '.serverless'), |
| 22 | + runtimeIsNode: get(this._s, 'service.provider.runtime', '').includes('nodejs'), |
| 23 | + packageIndividually: get(this._s, 'service.package.individually', false), |
| 24 | + globalBrowserifyConfig: {}, |
| 25 | + functionConfigCache: {}, |
| 26 | + localInvoke: false |
| 27 | + } |
| 28 | + this.hooks = { |
| 29 | + 'initialize': this.initialise.bind(this), |
| 30 | + // handles `sls deploy` |
| 31 | + 'before:package:createDeploymentArtifacts': this.prepareAndBundleAll.bind(this), |
| 32 | + // handles `sls deploy function` |
| 33 | + 'before:package:function:package': this.prepareAndBundleFunction.bind(this, false), |
| 34 | + // handles `sls invoke local` |
| 35 | + 'before:invoke:local:invoke': this.prepareAndBundleFunction.bind(this, true) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + initialise () { |
| 40 | + if (this._s.processedInput && this._s.processedInput.options) { |
| 41 | + this._o = this._s.processedInput.options |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + prepareAndBundleAll () { |
| 46 | + return Bb.bind(this) |
| 47 | + .then(this.prepareAllFunctions) |
| 48 | + .then(this.bundleAllFunctions) |
| 49 | + .catch(this.handleSkip) |
| 50 | + .tapCatch(this.warnFailure) |
| 51 | + } |
| 52 | + |
| 53 | + prepareAndBundleFunction (localInvoke) { |
| 54 | + this._b.localInvoke = localInvoke |
| 55 | + return Bb.bind(this) |
| 56 | + .then(this.prepareFunction) |
| 57 | + .then(this.bundleFunction) |
| 58 | + .catch(this.handleSkip) |
| 59 | + .tapCatch(this.warnFailure) |
| 60 | + } |
| 61 | + |
| 62 | + prepareAllFunctions () { |
| 63 | + return Bb.bind(this) |
| 64 | + .then(this._validate) |
| 65 | + .then(this._computeGlobalConfig) |
| 66 | + .then(() => { |
| 67 | + const functionList = this.getAllFunctions() |
| 68 | + this._s.cli.log(`Browserifier: Preparing ${functionList.length} function(s)...`) |
| 69 | + return Bb.map(functionList, name => this._bootstrap(name).reflect()) |
| 70 | + }) |
| 71 | + .then(results => { |
| 72 | + return results |
| 73 | + .filter(inspection => inspection.isRejected()) |
| 74 | + .forEach(inspection => this.handleSkip(inspection.reason())) |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + prepareFunction () { |
| 79 | + return Bb.bind(this) |
| 80 | + .then(this._validate) |
| 81 | + .then(this._computeGlobalConfig) |
| 82 | + .then(() => this._bootstrap(this._o.function)) |
| 83 | + } |
| 84 | + |
| 85 | + bundleAllFunctions () { |
| 86 | + return Bb.bind(this) |
| 87 | + .then(this._validate) |
| 88 | + .then(() => Bb.map(this.getAllFunctions(), name => this._bundle(name))) |
| 89 | + } |
| 90 | + |
| 91 | + bundleFunction () { |
| 92 | + return Bb.bind(this) |
| 93 | + .then(this._validate) |
| 94 | + .then(() => this._bundle(this._o.function)) |
| 95 | + } |
| 96 | + |
| 97 | + getAllFunctions () { |
| 98 | + return this._s.service.getAllFunctions() |
| 99 | + } |
| 100 | + |
| 101 | + handleSkip (err) { |
| 102 | + if (err instanceof errors.SkipError) { |
| 103 | + this._s.cli.log(`Browserifier: ${err.message}`) |
| 104 | + } else { |
| 105 | + throw err |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + warnFailure () { |
| 110 | + this._s.cli.log('Browserifier: Unexpected failure detected!') |
| 111 | + } |
| 112 | + |
| 113 | + static getName () { |
| 114 | + return 'com.digitalmaas.BrowserifierPlugin' |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +Object.assign(BrowserifierPlugin.prototype, configure, bundle, validate) |
| 119 | +module.exports = BrowserifierPlugin |
0 commit comments