Skip to content

Commit

Permalink
chore(cli): remove whitespace from bundled code (#23757)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
mrgrain committed Jan 19, 2023
1 parent 8bfa695 commit 4f8a2a5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/cli-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"entryPoints": [
"lib/index.js"
],
"sourcemap": "linked"
"sourcemap": "linked",
"minifyWhitespace": true
}
},
"author": {
Expand Down
45 changes: 44 additions & 1 deletion tools/@aws-cdk/node-bundle/src/api/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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[];
Expand All @@ -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] : []);

Expand Down Expand Up @@ -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 ?? [])],
Expand Down

0 comments on commit 4f8a2a5

Please sign in to comment.