Skip to content

Commit c64f63a

Browse files
authored
chore(node-bundle): produce a bundle metafile by default (#607)
Small QoL improvement so that engineers always have an esbuild bundle metafile available to [analyze](https://esbuild.github.io/analyze/). --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 03378bc commit c64f63a

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

packages/@aws-cdk/integ-runner/.projen/tasks.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ export interface BundleProps {
113113
* @default false
114114
*/
115115
readonly minifySyntax?: boolean;
116+
117+
/**
118+
* Write the metafile into this location.
119+
*
120+
* @default - no metafile is written
121+
*/
122+
readonly metafile?: string;
116123
}
117124

118125
/**
@@ -192,6 +199,7 @@ export class Bundle {
192199
private readonly minifyWhitespace?: boolean;
193200
private readonly minifyIdentifiers?: boolean;
194201
private readonly minifySyntax?: boolean;
202+
private readonly metafile?: string;
195203

196204
private _bundle?: esbuild.BuildResult;
197205
private _dependencies?: Package[];
@@ -214,6 +222,7 @@ export class Bundle {
214222
this.minifyWhitespace = props.minifyWhitespace;
215223
this.minifyIdentifiers = props.minifyIdentifiers;
216224
this.minifySyntax = props.minifySyntax;
225+
this.metafile = props.metafile;
217226

218227
const entryPoints = props.entryPoints ?? (this.manifest.main ? [this.manifest.main] : []);
219228

@@ -437,7 +446,7 @@ export class Bundle {
437446
target: 'node14',
438447
platform: 'node',
439448
sourcemap: this.sourcemap,
440-
metafile: true,
449+
metafile: true, // this is always required for some of our validation rules
441450
minify: this.minify,
442451
minifyWhitespace: this.minifyWhitespace,
443452
minifyIdentifiers: this.minifyIdentifiers,
@@ -450,6 +459,11 @@ export class Bundle {
450459
allowOverwrite: true,
451460
});
452461

462+
// Write metafile only when requested
463+
if (this.metafile) {
464+
fs.writeFileSync(this.metafile, JSON.stringify(bundle.metafile, null, 2));
465+
}
466+
453467
if (bundle.warnings.length > 0) {
454468
// esbuild warnings are usually important, lets try to be strict here.
455469
// the warnings themselves are printed on screen.

packages/@aws-cdk/node-bundle/src/cli-main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export async function cliMain(cliArgs: string[]) {
1818
.option('dont-attribute', { type: 'string', desc: 'Dependencies matching this regular expressions wont be added to the notice file' })
1919
.option('test', { type: 'string', desc: 'Validation command to sanity test the bundle after its created' })
2020
.option('minify-whitespace', { type: 'boolean', default: false, desc: 'Minify whitespace' })
21+
.option('metafile', { type: 'string', desc: 'Produce a metafile about the build that can be analyzed' })
2122
.command('validate', 'Validate the package is ready for bundling', args => args
2223
.option('fix', { type: 'boolean', default: false, alias: 'f', desc: 'Fix any fixable violations' }),
2324
)
@@ -83,6 +84,7 @@ export async function cliMain(cliArgs: string[]) {
8384
dontAttribute: argv['dont-attribute'],
8485
test: argv.test,
8586
minifyWhitespace: argv['minify-whitespace'],
87+
metafile: argv.metafile,
8688
};
8789

8890
const bundle = new Bundle(props);

packages/aws-cdk/.projen/tasks.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projenrc/bundle.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ export interface BundleProps {
9090
* @default false
9191
*/
9292
readonly minifySyntax?: boolean;
93+
94+
/**
95+
* Write a meatafile to the dist directory.
96+
*
97+
* @default false
98+
*/
99+
readonly metafile?: boolean;
93100
}
94101

95102
/**
@@ -138,9 +145,13 @@ export class BundleCli extends pj.Component {
138145
project.postCompileTask.exec(['node-bundle', 'validate', '--fix', ...args].join(' '));
139146

140147
// `node-bundle` replaces `npm pack`
148+
const packArgs = [
149+
...args,
150+
...(options.metafile ?? true) ? ['--metafile dist/metafile.json'] : [],
151+
];
141152
project.packageTask.reset();
142153
project.packageTask.exec('mkdir -p dist/js');
143-
project.packageTask.exec(['node-bundle', 'pack', '--destination', 'dist/js', ...args].join(' '));
154+
project.packageTask.exec(['node-bundle', 'pack', '--destination', 'dist/js', ...packArgs].join(' '));
144155
}
145156
}
146157

0 commit comments

Comments
 (0)