Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-lambda-nodejs): support additional esbuild configurations #17788

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Docker container even if `esbuild` is available in your environment. This can be

## Configuring `esbuild`

The `NodejsFunction` construct exposes some [esbuild options](https://esbuild.github.io/api/#build-api)
The `NodejsFunction` construct exposes [esbuild options](https://esbuild.github.io/api/#build-api)
via properties under `bundling`:

```ts
Expand Down Expand Up @@ -198,7 +198,11 @@ new lambda.NodejsFunction(this, 'my-handler', {
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
inject: ['./my-shim.js', './other-shim.js'] // allows to automatically replace a global variable with an import from another file
inject: ['./my-shim.js', './other-shim.js'], // allows to automatically replace a global variable with an import from another file
esbuildArgs: { // Pass additional arguments to esbuild
"--log-limit": "0",
"--splitting": true,
},
},
});
```
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 @@ -199,6 +199,7 @@ export class Bundling implements cdk.BundlingOptions {
...this.props.charset ? [`--charset=${this.props.charset}`] : [],
...this.props.mainFields ? [`--main-fields=${this.props.mainFields.join(',')}`] : [],
...this.props.inject ? this.props.inject.map(i => `--inject:${i}`) : [],
...this.props.esbuildArgs ? [Object.entries(this.props.esbuildArgs).map(([key, value]) => `${key}="${value}"`).join(' ')] : [],
];

let depsCommand = '';
Expand Down
22 changes: 21 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,32 @@ export interface BundlingOptions {
*/
readonly esbuildVersion?: string;

/**
* Build arguments to pass into esbuild.
*
* For example, to add the [--log-limit](https://esbuild.github.io/api/#log-limit) flag:
*
* ```text
* new NodejsFunction(scope, id, {
* ...
* bundling: {
* esbuildArgs: {
* "--log-limit": "0",
* }
* }
* });
* ```
*
* @default - no additional esbuild arguments are passed
*/
readonly esbuildArgs?: { [key: string]: string | boolean };

/**
* Build arguments to pass when building the bundling image.
*
* @default - no build arguments are passed
*/
readonly buildArgs?: { [key:string] : string };
readonly buildArgs?: { [key: string]: string };

/**
* Force bundling in a Docker container even if local bundling is
Expand Down
9 changes: 8 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 @@ -210,6 +210,11 @@ test('esbuild bundling with esbuild options', () => {
},
format: OutputFormat.ESM,
inject: ['./my-shim.js'],
esbuildArgs: {
'--log-limit': '0',
'--resolve-extensions': '.ts,.js',
'--splitting': 'true',
},
});

// Correctly bundles with esbuild
Expand All @@ -218,7 +223,8 @@ test('esbuild bundling with esbuild options', () => {
assetHashType: AssetHashType.OUTPUT,
bundling: expect.objectContaining({
command: [
'bash', '-c',
'bash',
'-c',
[
'esbuild --bundle "/asset-input/lib/handler.ts"',
'--target=es2020 --platform=node --format=esm --outfile="/asset-output/index.mjs"',
Expand All @@ -227,6 +233,7 @@ test('esbuild bundling with esbuild options', () => {
'--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 --main-fields=module,main --inject:./my-shim.js',
'--log-limit="0" --resolve-extensions=".ts,.js" --splitting="true"',
].join(' '),
],
}),
Expand Down