Skip to content

Commit

Permalink
feat(lambda-nodejs): Allow setting mainFields for esbuild (aws#18569)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
revmischa authored and TikiTDO committed Feb 21, 2022
1 parent 58d9c32 commit 2c77a94
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ new lambda.NodejsFunction(this, 'my-handler', {
footer: '/* comments */', // requires esbuild >= 0.9.0, defaults to none
charset: lambda.Charset.UTF8, // do not escape non-ASCII characters, defaults to Charset.ASCII
format: lambda.OutputFormat.ESM, // ECMAScript module output format, defaults to OutputFormat.CJS (OutputFormat.ESM requires Node.js 14.x)
mainFields: ['module', 'main'], // prefer ECMAScript versions of dependencies
},
});
```
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export class Bundling implements cdk.BundlingOptions {
...this.props.banner ? [`--banner:js=${JSON.stringify(this.props.banner)}`] : [],
...this.props.footer ? [`--footer:js=${JSON.stringify(this.props.footer)}`] : [],
...this.props.charset ? [`--charset=${this.props.charset}`] : [],
...this.props.mainFields ? [`--main-fields=${this.props.mainFields.join(',')}`] : [],
];

let depsCommand = '';
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ export interface BundlingOptions {
* @default OutputFormat.CJS
*/
readonly format?: OutputFormat;

/**
* How to determine the entry point for modules.
* Try ['module', 'main'] to default to ES module versions.
*
* @default ['main', 'module']
*/
readonly mainFields?: string[];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ test('esbuild bundling with esbuild options', () => {
footer: '/* comments */',
charset: Charset.UTF8,
forceDockerBundling: true,
mainFields: ['module', 'main'],
define: {
'process.env.KEY': JSON.stringify('VALUE'),
'process.env.BOOL': 'true',
Expand All @@ -224,7 +225,7 @@ test('esbuild bundling with esbuild options', () => {
defineInstructions,
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
'--metafile=/asset-output/index.meta.json --banner:js="/* comments */" --footer:js="/* comments */"',
'--charset=utf8',
'--charset=utf8 --main-fields=module,main',
].join(' '),
],
}),
Expand Down

0 comments on commit 2c77a94

Please sign in to comment.