forked from AnomalyInnovations/serverless-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (48 loc) · 1.68 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
"use strict";
const path = require("path");
const ServerlessWebpack = require("serverless-webpack");
const config = require("./src/config");
function getWebpackConfigPath(servicePath) {
return path.relative(servicePath, __dirname) + "/src/webpack.config.js";
}
function applyWebpackOptions(custom, config) {
if (custom.webpack) {
throw "serverless-webpack config detected in serverless.yml. serverless-bundle is not compatible with serverless-webpack.";
}
custom.webpack = {
packager: config.options.packager,
packagerOptions: config.options.packagerOptions,
webpackConfig: getWebpackConfigPath(config.servicePath),
includeModules: {
forceExclude: ["aws-sdk"],
forceInclude: config.options.forceInclude
}
};
}
function applyUserConfig(config, userConfig, servicePath, runtime) {
config.servicePath = servicePath;
config.options = Object.assign(config.options, userConfig);
// Default to Node 10 if no runtime found
config.nodeVersion =
Number.parseInt((runtime || "").replace("nodejs", ""), 10) || 10;
}
class ServerlessPlugin extends ServerlessWebpack {
constructor(serverless, options) {
super(serverless, options);
this.serverless = serverless;
this.options = options;
this.hooks["before:webpack:validate:validate"] = function() {
const service = this.serverless.service;
const servicePath = this.serverless.config.servicePath;
service.custom = service.custom || {};
applyUserConfig(
config,
service.custom.bundle,
servicePath,
service.provider.runtime
);
applyWebpackOptions(service.custom, config);
}.bind(this);
}
}
module.exports = ServerlessPlugin;