Skip to content

Commit 38ed278

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@angular-devkit/build-angular): sort bundle stats by size
Before ``` Initial Chunk Files | Names | Size runtime.js | runtime | 6.15 kB main.js | main | 56.9 kB vendor.js | vendor | 2.4 MB polyfills.js | polyfills | 141 kB styles.css | styles | 119 bytes ``` After ``` vendor.js | vendor | 2.07 MB polyfills.js | polyfills | 141 kB main.js | main | 55.8 kB runtime.js | runtime | 6.15 kB styles.css | styles | 119 bytes ```
1 parent ada9fa3 commit 38ed278

File tree

1 file changed

+29
-15
lines changed
  • packages/angular_devkit/build_angular/src/webpack/utils

1 file changed

+29
-15
lines changed

packages/angular_devkit/build_angular/src/webpack/utils/stats.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function formatSize(size: number): string {
2525
return `${+(size / Math.pow(1024, index)).toPrecision(3)} ${abbreviations[index]}`;
2626
}
2727

28-
export type BundleStatsData = [files: string, names: string, size: string];
28+
export type BundleStatsData = [files: string, names: string, size: number | string];
2929

3030
export interface BundleStats {
3131
initial: boolean;
@@ -43,35 +43,36 @@ export function generateBundleStats(
4343
},
4444
colors: boolean,
4545
): BundleStats {
46-
const g = (x: string) => (colors ? ansiColors.greenBright(x) : x);
47-
const c = (x: string) => (colors ? ansiColors.cyanBright(x) : x);
48-
49-
const size = typeof info.size === 'number' ? formatSize(info.size) : '-';
46+
const size = typeof info.size === 'number' ? info.size : '-';
5047
const files = info.files.filter(f => !f.endsWith('.map')).map(f => path.basename(f)).join(', ');
5148
const names = info.names?.length ? info.names.join(', ') : '-';
5249
const initial = !!(info.entry || info.initial);
5350

5451
return {
5552
initial,
56-
stats: [g(files), names, c(size)],
53+
stats: [files, names, size],
5754
}
5855
}
5956

6057
function generateBuildStatsTable(data: BundleStats[], colors: boolean): string {
58+
const g = (x: string) => colors ? ansiColors.greenBright(x) : x;
59+
const c = (x: string) => colors ? ansiColors.cyanBright(x) : x;
60+
const bold = (x: string) => colors ? ansiColors.bold(x) : x;
61+
const dim = (x: string) => colors ? ansiColors.dim(x) : x;
62+
6163
const changedEntryChunksStats: BundleStatsData[] = [];
6264
const changedLazyChunksStats: BundleStatsData[] = [];
65+
6366
for (const { initial, stats } of data) {
64-
if (initial) {
65-
changedEntryChunksStats.push(stats);
66-
} else {
67-
changedLazyChunksStats.push(stats);
68-
}
67+
const [files, names, size] = stats;
68+
(initial ? changedEntryChunksStats : changedLazyChunksStats).push([
69+
g(files),
70+
names,
71+
c(typeof size === 'string' ? size : formatSize(size)),
72+
]);
6973
}
7074

71-
const bundleInfo: string[][] = [];
72-
73-
const bold = (x: string) => colors ? ansiColors.bold(x) : x;
74-
const dim = (x: string) => colors ? ansiColors.dim(x) : x;
75+
const bundleInfo: (string | number)[][] = [];
7576

7677
// Entry chunks
7778
if (changedEntryChunksStats.length) {
@@ -124,6 +125,19 @@ function statsToString(json: any, statsConfig: any, bundleState?: BundleStats[])
124125
unchangedChunkNumber = json.chunks.length - changedChunksStats.length;
125126
}
126127

128+
// Sort chunks by size in descending order
129+
changedChunksStats.sort((a, b) => {
130+
if (a.stats[2] > b.stats[2]) {
131+
return -1;
132+
}
133+
134+
if (a.stats[2] < b.stats[2]) {
135+
return 1;
136+
}
137+
138+
return 0;
139+
});
140+
127141
const statsTable = generateBuildStatsTable(changedChunksStats, colors);
128142

129143
// In some cases we do things outside of webpack context

0 commit comments

Comments
 (0)