Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): limit the number of lazy chunks v…
Browse files Browse the repository at this point in the history
…isible in the stats table

Prior to this change in the stats table we listed all the lazy chunk, in some cases this could be hundreds of files.

With this change, we limit the number of files listed. To display all entire list of files users would need to use the `--verbose` flag.
  • Loading branch information
alan-agius4 committed Jan 30, 2024
1 parent 41ea985 commit 944cbcd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function executeBuild(
cacheOptions,
prerenderOptions,
ssrOptions,
verbose,
} = options;

// TODO: Consider integrating into watch mode. Would require full rebuild on target changes.
Expand Down Expand Up @@ -190,6 +191,7 @@ export async function executeBuild(
changedFiles,
estimatedTransferSizes,
!!ssrOptions,
verbose,
);

// Write metafile if stats option is enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function logBuildStats(
changedFiles?: Set<string>,
estimatedTransferSizes?: Map<string, number>,
ssrOutputEnabled?: boolean,
verbose?: boolean,
): void {
const browserStats: BundleStats[] = [];
const serverStats: BundleStats[] = [];
Expand Down Expand Up @@ -86,6 +87,7 @@ export function logBuildStats(
unchangedCount === 0,
!!estimatedTransferSizes,
budgetFailures,
verbose,
);

logger.info(tableText + '\n');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ export function generateEsbuildBuildStatsTable(
showTotalSize: boolean,
showEstimatedTransferSize: boolean,
budgetFailures?: BudgetCalculatorResult[],
verbose?: boolean,
): string {
const bundleInfo = generateBuildStatsData(
browserStats,
colors,
showTotalSize,
showEstimatedTransferSize,
budgetFailures,
verbose,
);

if (serverStats.length) {
Expand All @@ -100,7 +102,7 @@ export function generateEsbuildBuildStatsTable(

bundleInfo.push(
[m('Server bundles')],
...generateBuildStatsData(serverStats, colors, false, false, undefined),
...generateBuildStatsData(serverStats, colors, false, false, undefined, verbose),
);
}

Expand All @@ -120,6 +122,7 @@ export function generateBuildStatsTable(
showTotalSize,
showEstimatedTransferSize,
budgetFailures,
true,
);

return generateTableText(bundleInfo, colors);
Expand All @@ -131,6 +134,7 @@ function generateBuildStatsData(
showTotalSize: boolean,
showEstimatedTransferSize: boolean,
budgetFailures?: BudgetCalculatorResult[],
verbose?: boolean,
): (string | number)[][] {
if (data.length === 0) {
return [];
Expand Down Expand Up @@ -159,7 +163,9 @@ function generateBuildStatsData(
const changedLazyChunksStats: BundleStatsData[] = [];

let initialTotalRawSize = 0;
let changedLazyChunksCount = 0;
let initialTotalEstimatedTransferSize;
const maxLazyChunksWithoutBudgetFailures = 15;

const budgets = new Map<string, string>();
if (budgetFailures) {
Expand Down Expand Up @@ -187,9 +193,20 @@ function generateBuildStatsData(

for (const { initial, stats } of data) {
const [files, names, rawSize, estimatedTransferSize] = stats;
if (
!initial &&
!verbose &&
changedLazyChunksStats.length >= maxLazyChunksWithoutBudgetFailures &&
!budgets.has(names) &&
!budgets.has(files)
) {
// Limit the number of lazy chunks displayed in the stats table when there is no budget failure and not in verbose mode.
changedLazyChunksCount++;
continue;
}

const getRawSizeColor = getSizeColor(names, files);
let data: BundleStatsData;

if (showEstimatedTransferSize) {
data = [
g(files),
Expand Down Expand Up @@ -223,25 +240,22 @@ function generateBuildStatsData(
}
} else {
changedLazyChunksStats.push(data);
changedLazyChunksCount++;
}
}

const bundleInfo: (string | number)[][] = [];
const baseTitles = ['Names', 'Raw size'];
const tableAlign: ('l' | 'r')[] = ['l', 'l', 'r'];

if (showEstimatedTransferSize) {
baseTitles.push('Estimated transfer size');
tableAlign.push('r');
}

// Entry chunks
if (changedEntryChunksStats.length) {
bundleInfo.push(['Initial chunk files', ...baseTitles].map(bold), ...changedEntryChunksStats);

if (showTotalSize) {
bundleInfo.push([]);

const initialSizeTotalColor = getSizeColor('bundle initial', undefined, (x) => x);
const totalSizeElements = [
' ',
Expand All @@ -255,7 +269,7 @@ function generateBuildStatsData(
: '-',
);
}
bundleInfo.push(totalSizeElements.map(bold));
bundleInfo.push([], totalSizeElements.map(bold));
}
}

Expand All @@ -267,12 +281,22 @@ function generateBuildStatsData(
// Lazy chunks
if (changedLazyChunksStats.length) {
bundleInfo.push(['Lazy chunk files', ...baseTitles].map(bold), ...changedLazyChunksStats);

if (changedLazyChunksCount > changedLazyChunksStats.length) {
bundleInfo.push([
dim(
`...and ${changedLazyChunksCount - changedLazyChunksStats.length} more lazy chunks files. ` +
'Use "--verbose" to show all the files.',
),
]);
}
}

return bundleInfo;
}

function generateTableText(bundleInfo: (string | number)[][], colors: boolean): string {
const skipText = (value: string) => value.includes('...and ');
const longest: number[] = [];
for (const item of bundleInfo) {
for (let i = 0; i < item.length; i++) {
Expand All @@ -281,6 +305,10 @@ function generateTableText(bundleInfo: (string | number)[][], colors: boolean):
}

const currentItem = item[i].toString();
if (skipText(currentItem)) {
continue;
}

const currentLongest = (longest[i] ??= 0);
const currentItemLength = removeColor(currentItem).length;
if (currentLongest < currentItemLength) {
Expand All @@ -298,10 +326,14 @@ function generateTableText(bundleInfo: (string | number)[][], colors: boolean):
}

const currentItem = item[i].toString();
if (skipText(currentItem)) {
continue;
}

const currentItemLength = removeColor(currentItem).length;
const stringPad = ' '.repeat(longest[i] - currentItemLength);
// Last item is right aligned, thus we add the padding at the start.
item[i] = longest.length === i + 1 ? stringPad + currentItem : currentItem + stringPad;
// Values in columns at index 2 and 3 (Raw and Estimated sizes) are always right aligned.
item[i] = i >= 2 ? stringPad + currentItem : currentItem + stringPad;
}

outputTable.push(item.join(seperator));
Expand Down

0 comments on commit 944cbcd

Please sign in to comment.