-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
84 lines (73 loc) · 2.19 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
'use strict';
const BbPromise = require('bluebird');
const validate = require('./lib/validate'),
configure = require('./lib/configure'),
bundle = require('./lib/bundle');
class SlsBrowserify {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.globalBrowserifyConfig = {};
Object.assign(
this,
validate,
configure,
bundle
);
this.commands = {
browserify: {
usage: 'Bundle NodeJS lambda with Browserify',
lifecycleEvents: [
'validate',
'bundle',
],
options: {
out: {
usage: 'Path to output directory',
shortcut: 'o',
},
function: {
usage: 'Name of the function',
shortcut: 'f',
required: true,
},
},
commands: {},
},
};
this.hooks = {
//Handle `sls deploy`
'before:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.validate)
.then(this.globalConfig)
.then(() => {
const functionNames = this.serverless.service.getAllFunctions();
const bundleQueue = functionNames.map(functionName => {
return this.bundle(functionName);
});
return BbPromise.all(bundleQueue);
})
.catch(handleSkip),
//Handle `sls deploy function`
'before:deploy:function:packageFunction': () => BbPromise.bind(this)
.then(this.validate)
.then(this.globalConfig)
.then(() => this.bundle(this.options.function))
.catch(handleSkip),
//Handle `sls browserify`
'browserify:validate': () => BbPromise.bind(this)
.then(this.validate)
.then(this.globalConfig)
.then(() => this.bundle(this.options.function))
.catch(handleSkip),
};
}
}
function handleSkip(e) {
if ('skip' != e.statusCode) { //User explicitly chose to skip this function's browserification
throw e;
} else {
this.serverless.cli.log(`WARNING: ${e.message} SKIPPING bundling`);
}
}
module.exports = SlsBrowserify;