Skip to content

Commit ab313a4

Browse files
authored
feat(aws-lambda-nodejs): support additional esbuild configurations (#17788)
## Summary This PR adds support for passing in any additional esbuild args that are not already exposed in the `NodejsFunction` API. With this change a user should be able to add an esbuild option such as [`--log-limit`](https://esbuild.github.io/api/#log-limit): ```ts new NodejsFunction(scope, id, { ... bundling: { esbuildArgs: { "--log-limit": "0" } } }) ``` Resolves: #17768 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a6d52aa commit ab313a4

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

packages/@aws-cdk/aws-lambda-nodejs/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Docker container even if `esbuild` is available in your environment. This can be
170170

171171
## Configuring `esbuild`
172172

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

176176
```ts
@@ -198,7 +198,11 @@ new lambda.NodejsFunction(this, 'my-handler', {
198198
charset: lambda.Charset.UTF8, // do not escape non-ASCII characters, defaults to Charset.ASCII
199199
format: lambda.OutputFormat.ESM, // ECMAScript module output format, defaults to OutputFormat.CJS (OutputFormat.ESM requires Node.js 14.x)
200200
mainFields: ['module', 'main'], // prefer ECMAScript versions of dependencies
201-
inject: ['./my-shim.js', './other-shim.js'] // allows to automatically replace a global variable with an import from another file
201+
inject: ['./my-shim.js', './other-shim.js'], // allows to automatically replace a global variable with an import from another file
202+
esbuildArgs: { // Pass additional arguments to esbuild
203+
"--log-limit": "0",
204+
"--splitting": true,
205+
},
202206
},
203207
});
204208
```

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export class Bundling implements cdk.BundlingOptions {
199199
...this.props.charset ? [`--charset=${this.props.charset}`] : [],
200200
...this.props.mainFields ? [`--main-fields=${this.props.mainFields.join(',')}`] : [],
201201
...this.props.inject ? this.props.inject.map(i => `--inject:${i}`) : [],
202+
...this.props.esbuildArgs ? [Object.entries(this.props.esbuildArgs).map(([key, value]) => `${key}="${value}"`).join(' ')] : [],
202203
];
203204

204205
let depsCommand = '';

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,32 @@ export interface BundlingOptions {
203203
*/
204204
readonly esbuildVersion?: string;
205205

206+
/**
207+
* Build arguments to pass into esbuild.
208+
*
209+
* For example, to add the [--log-limit](https://esbuild.github.io/api/#log-limit) flag:
210+
*
211+
* ```text
212+
* new NodejsFunction(scope, id, {
213+
* ...
214+
* bundling: {
215+
* esbuildArgs: {
216+
* "--log-limit": "0",
217+
* }
218+
* }
219+
* });
220+
* ```
221+
*
222+
* @default - no additional esbuild arguments are passed
223+
*/
224+
readonly esbuildArgs?: { [key: string]: string | boolean };
225+
206226
/**
207227
* Build arguments to pass when building the bundling image.
208228
*
209229
* @default - no build arguments are passed
210230
*/
211-
readonly buildArgs?: { [key:string] : string };
231+
readonly buildArgs?: { [key: string]: string };
212232

213233
/**
214234
* Force bundling in a Docker container even if local bundling is

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ test('esbuild bundling with esbuild options', () => {
210210
},
211211
format: OutputFormat.ESM,
212212
inject: ['./my-shim.js'],
213+
esbuildArgs: {
214+
'--log-limit': '0',
215+
'--resolve-extensions': '.ts,.js',
216+
'--splitting': 'true',
217+
},
213218
});
214219

215220
// Correctly bundles with esbuild
@@ -218,7 +223,8 @@ test('esbuild bundling with esbuild options', () => {
218223
assetHashType: AssetHashType.OUTPUT,
219224
bundling: expect.objectContaining({
220225
command: [
221-
'bash', '-c',
226+
'bash',
227+
'-c',
222228
[
223229
'esbuild --bundle "/asset-input/lib/handler.ts"',
224230
'--target=es2020 --platform=node --format=esm --outfile="/asset-output/index.mjs"',
@@ -227,6 +233,7 @@ test('esbuild bundling with esbuild options', () => {
227233
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
228234
'--metafile=/asset-output/index.meta.json --banner:js="/* comments */" --footer:js="/* comments */"',
229235
'--charset=utf8 --main-fields=module,main --inject:./my-shim.js',
236+
'--log-limit="0" --resolve-extensions=".ts,.js" --splitting="true"',
230237
].join(' '),
231238
],
232239
}),

0 commit comments

Comments
 (0)