Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): do not print `Angular is running …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
alan-agius4 committed Oct 13, 2023
1 parent 3f679f1 commit 9ad99c1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export function createServerCodeBundleOptions(
`import moduleOrBootstrapFn from './${mainServerEntryPoint}';`,
`export default moduleOrBootstrapFn;`,
`export * from './${mainServerEntryPoint}';`,
`export { ɵConsole } from '@angular/core';`,
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createPlatformFactory,
platformCore,
ɵwhenStable as whenStable,
ɵConsole,
} from '@angular/core';
import {
INITIAL_CONFIG,
Expand Down Expand Up @@ -79,12 +80,26 @@ export async function* extractRoutes(
document: string,
): AsyncIterableIterator<RouterResult> {
const platformRef = createPlatformFactory(platformCore, 'server', [
[
{
provide: INITIAL_CONFIG,
useValue: { document, url: '' },
{
provide: INITIAL_CONFIG,
useValue: { document, url: '' },
},
{
provide: ɵConsole,
/** An Angular Console Provider that does not print a set of predefined logs. */
useFactory: () => {
class Console extends ɵConsole {
private readonly ignoredLogs = new Set(['Angular is running in development mode.']);
override log(message: string): void {
if (!this.ignoredLogs.has(message)) {
super.log(message);
}
}
}

return new Console();
},
],
},
...INTERNAL_SERVER_PLATFORM_PROVIDERS,
])();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import type { ApplicationRef, Type } from '@angular/core';
import type { ApplicationRef, Type, ɵConsole } from '@angular/core';
import type { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';
import type { extractRoutes } from '../routes-extractor/extractor';

Expand All @@ -27,4 +27,7 @@ export interface MainServerBundleExports {
extractRoutes: typeof extractRoutes;

ɵresetCompiledComponents?: () => void;

/** Angular Console token/class. */
ɵConsole: typeof ɵConsole;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { basename } from 'node:path';
import { InlineCriticalCssProcessor } from '../index-file/inline-critical-css';
import { loadEsmModule } from '../load-esm';
import { MainServerBundleExports } from './main-bundle-exports';
import { patchConsoleToIgnoreSpecificLogs } from './utils';

export interface RenderOptions {
route: string;
Expand Down Expand Up @@ -47,6 +46,7 @@ export async function renderPage({
renderModule,
renderApplication,
ɵresetCompiledComponents,
ɵConsole,
} = await loadBundle('./main.server.mjs');

// Need to clean up GENERATED_COMP_IDS map in `@angular/core`.
Expand All @@ -59,27 +59,38 @@ export async function renderPage({
provide: ɵSERVER_CONTEXT,
useValue: serverContext,
},
{
provide: ɵConsole,
/** An Angular Console Provider that does not print a set of predefined logs. */
useFactory: () => {
class Console extends ɵConsole {
private readonly ignoredLogs = new Set(['Angular is running in development mode.']);
override log(message: string): void {
if (!this.ignoredLogs.has(message)) {
super.log(message);
}
}
}

return new Console();
},
},
];

let html: string | undefined;

const resetPatchedConsole = patchConsoleToIgnoreSpecificLogs();
try {
if (isBootstrapFn(bootstrapAppFnOrModule)) {
html = await renderApplication(bootstrapAppFnOrModule, {
document,
url: route,
platformProviders,
});
} else {
html = await renderModule(bootstrapAppFnOrModule, {
document,
url: route,
extraProviders: platformProviders,
});
}
} finally {
resetPatchedConsole();
if (isBootstrapFn(bootstrapAppFnOrModule)) {
html = await renderApplication(bootstrapAppFnOrModule, {
document,
url: route,
platformProviders,
});
} else {
html = await renderModule(bootstrapAppFnOrModule, {
document,
url: route,
extraProviders: platformProviders,
});
}

if (inlineCriticalCss) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { workerData } from 'node:worker_threads';
import { loadEsmModule } from '../load-esm';
import type { ESMInMemoryFileLoaderWorkerData } from './esm-in-memory-loader/loader-hooks';
import { MainServerBundleExports } from './main-bundle-exports';
import { patchConsoleToIgnoreSpecificLogs } from './utils';

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

export default async function (): Promise<RoutersExtractorWorkerResult> {
patchConsoleToIgnoreSpecificLogs();

const { default: bootstrapAppFnOrModule, extractRoutes } =
await loadEsmModule<MainServerBundleExports>('./main.server.mjs');

Expand Down

This file was deleted.

0 comments on commit 9ad99c1

Please sign in to comment.