forked from angular-architects/module-federation-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-node-federation.ts
More file actions
94 lines (78 loc) · 2.73 KB
/
init-node-federation.ts
File metadata and controls
94 lines (78 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import * as fs from 'node:fs/promises';
import { register } from 'node:module';
import * as path from 'node:path';
import { pathToFileURL } from 'node:url';
import {
FederationInfo,
fetchAndRegisterRemotes,
ImportMap,
mergeImportMaps,
processHostInfo,
} from '@softarc/native-federation-runtime';
import { IMPORT_MAP_FILE_NAME } from '../utils/import-map-loader';
import { resolver } from '../utils/loader-as-data-url';
export type InitNodeFederationOptions = {
remotesOrManifestUrl: Record<string, string> | string;
relBundlePath: string;
throwIfRemoteNotFound: boolean;
cacheTag?: string;
};
const defaultOptions: InitNodeFederationOptions = {
remotesOrManifestUrl: {},
relBundlePath: '../browser',
throwIfRemoteNotFound: false,
};
export async function initNodeFederation(
options: Partial<InitNodeFederationOptions>,
): Promise<void> {
const mergedOptions = { ...defaultOptions, ...options };
const importMap = await createNodeImportMap(mergedOptions);
// const normalized = resolveAndComposeImportMap(importMap) as ImportMap;
// await writeImportMap(normalized);
await writeImportMap(importMap);
await writeResolver();
register(pathToFileURL('./federation-resolver.mjs').href);
}
async function createNodeImportMap(
options: InitNodeFederationOptions,
): Promise<ImportMap> {
const { remotesOrManifestUrl, relBundlePath } = options;
const remotes =
typeof remotesOrManifestUrl === 'object'
? remotesOrManifestUrl
: await loadFsManifest(remotesOrManifestUrl);
const hostInfo = await loadFsFederationInfo(relBundlePath);
const hostImportMap = await processHostInfo(hostInfo, './' + relBundlePath);
const remotesImportMap = await fetchAndRegisterRemotes(remotes, {
throwIfRemoteNotFound: options.throwIfRemoteNotFound,
cacheTag: options.cacheTag,
});
const importMap = mergeImportMaps(hostImportMap, remotesImportMap);
return importMap;
}
async function loadFsManifest(
manifestUrl: string,
): Promise<Record<string, string>> {
const content = await fs.readFile(manifestUrl, 'utf-8');
const manifest = JSON.parse(content) as Record<string, string>;
return manifest;
}
async function loadFsFederationInfo(
relBundlePath: string,
): Promise<FederationInfo> {
const manifestPath = path.join(relBundlePath, 'remoteEntry.json');
const content = await fs.readFile(manifestPath, 'utf-8');
const manifest = JSON.parse(content) as FederationInfo;
return manifest;
}
async function writeImportMap(map: ImportMap): Promise<void> {
await fs.writeFile(
IMPORT_MAP_FILE_NAME,
JSON.stringify(map, null, 2),
'utf-8',
);
}
async function writeResolver() {
const buffer = Buffer.from(resolver, 'base64');
await fs.writeFile('federation-resolver.mjs', buffer, 'utf-8');
}