Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.

Commit 78345a8

Browse files
committed
feat(externals): allow to configure externals
Useful and sometimes required for modules like @sentry/serverless where webpack/esbuild are breaking it. See: getsentry/sentry-javascript#2984
1 parent a8252f2 commit 78345a8

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/NodejsFunction.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export interface NodejsFunctionProps extends lambda.FunctionOptions {
4444
*/
4545
readonly modulesToIgnore?: string[];
4646

47+
/**
48+
* Externals not to be bundled with your lambda, default to all Node.js builtin modules and aws-sdk. Modules I use this for: @sentry/serverless for example
49+
*/
50+
readonly externals?: ["aws-sdk"];
51+
4752
/**
4853
* Whether to automatically reuse TCP connections when working with the AWS
4954
* SDK for JavaScript.
@@ -83,6 +88,9 @@ export class NodejsFunction extends lambda.Function {
8388
throw new Error(`Cannot find entry file at ${entryFullPath}`);
8489
}
8590

91+
const userExternals = props.externals ?? [];
92+
const defaultExternals = ["aws-sdk"];
93+
8694
const handler = props.handler ?? "handler";
8795
const defaultRunTime =
8896
nodeMajorVersion() >= 14
@@ -257,7 +265,10 @@ export class NodejsFunction extends lambda.Function {
257265
},
258266
},
259267
},
260-
externals: [...builtinModules, "aws-sdk"],
268+
externals: [...builtinModules, ...${JSON.stringify([
269+
...defaultExternals,
270+
...userExternals,
271+
])}],
261272
output: {
262273
filename: "[name].js",
263274
path: "${escapePathForNodeJs(outputDir)}",
@@ -277,7 +288,7 @@ export class NodejsFunction extends lambda.Function {
277288

278289
fs.writeFileSync(webpackConfigPath, webpackConfiguration);
279290

280-
console.time("aws-lambda-nodejs-webpack");
291+
console.time(`aws-lambda-nodejs-webpack-${props.entry}`);
281292
const webpack = spawn.sync(
282293
webpackBinPath,
283294
["--config", webpackConfigPath],
@@ -287,7 +298,7 @@ export class NodejsFunction extends lambda.Function {
287298
cwd: outputDir,
288299
},
289300
);
290-
console.timeEnd("aws-lambda-nodejs-webpack");
301+
console.timeEnd(`aws-lambda-nodejs-webpack-${props.entry}`);
291302

292303
if (webpack.status !== 0) {
293304
console.error(

0 commit comments

Comments
 (0)