-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathdynamic-federation.ts
More file actions
326 lines (276 loc) · 7.83 KB
/
dynamic-federation.ts
File metadata and controls
326 lines (276 loc) · 7.83 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
type Scope = unknown;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Factory = () => any;
type Container = {
init(shareScope: Scope): void;
get(module: string): Factory;
};
let config: Manifest = {};
export type ManifestFile<T extends RemoteConfig = RemoteConfig> = {
[key: string]: string | T;
};
export type Manifest<T extends RemoteConfig = RemoteConfig> = {
[key: string]: T;
};
export type RemoteConfig = {
type: 'module' | 'script';
remoteEntry: string;
[key: string]: unknown;
};
declare const __webpack_init_sharing__: (shareScope: string) => Promise<void>;
declare const __webpack_share_scopes__: { default: Scope };
type ContainerMap = { [key: string]: Container };
const containerMap: ContainerMap = {};
const remoteMap = {};
let isDefaultScopeInitialized = false;
async function lookupExposedModule<T>(
key: string,
exposedModule: string
): Promise<T> {
const container = containerMap[key];
const factory = await container.get(exposedModule);
const Module = factory();
return Module as T;
}
async function initRemote(container: Container, key: string) {
// const container = window[key] as Container;
// Do we still need to initialize the remote?
if (remoteMap[key]) {
return container;
}
// Do we still need to initialize the share scope?
if (!isDefaultScopeInitialized) {
await __webpack_init_sharing__('default');
isDefaultScopeInitialized = true;
}
await container.init(__webpack_share_scopes__.default);
remoteMap[key] = true;
return container;
}
export type LoadRemoteEntryOptions =
| LoadRemoteEntryScriptOptions
| LoadRemoteEntryEsmOptions;
export type LoadRemoteEntryScriptOptions = {
type?: 'script';
remoteEntry: string;
remoteName: string;
};
export type LoadRemoteEntryEsmOptions = {
type: 'module';
remoteEntry: string;
};
export async function loadRemoteEntry(
remoteEntry: string,
remoteName: string
): Promise<void>;
export async function loadRemoteEntry(
options: LoadRemoteEntryOptions
): Promise<void>;
export async function loadRemoteEntry(
remoteEntryOrOptions: string | LoadRemoteEntryOptions,
remoteName?: string
): Promise<void> {
if (typeof remoteEntryOrOptions === 'string') {
const remoteEntry = remoteEntryOrOptions;
return await loadRemoteScriptEntry(remoteEntry, remoteName);
} else if (remoteEntryOrOptions.type === 'script') {
const options = remoteEntryOrOptions;
return await loadRemoteScriptEntry(options.remoteEntry, options.remoteName);
} else if (remoteEntryOrOptions.type === 'module') {
const options = remoteEntryOrOptions;
return await loadRemoteModuleEntry(options.remoteEntry);
}
}
async function loadRemoteModuleEntry(remoteEntry: string): Promise<void> {
if (containerMap[remoteEntry]) {
return Promise.resolve();
}
return await import(/* webpackIgnore:true */ remoteEntry).then(
(container) => {
initRemote(container, remoteEntry);
containerMap[remoteEntry] = container;
}
);
}
async function loadRemoteScriptEntry(
remoteEntry: string,
remoteName: string
): Promise<void> {
return new Promise<void>((resolve, reject) => {
// Is remoteEntry already loaded?
if (containerMap[remoteName]) {
resolve();
return;
}
const script = document.createElement('script');
script.src = remoteEntry;
script.onerror = reject;
script.onload = () => {
const container = window[remoteName] as Container;
initRemote(container, remoteName);
containerMap[remoteName] = container;
resolve();
};
document.body.appendChild(script);
});
}
export type LoadRemoteModuleOptions =
| LoadRemoteModuleScriptOptions
| LoadRemoteModuleEsmOptions
| LoadRemoteModuleManifestOptions;
export type LoadRemoteModuleScriptOptions = {
type?: 'script';
remoteEntry?: string;
remoteName: string;
exposedModule: string;
};
export type LoadRemoteModuleEsmOptions = {
type: 'module';
remoteEntry: string;
exposedModule: string;
};
export type LoadRemoteModuleManifestOptions = {
type: 'manifest';
remoteName: string;
exposedModule: string;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function loadRemoteModule<T = any>(
remoteName: string,
exposedModule: string
): Promise<T>;
export async function loadRemoteModule<T = any>(
options: LoadRemoteModuleOptions
): Promise<T>;
export async function loadRemoteModule<T = any>(
optionsOrRemoteName: LoadRemoteModuleOptions | string,
exposedModule?: string
): Promise<T> {
let loadRemoteEntryOptions: LoadRemoteEntryOptions;
let key: string;
let remoteEntry: string;
let options: LoadRemoteModuleOptions;
if (typeof optionsOrRemoteName === 'string') {
options = {
type: 'manifest',
remoteName: optionsOrRemoteName,
exposedModule: exposedModule,
};
} else {
options = optionsOrRemoteName;
}
// To support legacy API (< ng 13)
if (!options.type) {
const hasManifest = Object.keys(config).length > 0;
options.type = hasManifest ? 'manifest' : 'script';
}
if (options.type === 'manifest') {
const manifestEntry = config[options.remoteName];
if (!manifestEntry) {
throw new Error('Manifest does not contain ' + options.remoteName);
}
options = {
type: manifestEntry.type,
exposedModule: options.exposedModule,
remoteEntry: manifestEntry.remoteEntry,
remoteName:
manifestEntry.type === 'script' ? options.remoteName : undefined,
};
remoteEntry = manifestEntry.remoteEntry;
} else {
remoteEntry = options.remoteEntry;
}
if (options.type === 'script') {
loadRemoteEntryOptions = {
type: 'script',
remoteEntry: options.remoteEntry,
remoteName: options.remoteName,
};
key = options.remoteName;
} else if (options.type === 'module') {
loadRemoteEntryOptions = {
type: 'module',
remoteEntry: options.remoteEntry,
};
key = options.remoteEntry;
}
if (remoteEntry) {
await loadRemoteEntry(loadRemoteEntryOptions);
}
return await lookupExposedModule<T>(key, options.exposedModule);
}
export async function setManifest(
manifest: ManifestFile,
skipRemoteEntries = false
) {
config = parseConfig(manifest);
if (!skipRemoteEntries) {
await loadRemoteEntries();
}
}
export function getManifest<T extends Manifest>(): T {
return config as T;
}
export async function initFederation(
manifest: string | ManifestFile,
skipRemoteEntries = false
): Promise<void> {
if (typeof manifest === 'string') {
return loadManifest(manifest, skipRemoteEntries);
} else {
return setManifest(manifest, skipRemoteEntries);
}
}
export async function loadManifest(
configFile: string,
skipRemoteEntries = false
): Promise<void> {
const result = await fetch(configFile);
if (!result.ok) {
throw Error('could not load configFile: ' + configFile);
}
config = parseConfig(await result.json());
if (!skipRemoteEntries) {
await loadRemoteEntries();
}
}
function parseConfig(config: ManifestFile): Manifest {
const result: Manifest = {};
for (const key in config) {
const value = config[key];
let entry: RemoteConfig;
if (typeof value === 'string') {
entry = {
remoteEntry: value,
type: 'module',
};
} else {
entry = {
...value,
type: value.type || 'module',
};
}
result[key] = entry;
}
return result;
}
async function loadRemoteEntries() {
const promises: Promise<void>[] = [];
for (const key in config) {
const entry = config[key];
if (entry.type === 'module') {
promises.push(
loadRemoteEntry({ type: 'module', remoteEntry: entry.remoteEntry })
);
} else {
promises.push(
loadRemoteEntry({
type: 'script',
remoteEntry: entry.remoteEntry,
remoteName: key,
})
);
}
}
await Promise.all(promises);
}