Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ new lambda.NodejsFunction(this, 'my-handler', {
loader: { // Use the 'dataurl' loader for '.png' files
'.png': 'dataurl',
},
define: { // Replace strings during build time
'process.env.API_KEY': JSON.stringify('xxx-xxxx-xxx'),
},
logLevel: LogLevel.SILENT, // defaults to LogLevel.WARNING
keepNames: true, // defaults to false
tsconfig: 'custom-tsconfig.json' // use custom-tsconfig.json instead of default,
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class Bundling implements cdk.BundlingOptions {

const npx = osPlatform === 'win32' ? 'npx.cmd' : 'npx';
const loaders = Object.entries(this.props.loader ?? {});
const defines = Object.entries(this.props.define ?? {});

const esbuildCommand: string = [
npx, 'esbuild',
Expand All @@ -147,6 +148,7 @@ export class Bundling implements cdk.BundlingOptions {
...this.props.sourceMap ? ['--sourcemap'] : [],
...this.externals.map(external => `--external:${external}`),
...loaders.map(([ext, name]) => `--loader:${ext}=${name}`),
...defines.map(([key, value]) => `--define:${key}=${value}`),
...this.props.logLevel ? [`--log-level=${this.props.logLevel}`] : [],
...this.props.keepNames ? ['--keep-names'] : [],
...this.relativeTsconfigPath ? [`--tsconfig=${pathJoin(inputDir, this.relativeTsconfigPath)}`] : [],
Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ export interface BundlingOptions {
*/
readonly environment?: { [key: string]: string; };

/**
* Replace global identifiers with constant expressions.
*
* @example { 'process.env.DEBUG': 'true' }
* @example { 'process.env.API_KEY': JSON.stringify('xxx-xxxx-xxx') }
*
* @default - no replacements are made
*/
readonly define?: { [key: string]: string };

/**
* A list of modules that should be considered as externals (already available
* in the runtime).
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ test('esbuild bundling with esbuild options', () => {
banner: '/* comments */',
footer: '/* comments */',
forceDockerBundling: true,
define: {
'DEBUG': 'true',
'process.env.KEY': JSON.stringify('VALUE'),
},
});

// Correctly bundles with esbuild
Expand All @@ -180,6 +184,7 @@ test('esbuild bundling with esbuild options', () => {
'npx esbuild --bundle /asset-input/lib/handler.ts',
'--target=es2020 --platform=node --outfile=/asset-output/index.js',
'--minify --sourcemap --external:aws-sdk --loader:.png=dataurl',
'--define:DEBUG=true --define:process.env.KEY="VALUE"',
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
'--metafile=/asset-output/index.meta.json --banner=\'/* comments */\' --footer=\'/* comments */\'',
].join(' '),
Expand Down