Skip to content

Commit ca0a836

Browse files
clydindgp1130
authored andcommitted
refactor(@angular-devkit/build-angular): move diagnostic logging out of build execution
The logging of diagnostic (error/warning) messages from the build execution within the `application` builder has been moved up one level. This allows the actual execution to focus more on generating a result and leaves the enclosing builder system to handle notification of the results. (cherry picked from commit b18bd20)
1 parent 433aef9 commit ca0a836

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import path from 'node:path';
1313
import { BuildOutputFile } from '../../tools/esbuild/bundler-context';
1414
import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result';
1515
import { shutdownSassWorkerPool } from '../../tools/esbuild/stylesheets/sass-language';
16-
import { withNoProgress, withSpinner, writeResultFiles } from '../../tools/esbuild/utils';
16+
import {
17+
logMessages,
18+
withNoProgress,
19+
withSpinner,
20+
writeResultFiles,
21+
} from '../../tools/esbuild/utils';
1722
import { deleteOutputDir } from '../../utils/delete-output-dir';
1823
import { shouldWatchRoot } from '../../utils/environment-options';
1924
import { NormalizedCachedOptions } from '../../utils/normalize-cache';
@@ -66,7 +71,11 @@ export async function* runEsBuildBuildAction(
6671
// Initial build
6772
let result: ExecutionResult;
6873
try {
74+
// Perform the build action
6975
result = await withProgress('Building...', () => action());
76+
77+
// Log all diagnostic (error/warning) messages from the build
78+
await logMessages(logger, result);
7079
} finally {
7180
// Ensure Sass workers are shutdown if not watching
7281
if (!watch) {
@@ -171,6 +180,9 @@ export async function* runEsBuildBuildAction(
171180
action(result.createRebuildState(changes)),
172181
);
173182

183+
// Log all diagnostic (error/warning) messages from the rebuild
184+
await logMessages(logger, result);
185+
174186
// Update watched locations provided by the new build result.
175187
// Keep watching all previous files if there are any errors; otherwise consider all
176188
// files stale until confirmed present in the new result's watch files.

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ import { BuildOutputFileType, BundlerContext } from '../../tools/esbuild/bundler
1313
import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result';
1414
import { checkCommonJSModules } from '../../tools/esbuild/commonjs-checker';
1515
import { extractLicenses } from '../../tools/esbuild/license-extractor';
16-
import {
17-
calculateEstimatedTransferSizes,
18-
logBuildStats,
19-
logMessages,
20-
} from '../../tools/esbuild/utils';
16+
import { calculateEstimatedTransferSizes, logBuildStats } from '../../tools/esbuild/utils';
2117
import { BudgetCalculatorResult, checkBudgets } from '../../utils/bundle-calculator';
2218
import { colors } from '../../utils/color';
2319
import { copyAssets } from '../../utils/copy-assets';
@@ -65,10 +61,8 @@ export async function executeBuild(
6561
rebuildState?.fileChanges.all,
6662
);
6763

68-
// Log all warnings and errors generated during bundling
69-
await logMessages(context, bundlingResult);
70-
7164
const executionResult = new ExecutionResult(bundlerContexts, codeBundleCache);
65+
executionResult.addWarnings(bundlingResult.warnings);
7266

7367
// Return if the bundling has errors
7468
if (bundlingResult.errors) {
@@ -188,16 +182,14 @@ export async function executeBuild(
188182
}
189183

190184
logBuildStats(
191-
context,
185+
context.logger,
192186
metafile,
193187
initialFiles,
194188
budgetFailures,
195189
changedFiles,
196190
estimatedTransferSizes,
197191
);
198192

199-
await logMessages(context, executionResult);
200-
201193
// Write metafile if stats option is enabled
202194
if (options.stats) {
203195
executionResult.addOutputFile(

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { BuilderContext } from '@angular-devkit/architect';
9+
import { logging } from '@angular-devkit/core';
1010
import { BuildOptions, Metafile, OutputFile, PartialMessage, formatMessages } from 'esbuild';
1111
import { createHash } from 'node:crypto';
1212
import { constants as fsConstants } from 'node:fs';
@@ -22,7 +22,7 @@ import { BuildOutputFile, BuildOutputFileType, InitialFileRecord } from './bundl
2222
import { BuildOutputAsset } from './bundler-execution-result';
2323

2424
export function logBuildStats(
25-
context: BuilderContext,
25+
logger: logging.LoggerApi,
2626
metafile: Metafile,
2727
initial: Map<string, InitialFileRecord>,
2828
budgetFailures: BudgetCalculatorResult[] | undefined,
@@ -71,12 +71,12 @@ export function logBuildStats(
7171
budgetFailures,
7272
);
7373

74-
context.logger.info('\n' + tableText + '\n');
74+
logger.info('\n' + tableText + '\n');
7575
} else if (changedFiles !== undefined) {
76-
context.logger.info('\nNo output file changes.\n');
76+
logger.info('\nNo output file changes.\n');
7777
}
7878
if (unchangedCount > 0) {
79-
context.logger.info(`Unchanged output files: ${unchangedCount}`);
79+
logger.info(`Unchanged output files: ${unchangedCount}`);
8080
}
8181
}
8282

@@ -143,17 +143,17 @@ export async function withNoProgress<T>(text: string, action: () => T | Promise<
143143
}
144144

145145
export async function logMessages(
146-
context: BuilderContext,
146+
logger: logging.LoggerApi,
147147
{ errors, warnings }: { errors?: PartialMessage[]; warnings?: PartialMessage[] },
148148
): Promise<void> {
149149
if (warnings?.length) {
150150
const warningMessages = await formatMessages(warnings, { kind: 'warning', color: true });
151-
context.logger.warn(warningMessages.join('\n'));
151+
logger.warn(warningMessages.join('\n'));
152152
}
153153

154154
if (errors?.length) {
155155
const errorMessages = await formatMessages(errors, { kind: 'error', color: true });
156-
context.logger.error(errorMessages.join('\n'));
156+
logger.error(errorMessages.join('\n'));
157157
}
158158
}
159159

0 commit comments

Comments
 (0)