forked from digitalmaas/serverless-plugin-browserifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (87 loc) · 3.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict'
const Promise = require('bluebird')
const get = require('lodash/get')
const path = require('path')
const os = require('os')
const bundle = require('./lib/bundle')
const configure = require('./lib/configure')
const errors = require('./lib/errors')
const validate = require('./lib/validate')
class BrowserifierPlugin {
constructor (serverless, options) {
this.S = serverless
this._b = {
options,
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', '').indexOf('nodejs') !== -1,
packageIndividually: get(this.S, 'service.package.individually', false),
globalBrowserifyConfig: {},
functionConfigCache: {}
}
this.hooks = {
// handle `sls deploy`
'before:package:createDeploymentArtifacts': this._prepareAllFunctions.bind(this),
'after:package:createDeploymentArtifacts': this._bundleAllFunctions.bind(this),
// handle `sls deploy function`
'before:package:function:package': this._prepareFunction.bind(this),
'after:package:function:package': this._bundleFunction.bind(this)
}
}
_prepareAllFunctions () {
return Promise.bind(this)
.then(this._validate)
.then(this._computeGlobalConfig)
.then(() => {
const fns = this._getAllFunctions()
this.S.cli.log(`Browserifier: Preparing ${fns.length} function(s)...`)
return Promise.map(fns, name => this._bootstrap(name).reflect())
})
.then(results => {
return results
.filter(inspection => inspection.isRejected())
.forEach(inspection => this._handleSkip(inspection.reason()))
})
.catch(this._handleSkip)
.tapCatch(this._warnFailure)
}
_prepareFunction () {
return Promise.bind(this)
.then(this._validate)
.then(this._computeGlobalConfig)
.then(() => this._bootstrap(this._b.options.function))
.catch(this._handleSkip)
.tapCatch(this._warnFailure)
}
_bundleAllFunctions () {
return Promise.bind(this)
.then(this._validate)
.then(() => Promise.all(this._getAllFunctions().map(name => this._bundle(name))))
.tapCatch(this._warnFailure)
}
_bundleFunction () {
return Promise.bind(this)
.then(this._validate)
.then(() => this._bundle(this._b.options.function))
.tapCatch(this._warnFailure)
}
_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