Skip to content

Commit b0a7112

Browse files
committed
feat(node): support appEngine in Angular 19 und 20
1 parent d348e37 commit b0a7112

File tree

11 files changed

+535
-7
lines changed

11 files changed

+535
-7
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import * as fs from 'node:fs';
2+
3+
export type FStartArgs = {
4+
entry: string;
5+
relBundlePath: string;
6+
remotesOrManifestUrl: string;
7+
};
8+
9+
const defaultArgs: FStartArgs = {
10+
entry: './server.mjs',
11+
remotesOrManifestUrl: '../browser/federation.manifest.json',
12+
relBundlePath: '../browser/',
13+
};
14+
15+
export function parseFStartArgs() {
16+
const args: FStartArgs = {
17+
entry: '',
18+
remotesOrManifestUrl: '',
19+
relBundlePath: '',
20+
};
21+
22+
let key = '';
23+
for (let i = 2; i < process.argv.length; i++) {
24+
const cand = process.argv[i];
25+
26+
if (cand.startsWith('--')) {
27+
const candKey = cand.substring(2);
28+
if (defaultArgs[candKey]) {
29+
key = candKey;
30+
} else {
31+
console.error(`switch ${cand} not supported!`);
32+
exitWithUsage(defaultArgs);
33+
}
34+
} else if (key) {
35+
args[key] = cand;
36+
key = '';
37+
} else {
38+
console.error(`unreladed value ${cand}!`);
39+
exitWithUsage(defaultArgs);
40+
}
41+
}
42+
43+
applyDefaultArgs(args);
44+
45+
return args;
46+
}
47+
48+
function applyDefaultArgs(args: FStartArgs) {
49+
if (args.relBundlePath && !args.remotesOrManifestUrl) {
50+
const cand = defaultArgs.relBundlePath + 'federation.manifest.json';
51+
if (fs.existsSync(cand)) {
52+
args.remotesOrManifestUrl = cand;
53+
}
54+
}
55+
56+
args.entry = args.entry || defaultArgs.entry;
57+
args.relBundlePath = args.relBundlePath || defaultArgs.relBundlePath;
58+
args.remotesOrManifestUrl =
59+
args.remotesOrManifestUrl || defaultArgs.remotesOrManifestUrl;
60+
61+
if (!fs.existsSync(args.remotesOrManifestUrl)) {
62+
args.remotesOrManifestUrl = undefined;
63+
}
64+
}
65+
66+
function exitWithUsage(defaultArgs: FStartArgs) {
67+
let args = '';
68+
for (const key in defaultArgs) {
69+
args += `[--${key} ${defaultArgs[key]}] `;
70+
}
71+
72+
console.log('usage: nfstart ' + args);
73+
process.exit(1);
74+
}

libs/native-federation-node/src/lib/utils/fstart.mjs

Lines changed: 401 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import { initNodeFederation } from '../node/init-node-federation';
3+
import { parseFStartArgs } from './fstart-args-parser';
4+
5+
6+
const args = parseFStartArgs();
7+
8+
(async () => {
9+
10+
await initNodeFederation({
11+
...(args.remotesOrManifestUrl ? {remotesOrManifestUrl: args.remotesOrManifestUrl}: {}),
12+
relBundlePath: args.relBundlePath
13+
});
14+
15+
await import(args.entry);
16+
17+
})();
18+
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"module": "ES2015",
5-
"target": "ES2015",
4+
"module": "ES2022",
5+
"target": "ES2022",
66
"outDir": "../../dist/out-tsc",
77
"declaration": true,
88
"types": ["node"]
@@ -12,5 +12,5 @@
1212
"src/**/*.ts",
1313
"src/lib/node/import-map-store.ts.bak",
1414
"src/lib/node/federation-resolver.ts.bak"
15-
]
15+
, "../native-federation/tools/fstart-args-parser.ts", "../native-federation/tools/fstart.ts" ]
1616
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const source = '../../native-federation-node/src/lib/utils/fstart.mjs';
5+
const dataUrlFile = '../src/tools/fstart-as-data-url.ts';
6+
7+
const filePath = path.resolve(__dirname, source);
8+
const fileContent = fs.readFileSync(filePath, 'utf8');
9+
const base64Content = Buffer.from(fileContent).toString('base64');
10+
11+
// const dataUrl = `data:text/javascript;base64,${base64Content}`;
12+
13+
const outputPath = path.resolve(__dirname, dataUrlFile);
14+
fs.writeFileSync(outputPath, `export const fstart = "${base64Content}";\n`);
15+
16+
console.log('Created data url for fstart');

libs/native-federation/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@angular-architects/native-federation",
3-
"version": "20.0.1",
3+
"version": "20.0.2",
44
"main": "src/index.js",
55
"generators": "./collection.json",
66
"builders": "./builders.json",

libs/native-federation/src/builders/build/builder.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
I18nConfig,
4949
translateFederationArtefacts,
5050
} from '../../utils/i18n';
51+
import { fstart } from '../../tools/fstart-as-data-url';
5152

5253
function _buildApplication(options, context, pluginsOrExtensions) {
5354
let extensions;
@@ -266,6 +267,10 @@ export async function* runBuilder(
266267
}
267268
}
268269

270+
if (activateSsr) {
271+
writeFstartScript(fedOptions);
272+
}
273+
269274
const hasLocales = i18n?.locales && Object.keys(i18n.locales).length > 0;
270275
if (hasLocales && localeFilter) {
271276
translateFederationArtefacts(
@@ -361,6 +366,14 @@ export async function* runBuilder(
361366
// eslint-disable-next-line @typescript-eslint/no-explicit-any
362367
export default createBuilder(runBuilder) as any;
363368

369+
function writeFstartScript(fedOptions: FederationOptions) {
370+
const serverOutpath = path.join(fedOptions.outputPath, '../server');
371+
const fstartPath = path.join(serverOutpath, 'fstart.mjs');
372+
const buffer = Buffer.from(fstart, 'base64');
373+
fs.mkdirSync(serverOutpath, { recursive: true });
374+
fs.writeFileSync(fstartPath, buffer, 'utf-8');
375+
}
376+
364377
function getLocaleFilter(
365378
options: ApplicationBuilderOptions,
366379
runServer: boolean

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ function updateWorkspaceConfig(
266266

267267
if (ssr) {
268268
projectConfig.architect.build.options.ssr = true;
269-
projectConfig.architect.esbuild.options.prerender = false;
269+
// projectConfig.architect.esbuild.options.prerender = false;
270270
}
271271

272272
const serve = projectConfig.architect.serve;

libs/native-federation/src/tools/fstart-as-data-url.ts

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ export function loadEsmModule<T>(modulePath: string | URL): Promise<T> {
424424
//
425425
function setNgServerMode(): void {
426426
const fileToPatch = 'node_modules/@angular/core/fesm2022/core.mjs';
427-
const lineToAdd = `const ngServerMode = (typeof window === 'undefined') ? true : false;`;
428-
427+
const lineToAdd = `if (typeof globalThis.ngServerMode ==='undefined') globalThis.ngServerMode = (typeof window === 'undefined') ? true : false;`;
428+
429429
try {
430430
if (fs.existsSync(fileToPatch)) {
431431
let content = fs.readFileSync(fileToPatch, 'utf-8');

0 commit comments

Comments
 (0)