Skip to content

Commit 3432c45

Browse files
authored
feat(lambda-nodejs): support esbuild inject (#19221)
Closes #19133 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5dc61ea commit 3432c45

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ 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
201202
},
202203
});
203204
```

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

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export class Bundling implements cdk.BundlingOptions {
198198
...this.props.footer ? [`--footer:js=${JSON.stringify(this.props.footer)}`] : [],
199199
...this.props.charset ? [`--charset=${this.props.charset}`] : [],
200200
...this.props.mainFields ? [`--main-fields=${this.props.mainFields.join(',')}`] : [],
201+
...this.props.inject ? this.props.inject.map(i => `--inject:${i}`) : [],
201202
];
202203

203204
let depsCommand = '';

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

+9
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,15 @@ export interface BundlingOptions {
279279
* @default ['main', 'module']
280280
*/
281281
readonly mainFields?: string[];
282+
283+
/**
284+
* This option allows you to automatically replace a global variable with an
285+
* import from another file.
286+
*
287+
* @see https://esbuild.github.io/api/#inject
288+
* @default - no code is injected
289+
*/
290+
readonly inject?: string[]
282291
}
283292

284293
/**

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ test('esbuild bundling with esbuild options', () => {
208208
'process.env.STRING': JSON.stringify('this is a "test"'),
209209
},
210210
format: OutputFormat.ESM,
211+
inject: ['./my-shim.js'],
211212
});
212213

213214
// Correctly bundles with esbuild
@@ -224,7 +225,7 @@ test('esbuild bundling with esbuild options', () => {
224225
defineInstructions,
225226
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
226227
'--metafile=/asset-output/index.meta.json --banner:js="/* comments */" --footer:js="/* comments */"',
227-
'--charset=utf8 --main-fields=module,main',
228+
'--charset=utf8 --main-fields=module,main --inject:./my-shim.js',
228229
].join(' '),
229230
],
230231
}),

0 commit comments

Comments
 (0)