|
| 1 | +import { basename, dirname, posix } from 'path'; |
| 2 | +import { Instance as Chalk } from 'chalk'; |
1 | 3 | import * as esbuild from 'esbuild';
|
2 | 4 |
|
| 5 | +const chalk = new Chalk({ |
| 6 | + level: process.env.NO_COLOR ? 0 : 1, |
| 7 | +}); |
| 8 | + |
3 | 9 | async function main(args: string[]) {
|
4 | 10 | const [entrypoint, outfile, maybeWatch] = args;
|
5 | 11 | if (!outfile) {
|
@@ -53,12 +59,58 @@ async function main(args: string[]) {
|
53 | 59 | `Bundle built from '${entrypoint}' contains 'aws-cdk-lib' somewhere in its dependency closure. Shake it!`
|
54 | 60 | );
|
55 | 61 | }
|
| 62 | + |
| 63 | + if (result.metafile) { |
| 64 | + console.log('\n' + formatOutputs(result.metafile).join('\n') + '\n'); |
| 65 | + } |
56 | 66 | }
|
57 | 67 | } finally {
|
58 | 68 | await context.dispose();
|
59 | 69 | }
|
60 | 70 | }
|
61 | 71 |
|
| 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 | + |
62 | 114 | function ignoreWarnings(
|
63 | 115 | result: esbuild.BuildResult,
|
64 | 116 | packagesWithAllowedWarnings: string[]
|
|
0 commit comments