-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRuntimeWebpack.js
executable file
·287 lines (242 loc) · 8.92 KB
/
RuntimeWebpack.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
module.exports = function(S) {
const SError = require(S.getServerlessPath('Error')),
SCli = require(S.getServerlessPath('utils/cli')),
R = require('ramda'),
webpack = require('webpack'),
BbPromise = require('bluebird'),
chalk = require('chalk'),
spawnSync = require('child_process').spawnSync,
path = require('path'),
fs = BbPromise.promisifyAll(require('fs'));
class RuntimeWebpack extends S.classes.Runtime {
constructor() {
super();
}
static getName() {
return 'webpack';
}
getName(providerName) {
if (providerName === 'aws') {
return 'nodejs4.3'
} else {
return RuntimeWebpack.getName();
}
}
/**
* Scaffold
* - Create scaffolding for new Node.js function
*/
scaffold(func) {
const handlerPath = path.resolve(__dirname, '..', 'templates', 'handler.js');
const webpackPath = path.resolve(__dirname, '..', 'templates', 'webpack.config.js');
func.handler = 'handler.default';
func.custom.webpack = {
configPath: path.join(func.name, 'webpack.config.js'),
};
func.custom.handlerExt = 'js';
return BbPromise.all([
fs.readFileAsync(handlerPath),
fs.readFileAsync(webpackPath),
])
.then(files => BbPromise.all([
func.save(),
S.utils.writeFile(func.getRootPath('handler.js'), files[0]),
S.utils.writeFile(func.getRootPath('webpack.config.js'), files[1]),
S.utils.writeFile(func.getRootPath('event.json'), {})
]));
}
/**
* Run
* - Run this function locally
*/
run(func, stage, region, event) {
return this
.getEnvVars(func, stage, region)
.then((envVars) => S.utils.readFile(func.getRootPath('event.json')).then(localEvent => ({
envVars,
localEvent,
})))
.then((locals) => {
const childArgs = [__dirname + '/webpack-runner'];
const resultSep = '___serverless_function_run_results___';
const input = JSON.stringify({
event: event || locals.localEvent,
resultSep,
handler: func.handler,
name: func.getDeployedName({stage, region}),
dir: func.getRootPath(),
projectDir: S.config.projectPath,
webpackConfig: this.getWebpackConfig(func, path.join(S.config.projectPath, '_meta/_tmp/', func.name), true),
});
const env = Object.assign(locals.envVars, process.env, {
NODE_PATH: path.resolve(__dirname, '..', 'node_modules')
});
const child = spawnSync(process.execPath, childArgs, {env, input});
if (child.error) return BbPromise.reject(child.error);
if (!R.isEmpty(child.stderr.toString())) {
SCli.log(chalk.red.bold('Failed - This Error Was Thrown:'));
console.error(child.stderr.toString());
return BbPromise.resolve();
}
const resultArray = child.stdout.toString().split(resultSep);
let results = resultArray[1]
try {
results = JSON.parse(resultArray[1]);
} catch(e) {}
if (!R.isEmpty(resultArray[0])) process.stdout.write(resultArray[0]);
if (results.status === 'success') {
SCli.log(chalk.green.bold('Success! - This Response Was Returned:'));
console.log(JSON.stringify(results.response, null, 2));
} else {
SCli.log(chalk.red.bold('Failed - This Error Was Returned:'));
SCli.log(results.response);
if (results.stack) console.log(results.stack);
}
return BbPromise.resolve(results);
});
}
/**
* Build
* - Build the function in this runtime
*/
build(func, stage, region) {
// Validate
if (!func._class || func._class !== 'Function') return BbPromise.reject(new SError('A function instance is required'));
return this.createDistDir(func.name).then(pathDist => {
return this.copyFunction(func, pathDist, stage, region)
.then(() => this._addEnvVarsInline(func, pathDist, stage, region))
.then(() => this.getWebpackConfig(func, pathDist))
.then((webpackConfig) => this._webpackCompile(R.merge(webpackConfig, {
entry: path.join(pathDist, webpackConfig.entry),
})))
.then((stats) =>
this.createDistDir(path.join(pathDist, 'node_modules'))
.then((modulesDist) => this._copyExternalModules(stats, modulesDist))
)
.then(() => pathDist);
});
}
getWebpackConfig(func, pathDist, ignoreConfigPath) {
const project = S.getProject();
let webpackConfig = (
R.path(['custom', 'webpack'], func) ||
R.path(['custom', 'webpack'], project) ||
{
configPath: path.join(func.name, 'webpack.config.js'),
}
);
const handlerName = func.getHandler().split('.')[0];
const handlerExt = (
R.path(['custom', 'handlerExt'], func) ||
R.path(['custom', 'handlerExt'], project) ||
'js'
);
const handlerFileName = handlerName + '.' + handlerExt;
if (!ignoreConfigPath && webpackConfig.configPath) {
const projectPath = S.config.projectPath;
const configPath = path.join(projectPath, webpackConfig.configPath);
webpackConfig = require(configPath);
}
webpackConfig = R.merge({
context: path.dirname(func.getFilePath()),
entry: './' + handlerFileName,
output: {
libraryTarget: 'commonjs',
path: pathDist,
filename: handlerFileName,
},
}, webpackConfig);
return webpackConfig;
}
_webpackCompile(webpackConfig) {
const compiler = webpack(webpackConfig);
return BbPromise
.fromCallback(cb => compiler.run(cb))
.then(stats => {
SCli.log(stats.toString({
colors: true,
hash: false,
version: false,
chunks: false,
children: false
}));
if (stats.compilation.errors.length) {
throw new Error('Webpack compilation error, see above');
}
return stats;
});
}
_copyExternalModules(stats, pathDist) {
const options = {
hash: false,
version: false,
timings: false,
assets: false,
chunks: false,
modules: true,
reasons: false,
children: false,
source: false,
errors: false,
errorDetails: false,
warnings: false,
publicPath: false,
exclude: [/^(?!external )/],
};
const natives = process.binding('natives');
const projectPath = S.config.projectPath;
const externalModules = stats.toJson(options).modules;
const moduleNames = externalModules
.map(module => /external "(.+)"/.exec(module.identifier)[1])
// exclude aws-sdk since it is provided by lambda
// also exclude native node.js modules
.filter(id => id !== 'aws-sdk' && !natives[id]);
return Promise.all(moduleNames.map(moduleName => {
const modulePath = path.join(projectPath, 'node_modules', moduleName);
SCli.log('Copy module: ' + moduleName);
return fs.copy(modulePath, path.join(pathDist, moduleName));
}));
}
/**
* Get Handler
*/
getHandler(func) {
return path.join(path.dirname(func.handler), "_serverless_handler.handler").replace(/\\/g, '/');
}
/**
* Add ENV Vars In-line
* - Adds a new handler that loads in ENV vars before running the main handler
*/
_addEnvVarsInline(func, pathDist, stage, region) {
return this.getEnvVars(func, stage, region)
.then(envVars => {
const handlerArr = func.handler.split('.');
const handlerDir = path.dirname(func.handler);
const handlerFile = handlerArr[0].split('/').pop();
const handlerMethod = handlerArr[1];
const handlerRequire = path.join(path.dirname(func.getFilePath()), handlerFile);
const loader = `
const envVars = ${JSON.stringify(envVars, null, 2)};
for (let key in envVars) {
process.env[key] = envVars[key];
}
exports.handler = (event, context) => {
try {
const result = require('${handlerRequire}')['${handlerMethod}'](event, context);
if (result && typeof result.then == 'function') {
result.then(context.succeed).catch(context.fail);
return;
}
if(result !== undefined) context.succeed(result);
} catch(e) {
context.fail(e);
}
};
`;
return fs.writeFileAsync(path.join(pathDist, handlerDir, '_serverless_handler.js'), loader);
});
}
}
return RuntimeWebpack;
};