Skip to content

Commit 9ad99c1

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.
1 parent 3f679f1 commit 9ad99c1

File tree

6 files changed

+54
-61
lines changed

6 files changed

+54
-61
lines changed

packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ export function createServerCodeBundleOptions(
278278
`import moduleOrBootstrapFn from './${mainServerEntryPoint}';`,
279279
`export default moduleOrBootstrapFn;`,
280280
`export * from './${mainServerEntryPoint}';`,
281+
`export { ɵConsole } from '@angular/core';`,
281282
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
282283
];
283284

packages/angular_devkit/build_angular/src/utils/routes-extractor/extractor.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
createPlatformFactory,
1515
platformCore,
1616
ɵwhenStable as whenStable,
17+
ɵConsole,
1718
} from '@angular/core';
1819
import {
1920
INITIAL_CONFIG,
@@ -79,12 +80,26 @@ export async function* extractRoutes(
7980
document: string,
8081
): AsyncIterableIterator<RouterResult> {
8182
const platformRef = createPlatformFactory(platformCore, 'server', [
82-
[
83-
{
84-
provide: INITIAL_CONFIG,
85-
useValue: { document, url: '' },
83+
{
84+
provide: INITIAL_CONFIG,
85+
useValue: { document, url: '' },
86+
},
87+
{
88+
provide: ɵConsole,
89+
/** An Angular Console Provider that does not print a set of predefined logs. */
90+
useFactory: () => {
91+
class Console extends ɵConsole {
92+
private readonly ignoredLogs = new Set(['Angular is running in development mode.']);
93+
override log(message: string): void {
94+
if (!this.ignoredLogs.has(message)) {
95+
super.log(message);
96+
}
97+
}
98+
}
99+
100+
return new Console();
86101
},
87-
],
102+
},
88103
...INTERNAL_SERVER_PLATFORM_PROVIDERS,
89104
])();
90105

packages/angular_devkit/build_angular/src/utils/server-rendering/main-bundle-exports.ts

Lines changed: 4 additions & 1 deletion
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 type { ApplicationRef, Type } from '@angular/core';
9+
import type { ApplicationRef, Type, ɵConsole } from '@angular/core';
1010
import type { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';
1111
import type { extractRoutes } from '../routes-extractor/extractor';
1212

@@ -27,4 +27,7 @@ export interface MainServerBundleExports {
2727
extractRoutes: typeof extractRoutes;
2828

2929
ɵresetCompiledComponents?: () => void;
30+
31+
/** Angular Console token/class. */
32+
ɵConsole: typeof ɵConsole;
3033
}

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ 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';
1514

1615
export interface RenderOptions {
1716
route: string;
@@ -47,6 +46,7 @@ export async function renderPage({
4746
renderModule,
4847
renderApplication,
4948
ɵresetCompiledComponents,
49+
ɵConsole,
5050
} = await loadBundle('./main.server.mjs');
5151

5252
// Need to clean up GENERATED_COMP_IDS map in `@angular/core`.
@@ -59,27 +59,38 @@ export async function renderPage({
5959
provide: ɵSERVER_CONTEXT,
6060
useValue: serverContext,
6161
},
62+
{
63+
provide: ɵConsole,
64+
/** An Angular Console Provider that does not print a set of predefined logs. */
65+
useFactory: () => {
66+
class Console extends ɵConsole {
67+
private readonly ignoredLogs = new Set(['Angular is running in development mode.']);
68+
override log(message: string): void {
69+
if (!this.ignoredLogs.has(message)) {
70+
super.log(message);
71+
}
72+
}
73+
}
74+
75+
return new Console();
76+
},
77+
},
6278
];
6379

6480
let html: string | undefined;
6581

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();
82+
if (isBootstrapFn(bootstrapAppFnOrModule)) {
83+
html = await renderApplication(bootstrapAppFnOrModule, {
84+
document,
85+
url: route,
86+
platformProviders,
87+
});
88+
} else {
89+
html = await renderModule(bootstrapAppFnOrModule, {
90+
document,
91+
url: route,
92+
extraProviders: platformProviders,
93+
});
8394
}
8495

8596
if (inlineCriticalCss) {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ 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';
1413

1514
export interface RoutesExtractorWorkerData extends ESMInMemoryFileLoaderWorkerData {
1615
document: string;
@@ -28,8 +27,6 @@ export interface RoutersExtractorWorkerResult {
2827
const { document, verbose } = workerData as RoutesExtractorWorkerData;
2928

3029
export default async function (): Promise<RoutersExtractorWorkerResult> {
31-
patchConsoleToIgnoreSpecificLogs();
32-
3330
const { default: bootstrapAppFnOrModule, extractRoutes } =
3431
await loadEsmModule<MainServerBundleExports>('./main.server.mjs');
3532

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

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)