From 744ff8cdbbbd64e99c44e70976742b41554c801a Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Tue, 24 Mar 2026 12:40:43 +0000 Subject: [PATCH] fix(angular-rspack): ensure rebuild chunks emitted summary accurate --- .../src/compilation/setup-compilation.ts | 7 +++-- .../angular-rspack/src/lib/utils/stats.ts | 30 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/angular-rspack-compiler/src/compilation/setup-compilation.ts b/packages/angular-rspack-compiler/src/compilation/setup-compilation.ts index 426b61a36b7..0fe22ea4afd 100644 --- a/packages/angular-rspack-compiler/src/compilation/setup-compilation.ts +++ b/packages/angular-rspack-compiler/src/compilation/setup-compilation.ts @@ -45,6 +45,9 @@ export const DEFAULT_NG_COMPILER_OPTIONS: ts.CompilerOptions = { supportJitMode: false, }; +let COMPONENT_STYLESHEET_BUNDLER: ComponentStylesheetBundler | undefined = + undefined; + export async function setupCompilation( config: Pick, options: SetupCompilationOptions @@ -86,7 +89,7 @@ export async function setupCompilation( } } - const componentStylesheetBundler = new ComponentStylesheetBundler( + COMPONENT_STYLESHEET_BUNDLER ??= new ComponentStylesheetBundler( { workspaceRoot: options.root, optimization: config.mode === 'production', @@ -116,7 +119,7 @@ export async function setupCompilation( return { rootNames, compilerOptions, - componentStylesheetBundler, + componentStylesheetBundler: COMPONENT_STYLESHEET_BUNDLER, }; } diff --git a/packages/angular-rspack/src/lib/utils/stats.ts b/packages/angular-rspack/src/lib/utils/stats.ts index 4e97867112d..93e25837c9c 100644 --- a/packages/angular-rspack/src/lib/utils/stats.ts +++ b/packages/angular-rspack/src/lib/utils/stats.ts @@ -101,6 +101,7 @@ function generateBundleStats(info: { // Ideally, we should create the logging callback as a factory, but that would need a refactoring. const runsCache = new Set(); +const chunkHashCache = new Map>(); function statsToString( stats: Stats, @@ -125,11 +126,20 @@ function statsToString( let hasEstimatedTransferSizes = false; const isFirstRun = !runsCache.has(json.outputPath || ''); + const prevHashes = + chunkHashCache.get(json.outputPath || '') ?? + new Map(); + const nextHashes = new Map(); for (const chunk of json.chunks) { - // During first build we want to display unchanged chunks - // but unchanged cached chunks are always marked as not rendered. - if (!isFirstRun && !chunk.rendered) { + const key = chunk.id ?? chunk.names?.[0] ?? ''; + const hash = chunk.hash ?? ''; + nextHashes.set(key, hash); + + // During first build we want to display all chunks. + // On rebuilds, skip chunks whose hash hasn't changed. + if (!isFirstRun && prevHashes.get(key) === hash) { + unchangedChunkNumber++; continue; } @@ -199,9 +209,8 @@ function statsToString( generateBundleStats({ ...chunk, rawSize, estimatedTransferSize }) ); } - unchangedChunkNumber = json.chunks.length - changedChunksStats.length; - runsCache.add(json.outputPath || ''); + chunkHashCache.set(json.outputPath || '', nextHashes); const statsTable = generateBuildStatsTable( changedChunksStats, @@ -421,11 +430,18 @@ export function generateBuildEventStats( const allChunksCount = chunks.length; const isFirstRun = !runsCache.has(rspackStats.outputPath || ''); + const prevHashes = + chunkHashCache.get(rspackStats.outputPath || '') ?? + new Map(); const chunkFiles = new Set(); for (const chunk of chunks) { - if (!isFirstRun && chunk.rendered) { - changedChunksCount++; + if (!isFirstRun) { + const key = chunk.id ?? chunk.names?.[0] ?? ''; + const hash = chunk.hash ?? ''; + if (prevHashes.get(key) !== hash) { + changedChunksCount++; + } } if (chunk.initial) {