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

Commit

Permalink
feat(externals): allow to configure externals
Browse files Browse the repository at this point in the history
Useful and sometimes required for modules like @sentry/serverless where
webpack/esbuild are breaking it. See: getsentry/sentry-javascript#2984
  • Loading branch information
vvo committed Feb 15, 2021
1 parent a8252f2 commit 78345a8
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/NodejsFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export interface NodejsFunctionProps extends lambda.FunctionOptions {
*/
readonly modulesToIgnore?: string[];

/**
* 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
*/
readonly externals?: ["aws-sdk"];

/**
* Whether to automatically reuse TCP connections when working with the AWS
* SDK for JavaScript.
Expand Down Expand Up @@ -83,6 +88,9 @@ export class NodejsFunction extends lambda.Function {
throw new Error(`Cannot find entry file at ${entryFullPath}`);
}

const userExternals = props.externals ?? [];
const defaultExternals = ["aws-sdk"];

const handler = props.handler ?? "handler";
const defaultRunTime =
nodeMajorVersion() >= 14
Expand Down Expand Up @@ -257,7 +265,10 @@ export class NodejsFunction extends lambda.Function {
},
},
},
externals: [...builtinModules, "aws-sdk"],
externals: [...builtinModules, ...${JSON.stringify([
...defaultExternals,
...userExternals,
])}],
output: {
filename: "[name].js",
path: "${escapePathForNodeJs(outputDir)}",
Expand All @@ -277,7 +288,7 @@ export class NodejsFunction extends lambda.Function {

fs.writeFileSync(webpackConfigPath, webpackConfiguration);

console.time("aws-lambda-nodejs-webpack");
console.time(`aws-lambda-nodejs-webpack-${props.entry}`);
const webpack = spawn.sync(
webpackBinPath,
["--config", webpackConfigPath],
Expand All @@ -287,7 +298,7 @@ export class NodejsFunction extends lambda.Function {
cwd: outputDir,
},
);
console.timeEnd("aws-lambda-nodejs-webpack");
console.timeEnd(`aws-lambda-nodejs-webpack-${props.entry}`);

if (webpack.status !== 0) {
console.error(
Expand Down

0 comments on commit 78345a8

Please sign in to comment.