diff --git a/packages/@aws-cdk/cli-lib/package.json b/packages/@aws-cdk/cli-lib/package.json index 19fcfc134a9b8..1240d7057b1b9 100644 --- a/packages/@aws-cdk/cli-lib/package.json +++ b/packages/@aws-cdk/cli-lib/package.json @@ -46,7 +46,7 @@ "build+test": "yarn build && yarn test", "build+test+extract": "yarn build+test && yarn rosetta:extract", "build+test+package": "yarn build+test && yarn package", - "bundle": "esbuild --bundle lib/index.ts --target=node14 --platform=node --external:fsevents --outfile=lib/main.js", + "bundle": "esbuild --bundle lib/index.ts --target=node14 --platform=node --external:fsevents --minify-whitespace --outfile=lib/main.js", "compat": "cdk-compat", "gen": "../../../packages/aws-cdk/generate.sh", "lint": "cdk-lint", diff --git a/packages/aws-cdk/package.json b/packages/aws-cdk/package.json index 528c8b14c766a..2ffe78205e747 100644 --- a/packages/aws-cdk/package.json +++ b/packages/aws-cdk/package.json @@ -50,7 +50,8 @@ "entryPoints": [ "lib/index.js" ], - "sourcemap": "linked" + "sourcemap": "linked", + "minifyWhitespace": true } }, "author": { diff --git a/tools/@aws-cdk/node-bundle/src/api/bundle.ts b/tools/@aws-cdk/node-bundle/src/api/bundle.ts index 82e90636606fb..929a9248b3e4e 100644 --- a/tools/@aws-cdk/node-bundle/src/api/bundle.ts +++ b/tools/@aws-cdk/node-bundle/src/api/bundle.ts @@ -75,11 +75,42 @@ export interface BundleProps { readonly test?: string; /** - * Basic sanity check to run against the created bundle. + * Include a sourcemap in the bundle. * * @default "inline" */ readonly sourcemap?: 'linked' | 'inline' | 'external' | 'both'; + + /** + * Minifies the bundled code. + * + * @default false + */ + readonly minify?: boolean; + + /** + * Removes whitespace from the code. + * This is enabled by default when `minify` is used. + * + * @default false + */ + readonly minifyWhitespace?: boolean; + + /** + * Renames local variables to be shorter. + * This is enabled by default when `minify` is used. + * + * @default false + */ + readonly minifyIdentifiers?: boolean; + + /** + * Rewrites syntax to a more compact format. + * This is enabled by default when `minify` is used. + * + * @default false + */ + readonly minifySyntax?: boolean; } /** @@ -156,6 +187,10 @@ export class Bundle { private readonly dontAttribute?: string; private readonly test?: string; private readonly sourcemap?: 'linked' | 'inline' | 'external' | 'both'; + private readonly minify?: boolean; + private readonly minifyWhitespace?: boolean; + private readonly minifyIdentifiers?: boolean; + private readonly minifySyntax?: boolean; private _bundle?: esbuild.BuildResult; private _dependencies?: Package[]; @@ -174,6 +209,10 @@ export class Bundle { this.dontAttribute = props.dontAttribute; this.entryPoints = {}; this.sourcemap = props.sourcemap; + this.minify = props.minify; + this.minifyWhitespace = props.minifyWhitespace; + this.minifyIdentifiers = props.minifyIdentifiers; + this.minifySyntax = props.minifySyntax; const entryPoints = props.entryPoints ?? (this.manifest.main ? [this.manifest.main] : []); @@ -405,6 +444,10 @@ export class Bundle { platform: 'node', sourcemap: this.sourcemap ?? 'inline', metafile: true, + minify: this.minify, + minifyWhitespace: this.minifyWhitespace, + minifyIdentifiers: this.minifyIdentifiers, + minifySyntax: this.minifySyntax, treeShaking: true, absWorkingDir: this.packageDir, external: [...(this.externals.dependencies ?? []), ...(this.externals.optionalDependencies ?? [])],