Skip to content

Commit e817656

Browse files
committed
fix(@angular-devkit/build-angular): do not print Angular is running in development mode. in the server console when running prerender in dev mode
Prior to this change `Angular is running in development mode` was printed multiple times when running prerendering in devmode. (cherry picked from commit 3f679f1)
1 parent b78508f commit e817656

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,6 @@ export async function setupServer(
483483
}
484484

485485
transformIndexHtmlAndAddHeaders(url, rawHtml, res, next, async (html) => {
486-
/* eslint-disable no-console */
487-
const originalConsoleLog = console.log;
488-
console.log = (...args) => {
489-
if (args[0] !== 'Angular is running in development mode.') {
490-
originalConsoleLog.apply(args);
491-
}
492-
};
493-
494486
const { content } = await renderPage({
495487
document: html,
496488
route: pathnameWithoutServePath(url, serverOptions),
@@ -505,9 +497,6 @@ export async function setupServer(
505497
inlineCriticalCss: false,
506498
});
507499

508-
console.log = originalConsoleLog;
509-
/* eslint-enable no-console */
510-
511500
return content;
512501
});
513502
}

packages/angular_devkit/build_angular/src/utils/server-rendering/render-page.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { basename } from 'node:path';
1111
import { InlineCriticalCssProcessor } from '../index-file/inline-critical-css';
1212
import { loadEsmModule } from '../load-esm';
1313
import { MainServerBundleExports } from './main-bundle-exports';
14+
import { patchConsoleToIgnoreSpecificLogs } from './utils';
1415

1516
export interface RenderOptions {
1617
route: string;
@@ -62,18 +63,23 @@ export async function renderPage({
6263

6364
let html: string | undefined;
6465

65-
if (isBootstrapFn(bootstrapAppFnOrModule)) {
66-
html = await renderApplication(bootstrapAppFnOrModule, {
67-
document,
68-
url: route,
69-
platformProviders,
70-
});
71-
} else {
72-
html = await renderModule(bootstrapAppFnOrModule, {
73-
document,
74-
url: route,
75-
extraProviders: platformProviders,
76-
});
66+
const resetPatchedConsole = patchConsoleToIgnoreSpecificLogs();
67+
try {
68+
if (isBootstrapFn(bootstrapAppFnOrModule)) {
69+
html = await renderApplication(bootstrapAppFnOrModule, {
70+
document,
71+
url: route,
72+
platformProviders,
73+
});
74+
} else {
75+
html = await renderModule(bootstrapAppFnOrModule, {
76+
document,
77+
url: route,
78+
extraProviders: platformProviders,
79+
});
80+
}
81+
} finally {
82+
resetPatchedConsole();
7783
}
7884

7985
if (inlineCriticalCss) {

packages/angular_devkit/build_angular/src/utils/server-rendering/routes-extractor-worker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { workerData } from 'node:worker_threads';
1010
import { loadEsmModule } from '../load-esm';
1111
import type { ESMInMemoryFileLoaderWorkerData } from './esm-in-memory-loader/loader-hooks';
1212
import { MainServerBundleExports } from './main-bundle-exports';
13+
import { patchConsoleToIgnoreSpecificLogs } from './utils';
1314

1415
export interface RoutesExtractorWorkerData extends ESMInMemoryFileLoaderWorkerData {
1516
document: string;
@@ -27,6 +28,8 @@ export interface RoutersExtractorWorkerResult {
2728
const { document, verbose } = workerData as RoutesExtractorWorkerData;
2829

2930
export default async function (): Promise<RoutersExtractorWorkerResult> {
31+
patchConsoleToIgnoreSpecificLogs();
32+
3033
const { default: bootstrapAppFnOrModule, extractRoutes } =
3134
await loadEsmModule<MainServerBundleExports>('./main.server.mjs');
3235

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
const IGNORED_LOGS = new Set(['Angular is running in development mode.']);
10+
const PATCHED_CONSOLE_SYMBOL = Symbol.for('Angular CLI Console Patched');
11+
12+
/** Method to filter a number of console.log from the output.
13+
* @returns a function that when invoked restores the default console.log behaviour.
14+
*/
15+
export function patchConsoleToIgnoreSpecificLogs(): () => void {
16+
/* eslint-disable no-console, @typescript-eslint/no-explicit-any */
17+
if (!(console as any)[PATCHED_CONSOLE_SYMBOL]) {
18+
const originalConsoleLog = console.log;
19+
20+
console.log = (...args) => {
21+
if (!IGNORED_LOGS.has(args[0])) {
22+
originalConsoleLog.apply(args);
23+
}
24+
};
25+
26+
(console as any)[PATCHED_CONSOLE_SYMBOL] = () => {
27+
console.log = originalConsoleLog;
28+
delete (console as any)[PATCHED_CONSOLE_SYMBOL];
29+
};
30+
}
31+
32+
return (console as any)[PATCHED_CONSOLE_SYMBOL];
33+
/* eslint-enable no-console, @typescript-eslint/no-explicit-any */
34+
}

0 commit comments

Comments
 (0)