Skip to content

Commit 145bfe8

Browse files
committed
fix(@angular/build): enhance build logs and output location for library builder
Align library builder logs with the application builder format by logging: - `Library bundle generation complete. [X seconds] - <timestamp>` on build completion - `Output location: <outputPath>` for successful builds - `Watch mode enabled. Watching for file changes...` after initial build when watch mode is active
1 parent 643947e commit 145bfe8

2 files changed

Lines changed: 71 additions & 12 deletions

File tree

‎packages/angular/build/src/builders/library/builder.ts‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ export async function* executeLibraryBuilder(
118118
}
119119

120120
if (isWatchMode) {
121-
if (progress) {
122-
logger.info('Watch mode enabled. Watching for file changes...');
123-
}
124-
125121
const { setupWatcher } = await import('../../utils/watcher');
126122
watcher = await setupWatcher({
127123
workspaceRoot,
@@ -158,6 +154,10 @@ export async function* executeLibraryBuilder(
158154
return;
159155
}
160156

157+
if (progress) {
158+
logger.info('Watch mode enabled. Watching for file changes...');
159+
}
160+
161161
yield* runWatchLoop(
162162
watcher,
163163
normalizedOptions,
@@ -190,11 +190,11 @@ async function executeBuild(
190190
buildAction: typeof import('./pipeline/build-action').buildAction,
191191
): Promise<BuilderOutput> {
192192
const startTime = process.hrtime.bigint();
193-
const { context, watchedCompilationFiles, isWatchMode } = actionContext;
193+
const { context, watchedCompilationFiles, isWatchMode, options } = actionContext;
194194

195195
try {
196196
await withProgress(message, () => buildAction(actionContext));
197-
logBuildResult(context.logger, startTime, true);
197+
logBuildResult(context.logger, startTime, true, options.outputPath);
198198
if (isWatchMode) {
199199
logCumulativeDurations();
200200
}
@@ -343,14 +343,23 @@ async function* runWatchLoop(
343343
/**
344344
* Logs the build completion time and status.
345345
*/
346-
function logBuildResult(logger: logging.LoggerApi, startTime: bigint, success: boolean): void {
347-
const durationMs = Number(process.hrtime.bigint() - startTime) / 1_000_000;
348-
const durationSec = (durationMs / 1000).toFixed(2);
346+
function logBuildResult(
347+
logger: logging.LoggerApi,
348+
startTime: bigint,
349+
success: boolean,
350+
outputPath?: string,
351+
): void {
352+
const buildTime = Number(process.hrtime.bigint() - startTime) / 10 ** 9;
353+
const message =
354+
`Library bundle generation ${success ? 'complete' : 'failed'}.` +
355+
` [${buildTime.toFixed(3)} seconds] - ${new Date().toISOString()}`;
349356

350357
if (success) {
351-
logger.info(`Build at: ${new Date().toISOString()} - Time: ${durationMs.toFixed(0)}ms`);
352-
logger.info(`Built Angular library in ${durationSec}s.`);
358+
logger.info(message);
359+
if (outputPath) {
360+
logger.info(`Output location: ${outputPath}\n`);
361+
}
353362
} else {
354-
logger.error(`Build failed after ${durationSec}s.`);
363+
logger.error(message);
355364
}
356365
}

‎packages/angular/build/src/builders/library/tests/behavior/build_spec.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,55 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
139139
tslib: '^2.0.0',
140140
});
141141
});
142+
143+
it('should log completion and output location', async () => {
144+
harness.useTarget('build', {
145+
...BASE_OPTIONS,
146+
});
147+
148+
const { result, logs } = await harness.executeOnce();
149+
expect(result?.success).toBeTrue();
150+
expect(logs).toContain(
151+
jasmine.objectContaining({
152+
level: 'info',
153+
message: jasmine.stringMatching(
154+
/Library bundle generation complete\. \[\d+\.\d{3} seconds\] - \d{4}-\d{2}-\d{2}T/,
155+
),
156+
}),
157+
);
158+
expect(logs).toContain(
159+
jasmine.objectContaining({
160+
level: 'info',
161+
message: jasmine.stringMatching(/Output location: .*dist[/\\]lib/),
162+
}),
163+
);
164+
});
165+
166+
it('should log failure on build error', async () => {
167+
await harness.writeFile(
168+
'projects/lib/src/public-api.ts',
169+
`export const title: number = 'invalid type';`,
170+
);
171+
172+
harness.useTarget('build', {
173+
...BASE_OPTIONS,
174+
});
175+
176+
const { result, logs } = await harness.executeOnce();
177+
expect(result?.success).toBeFalse();
178+
expect(logs).toContain(
179+
jasmine.objectContaining({
180+
level: 'error',
181+
message: jasmine.stringMatching(
182+
/Library bundle generation failed\. \[\d+\.\d{3} seconds\] - \d{4}-\d{2}-\d{2}T/,
183+
),
184+
}),
185+
);
186+
expect(logs).not.toContain(
187+
jasmine.objectContaining({
188+
message: jasmine.stringMatching(/Output location:/),
189+
}),
190+
);
191+
});
142192
});
143193
});

0 commit comments

Comments
 (0)