From 2394c07299bcf44122eef57ce2bb734a69591bce Mon Sep 17 00:00:00 2001 From: Vandita2020 Date: Mon, 19 Feb 2024 10:47:55 -0800 Subject: [PATCH] Fixing esbuildArgs --- .../test/integ.esbuildArgs.ts | 33 +++++++++++++++++++ .../aws-lambda-nodejs/lib/bundling.ts | 3 ++ .../aws-lambda-nodejs/test/bundling.test.ts | 3 +- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.esbuildArgs.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.esbuildArgs.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.esbuildArgs.ts new file mode 100644 index 0000000000000..9a2f913547692 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/test/integ.esbuildArgs.ts @@ -0,0 +1,33 @@ +import * as path from 'path'; +import { App, Stack, StackProps } from 'aws-cdk-lib'; +import { Construct } from 'constructs'; +import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; +import { STANDARD_NODEJS_RUNTIME } from '../../config'; + +class TestStack extends Stack { + constructor(scope: Construct, id: string, props?: StackProps) { + super(scope, id, props); + + new lambda.NodejsFunction(this, 'ts-handler', { + entry: path.join(__dirname, 'integ-handlers/ts-handler.ts'), + runtime: STANDARD_NODEJS_RUNTIME, + bundling: { + minify: true, + sourceMap: true, + sourceMapMode: lambda.SourceMapMode.BOTH, + esbuildArgs: { + '--log-limit': '0', + '--out-extension': '.js=.mjs', + }, + }, + }); + } +} + +const app = new App(); +const stack = new TestStack(app, 'cdk-integ-lambda-nodejs-esbuildArgs'); + +new IntegTest(app, 'LambdaNodeJsEsbuildArgsInteg', { + testCases: [stack], +}); diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts b/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts index 4b1c428b72261..af98391b9c075 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts @@ -418,10 +418,13 @@ function toTarget(runtime: Runtime): string { function toCliArgs(esbuildArgs: { [key: string]: string | boolean }): string { const args = new Array(); + const ls = ['--alias', '--drop', '--pure', '--log-override', '--out-extension']; for (const [key, value] of Object.entries(esbuildArgs)) { if (value === true || value === '') { args.push(key); + } else if (ls.includes(key)) { + args.push(`${key}:"${value}"`); } else if (value) { args.push(`${key}="${value}"`); } diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts b/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts index c0010a3328fe6..6bdda5b0d7b2e 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts @@ -245,6 +245,7 @@ test('esbuild bundling with esbuild options', () => { '--resolve-extensions': '.ts,.js', '--splitting': true, '--keep-names': '', + '--out-extension': '.js=.mjs', }, }); @@ -264,7 +265,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 */"', '--main-fields=module,main --inject:./my-shim.js', - '--log-limit="0" --resolve-extensions=".ts,.js" --splitting --keep-names', + '--log-limit="0" --resolve-extensions=".ts,.js" --splitting --keep-names --out-extension:".js=.mjs"', ].join(' '), ], }),