Skip to content

Commit 4f8a2a5

Browse files
authored
chore(cli): remove whitespace from bundled code (#23757)
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*
1 parent 8bfa695 commit 4f8a2a5

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

packages/@aws-cdk/cli-lib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"build+test": "yarn build && yarn test",
4747
"build+test+extract": "yarn build+test && yarn rosetta:extract",
4848
"build+test+package": "yarn build+test && yarn package",
49-
"bundle": "esbuild --bundle lib/index.ts --target=node14 --platform=node --external:fsevents --outfile=lib/main.js",
49+
"bundle": "esbuild --bundle lib/index.ts --target=node14 --platform=node --external:fsevents --minify-whitespace --outfile=lib/main.js",
5050
"compat": "cdk-compat",
5151
"gen": "../../../packages/aws-cdk/generate.sh",
5252
"lint": "cdk-lint",

packages/aws-cdk/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"entryPoints": [
5151
"lib/index.js"
5252
],
53-
"sourcemap": "linked"
53+
"sourcemap": "linked",
54+
"minifyWhitespace": true
5455
}
5556
},
5657
"author": {

tools/@aws-cdk/node-bundle/src/api/bundle.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,42 @@ export interface BundleProps {
7575
readonly test?: string;
7676

7777
/**
78-
* Basic sanity check to run against the created bundle.
78+
* Include a sourcemap in the bundle.
7979
*
8080
* @default "inline"
8181
*/
8282
readonly sourcemap?: 'linked' | 'inline' | 'external' | 'both';
83+
84+
/**
85+
* Minifies the bundled code.
86+
*
87+
* @default false
88+
*/
89+
readonly minify?: boolean;
90+
91+
/**
92+
* Removes whitespace from the code.
93+
* This is enabled by default when `minify` is used.
94+
*
95+
* @default false
96+
*/
97+
readonly minifyWhitespace?: boolean;
98+
99+
/**
100+
* Renames local variables to be shorter.
101+
* This is enabled by default when `minify` is used.
102+
*
103+
* @default false
104+
*/
105+
readonly minifyIdentifiers?: boolean;
106+
107+
/**
108+
* Rewrites syntax to a more compact format.
109+
* This is enabled by default when `minify` is used.
110+
*
111+
* @default false
112+
*/
113+
readonly minifySyntax?: boolean;
83114
}
84115

85116
/**
@@ -156,6 +187,10 @@ export class Bundle {
156187
private readonly dontAttribute?: string;
157188
private readonly test?: string;
158189
private readonly sourcemap?: 'linked' | 'inline' | 'external' | 'both';
190+
private readonly minify?: boolean;
191+
private readonly minifyWhitespace?: boolean;
192+
private readonly minifyIdentifiers?: boolean;
193+
private readonly minifySyntax?: boolean;
159194

160195
private _bundle?: esbuild.BuildResult;
161196
private _dependencies?: Package[];
@@ -174,6 +209,10 @@ export class Bundle {
174209
this.dontAttribute = props.dontAttribute;
175210
this.entryPoints = {};
176211
this.sourcemap = props.sourcemap;
212+
this.minify = props.minify;
213+
this.minifyWhitespace = props.minifyWhitespace;
214+
this.minifyIdentifiers = props.minifyIdentifiers;
215+
this.minifySyntax = props.minifySyntax;
177216

178217
const entryPoints = props.entryPoints ?? (this.manifest.main ? [this.manifest.main] : []);
179218

@@ -405,6 +444,10 @@ export class Bundle {
405444
platform: 'node',
406445
sourcemap: this.sourcemap ?? 'inline',
407446
metafile: true,
447+
minify: this.minify,
448+
minifyWhitespace: this.minifyWhitespace,
449+
minifyIdentifiers: this.minifyIdentifiers,
450+
minifySyntax: this.minifySyntax,
408451
treeShaking: true,
409452
absWorkingDir: this.packageDir,
410453
external: [...(this.externals.dependencies ?? []), ...(this.externals.optionalDependencies ?? [])],

0 commit comments

Comments
 (0)