From 4f8a2a5da64ad5ffdaa0461b20115aa4d6943eb8 Mon Sep 17 00:00:00 2001 From: Momo Kornher Date: Thu, 19 Jan 2023 21:08:52 +0000 Subject: [PATCH] chore(cli): remove whitespace from bundled code (#23757) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the best performing bundle. Un-minified: Time (mean ± σ): 736.3 ms ± 25.8 ms Minified: Time (mean ± σ): 665.3 ms ± 13.6 ms Whitespace: Time (mean ± σ): 481.6 ms ± 6.2 ms <--- winner Identifiers: Time (mean ± σ): 1.042 s ± 0.113 s Syntax: Time (mean ± σ): 662.9 ms ± 22.8 ms ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/cli-lib/package.json | 2 +- packages/aws-cdk/package.json | 3 +- tools/@aws-cdk/node-bundle/src/api/bundle.ts | 45 +++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) 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 ?? [])],