Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda-nodejs): add sourcesContent in BundlingOptions #17280

Merged
merged 8 commits into from Nov 4, 2021
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Expand Up @@ -174,6 +174,7 @@ new lambda.NodejsFunction(this, 'my-handler', {
minify: true, // minify code, defaults to false
sourceMap: true, // include source map, defaults to false
sourceMapMode: SourceMapMode.INLINE, // defaults to SourceMapMode.DEFAULT
sourcesContent: false, // include original source into source map, defaults to true
suds-sky marked this conversation as resolved.
Show resolved Hide resolved
target: 'es2020', // target environment for the generated JavaScript code
loader: { // Use the 'dataurl' loader for '.png' files
'.png': 'dataurl',
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Expand Up @@ -182,6 +182,7 @@ export class Bundling implements cdk.BundlingOptions {
const sourceMapEnabled = this.props.sourceMapMode ?? this.props.sourceMap;
const sourceMapMode = this.props.sourceMapMode ?? SourceMapMode.DEFAULT;
const sourceMapValue = sourceMapMode === SourceMapMode.DEFAULT ? '' : `=${this.props.sourceMapMode}`;
const sourcesContent = this.props.sourcesContent ?? true;

const esbuildCommand: string[] = [
options.esbuildRunner,
Expand All @@ -191,6 +192,7 @@ export class Bundling implements cdk.BundlingOptions {
`--outfile="${pathJoin(options.outputDir, 'index.js')}"`,
...this.props.minify ? ['--minify'] : [],
...sourceMapEnabled ? [`--sourcemap${sourceMapValue}`] : [],
...sourcesContent ? [] : [`--sources-content=${sourcesContent}`],
...this.externals.map(external => `--external:${external}`),
...loaders.map(([ext, name]) => `--loader:${ext}=${name}`),
...defines.map(([key, value]) => `--define:${key}=${JSON.stringify(value)}`),
Expand Down
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts
Expand Up @@ -26,6 +26,15 @@ export interface BundlingOptions {
*/
readonly sourceMapMode?: SourceMapMode;

/**
* Whether to include original source code in source maps when bundling.
*
* @see https://esbuild.github.io/api/#sources-content
*
* @default true
*/
readonly sourcesContent?: boolean;

/**
* Target environment for the generated JavaScript code.
*
Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Expand Up @@ -188,6 +188,7 @@ test('esbuild bundling with esbuild options', () => {
architecture: Architecture.X86_64,
minify: true,
sourceMap: true,
sourcesContent: false,
target: 'es2020',
loader: {
'.png': 'dataurl',
Expand Down Expand Up @@ -218,7 +219,7 @@ test('esbuild bundling with esbuild options', () => {
[
'esbuild --bundle "/asset-input/lib/handler.ts"',
'--target=es2020 --platform=node --outfile="/asset-output/index.js"',
'--minify --sourcemap --external:aws-sdk --loader:.png=dataurl',
'--minify --sourcemap --sources-content=false --external:aws-sdk --loader:.png=dataurl',
defineInstructions,
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
'--metafile=/asset-output/index.meta.json --banner:js="/* comments */" --footer:js="/* comments */"',
Expand Down Expand Up @@ -656,4 +657,4 @@ test('esbuild bundling with pre compilations and undefined tsconfig ( Should thr
});
}).toThrow('Cannot find a tsconfig.json, please specify the prop: tsconfig');

});
});