Skip to content

Commit d4f37da

Browse files
clydindgp1130
authored andcommitted
fix(@angular-devkit/build-angular): only show changed output files in watch mode with esbuild
Similar to how the Webpack-based `browser` builder only shows the output files that have changed during watch mode (including `ng serve`), the esbuild-based builders will now also only show changed files. For large applications, this can greatly reduce the amount of text shown in the console when actively developing in watch mode. (cherry picked from commit 5a25398)
1 parent 9994b2d commit d4f37da

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

packages/angular_devkit/build_angular/src/builders/application/execute-build.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,16 @@ export async function executeBuild(
256256
}
257257

258258
printWarningsAndErrorsToConsole(context, warnings, errors);
259-
logBuildStats(context, metafile, initialFiles, budgetFailures, estimatedTransferSizes);
259+
const changedFiles =
260+
rebuildState && executionResult.findChangedFiles(rebuildState.previousOutputHashes);
261+
logBuildStats(
262+
context,
263+
metafile,
264+
initialFiles,
265+
budgetFailures,
266+
changedFiles,
267+
estimatedTransferSizes,
268+
);
260269

261270
// Write metafile if stats option is enabled
262271
if (options.stats) {

packages/angular_devkit/build_angular/src/tools/esbuild/bundler-execution-result.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface RebuildState {
2121
rebuildContexts: BundlerContext[];
2222
codeBundleCache?: SourceFileCache;
2323
fileChanges: ChangedFiles;
24+
previousOutputHashes: Map<string, string>;
2425
}
2526

2627
/**
@@ -91,9 +92,22 @@ export class ExecutionResult {
9192
rebuildContexts: this.rebuildContexts,
9293
codeBundleCache: this.codeBundleCache,
9394
fileChanges,
95+
previousOutputHashes: new Map(this.outputFiles.map((file) => [file.path, file.hash])),
9496
};
9597
}
9698

99+
findChangedFiles(previousOutputHashes: Map<string, string>): Set<string> {
100+
const changed = new Set<string>();
101+
for (const file of this.outputFiles) {
102+
const previousHash = previousOutputHashes.get(file.path);
103+
if (previousHash === undefined || previousHash !== file.hash) {
104+
changed.add(file.path);
105+
}
106+
}
107+
108+
return changed;
109+
}
110+
97111
async dispose(): Promise<void> {
98112
await Promise.allSettled(this.rebuildContexts.map((context) => context.dispose()));
99113
}

packages/angular_devkit/build_angular/src/tools/esbuild/utils.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ export function logBuildStats(
2828
metafile: Metafile,
2929
initial: Map<string, InitialFileRecord>,
3030
budgetFailures: BudgetCalculatorResult[] | undefined,
31+
changedFiles?: Set<string>,
3132
estimatedTransferSizes?: Map<string, number>,
3233
): void {
3334
const stats: BundleStats[] = [];
35+
let unchangedCount = 0;
3436
for (const [file, output] of Object.entries(metafile.outputs)) {
3537
// Only display JavaScript and CSS files
3638
if (!file.endsWith('.js') && !file.endsWith('.css')) {
@@ -42,6 +44,12 @@ export function logBuildStats(
4244
continue;
4345
}
4446

47+
// Show only changed files if a changed list is provided
48+
if (changedFiles && !changedFiles.has(file)) {
49+
++unchangedCount;
50+
continue;
51+
}
52+
4553
let name = initial.get(file)?.name;
4654
if (name === undefined && output.entryPoint) {
4755
name = path
@@ -56,15 +64,22 @@ export function logBuildStats(
5664
});
5765
}
5866

59-
const tableText = generateBuildStatsTable(
60-
stats,
61-
true,
62-
true,
63-
!!estimatedTransferSizes,
64-
budgetFailures,
65-
);
67+
if (stats.length > 0) {
68+
const tableText = generateBuildStatsTable(
69+
stats,
70+
true,
71+
unchangedCount === 0,
72+
!!estimatedTransferSizes,
73+
budgetFailures,
74+
);
6675

67-
context.logger.info('\n' + tableText + '\n');
76+
context.logger.info('\n' + tableText + '\n');
77+
} else if (changedFiles !== undefined) {
78+
context.logger.info('\nNo output file changes.\n');
79+
}
80+
if (unchangedCount > 0) {
81+
context.logger.info(`Unchanged output files: ${unchangedCount}`);
82+
}
6883
}
6984

7085
export async function calculateEstimatedTransferSizes(

0 commit comments

Comments
 (0)