forked from AnomalyInnovations/serverless-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (77 loc) · 2.77 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
"use strict";
const path = require("path");
const pkgUp = require("pkg-up");
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: config.options.forceExclude,
forceInclude: config.options.forceInclude,
// Generate relative path for the package.json
// For cases where the services are nested and don't have their own package.json
// Traverse up the tree to find the path to the nearest package.json
//
// Certain plugins like serverless-plugin-typescript change the cwd, so when
// searching, reset the the cwd to the service path
packagePath: path.relative(
config.servicePath,
pkgUp.sync({ cwd: config.servicePath })
)
}
};
}
function applyUserConfig(config, userConfig, servicePath, runtime) {
config.servicePath = servicePath;
// Concat forceExclude if provided
if (userConfig.forceExclude) {
userConfig.forceExclude = config.options.forceExclude.concat(
userConfig.forceExclude
);
}
// Concat externals if a list of packages are provided
if (userConfig.externals && Array.isArray(userConfig.externals)) {
userConfig.externals = config.options.externals.concat(
userConfig.externals
);
}
// Concat externals if provided
if (userConfig.rawFileExtensions) {
userConfig.rawFileExtensions = config.options.rawFileExtensions.concat(
userConfig.rawFileExtensions
);
}
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;