-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
81 lines (73 loc) · 2.89 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
"use strict";
const optionalChaining = require("es5-ext/optional-chaining")
, { join, resolve } = require("path")
, globby = require("globby")
, multimatch = require("multimatch")
, getDependencies = require("./lib/private/get-dependencies")
, resolveLambdaModulePaths = require("./lib/private/resolve-lambda-module-paths");
module.exports = class ServerlessPluginReducer {
constructor(serverless) {
const options = optionalChaining(serverless.service.custom, "reducer") || {};
const packagePlugin = serverless.pluginManager.plugins.find(
plugin => plugin.constructor.name === "Package"
);
const ServerlessError = serverless.classes.Error;
const originalResolveFilePathsFunction = packagePlugin.resolveFilePathsFunction;
packagePlugin.resolveFilePathsFunction = async function (functionName) {
const functionObject = this.serverless.service.getFunction(functionName);
const runtime =
functionObject.runtime || this.serverless.service.provider.runtime || "nodejs4.3";
if (!runtime.startsWith("nodejs")) {
originalResolveFilePathsFunction.call(this, functionName);
}
const funcPackageConfig = functionObject.package || {};
const { servicePath } = serverless.config;
if (!functionObject.handler) return null; // image case
const patterns = [];
for (const excludePattern of this.getExcludes(funcPackageConfig.exclude, true)) {
patterns.push(
excludePattern[0] === "!" ? excludePattern.slice(1) : `!${ excludePattern }`
);
}
patterns.push(
...this.getIncludes([
...(funcPackageConfig.include || []), ...(funcPackageConfig.patterns || [])
])
);
const [modulePaths, includeModulePaths] = await Promise.all([
// Get all lambda dependencies resolved by walking require paths
resolveLambdaModulePaths(servicePath, functionObject, {
...options,
ServerlessError
}),
// Get all files mentioned specifically in 'include' option
globby(patterns, {
cwd: this.serverless.config.servicePath,
dot: true,
silent: true,
follow: true,
nodir: true
})
]);
const normalizedIncludeModulePaths = includeModulePaths.map(path => join(path));
const modulePathsSet = new Set(modulePaths);
await Promise.all(
normalizedIncludeModulePaths.map(includeModulePath => {
if (!includeModulePath.endsWith(".js")) {
modulePathsSet.add(includeModulePath);
return null;
}
return getDependencies(servicePath, resolve(servicePath, includeModulePath), {
...options,
ServerlessError
}).then(dependencies => {
for (const dependency of dependencies) modulePathsSet.add(dependency);
});
})
);
// Apply eventual 'exclude' rules to automatically resolved dependencies
const result = new Set(multimatch(Array.from(modulePathsSet), ["**", ...patterns]));
return Array.from(result);
};
}
};