From 3f679f165ba8ee8b5ed88713bb984044e5b8e8f0 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 12 Oct 2023 07:51:07 +0000 Subject: [PATCH] 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. --- .../src/builders/dev-server/vite-server.ts | 11 ------ .../src/utils/server-rendering/render-page.ts | 30 +++++++++------- .../routes-extractor-worker.ts | 3 ++ .../src/utils/server-rendering/utils.ts | 34 +++++++++++++++++++ 4 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 packages/angular_devkit/build_angular/src/utils/server-rendering/utils.ts diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts index 8313e1be1667..4e42a1b3b194 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts @@ -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), @@ -505,9 +497,6 @@ export async function setupServer( inlineCriticalCss: false, }); - console.log = originalConsoleLog; - /* eslint-enable no-console */ - return content; }); } diff --git a/packages/angular_devkit/build_angular/src/utils/server-rendering/render-page.ts b/packages/angular_devkit/build_angular/src/utils/server-rendering/render-page.ts index 20548499daa2..756ecf368c14 100644 --- a/packages/angular_devkit/build_angular/src/utils/server-rendering/render-page.ts +++ b/packages/angular_devkit/build_angular/src/utils/server-rendering/render-page.ts @@ -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; @@ -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) { diff --git a/packages/angular_devkit/build_angular/src/utils/server-rendering/routes-extractor-worker.ts b/packages/angular_devkit/build_angular/src/utils/server-rendering/routes-extractor-worker.ts index 1b4c71bd33b1..8c18b1871816 100644 --- a/packages/angular_devkit/build_angular/src/utils/server-rendering/routes-extractor-worker.ts +++ b/packages/angular_devkit/build_angular/src/utils/server-rendering/routes-extractor-worker.ts @@ -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; @@ -27,6 +28,8 @@ export interface RoutersExtractorWorkerResult { const { document, verbose } = workerData as RoutesExtractorWorkerData; export default async function (): Promise { + patchConsoleToIgnoreSpecificLogs(); + const { default: bootstrapAppFnOrModule, extractRoutes } = await loadEsmModule('./main.server.mjs'); diff --git a/packages/angular_devkit/build_angular/src/utils/server-rendering/utils.ts b/packages/angular_devkit/build_angular/src/utils/server-rendering/utils.ts new file mode 100644 index 000000000000..3e467af24930 --- /dev/null +++ b/packages/angular_devkit/build_angular/src/utils/server-rendering/utils.ts @@ -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 */ +}