Skip to content

Commit

Permalink
fix(lambda-nodejs): cannot use esbuildArgs with older esbuild versions (
Browse files Browse the repository at this point in the history
#19343)

Older `esbuild` versions do not support passing `--arg=true`, only
`--arg` is accepted.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold authored Mar 11, 2022
1 parent 20da648 commit 59a4d81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 15 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +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(' ')] : [],
...this.props.esbuildArgs ? [toCliArgs(this.props.esbuildArgs)] : [],
];

let depsCommand = '';
Expand Down Expand Up @@ -353,3 +353,17 @@ function toTarget(runtime: Runtime): string {

return `node${match[1]}`;
}

function toCliArgs(esbuildArgs: { [key: string]: string | boolean }): string {
const args = [];

for (const [key, value] of Object.entries(esbuildArgs)) {
if (value === true || value === '') {
args.push(key);
} else if (value) {
args.push(`${key}="${value}"`);
}
}

return args.join(' ');
}
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ test('esbuild bundling with esbuild options', () => {
esbuildArgs: {
'--log-limit': '0',
'--resolve-extensions': '.ts,.js',
'--splitting': 'true',
'--splitting': true,
'--keep-names': '',
},
});

Expand All @@ -233,7 +234,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"',
'--log-limit="0" --resolve-extensions=".ts,.js" --splitting --keep-names',
].join(' '),
],
}),
Expand Down

0 comments on commit 59a4d81

Please sign in to comment.