-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
index.js
109 lines (90 loc) · 3.3 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
100
101
102
103
104
105
106
107
108
109
"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,
concurrency: config.options.concurrency,
packagerOptions: config.options.packagerOptions,
webpackConfig: getWebpackConfigPath(config.servicePath),
includeModules: {
forceExclude: config.options.forceExclude,
forceInclude: config.options.forceInclude,
nodeModulesRelativeDir:
config.options.nodeModulesRelativeDir != null
? config.options.nodeModulesRelativeDir
: "./",
// 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 cwd to the service path
packagePath: path.relative(
config.servicePath,
pkgUp.sync({ cwd: config.servicePath })
),
},
excludeFiles: config.options.excludeFiles,
excludeRegex: /bundle_stats\.(html|json)$/,
keepOutputDirectory: config.options.generateStatsFiles,
};
}
function applyUserConfig(config, userConfig, servicePath, runtime) {
config.servicePath = servicePath;
// Default to Node 12 if no runtime found
const runtimeVersion =
Number.parseInt((runtime || "").replace("nodejs", ""), 10) || 12;
// Force exclude aws-sdk for versions below Node 18
if (runtimeVersion < 18) {
config.options.forceExclude.push("aws-sdk");
}
// 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 rawFileExtensions if provided
if (userConfig.rawFileExtensions) {
userConfig.rawFileExtensions = config.options.rawFileExtensions.concat(
userConfig.rawFileExtensions
);
}
Object.assign(config.options, userConfig);
config.nodeVersion = runtimeVersion;
}
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;