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 b2678bb commit 3f679f1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,6 @@ export async function setupServer(
}

transformIndexHtmlAndAddHeaders(url, rawHtml, res, next, async (html) => {
/* eslint-disable no-console */
const originalConsoleLog = console.log;
console.log = (...args) => {
if (args[0] !== 'Angular is running in development mode.') {
originalConsoleLog.apply(args);
}
};

const { content } = await renderPage({
document: html,
route: pathnameWithoutServePath(url, serverOptions),
Expand All @@ -505,9 +497,6 @@ export async function setupServer(
inlineCriticalCss: false,
});

console.log = originalConsoleLog;
/* eslint-enable no-console */

return content;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 @@ -62,18 +63,23 @@ export async function renderPage({

let html: string | undefined;

if (isBootstrapFn(bootstrapAppFnOrModule)) {
html = await renderApplication(bootstrapAppFnOrModule, {
document,
url: route,
platformProviders,
});
} else {
html = await renderModule(bootstrapAppFnOrModule, {
document,
url: route,
extraProviders: platformProviders,
});
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 (inlineCriticalCss) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 @@ -27,6 +28,8 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

const IGNORED_LOGS = new Set(['Angular is running in development mode.']);
const PATCHED_CONSOLE_SYMBOL = Symbol.for('Angular CLI Console Patched');

/** Method to filter a number of console.log from the output.
* @returns a function that when invoked restores the default console.log behaviour.
*/
export function patchConsoleToIgnoreSpecificLogs(): () => void {
/* eslint-disable no-console, @typescript-eslint/no-explicit-any */
if (!(console as any)[PATCHED_CONSOLE_SYMBOL]) {
const originalConsoleLog = console.log;

console.log = (...args) => {
if (!IGNORED_LOGS.has(args[0])) {
originalConsoleLog.apply(args);
}
};

(console as any)[PATCHED_CONSOLE_SYMBOL] = () => {
console.log = originalConsoleLog;
delete (console as any)[PATCHED_CONSOLE_SYMBOL];
};
}

return (console as any)[PATCHED_CONSOLE_SYMBOL];
/* eslint-enable no-console, @typescript-eslint/no-explicit-any */
}

0 comments on commit 3f679f1

Please sign in to comment.