From cee278fd8aa2fd874a50767d586dfb075260e86a Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Thu, 30 Sep 2021 10:05:41 +0200 Subject: [PATCH] feat(lambda-nodejs): esbuild charset option Closes #16668 --- packages/@aws-cdk/aws-lambda-nodejs/README.md | 5 ++- .../aws-lambda-nodejs/lib/bundling.ts | 1 + .../@aws-cdk/aws-lambda-nodejs/lib/types.ts | 38 +++++++++++++++++-- .../aws-lambda-nodejs/test/bundling.test.ts | 4 +- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda-nodejs/README.md b/packages/@aws-cdk/aws-lambda-nodejs/README.md index 22347b28b1dd9..5d80e57b659ca 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/README.md +++ b/packages/@aws-cdk/aws-lambda-nodejs/README.md @@ -186,8 +186,9 @@ new lambda.NodejsFunction(this, 'my-handler', { keepNames: true, // defaults to false tsconfig: 'custom-tsconfig.json', // use custom-tsconfig.json instead of default, metafile: true, // include meta file, defaults to false - banner : '/* comments */', // requires esbuild >= 0.9.0, defaults to none - footer : '/* comments */', // requires esbuild >= 0.9.0, defaults to none + banner: '/* comments */', // requires esbuild >= 0.9.0, defaults to none + footer: '/* comments */', // requires esbuild >= 0.9.0, defaults to none + charset: Charset.UTF8, // do not escape non-ASCII characters, defaults to Charset.ASCII }, }); ``` diff --git a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts index 3c33ad74f2471..0d08b70a7be48 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts @@ -151,6 +151,7 @@ export class Bundling implements cdk.BundlingOptions { ...this.props.metafile ? [`--metafile=${pathJoin(options.outputDir, 'index.meta.json')}`] : [], ...this.props.banner ? [`--banner:js=${JSON.stringify(this.props.banner)}`] : [], ...this.props.footer ? [`--footer:js=${JSON.stringify(this.props.footer)}`] : [], + ...this.props.charset ? [`--charset=${this.props.charset}`] : [], ]; let depsCommand = ''; diff --git a/packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts b/packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts index 8d0d263fe64e6..ea3e364dde28b 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/lib/types.ts @@ -113,7 +113,7 @@ export interface BundlingOptions { * bundle buddy can consume esbuild's metadata format and generates a treemap visualization * of the modules in your bundle and how much space each one takes up. * @see https://esbuild.github.io/api/#metafile - * @default - false + * @default false */ readonly metafile?: boolean @@ -124,7 +124,7 @@ export interface BundlingOptions { * * This is commonly used to insert comments: * - * @default - no comments are passed + * @default - no comments are passed */ readonly banner? : string @@ -135,10 +135,23 @@ export interface BundlingOptions { * * This is commonly used to insert comments * - * @default - no comments are passed + * @default - no comments are passed */ readonly footer? : string + /** + * The charset to use for esbuild's output. + * + * By default esbuild's output is ASCII-only. Any non-ASCII characters are escaped + * using backslash escape sequences. Using escape sequences makes the generated output + * slightly bigger, and also makes it harder to read. If you would like for esbuild to print + * the original characters without using escape sequences, use `Charset.UTF8`. + * + * @see https://esbuild.github.io/api/#charset + * @default Charset.ASCII + */ + readonly charset?: Charset; + /** * Environment variables defined when bundling runs. * @@ -300,3 +313,22 @@ export enum SourceMapMode { */ BOTH = 'both' } + +/** + * Charset for esbuild's output + */ +export enum Charset { + /** + * ASCII + * + * Any non-ASCII characters are escaped using backslash escape sequences + */ + ASCII = 'ascii', + + /** + * UTF-8 + * + * Keep original characters without using escape sequences + */ + UTF8 = 'utf8' +} diff --git a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts index a38a6fa08d5bc..21c4a7f2343d4 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts @@ -6,7 +6,7 @@ import { AssetHashType, DockerImage } from '@aws-cdk/core'; import { version as delayVersion } from 'delay/package.json'; import { Bundling } from '../lib/bundling'; import { EsbuildInstallation } from '../lib/esbuild-installation'; -import { LogLevel, SourceMapMode } from '../lib/types'; +import { Charset, LogLevel, SourceMapMode } from '../lib/types'; import * as util from '../lib/util'; jest.mock('@aws-cdk/aws-lambda'); @@ -185,6 +185,7 @@ test('esbuild bundling with esbuild options', () => { metafile: true, banner: '/* comments */', footer: '/* comments */', + charset: Charset.UTF8, forceDockerBundling: true, define: { 'process.env.KEY': JSON.stringify('VALUE'), @@ -208,6 +209,7 @@ test('esbuild bundling with esbuild options', () => { 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 */"', + '--charset=utf8', ].join(' '), ], }),