Skip to content

Commit 0e78aeb

Browse files
authored
feat(lambda-nodejs): Allow setting mainFields for esbuild (#18569)
I’m trying to use the new format=esm bundling with NodejsFunction I noticed that it bundles the “CJS” version of dependencies not the ESM version (if available). esbuild has some wack defaults - https://esbuild.github.io/api/#main-fields This causes problems because the CJS versions usually have stuff like require() and they don’t work for tree-shaking. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 56fc11b commit 0e78aeb

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

Diff for: packages/@aws-cdk/aws-lambda-nodejs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ new lambda.NodejsFunction(this, 'my-handler', {
192192
footer: '/* comments */', // requires esbuild >= 0.9.0, defaults to none
193193
charset: lambda.Charset.UTF8, // do not escape non-ASCII characters, defaults to Charset.ASCII
194194
format: lambda.OutputFormat.ESM, // ECMAScript module output format, defaults to OutputFormat.CJS (OutputFormat.ESM requires Node.js 14.x)
195+
mainFields: ['module', 'main'], // prefer ECMAScript versions of dependencies
195196
},
196197
});
197198
```

Diff for: packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ export class Bundling implements cdk.BundlingOptions {
211211
...this.props.banner ? [`--banner:js=${JSON.stringify(this.props.banner)}`] : [],
212212
...this.props.footer ? [`--footer:js=${JSON.stringify(this.props.footer)}`] : [],
213213
...this.props.charset ? [`--charset=${this.props.charset}`] : [],
214+
...this.props.mainFields ? [`--main-fields=${this.props.mainFields.join(',')}`] : [],
214215
];
215216

216217
let depsCommand = '';

Diff for: packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ export interface BundlingOptions {
270270
* @default OutputFormat.CJS
271271
*/
272272
readonly format?: OutputFormat;
273+
274+
/**
275+
* How to determine the entry point for modules.
276+
* Try ['module', 'main'] to default to ES module versions.
277+
*
278+
* @default ['main', 'module']
279+
*/
280+
readonly mainFields?: string[];
273281
}
274282

275283
/**

Diff for: packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ test('esbuild bundling with esbuild options', () => {
201201
footer: '/* comments */',
202202
charset: Charset.UTF8,
203203
forceDockerBundling: true,
204+
mainFields: ['module', 'main'],
204205
define: {
205206
'process.env.KEY': JSON.stringify('VALUE'),
206207
'process.env.BOOL': 'true',
@@ -224,7 +225,7 @@ test('esbuild bundling with esbuild options', () => {
224225
defineInstructions,
225226
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
226227
'--metafile=/asset-output/index.meta.json --banner:js="/* comments */" --footer:js="/* comments */"',
227-
'--charset=utf8',
228+
'--charset=utf8 --main-fields=module,main',
228229
].join(' '),
229230
],
230231
}),

0 commit comments

Comments
 (0)