Skip to content

Commit bbb1625

Browse files
authored
chore: print the bundle size of ecs tasks (#1439)
This was lost when we moved to a custom bundler. Manually recreates the output format that esbuild uses. Previously: <img width="1668" alt="image" src="https://github.com/user-attachments/assets/cd65b09f-1252-4f42-b0b7-42c1b952b36a"> Now: <img width="1664" alt="image" src="https://github.com/user-attachments/assets/9d722f38-884b-45fd-b0a8-24f4942b024e"> ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d14635a commit bbb1625

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

.projen/deps.json

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projen/tasks.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const project = new CdklabsConstructLibrary({
5555
'aws-xray-sdk-core',
5656
'case',
5757
'cdk-dia',
58+
'chalk',
5859
'esbuild',
5960
'feed',
6061
'fs-extra',

package.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projenrc/bundle-javascript-for-ecs.exec.ts

+52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import { basename, dirname, posix } from 'path';
2+
import { Instance as Chalk } from 'chalk';
13
import * as esbuild from 'esbuild';
24

5+
const chalk = new Chalk({
6+
level: process.env.NO_COLOR ? 0 : 1,
7+
});
8+
39
async function main(args: string[]) {
410
const [entrypoint, outfile, maybeWatch] = args;
511
if (!outfile) {
@@ -53,12 +59,58 @@ async function main(args: string[]) {
5359
`Bundle built from '${entrypoint}' contains 'aws-cdk-lib' somewhere in its dependency closure. Shake it!`
5460
);
5561
}
62+
63+
if (result.metafile) {
64+
console.log('\n' + formatOutputs(result.metafile).join('\n') + '\n');
65+
}
5666
}
5767
} finally {
5868
await context.dispose();
5969
}
6070
}
6171

72+
function formatOutputs(metafile: esbuild.Metafile): string[] {
73+
const files = new Array<[string, number]>();
74+
for (const output of Object.entries(metafile.outputs)) {
75+
files.push([output[0], output[1].bytes]);
76+
}
77+
78+
// get the length of the longest file
79+
const max = files.reduce((a, b) => (a > b[0].length ? a : b[0].length), 0);
80+
81+
return files
82+
.sort((a, b) => a[0].localeCompare(b[0]))
83+
.map(
84+
([file, size]) =>
85+
' '.repeat(2) +
86+
formatPath(file) +
87+
' '.repeat(2 + (max - file.length)) +
88+
formatSize(size)
89+
);
90+
}
91+
92+
function formatPath(path: string): string {
93+
return `${dirname(path)}${posix.sep}${chalk.bold(basename(path))}`;
94+
}
95+
96+
function formatSize(bytes: number, warnLimit = 10_000_000): string {
97+
const shouldWarn = bytes >= warnLimit;
98+
const paint = shouldWarn ? chalk.yellow : chalk.cyan;
99+
100+
const result = [paint(formatBytes(bytes))];
101+
if (shouldWarn) {
102+
result.push(chalk.yellow(`⚠`));
103+
}
104+
return result.join(' ');
105+
}
106+
107+
function formatBytes(bytes: number): string {
108+
const k = bytes > 0 ? Math.floor(Math.log2(bytes) / 10) : 0;
109+
const rank = ['b', 'kb', 'mb', 'gb', 'tb'][k];
110+
const value = bytes / Math.pow(1000, k);
111+
return value.toFixed(1) + rank;
112+
}
113+
62114
function ignoreWarnings(
63115
result: esbuild.BuildResult,
64116
packagesWithAllowedWarnings: string[]

0 commit comments

Comments
 (0)