diff --git a/packages/@aws-cdk/aws-lambda-nodejs/README.md b/packages/@aws-cdk/aws-lambda-nodejs/README.md index 1ce7d9786a4d1..a21e5c9a6001d 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/README.md +++ b/packages/@aws-cdk/aws-lambda-nodejs/README.md @@ -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 @@ -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, + }, }, }); ``` diff --git a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts index ee6b395c5d262..52b778220e179 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts @@ -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 = ''; diff --git a/packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts b/packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts index 7b6b5c642f755..c9bc8f1151035 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts @@ -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 diff --git a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts index 37e7fa0c94b6b..9ff0bd389e812 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts @@ -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 @@ -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"', @@ -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(' '), ], }),