From 951f5ee933cf0e0f8ffbdb560c848e021fa4abe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20D=C3=ADaz=20D=C3=ADaz?= Date: Sat, 15 Mar 2025 13:35:55 +0100 Subject: [PATCH] fix(nf-node): update ngServerMode declaration to use var and handle existing const --- .../src/utils/angular-esbuild-adapter.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libs/native-federation/src/utils/angular-esbuild-adapter.ts b/libs/native-federation/src/utils/angular-esbuild-adapter.ts index dd630089..cbff642b 100644 --- a/libs/native-federation/src/utils/angular-esbuild-adapter.ts +++ b/libs/native-federation/src/utils/angular-esbuild-adapter.ts @@ -424,15 +424,19 @@ export function loadEsmModule(modulePath: string | URL): Promise { // function setNgServerMode(): void { const fileToPatch = 'node_modules/@angular/core/fesm2022/core.mjs'; - const lineToAdd = `const ngServerMode = (typeof window === 'undefined') ? true : false;`; - + const lineToAdd = `var ngServerMode = (typeof window === 'undefined') ? true : false;`; try { if (fs.existsSync(fileToPatch)) { let content = fs.readFileSync(fileToPatch, 'utf-8'); - if (!content.includes(lineToAdd)) { + // Check if const ngServerMode exists and replace it + if (content.includes('const ngServerMode')) { + content = content.replace(/const ngServerMode.*/, lineToAdd); + } + // If not found, add it at the beginning + else if (!content.includes(lineToAdd)) { content = lineToAdd + '\n' + content; - fs.writeFileSync(fileToPatch, content); } + fs.writeFileSync(fileToPatch, content); } } catch (e) { console.error(