Skip to content

Commit f244fd0

Browse files
committed
fix(nf-node): set ngServerMode at runtime
1 parent fbd2b4b commit f244fd0

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

libs/native-federation-node/src/lib/node/init-node-federation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async function createNodeImportMap(
5353
: await loadFsManifest(remotesOrManifestUrl);
5454

5555
const hostInfo = await loadFsFederationInfo(relBundlePath);
56-
const hostImportMap = await processHostInfo(hostInfo, relBundlePath);
56+
const hostImportMap = await processHostInfo(hostInfo, './' + relBundlePath);
5757
const remotesImportMap = await processRemoteInfos(remotes, {
5858
throwIfRemoteNotFound: options.throwIfRemoteNotFound,
5959
cacheTag: options.cacheTag,

libs/native-federation/src/schematics/init/schematic.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,12 @@ function makeServerAsync(
484484
return;
485485
}
486486

487+
const cors = `import { createRequire } from "module";
488+
const require = createRequire(import.meta.url);
489+
const cors = require("cors");
490+
`;
487491
const mainContent = tree.read(server).toString('utf8');
488-
const updatedContent = (`import cors from 'cors';\n` + mainContent)
492+
const updatedContent = (cors + mainContent)
489493
.replace(
490494
`const port = process.env['PORT'] || 4000`,
491495
`const port = process.env['PORT'] || ${options.port || 4000}`

libs/native-federation/src/utils/angular-esbuild-adapter.ts

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ import {
3838
import { RebuildEvents, RebuildHubs } from './rebuild-events';
3939

4040
import JSON5 from 'json5';
41-
// import { ComponentStylesheetBundler } from '@angular/build/src/tools/esbuild/angular/component-stylesheets';
42-
43-
// const fesmFolderRegExp = /[/\\]fesm\d+[/\\]/;
4441

4542
export type MemResultHandler = (
4643
outfiles: esbuild.OutputFile[],
@@ -72,6 +69,8 @@ export function createAngularBuildAdapter(
7269
platform,
7370
} = options;
7471

72+
setNgServerMode();
73+
7574
const files = await runEsbuild(
7675
builderOptions,
7776
context,
@@ -285,26 +284,6 @@ async function runEsbuild(
285284
createCompilerPlugin(
286285
pluginOptions.pluginOptions,
287286
pluginOptions.styleOptions
288-
289-
// TODO: Once available, use helper functions
290-
// for creating these config objects:
291-
// @angular_devkit/build_angular/src/tools/esbuild/compiler-plugin-options.ts
292-
// {
293-
// jit: false,
294-
// sourcemap: dev,
295-
// tsconfig: tsConfigPath,
296-
// advancedOptimizations: !dev,
297-
// thirdPartySourcemaps: false,
298-
// },
299-
// {
300-
// optimization: !dev,
301-
// sourcemap: dev ? 'inline' : false,
302-
// workspaceRoot: __dirname,
303-
// inlineStyleLanguage: builderOptions.inlineStyleLanguage,
304-
// // browsers: browsers,
305-
306-
// target: target,
307-
// }
308287
),
309288
...(mappedPaths && mappedPaths.length > 0
310289
? [createSharedMappingsPlugin(mappedPaths)]
@@ -338,17 +317,9 @@ async function runEsbuild(
338317
ctx.dispose();
339318
}
340319

341-
// cleanUpTsConfigForFederation(tsConfigPath);
342-
343320
return writtenFiles;
344321
}
345322

346-
// function cleanUpTsConfigForFederation(tsConfigPath: string) {
347-
// if (tsConfigPath.includes('.federation.')) {
348-
// fs.unlinkSync(tsConfigPath);
349-
// }
350-
// }
351-
352323
function createTsConfigForFederation(
353324
workspaceRoot: string,
354325
tsConfigPath: string,
@@ -365,11 +336,6 @@ function createTsConfigForFederation(
365336
.map((ep) => path.relative(tsconfigDir, ep.fileName).replace(/\\\\/g, '/'));
366337

367338
const tsconfigAsString = fs.readFileSync(fullTsConfigPath, 'utf-8');
368-
// const tsconfigWithoutComments = tsconfigAsString.replace(
369-
// /\/\*.+?\*\/|\/\/.*(?=[\n\r])/g,
370-
// ''
371-
// );
372-
373339
const tsconfig = JSON5.parse(tsconfigAsString);
374340

375341
if (!tsconfig.include) {
@@ -450,3 +416,32 @@ export function loadEsmModule<T>(modulePath: string | URL): Promise<T> {
450416
modulePath
451417
) as Promise<T>;
452418
}
419+
420+
//
421+
// Usually, ngServerMode is set during bundling. However, we need to infer this
422+
// value at runtime as we are using the same shared bundle for @angular/core
423+
// on the server and in the browser.
424+
//
425+
function setNgServerMode(): void {
426+
const fileToPatch = 'node_modules/@angular/core/fesm2022/core.mjs';
427+
const lineToAdd = `const ngServerMode = (typeof window === 'undefined') ? true : false;`;
428+
429+
try {
430+
if (fs.existsSync(fileToPatch)) {
431+
let content = fs.readFileSync(fileToPatch, 'utf-8');
432+
if (!content.includes(lineToAdd)) {
433+
content = lineToAdd + '\n' + content;
434+
fs.writeFileSync(fileToPatch, content);
435+
console.log('patched!');
436+
} else {
437+
console.log('not patched!');
438+
}
439+
}
440+
} catch (e) {
441+
console.error(
442+
'Error patching file ',
443+
fileToPatch,
444+
'\nIs it write-protected?'
445+
);
446+
}
447+
}

0 commit comments

Comments
 (0)