@module-federation/runtime 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -784
- package/package.json +8 -8
- package/dist/LICENSE +0 -21
- package/dist/helpers.cjs.d.ts +0 -2
- package/dist/helpers.cjs.js +0 -34
- package/dist/helpers.esm.js +0 -32
- package/dist/index.cjs.d.ts +0 -1
- package/dist/index.cjs.js +0 -1767
- package/dist/index.esm.js +0 -1747
- package/dist/package.json +0 -50
- package/dist/share.cjs.js +0 -843
- package/dist/share.esm.js +0 -806
- package/dist/src/constant.d.ts +0 -2
- package/dist/src/core.d.ts +0 -164
- package/dist/src/global.d.ts +0 -43
- package/dist/src/helpers.d.ts +0 -33
- package/dist/src/index.d.ts +0 -15
- package/dist/src/module/index.d.ts +0 -19
- package/dist/src/plugins/generate-preload-assets.d.ts +0 -8
- package/dist/src/plugins/snapshot/SnapshotHandler.d.ts +0 -44
- package/dist/src/plugins/snapshot/index.d.ts +0 -5
- package/dist/src/type/config.d.ts +0 -100
- package/dist/src/type/index.d.ts +0 -3
- package/dist/src/type/plugin.d.ts +0 -20
- package/dist/src/type/preload.d.ts +0 -26
- package/dist/src/types.d.ts +0 -1
- package/dist/src/utils/env.d.ts +0 -4
- package/dist/src/utils/hooks/asyncHook.d.ts +0 -6
- package/dist/src/utils/hooks/asyncWaterfallHooks.d.ts +0 -10
- package/dist/src/utils/hooks/index.d.ts +0 -6
- package/dist/src/utils/hooks/pluginSystem.d.ts +0 -15
- package/dist/src/utils/hooks/syncHook.d.ts +0 -12
- package/dist/src/utils/hooks/syncWaterfallHook.d.ts +0 -9
- package/dist/src/utils/index.d.ts +0 -6
- package/dist/src/utils/load.d.ts +0 -18
- package/dist/src/utils/logger.d.ts +0 -3
- package/dist/src/utils/manifest.d.ts +0 -7
- package/dist/src/utils/plugin.d.ts +0 -4
- package/dist/src/utils/preload.d.ts +0 -6
- package/dist/src/utils/semver/compare.d.ts +0 -9
- package/dist/src/utils/semver/constants.d.ts +0 -10
- package/dist/src/utils/semver/index.d.ts +0 -2
- package/dist/src/utils/semver/parser.d.ts +0 -9
- package/dist/src/utils/semver/utils.d.ts +0 -11
- package/dist/src/utils/share.d.ts +0 -16
- package/dist/src/utils/tool.d.ts +0 -12
- package/dist/types.cjs.d.ts +0 -1
- package/dist/types.cjs.js +0 -2
- package/dist/types.esm.js +0 -1
package/dist/index.esm.js
DELETED
|
@@ -1,1747 +0,0 @@
|
|
|
1
|
-
import { g as getGlobalHostPlugins, a as globalLoading, D as DEFAULT_REMOTE_TYPE, b as DEFAULT_SCOPE, c as getRemoteEntryExports, d as assert, s as safeToString, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as getRegisteredShare, m as getInfoWithoutType, n as getPreloaded, o as setPreloaded, p as getGlobalSnapshotInfoByModuleInfo, q as addGlobalSnapshot, r as setGlobalSnapshotInfoByModuleInfo, t as getGlobalSnapshot, G as Global, u as getGlobalShareScope, v as formatShareConfigs, x as getBuilderId, y as isBrowserEnv, z as addUniqueItem, A as setGlobalFederationConstructor, B as getGlobalFederationInstance, C as getGlobalFederationConstructor, E as setGlobalFederationInstance } from './share.esm.js';
|
|
2
|
-
export { F as registerGlobalPlugins } from './share.esm.js';
|
|
3
|
-
import { loadScriptNode, loadScript, composeKeyWithSeparator, createLink, getResourceUrl, isManifestProvider, generateSnapshotFromManifest } from '@module-federation/sdk';
|
|
4
|
-
export { loadScript, loadScriptNode } from '@module-federation/sdk';
|
|
5
|
-
|
|
6
|
-
// Function to match a remote with its name and expose
|
|
7
|
-
// id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
|
|
8
|
-
// id: alias(app1) + expose(button) = app1/button
|
|
9
|
-
// id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort
|
|
10
|
-
function matchRemoteWithNameAndExpose(remotes, id) {
|
|
11
|
-
for (const remote of remotes){
|
|
12
|
-
// match pkgName
|
|
13
|
-
const isNameMatched = id.startsWith(remote.name);
|
|
14
|
-
let expose = id.replace(remote.name, '');
|
|
15
|
-
if (isNameMatched) {
|
|
16
|
-
if (expose.startsWith('/')) {
|
|
17
|
-
const pkgNameOrAlias = remote.name;
|
|
18
|
-
expose = `.${expose}`;
|
|
19
|
-
return {
|
|
20
|
-
pkgNameOrAlias,
|
|
21
|
-
expose,
|
|
22
|
-
remote
|
|
23
|
-
};
|
|
24
|
-
} else if (expose === '') {
|
|
25
|
-
return {
|
|
26
|
-
pkgNameOrAlias: remote.name,
|
|
27
|
-
expose: '.',
|
|
28
|
-
remote
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
// match alias
|
|
33
|
-
const isAliasMatched = remote.alias && id.startsWith(remote.alias);
|
|
34
|
-
let exposeWithAlias = remote.alias && id.replace(remote.alias, '');
|
|
35
|
-
if (remote.alias && isAliasMatched) {
|
|
36
|
-
if (exposeWithAlias && exposeWithAlias.startsWith('/')) {
|
|
37
|
-
const pkgNameOrAlias = remote.alias;
|
|
38
|
-
exposeWithAlias = `.${exposeWithAlias}`;
|
|
39
|
-
return {
|
|
40
|
-
pkgNameOrAlias,
|
|
41
|
-
expose: exposeWithAlias,
|
|
42
|
-
remote
|
|
43
|
-
};
|
|
44
|
-
} else if (exposeWithAlias === '') {
|
|
45
|
-
return {
|
|
46
|
-
pkgNameOrAlias: remote.alias,
|
|
47
|
-
expose: '.',
|
|
48
|
-
remote
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
// Function to match a remote with its name or alias
|
|
56
|
-
function matchRemote(remotes, nameOrAlias) {
|
|
57
|
-
for (const remote of remotes){
|
|
58
|
-
const isNameMatched = nameOrAlias === remote.name;
|
|
59
|
-
if (isNameMatched) {
|
|
60
|
-
return remote;
|
|
61
|
-
}
|
|
62
|
-
const isAliasMatched = remote.alias && nameOrAlias === remote.alias;
|
|
63
|
-
if (isAliasMatched) {
|
|
64
|
-
return remote;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function registerPlugins$1(plugins, hookInstances) {
|
|
71
|
-
const globalPlugins = getGlobalHostPlugins();
|
|
72
|
-
// Incorporate global plugins
|
|
73
|
-
if (globalPlugins.length > 0) {
|
|
74
|
-
globalPlugins.forEach((plugin)=>{
|
|
75
|
-
if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
|
|
76
|
-
plugins.push(plugin);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
if (plugins && plugins.length > 0) {
|
|
81
|
-
plugins.forEach((plugin)=>{
|
|
82
|
-
hookInstances.forEach((hookInstance)=>{
|
|
83
|
-
hookInstance.applyPlugin(plugin);
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function _extends$5() {
|
|
90
|
-
_extends$5 = Object.assign || function(target) {
|
|
91
|
-
for(var i = 1; i < arguments.length; i++){
|
|
92
|
-
var source = arguments[i];
|
|
93
|
-
for(var key in source){
|
|
94
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
95
|
-
target[key] = source[key];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return target;
|
|
100
|
-
};
|
|
101
|
-
return _extends$5.apply(this, arguments);
|
|
102
|
-
}
|
|
103
|
-
async function loadEsmEntry({ entry, remoteEntryExports }) {
|
|
104
|
-
return new Promise((resolve, reject)=>{
|
|
105
|
-
try {
|
|
106
|
-
if (!remoteEntryExports) {
|
|
107
|
-
// eslint-disable-next-line no-eval
|
|
108
|
-
new Function('callbacks', `import("${entry}").then(callbacks[0]).catch(callbacks[1])`)([
|
|
109
|
-
resolve,
|
|
110
|
-
reject
|
|
111
|
-
]);
|
|
112
|
-
} else {
|
|
113
|
-
resolve(remoteEntryExports);
|
|
114
|
-
}
|
|
115
|
-
} catch (e) {
|
|
116
|
-
reject(e);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
async function loadEntryScript({ name, globalName, entry, createScriptHook }) {
|
|
121
|
-
const { entryExports: remoteEntryExports } = getRemoteEntryExports(name, globalName);
|
|
122
|
-
if (remoteEntryExports) {
|
|
123
|
-
return remoteEntryExports;
|
|
124
|
-
}
|
|
125
|
-
if (typeof document === 'undefined') {
|
|
126
|
-
return loadScriptNode(entry, {
|
|
127
|
-
attrs: {
|
|
128
|
-
name,
|
|
129
|
-
globalName
|
|
130
|
-
},
|
|
131
|
-
createScriptHook
|
|
132
|
-
}).then(()=>{
|
|
133
|
-
const { remoteEntryKey, entryExports } = getRemoteEntryExports(name, globalName);
|
|
134
|
-
assert(entryExports, `
|
|
135
|
-
Unable to use the ${name}'s '${entry}' URL with ${remoteEntryKey}'s globalName to get remoteEntry exports.
|
|
136
|
-
Possible reasons could be:\n
|
|
137
|
-
1. '${entry}' is not the correct URL, or the remoteEntry resource or name is incorrect.\n
|
|
138
|
-
2. ${remoteEntryKey} cannot be used to get remoteEntry exports in the window object.
|
|
139
|
-
`);
|
|
140
|
-
return entryExports;
|
|
141
|
-
}).catch((e)=>{
|
|
142
|
-
return e;
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
return loadScript(entry, {
|
|
146
|
-
attrs: {},
|
|
147
|
-
createScriptHook
|
|
148
|
-
}).then(()=>{
|
|
149
|
-
const { remoteEntryKey, entryExports } = getRemoteEntryExports(name, globalName);
|
|
150
|
-
assert(entryExports, `
|
|
151
|
-
Unable to use the ${name}'s '${entry}' URL with ${remoteEntryKey}'s globalName to get remoteEntry exports.
|
|
152
|
-
Possible reasons could be:\n
|
|
153
|
-
1. '${entry}' is not the correct URL, or the remoteEntry resource or name is incorrect.\n
|
|
154
|
-
2. ${remoteEntryKey} cannot be used to get remoteEntry exports in the window object.
|
|
155
|
-
`);
|
|
156
|
-
return entryExports;
|
|
157
|
-
}).catch((e)=>{
|
|
158
|
-
return e;
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
function getRemoteEntryUniqueKey(remoteInfo) {
|
|
162
|
-
const { entry, name } = remoteInfo;
|
|
163
|
-
return composeKeyWithSeparator(name, entry);
|
|
164
|
-
}
|
|
165
|
-
async function getRemoteEntry({ remoteEntryExports, remoteInfo, createScriptHook }) {
|
|
166
|
-
const { entry, name, type, entryGlobalName } = remoteInfo;
|
|
167
|
-
const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
|
|
168
|
-
if (remoteEntryExports) {
|
|
169
|
-
return remoteEntryExports;
|
|
170
|
-
}
|
|
171
|
-
if (!globalLoading[uniqueKey]) {
|
|
172
|
-
if (type === 'esm') {
|
|
173
|
-
globalLoading[uniqueKey] = loadEsmEntry({
|
|
174
|
-
entry,
|
|
175
|
-
remoteEntryExports
|
|
176
|
-
});
|
|
177
|
-
} else {
|
|
178
|
-
globalLoading[uniqueKey] = loadEntryScript({
|
|
179
|
-
name,
|
|
180
|
-
globalName: entryGlobalName,
|
|
181
|
-
entry,
|
|
182
|
-
createScriptHook
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
return globalLoading[uniqueKey];
|
|
187
|
-
}
|
|
188
|
-
function getRemoteInfo(remote) {
|
|
189
|
-
return _extends$5({}, remote, {
|
|
190
|
-
entry: 'entry' in remote ? remote.entry : '',
|
|
191
|
-
type: remote.type || DEFAULT_REMOTE_TYPE,
|
|
192
|
-
entryGlobalName: remote.entryGlobalName || remote.name,
|
|
193
|
-
shareScope: remote.shareScope || DEFAULT_SCOPE
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function _extends$4() {
|
|
198
|
-
_extends$4 = Object.assign || function(target) {
|
|
199
|
-
for(var i = 1; i < arguments.length; i++){
|
|
200
|
-
var source = arguments[i];
|
|
201
|
-
for(var key in source){
|
|
202
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
203
|
-
target[key] = source[key];
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return target;
|
|
208
|
-
};
|
|
209
|
-
return _extends$4.apply(this, arguments);
|
|
210
|
-
}
|
|
211
|
-
let Module = class Module {
|
|
212
|
-
async getEntry() {
|
|
213
|
-
if (this.remoteEntryExports) {
|
|
214
|
-
return this.remoteEntryExports;
|
|
215
|
-
}
|
|
216
|
-
// Get remoteEntry.js
|
|
217
|
-
const remoteEntryExports = await getRemoteEntry({
|
|
218
|
-
remoteInfo: this.remoteInfo,
|
|
219
|
-
remoteEntryExports: this.remoteEntryExports,
|
|
220
|
-
createScriptHook: (url)=>{
|
|
221
|
-
const res = this.host.loaderHook.lifecycle.createScript.emit({
|
|
222
|
-
url
|
|
223
|
-
});
|
|
224
|
-
if (typeof document === 'undefined') {
|
|
225
|
-
//todo: needs real fix
|
|
226
|
-
return res;
|
|
227
|
-
}
|
|
228
|
-
if (res instanceof HTMLScriptElement) {
|
|
229
|
-
return res;
|
|
230
|
-
}
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
assert(remoteEntryExports, `remoteEntryExports is undefined \n ${safeToString(this.remoteInfo)}`);
|
|
235
|
-
this.remoteEntryExports = remoteEntryExports;
|
|
236
|
-
return this.remoteEntryExports;
|
|
237
|
-
}
|
|
238
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
239
|
-
async get(expose, options) {
|
|
240
|
-
const { loadFactory = true } = options || {
|
|
241
|
-
loadFactory: true
|
|
242
|
-
};
|
|
243
|
-
// Get remoteEntry.js
|
|
244
|
-
const remoteEntryExports = await this.getEntry();
|
|
245
|
-
if (!this.inited) {
|
|
246
|
-
const localShareScopeMap = this.host.shareScopeMap;
|
|
247
|
-
const remoteShareScope = this.remoteInfo.shareScope || 'default';
|
|
248
|
-
if (!localShareScopeMap[remoteShareScope]) {
|
|
249
|
-
localShareScopeMap[remoteShareScope] = {};
|
|
250
|
-
}
|
|
251
|
-
const shareScope = localShareScopeMap[remoteShareScope];
|
|
252
|
-
const initScope = [];
|
|
253
|
-
const remoteEntryInitOptions = {
|
|
254
|
-
version: this.remoteInfo.version || ''
|
|
255
|
-
};
|
|
256
|
-
// Help to find host instance
|
|
257
|
-
Object.defineProperty(remoteEntryInitOptions, 'hostId', {
|
|
258
|
-
value: this.host.options.id || this.host.name,
|
|
259
|
-
// remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
|
|
260
|
-
enumerable: false
|
|
261
|
-
});
|
|
262
|
-
const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
|
|
263
|
-
shareScope,
|
|
264
|
-
// @ts-ignore hostId will be set by Object.defineProperty
|
|
265
|
-
remoteEntryInitOptions,
|
|
266
|
-
initScope,
|
|
267
|
-
remoteInfo: this.remoteInfo,
|
|
268
|
-
origin: this.host
|
|
269
|
-
});
|
|
270
|
-
await remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
|
|
271
|
-
await this.host.hooks.lifecycle.initContainer.emit(_extends$4({}, initContainerOptions, {
|
|
272
|
-
remoteEntryExports
|
|
273
|
-
}));
|
|
274
|
-
}
|
|
275
|
-
this.lib = remoteEntryExports;
|
|
276
|
-
this.inited = true;
|
|
277
|
-
// get exposeGetter
|
|
278
|
-
const moduleFactory = await remoteEntryExports.get(expose);
|
|
279
|
-
assert(moduleFactory, `${getFMId(this.remoteInfo)} remote don't export ${expose}.`);
|
|
280
|
-
if (!loadFactory) {
|
|
281
|
-
return moduleFactory;
|
|
282
|
-
}
|
|
283
|
-
const exposeContent = await moduleFactory();
|
|
284
|
-
return exposeContent;
|
|
285
|
-
}
|
|
286
|
-
constructor({ remoteInfo, host }){
|
|
287
|
-
this.inited = false;
|
|
288
|
-
this.lib = undefined;
|
|
289
|
-
this.remoteInfo = remoteInfo;
|
|
290
|
-
this.host = host;
|
|
291
|
-
}
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
class SyncHook {
|
|
295
|
-
on(fn) {
|
|
296
|
-
if (typeof fn === 'function') {
|
|
297
|
-
this.listeners.add(fn);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
once(fn) {
|
|
301
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
302
|
-
const self = this;
|
|
303
|
-
this.on(function wrapper(...args) {
|
|
304
|
-
self.remove(wrapper);
|
|
305
|
-
// eslint-disable-next-line prefer-spread
|
|
306
|
-
return fn.apply(null, args);
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
emit(...data) {
|
|
310
|
-
let result;
|
|
311
|
-
if (this.listeners.size > 0) {
|
|
312
|
-
// eslint-disable-next-line prefer-spread
|
|
313
|
-
this.listeners.forEach((fn)=>{
|
|
314
|
-
result = fn(...data);
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
return result;
|
|
318
|
-
}
|
|
319
|
-
remove(fn) {
|
|
320
|
-
this.listeners.delete(fn);
|
|
321
|
-
}
|
|
322
|
-
removeAll() {
|
|
323
|
-
this.listeners.clear();
|
|
324
|
-
}
|
|
325
|
-
constructor(type){
|
|
326
|
-
this.type = '';
|
|
327
|
-
this.listeners = new Set();
|
|
328
|
-
if (type) {
|
|
329
|
-
this.type = type;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
class AsyncHook extends SyncHook {
|
|
335
|
-
emit(...data) {
|
|
336
|
-
let result;
|
|
337
|
-
const ls = Array.from(this.listeners);
|
|
338
|
-
if (ls.length > 0) {
|
|
339
|
-
let i = 0;
|
|
340
|
-
const call = (prev)=>{
|
|
341
|
-
if (prev === false) {
|
|
342
|
-
return false; // Abort process
|
|
343
|
-
} else if (i < ls.length) {
|
|
344
|
-
return Promise.resolve(ls[i++].apply(null, data)).then(call);
|
|
345
|
-
} else {
|
|
346
|
-
return prev;
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
result = call();
|
|
350
|
-
}
|
|
351
|
-
return Promise.resolve(result);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
356
|
-
function checkReturnData(originalData, returnedData) {
|
|
357
|
-
if (!isObject(returnedData)) {
|
|
358
|
-
return false;
|
|
359
|
-
}
|
|
360
|
-
if (originalData !== returnedData) {
|
|
361
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
362
|
-
for(const key in originalData){
|
|
363
|
-
if (!(key in returnedData)) {
|
|
364
|
-
return false;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
return true;
|
|
369
|
-
}
|
|
370
|
-
class SyncWaterfallHook extends SyncHook {
|
|
371
|
-
emit(data) {
|
|
372
|
-
if (!isObject(data)) {
|
|
373
|
-
error(`The data for the "${this.type}" hook should be an object.`);
|
|
374
|
-
}
|
|
375
|
-
for (const fn of this.listeners){
|
|
376
|
-
try {
|
|
377
|
-
const tempData = fn(data);
|
|
378
|
-
if (checkReturnData(data, tempData)) {
|
|
379
|
-
data = tempData;
|
|
380
|
-
} else {
|
|
381
|
-
this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
|
|
382
|
-
break;
|
|
383
|
-
}
|
|
384
|
-
} catch (e) {
|
|
385
|
-
warn(e);
|
|
386
|
-
this.onerror(e);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
return data;
|
|
390
|
-
}
|
|
391
|
-
constructor(type){
|
|
392
|
-
super();
|
|
393
|
-
this.onerror = error;
|
|
394
|
-
this.type = type;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
class AsyncWaterfallHook extends SyncHook {
|
|
399
|
-
emit(data) {
|
|
400
|
-
if (!isObject(data)) {
|
|
401
|
-
error(`The response data for the "${this.type}" hook must be an object.`);
|
|
402
|
-
}
|
|
403
|
-
const ls = Array.from(this.listeners);
|
|
404
|
-
if (ls.length > 0) {
|
|
405
|
-
let i = 0;
|
|
406
|
-
const processError = (e)=>{
|
|
407
|
-
warn(e);
|
|
408
|
-
this.onerror(e);
|
|
409
|
-
return data;
|
|
410
|
-
};
|
|
411
|
-
const call = (prevData)=>{
|
|
412
|
-
if (checkReturnData(data, prevData)) {
|
|
413
|
-
data = prevData;
|
|
414
|
-
if (i < ls.length) {
|
|
415
|
-
try {
|
|
416
|
-
return Promise.resolve(ls[i++](data)).then(call, processError);
|
|
417
|
-
} catch (e) {
|
|
418
|
-
return processError(e);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
} else {
|
|
422
|
-
this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
|
|
423
|
-
}
|
|
424
|
-
return data;
|
|
425
|
-
};
|
|
426
|
-
return Promise.resolve(call(data));
|
|
427
|
-
}
|
|
428
|
-
return Promise.resolve(data);
|
|
429
|
-
}
|
|
430
|
-
constructor(type){
|
|
431
|
-
super();
|
|
432
|
-
this.onerror = error;
|
|
433
|
-
this.type = type;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
class PluginSystem {
|
|
438
|
-
applyPlugin(plugin) {
|
|
439
|
-
assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
|
|
440
|
-
// The plugin's name is mandatory and must be unique
|
|
441
|
-
const pluginName = plugin.name;
|
|
442
|
-
assert(pluginName, 'A name must be provided by the plugin.');
|
|
443
|
-
if (!this.registerPlugins[pluginName]) {
|
|
444
|
-
this.registerPlugins[pluginName] = plugin;
|
|
445
|
-
Object.keys(this.lifecycle).forEach((key)=>{
|
|
446
|
-
const pluginLife = plugin[key];
|
|
447
|
-
if (pluginLife) {
|
|
448
|
-
this.lifecycle[key].on(pluginLife);
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
removePlugin(pluginName) {
|
|
454
|
-
assert(pluginName, 'A name is required.');
|
|
455
|
-
const plugin = this.registerPlugins[pluginName];
|
|
456
|
-
assert(plugin, `The plugin "${pluginName}" is not registered.`);
|
|
457
|
-
Object.keys(plugin).forEach((key)=>{
|
|
458
|
-
if (key !== 'name') {
|
|
459
|
-
this.lifecycle[key].remove(plugin[key]);
|
|
460
|
-
}
|
|
461
|
-
});
|
|
462
|
-
}
|
|
463
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
464
|
-
inherit({ lifecycle, registerPlugins }) {
|
|
465
|
-
Object.keys(lifecycle).forEach((hookName)=>{
|
|
466
|
-
assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
|
|
467
|
-
this.lifecycle[hookName] = lifecycle[hookName];
|
|
468
|
-
});
|
|
469
|
-
Object.keys(registerPlugins).forEach((pluginName)=>{
|
|
470
|
-
assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
|
|
471
|
-
this.applyPlugin(registerPlugins[pluginName]);
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
constructor(lifecycle){
|
|
475
|
-
this.registerPlugins = {};
|
|
476
|
-
this.lifecycle = lifecycle;
|
|
477
|
-
this.lifecycleKeys = Object.keys(lifecycle);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
function _extends$3() {
|
|
482
|
-
_extends$3 = Object.assign || function(target) {
|
|
483
|
-
for(var i = 1; i < arguments.length; i++){
|
|
484
|
-
var source = arguments[i];
|
|
485
|
-
for(var key in source){
|
|
486
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
487
|
-
target[key] = source[key];
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
return target;
|
|
492
|
-
};
|
|
493
|
-
return _extends$3.apply(this, arguments);
|
|
494
|
-
}
|
|
495
|
-
function defaultPreloadArgs(preloadConfig) {
|
|
496
|
-
return _extends$3({
|
|
497
|
-
resourceCategory: 'sync',
|
|
498
|
-
share: true,
|
|
499
|
-
depsRemote: true,
|
|
500
|
-
prefetchInterface: false
|
|
501
|
-
}, preloadConfig);
|
|
502
|
-
}
|
|
503
|
-
function formatPreloadArgs(remotes, preloadArgs) {
|
|
504
|
-
return preloadArgs.map((args)=>{
|
|
505
|
-
const remoteInfo = matchRemote(remotes, args.nameOrAlias);
|
|
506
|
-
assert(remoteInfo, `Unable to preload ${args.nameOrAlias} as it is not included in ${!remoteInfo && safeToString({
|
|
507
|
-
remoteInfo,
|
|
508
|
-
remotes
|
|
509
|
-
})}`);
|
|
510
|
-
return {
|
|
511
|
-
remote: remoteInfo,
|
|
512
|
-
preloadConfig: defaultPreloadArgs(args)
|
|
513
|
-
};
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
function normalizePreloadExposes(exposes) {
|
|
517
|
-
if (!exposes) {
|
|
518
|
-
return [];
|
|
519
|
-
}
|
|
520
|
-
return exposes.map((expose)=>{
|
|
521
|
-
if (expose === '.') {
|
|
522
|
-
return expose;
|
|
523
|
-
}
|
|
524
|
-
if (expose.startsWith('./')) {
|
|
525
|
-
return expose.replace('./', '');
|
|
526
|
-
}
|
|
527
|
-
return expose;
|
|
528
|
-
});
|
|
529
|
-
}
|
|
530
|
-
function preloadAssets(remoteInfo, host, assets) {
|
|
531
|
-
const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
|
|
532
|
-
if (host.options.inBrowser) {
|
|
533
|
-
entryAssets.forEach((asset)=>{
|
|
534
|
-
const { moduleInfo } = asset;
|
|
535
|
-
const module = host.moduleCache.get(remoteInfo.name);
|
|
536
|
-
if (module) {
|
|
537
|
-
getRemoteEntry({
|
|
538
|
-
remoteInfo: moduleInfo,
|
|
539
|
-
remoteEntryExports: module.remoteEntryExports,
|
|
540
|
-
createScriptHook: (url)=>{
|
|
541
|
-
const res = host.loaderHook.lifecycle.createScript.emit({
|
|
542
|
-
url
|
|
543
|
-
});
|
|
544
|
-
if (res instanceof HTMLScriptElement) {
|
|
545
|
-
return res;
|
|
546
|
-
}
|
|
547
|
-
return;
|
|
548
|
-
}
|
|
549
|
-
});
|
|
550
|
-
} else {
|
|
551
|
-
getRemoteEntry({
|
|
552
|
-
remoteInfo: moduleInfo,
|
|
553
|
-
remoteEntryExports: undefined,
|
|
554
|
-
createScriptHook: (url)=>{
|
|
555
|
-
const res = host.loaderHook.lifecycle.createScript.emit({
|
|
556
|
-
url
|
|
557
|
-
});
|
|
558
|
-
if (res instanceof HTMLScriptElement) {
|
|
559
|
-
return res;
|
|
560
|
-
}
|
|
561
|
-
return;
|
|
562
|
-
}
|
|
563
|
-
});
|
|
564
|
-
}
|
|
565
|
-
});
|
|
566
|
-
const fragment = document.createDocumentFragment();
|
|
567
|
-
cssAssets.forEach((cssUrl)=>{
|
|
568
|
-
const { link: cssEl, needAttach } = createLink(cssUrl, ()=>{}, {
|
|
569
|
-
rel: 'preload',
|
|
570
|
-
as: 'style'
|
|
571
|
-
}, (url)=>{
|
|
572
|
-
const res = host.loaderHook.lifecycle.createLink.emit({
|
|
573
|
-
url
|
|
574
|
-
});
|
|
575
|
-
if (res instanceof HTMLLinkElement) {
|
|
576
|
-
return res;
|
|
577
|
-
}
|
|
578
|
-
return;
|
|
579
|
-
});
|
|
580
|
-
needAttach && fragment.appendChild(cssEl);
|
|
581
|
-
});
|
|
582
|
-
jsAssetsWithoutEntry.forEach((jsUrl)=>{
|
|
583
|
-
const { link: linkEl, needAttach } = createLink(jsUrl, ()=>{
|
|
584
|
-
// noop
|
|
585
|
-
}, {
|
|
586
|
-
rel: 'preload',
|
|
587
|
-
as: 'script'
|
|
588
|
-
}, (url)=>{
|
|
589
|
-
const res = host.loaderHook.lifecycle.createLink.emit({
|
|
590
|
-
url
|
|
591
|
-
});
|
|
592
|
-
if (res instanceof HTMLLinkElement) {
|
|
593
|
-
return res;
|
|
594
|
-
}
|
|
595
|
-
return;
|
|
596
|
-
});
|
|
597
|
-
needAttach && document.head.appendChild(linkEl);
|
|
598
|
-
});
|
|
599
|
-
document.head.appendChild(fragment);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
function _extends$2() {
|
|
604
|
-
_extends$2 = Object.assign || function(target) {
|
|
605
|
-
for(var i = 1; i < arguments.length; i++){
|
|
606
|
-
var source = arguments[i];
|
|
607
|
-
for(var key in source){
|
|
608
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
609
|
-
target[key] = source[key];
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
return target;
|
|
614
|
-
};
|
|
615
|
-
return _extends$2.apply(this, arguments);
|
|
616
|
-
}
|
|
617
|
-
function assignRemoteInfo(remoteInfo, remoteSnapshot) {
|
|
618
|
-
if (!('remoteEntry' in remoteSnapshot) || !remoteSnapshot.remoteEntry) {
|
|
619
|
-
error(`The attribute remoteEntry of ${name} must not be undefined.`);
|
|
620
|
-
}
|
|
621
|
-
const { remoteEntry } = remoteSnapshot;
|
|
622
|
-
const entryUrl = getResourceUrl(remoteSnapshot, remoteEntry);
|
|
623
|
-
remoteInfo.type = remoteSnapshot.remoteEntryType;
|
|
624
|
-
remoteInfo.entryGlobalName = remoteSnapshot.globalName;
|
|
625
|
-
remoteInfo.entry = entryUrl;
|
|
626
|
-
remoteInfo.version = remoteSnapshot.version;
|
|
627
|
-
remoteInfo.buildVersion = remoteSnapshot.buildVersion;
|
|
628
|
-
}
|
|
629
|
-
function snapshotPlugin() {
|
|
630
|
-
return {
|
|
631
|
-
name: 'snapshot-plugin',
|
|
632
|
-
async afterResolve (args) {
|
|
633
|
-
const { remote, pkgNameOrAlias, expose, origin, remoteInfo } = args;
|
|
634
|
-
if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {
|
|
635
|
-
const { remoteSnapshot, globalSnapshot } = await origin.snapshotHandler.loadRemoteSnapshotInfo(remote);
|
|
636
|
-
assignRemoteInfo(remoteInfo, remoteSnapshot);
|
|
637
|
-
// preloading assets
|
|
638
|
-
const preloadOptions = {
|
|
639
|
-
remote,
|
|
640
|
-
preloadConfig: {
|
|
641
|
-
nameOrAlias: pkgNameOrAlias,
|
|
642
|
-
exposes: [
|
|
643
|
-
expose
|
|
644
|
-
],
|
|
645
|
-
resourceCategory: 'sync',
|
|
646
|
-
share: false,
|
|
647
|
-
depsRemote: false
|
|
648
|
-
}
|
|
649
|
-
};
|
|
650
|
-
const assets = await origin.hooks.lifecycle.generatePreloadAssets.emit({
|
|
651
|
-
origin,
|
|
652
|
-
preloadOptions,
|
|
653
|
-
remoteInfo,
|
|
654
|
-
remote,
|
|
655
|
-
remoteSnapshot,
|
|
656
|
-
globalSnapshot
|
|
657
|
-
});
|
|
658
|
-
if (assets) {
|
|
659
|
-
preloadAssets(remoteInfo, origin, assets);
|
|
660
|
-
}
|
|
661
|
-
return _extends$2({}, args, {
|
|
662
|
-
remoteSnapshot
|
|
663
|
-
});
|
|
664
|
-
}
|
|
665
|
-
return args;
|
|
666
|
-
}
|
|
667
|
-
};
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
// name
|
|
671
|
-
// name:version
|
|
672
|
-
function splitId(id) {
|
|
673
|
-
const splitInfo = id.split(':');
|
|
674
|
-
if (splitInfo.length === 1) {
|
|
675
|
-
return {
|
|
676
|
-
name: splitInfo[0],
|
|
677
|
-
version: undefined
|
|
678
|
-
};
|
|
679
|
-
} else if (splitInfo.length === 2) {
|
|
680
|
-
return {
|
|
681
|
-
name: splitInfo[0],
|
|
682
|
-
version: splitInfo[1]
|
|
683
|
-
};
|
|
684
|
-
} else {
|
|
685
|
-
return {
|
|
686
|
-
name: splitInfo[1],
|
|
687
|
-
version: splitInfo[2]
|
|
688
|
-
};
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
// Traverse all nodes in moduleInfo and traverse the entire snapshot
|
|
692
|
-
function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo = {}, remoteSnapshot) {
|
|
693
|
-
const id = getFMId(remoteInfo);
|
|
694
|
-
const { value: snapshotValue } = getInfoWithoutType(globalSnapshot, id);
|
|
695
|
-
const effectiveRemoteSnapshot = remoteSnapshot || snapshotValue;
|
|
696
|
-
if (effectiveRemoteSnapshot && !isManifestProvider(effectiveRemoteSnapshot)) {
|
|
697
|
-
traverse(effectiveRemoteSnapshot, remoteInfo, isRoot);
|
|
698
|
-
if (effectiveRemoteSnapshot.remotesInfo) {
|
|
699
|
-
const remoteKeys = Object.keys(effectiveRemoteSnapshot.remotesInfo);
|
|
700
|
-
for (const key of remoteKeys){
|
|
701
|
-
if (memo[key]) {
|
|
702
|
-
continue;
|
|
703
|
-
}
|
|
704
|
-
memo[key] = true;
|
|
705
|
-
const subRemoteInfo = splitId(key);
|
|
706
|
-
const remoteValue = effectiveRemoteSnapshot.remotesInfo[key];
|
|
707
|
-
traverseModuleInfo(globalSnapshot, {
|
|
708
|
-
name: subRemoteInfo.name,
|
|
709
|
-
version: remoteValue.matchedVersion
|
|
710
|
-
}, traverse, false, memo, undefined);
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
// eslint-disable-next-line max-lines-per-function
|
|
716
|
-
function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, remoteSnapshot) {
|
|
717
|
-
const cssAssets = [];
|
|
718
|
-
const jsAssets = [];
|
|
719
|
-
const entryAssets = [];
|
|
720
|
-
const loadedSharedJsAssets = new Set();
|
|
721
|
-
const loadedSharedCssAssets = new Set();
|
|
722
|
-
const { options } = origin;
|
|
723
|
-
const { preloadConfig: rootPreloadConfig } = preloadOptions;
|
|
724
|
-
const { depsRemote } = rootPreloadConfig;
|
|
725
|
-
const memo = {};
|
|
726
|
-
traverseModuleInfo(globalSnapshot, remote, (moduleInfoSnapshot, remoteInfo, isRoot)=>{
|
|
727
|
-
let preloadConfig;
|
|
728
|
-
if (isRoot) {
|
|
729
|
-
preloadConfig = rootPreloadConfig;
|
|
730
|
-
} else {
|
|
731
|
-
if (Array.isArray(depsRemote)) {
|
|
732
|
-
// eslint-disable-next-line array-callback-return
|
|
733
|
-
const findPreloadConfig = depsRemote.find((remoteConfig)=>{
|
|
734
|
-
if (remoteConfig.nameOrAlias === remoteInfo.name || remoteConfig.nameOrAlias === remoteInfo.alias) {
|
|
735
|
-
return true;
|
|
736
|
-
}
|
|
737
|
-
return false;
|
|
738
|
-
});
|
|
739
|
-
if (!findPreloadConfig) {
|
|
740
|
-
return;
|
|
741
|
-
}
|
|
742
|
-
preloadConfig = defaultPreloadArgs(findPreloadConfig);
|
|
743
|
-
} else if (depsRemote === true) {
|
|
744
|
-
preloadConfig = rootPreloadConfig;
|
|
745
|
-
} else {
|
|
746
|
-
return;
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
const remoteEntryUrl = getResourceUrl(moduleInfoSnapshot, 'remoteEntry' in moduleInfoSnapshot ? moduleInfoSnapshot.remoteEntry : '');
|
|
750
|
-
if (remoteEntryUrl) {
|
|
751
|
-
entryAssets.push({
|
|
752
|
-
name: remoteInfo.name,
|
|
753
|
-
moduleInfo: {
|
|
754
|
-
name: remoteInfo.name,
|
|
755
|
-
entry: remoteEntryUrl,
|
|
756
|
-
type: 'remoteEntryType' in moduleInfoSnapshot ? moduleInfoSnapshot.remoteEntryType : 'global',
|
|
757
|
-
entryGlobalName: 'globalName' in moduleInfoSnapshot ? moduleInfoSnapshot.globalName : remoteInfo.name,
|
|
758
|
-
shareScope: '',
|
|
759
|
-
version: 'version' in moduleInfoSnapshot ? moduleInfoSnapshot.version : undefined
|
|
760
|
-
},
|
|
761
|
-
url: remoteEntryUrl
|
|
762
|
-
});
|
|
763
|
-
}
|
|
764
|
-
let moduleAssetsInfo = 'modules' in moduleInfoSnapshot ? moduleInfoSnapshot.modules : [];
|
|
765
|
-
const normalizedPreloadExposes = normalizePreloadExposes(preloadConfig.exposes);
|
|
766
|
-
if (normalizedPreloadExposes.length && 'modules' in moduleInfoSnapshot) {
|
|
767
|
-
var _moduleInfoSnapshot_modules;
|
|
768
|
-
moduleAssetsInfo = moduleInfoSnapshot == null ? void 0 : (_moduleInfoSnapshot_modules = moduleInfoSnapshot.modules) == null ? void 0 : _moduleInfoSnapshot_modules.reduce((assets, moduleAssetInfo)=>{
|
|
769
|
-
if ((normalizedPreloadExposes == null ? void 0 : normalizedPreloadExposes.indexOf(moduleAssetInfo.moduleName)) !== -1) {
|
|
770
|
-
assets.push(moduleAssetInfo);
|
|
771
|
-
}
|
|
772
|
-
return assets;
|
|
773
|
-
}, []);
|
|
774
|
-
}
|
|
775
|
-
function handleAssets(assets) {
|
|
776
|
-
const assetsRes = assets.map((asset)=>getResourceUrl(moduleInfoSnapshot, asset));
|
|
777
|
-
if (preloadConfig.filter) {
|
|
778
|
-
return assetsRes.filter(preloadConfig.filter);
|
|
779
|
-
}
|
|
780
|
-
return assetsRes;
|
|
781
|
-
}
|
|
782
|
-
if (moduleAssetsInfo) {
|
|
783
|
-
const assetsLength = moduleAssetsInfo.length;
|
|
784
|
-
for(let index = 0; index < assetsLength; index++){
|
|
785
|
-
const assetsInfo = moduleAssetsInfo[index];
|
|
786
|
-
const exposeFullPath = `${remoteInfo.name}/${assetsInfo.moduleName}`;
|
|
787
|
-
origin.hooks.lifecycle.handlePreloadModule.emit({
|
|
788
|
-
id: assetsInfo.moduleName === '.' ? remoteInfo.name : exposeFullPath,
|
|
789
|
-
name: remoteInfo.name,
|
|
790
|
-
remoteSnapshot: moduleInfoSnapshot,
|
|
791
|
-
preloadConfig,
|
|
792
|
-
remote: remoteInfo,
|
|
793
|
-
origin
|
|
794
|
-
});
|
|
795
|
-
const preloaded = getPreloaded(exposeFullPath);
|
|
796
|
-
if (preloaded) {
|
|
797
|
-
continue;
|
|
798
|
-
}
|
|
799
|
-
if (preloadConfig.resourceCategory === 'all') {
|
|
800
|
-
cssAssets.push(...handleAssets(assetsInfo.assets.css.async));
|
|
801
|
-
cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
|
|
802
|
-
jsAssets.push(...handleAssets(assetsInfo.assets.js.async));
|
|
803
|
-
jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
|
|
804
|
-
// eslint-disable-next-line no-constant-condition
|
|
805
|
-
} else if (preloadConfig.resourceCategory = 'sync') {
|
|
806
|
-
cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
|
|
807
|
-
jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
|
|
808
|
-
}
|
|
809
|
-
setPreloaded(exposeFullPath);
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
}, true, memo, remoteSnapshot);
|
|
813
|
-
if (remoteSnapshot.shared) {
|
|
814
|
-
remoteSnapshot.shared.forEach((shared)=>{
|
|
815
|
-
var _options_shared;
|
|
816
|
-
const shareInfo = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
|
|
817
|
-
// When data is downgraded, the shared configuration may be different.
|
|
818
|
-
if (!shareInfo) {
|
|
819
|
-
return;
|
|
820
|
-
}
|
|
821
|
-
const registeredShared = getRegisteredShare(origin.shareScopeMap, shared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
|
|
822
|
-
// If the global share does not exist, or the lib function does not exist, it means that the shared has not been loaded yet and can be preloaded.
|
|
823
|
-
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
824
|
-
shared.assets.js.sync.forEach((asset)=>{
|
|
825
|
-
loadedSharedJsAssets.add(asset);
|
|
826
|
-
});
|
|
827
|
-
shared.assets.css.sync.forEach((asset)=>{
|
|
828
|
-
loadedSharedCssAssets.add(asset);
|
|
829
|
-
});
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
|
-
const needPreloadJsAssets = jsAssets.filter((asset)=>!loadedSharedJsAssets.has(asset));
|
|
834
|
-
const needPreloadCssAssets = cssAssets.filter((asset)=>!loadedSharedCssAssets.has(asset));
|
|
835
|
-
return {
|
|
836
|
-
cssAssets: needPreloadCssAssets,
|
|
837
|
-
jsAssetsWithoutEntry: needPreloadJsAssets,
|
|
838
|
-
entryAssets
|
|
839
|
-
};
|
|
840
|
-
}
|
|
841
|
-
const generatePreloadAssetsPlugin = function() {
|
|
842
|
-
return {
|
|
843
|
-
name: 'generate-preload-assets-plugin',
|
|
844
|
-
async generatePreloadAssets (args) {
|
|
845
|
-
const { origin, preloadOptions, remoteInfo, remote, globalSnapshot, remoteSnapshot } = args;
|
|
846
|
-
if (isRemoteInfoWithEntry(remote) && isPureRemoteEntry(remote)) {
|
|
847
|
-
return {
|
|
848
|
-
cssAssets: [],
|
|
849
|
-
jsAssetsWithoutEntry: [],
|
|
850
|
-
entryAssets: [
|
|
851
|
-
{
|
|
852
|
-
name: remote.name,
|
|
853
|
-
url: remote.entry,
|
|
854
|
-
moduleInfo: {
|
|
855
|
-
name: remoteInfo.name,
|
|
856
|
-
entry: remote.entry,
|
|
857
|
-
type: 'global',
|
|
858
|
-
entryGlobalName: '',
|
|
859
|
-
shareScope: ''
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
]
|
|
863
|
-
};
|
|
864
|
-
}
|
|
865
|
-
assignRemoteInfo(remoteInfo, remoteSnapshot);
|
|
866
|
-
const assets = generatePreloadAssets(origin, preloadOptions, remoteInfo, globalSnapshot, remoteSnapshot);
|
|
867
|
-
return assets;
|
|
868
|
-
}
|
|
869
|
-
};
|
|
870
|
-
};
|
|
871
|
-
|
|
872
|
-
function _extends$1() {
|
|
873
|
-
_extends$1 = Object.assign || function(target) {
|
|
874
|
-
for(var i = 1; i < arguments.length; i++){
|
|
875
|
-
var source = arguments[i];
|
|
876
|
-
for(var key in source){
|
|
877
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
878
|
-
target[key] = source[key];
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
return target;
|
|
883
|
-
};
|
|
884
|
-
return _extends$1.apply(this, arguments);
|
|
885
|
-
}
|
|
886
|
-
class SnapshotHandler {
|
|
887
|
-
async loadSnapshot(moduleInfo) {
|
|
888
|
-
const { options } = this.HostInstance;
|
|
889
|
-
const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } = this.getGlobalRemoteInfo(moduleInfo);
|
|
890
|
-
const { remoteSnapshot: globalRemoteSnapshot, globalSnapshot: globalSnapshotRes } = await this.hooks.lifecycle.loadSnapshot.emit({
|
|
891
|
-
options,
|
|
892
|
-
moduleInfo,
|
|
893
|
-
hostGlobalSnapshot,
|
|
894
|
-
remoteSnapshot,
|
|
895
|
-
globalSnapshot
|
|
896
|
-
});
|
|
897
|
-
return {
|
|
898
|
-
remoteSnapshot: globalRemoteSnapshot,
|
|
899
|
-
globalSnapshot: globalSnapshotRes
|
|
900
|
-
};
|
|
901
|
-
}
|
|
902
|
-
// eslint-disable-next-line max-lines-per-function
|
|
903
|
-
async loadRemoteSnapshotInfo(moduleInfo) {
|
|
904
|
-
const { options } = this.HostInstance;
|
|
905
|
-
await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({
|
|
906
|
-
options,
|
|
907
|
-
moduleInfo
|
|
908
|
-
});
|
|
909
|
-
let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({
|
|
910
|
-
name: this.HostInstance.options.name,
|
|
911
|
-
version: this.HostInstance.options.version
|
|
912
|
-
});
|
|
913
|
-
if (!hostSnapshot) {
|
|
914
|
-
hostSnapshot = {
|
|
915
|
-
version: this.HostInstance.options.version || '',
|
|
916
|
-
remoteEntry: '',
|
|
917
|
-
remotesInfo: {}
|
|
918
|
-
};
|
|
919
|
-
addGlobalSnapshot({
|
|
920
|
-
[this.HostInstance.options.name]: hostSnapshot
|
|
921
|
-
});
|
|
922
|
-
}
|
|
923
|
-
// In dynamic loadRemote scenarios, incomplete remotesInfo delivery may occur. In such cases, the remotesInfo in the host needs to be completed in the snapshot at runtime.
|
|
924
|
-
// This ensures the snapshot's integrity and helps the chrome plugin correctly identify all producer modules, ensuring that proxyable producer modules will not be missing.
|
|
925
|
-
if (hostSnapshot && 'remotesInfo' in hostSnapshot && !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
|
|
926
|
-
if ('version' in moduleInfo || 'entry' in moduleInfo) {
|
|
927
|
-
hostSnapshot.remotesInfo = _extends$1({}, hostSnapshot == null ? void 0 : hostSnapshot.remotesInfo, {
|
|
928
|
-
[moduleInfo.name]: {
|
|
929
|
-
matchedVersion: 'version' in moduleInfo ? moduleInfo.version : moduleInfo.entry
|
|
930
|
-
}
|
|
931
|
-
});
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } = this.getGlobalRemoteInfo(moduleInfo);
|
|
935
|
-
const { remoteSnapshot: globalRemoteSnapshot, globalSnapshot: globalSnapshotRes } = await this.hooks.lifecycle.loadSnapshot.emit({
|
|
936
|
-
options,
|
|
937
|
-
moduleInfo,
|
|
938
|
-
hostGlobalSnapshot,
|
|
939
|
-
remoteSnapshot,
|
|
940
|
-
globalSnapshot
|
|
941
|
-
});
|
|
942
|
-
// global snapshot includes manifest or module info includes manifest
|
|
943
|
-
if (globalRemoteSnapshot) {
|
|
944
|
-
if (isManifestProvider(globalRemoteSnapshot)) {
|
|
945
|
-
const moduleSnapshot = await this.getManifestJson(globalRemoteSnapshot.remoteEntry, moduleInfo, {});
|
|
946
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
947
|
-
const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(_extends$1({}, moduleInfo, {
|
|
948
|
-
// The global remote may be overridden
|
|
949
|
-
// Therefore, set the snapshot key to the global address of the actual request
|
|
950
|
-
entry: globalRemoteSnapshot.remoteEntry
|
|
951
|
-
}), moduleSnapshot);
|
|
952
|
-
return {
|
|
953
|
-
remoteSnapshot: moduleSnapshot,
|
|
954
|
-
globalSnapshot: globalSnapshotRes
|
|
955
|
-
};
|
|
956
|
-
} else {
|
|
957
|
-
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
958
|
-
options: this.HostInstance.options,
|
|
959
|
-
moduleInfo,
|
|
960
|
-
remoteSnapshot: globalRemoteSnapshot,
|
|
961
|
-
from: 'global'
|
|
962
|
-
});
|
|
963
|
-
return {
|
|
964
|
-
remoteSnapshot: remoteSnapshotRes,
|
|
965
|
-
globalSnapshot: globalSnapshotRes
|
|
966
|
-
};
|
|
967
|
-
}
|
|
968
|
-
} else {
|
|
969
|
-
if (isRemoteInfoWithEntry(moduleInfo)) {
|
|
970
|
-
// get from manifest.json and merge remote info from remote server
|
|
971
|
-
const moduleSnapshot = await this.getManifestJson(moduleInfo.entry, moduleInfo, {});
|
|
972
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
973
|
-
const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(moduleInfo, moduleSnapshot);
|
|
974
|
-
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
975
|
-
options: this.HostInstance.options,
|
|
976
|
-
moduleInfo,
|
|
977
|
-
remoteSnapshot: moduleSnapshot,
|
|
978
|
-
from: 'global'
|
|
979
|
-
});
|
|
980
|
-
return {
|
|
981
|
-
remoteSnapshot: remoteSnapshotRes,
|
|
982
|
-
globalSnapshot: globalSnapshotRes
|
|
983
|
-
};
|
|
984
|
-
} else {
|
|
985
|
-
error(`
|
|
986
|
-
Cannot get remoteSnapshot with the name: '${moduleInfo.name}', version: '${moduleInfo.version}' from __FEDERATION__.moduleInfo. The following reasons may be causing the problem:\n
|
|
987
|
-
1. The Deploy platform did not deliver the correct data. You can use __FEDERATION__.moduleInfo to check the remoteInfo.\n
|
|
988
|
-
2. The remote '${moduleInfo.name}' version '${moduleInfo.version}' is not released.\n
|
|
989
|
-
The transformed module info: ${JSON.stringify(globalSnapshotRes)}
|
|
990
|
-
`);
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
getGlobalRemoteInfo(moduleInfo) {
|
|
995
|
-
const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({
|
|
996
|
-
name: this.HostInstance.options.name,
|
|
997
|
-
version: this.HostInstance.options.version
|
|
998
|
-
});
|
|
999
|
-
// get remote detail info from global
|
|
1000
|
-
const globalRemoteInfo = hostGlobalSnapshot && 'remotesInfo' in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;
|
|
1001
|
-
if (globalRemoteInfo && globalRemoteInfo.matchedVersion) {
|
|
1002
|
-
return {
|
|
1003
|
-
hostGlobalSnapshot,
|
|
1004
|
-
globalSnapshot: getGlobalSnapshot(),
|
|
1005
|
-
remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
|
|
1006
|
-
name: moduleInfo.name,
|
|
1007
|
-
version: globalRemoteInfo.matchedVersion
|
|
1008
|
-
})
|
|
1009
|
-
};
|
|
1010
|
-
}
|
|
1011
|
-
return {
|
|
1012
|
-
hostGlobalSnapshot: undefined,
|
|
1013
|
-
globalSnapshot: getGlobalSnapshot(),
|
|
1014
|
-
remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
|
|
1015
|
-
name: moduleInfo.name,
|
|
1016
|
-
version: 'version' in moduleInfo ? moduleInfo.version : undefined
|
|
1017
|
-
})
|
|
1018
|
-
};
|
|
1019
|
-
}
|
|
1020
|
-
async getManifestJson(manifestUrl, moduleInfo, extraOptions) {
|
|
1021
|
-
const getManifest = async ()=>{
|
|
1022
|
-
let manifestJson = this.manifestCache.get(manifestUrl);
|
|
1023
|
-
if (manifestJson) {
|
|
1024
|
-
return manifestJson;
|
|
1025
|
-
}
|
|
1026
|
-
try {
|
|
1027
|
-
let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {});
|
|
1028
|
-
if (!res || !(res instanceof Response)) {
|
|
1029
|
-
res = await fetch(manifestUrl, {});
|
|
1030
|
-
}
|
|
1031
|
-
manifestJson = await res.json();
|
|
1032
|
-
assert(manifestJson.metaData && manifestJson.exposes && manifestJson.shared, `${manifestUrl} is not a federation manifest`);
|
|
1033
|
-
this.manifestCache.set(manifestUrl, manifestJson);
|
|
1034
|
-
return manifestJson;
|
|
1035
|
-
} catch (err) {
|
|
1036
|
-
error(`Failed to get manifestJson for ${moduleInfo.name}. The manifest URL is ${manifestUrl}. Please ensure that the manifestUrl is accessible.
|
|
1037
|
-
\n Error message:
|
|
1038
|
-
\n ${err}`);
|
|
1039
|
-
}
|
|
1040
|
-
};
|
|
1041
|
-
const asyncLoadProcess = async ()=>{
|
|
1042
|
-
const manifestJson = await getManifest();
|
|
1043
|
-
const remoteSnapshot = generateSnapshotFromManifest(manifestJson, {
|
|
1044
|
-
version: manifestUrl
|
|
1045
|
-
});
|
|
1046
|
-
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
1047
|
-
options: this.HostInstance.options,
|
|
1048
|
-
moduleInfo,
|
|
1049
|
-
manifestJson,
|
|
1050
|
-
remoteSnapshot,
|
|
1051
|
-
manifestUrl,
|
|
1052
|
-
from: 'manifest'
|
|
1053
|
-
});
|
|
1054
|
-
return remoteSnapshotRes;
|
|
1055
|
-
};
|
|
1056
|
-
if (!this.manifestLoading[manifestUrl]) {
|
|
1057
|
-
this.manifestLoading[manifestUrl] = asyncLoadProcess().then((res)=>res);
|
|
1058
|
-
}
|
|
1059
|
-
return this.manifestLoading[manifestUrl];
|
|
1060
|
-
}
|
|
1061
|
-
constructor(HostInstance){
|
|
1062
|
-
this.loadingHostSnapshot = null;
|
|
1063
|
-
this.manifestCache = new Map();
|
|
1064
|
-
this.hooks = new PluginSystem({
|
|
1065
|
-
beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
|
|
1066
|
-
loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
|
|
1067
|
-
loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot')
|
|
1068
|
-
});
|
|
1069
|
-
this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
|
|
1070
|
-
this.HostInstance = HostInstance;
|
|
1071
|
-
this.loaderHook = HostInstance.loaderHook;
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
|
-
function _extends() {
|
|
1076
|
-
_extends = Object.assign || function(target) {
|
|
1077
|
-
for(var i = 1; i < arguments.length; i++){
|
|
1078
|
-
var source = arguments[i];
|
|
1079
|
-
for(var key in source){
|
|
1080
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
1081
|
-
target[key] = source[key];
|
|
1082
|
-
}
|
|
1083
|
-
}
|
|
1084
|
-
}
|
|
1085
|
-
return target;
|
|
1086
|
-
};
|
|
1087
|
-
return _extends.apply(this, arguments);
|
|
1088
|
-
}
|
|
1089
|
-
function _object_without_properties_loose(source, excluded) {
|
|
1090
|
-
if (source == null) return {};
|
|
1091
|
-
var target = {};
|
|
1092
|
-
var sourceKeys = Object.keys(source);
|
|
1093
|
-
var key, i;
|
|
1094
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
1095
|
-
key = sourceKeys[i];
|
|
1096
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
1097
|
-
target[key] = source[key];
|
|
1098
|
-
}
|
|
1099
|
-
return target;
|
|
1100
|
-
}
|
|
1101
|
-
class FederationHost {
|
|
1102
|
-
_setGlobalShareScopeMap() {
|
|
1103
|
-
const globalShareScopeMap = getGlobalShareScope();
|
|
1104
|
-
const identifier = this.options.id || this.options.name;
|
|
1105
|
-
if (identifier && !globalShareScopeMap[identifier]) {
|
|
1106
|
-
globalShareScopeMap[identifier] = this.shareScopeMap;
|
|
1107
|
-
}
|
|
1108
|
-
}
|
|
1109
|
-
initOptions(userOptions) {
|
|
1110
|
-
this.registerPlugins(userOptions.plugins);
|
|
1111
|
-
const options = this.formatOptions(this.options, userOptions);
|
|
1112
|
-
this.options = options;
|
|
1113
|
-
return options;
|
|
1114
|
-
}
|
|
1115
|
-
async loadShare(pkgName, customShareInfo) {
|
|
1116
|
-
var _this_options_shared;
|
|
1117
|
-
// This function performs the following steps:
|
|
1118
|
-
// 1. Checks if the currently loaded share already exists, if not, it throws an error
|
|
1119
|
-
// 2. Searches globally for a matching share, if found, it uses it directly
|
|
1120
|
-
// 3. If not found, it retrieves it from the current share and stores the obtained share globally.
|
|
1121
|
-
const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
|
|
1122
|
-
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1123
|
-
await Promise.all(shareInfo.scope.map(async (shareScope)=>{
|
|
1124
|
-
await Promise.all(this.initializeSharing(shareScope, shareInfo.strategy));
|
|
1125
|
-
return;
|
|
1126
|
-
}));
|
|
1127
|
-
}
|
|
1128
|
-
const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
|
|
1129
|
-
pkgName,
|
|
1130
|
-
shareInfo,
|
|
1131
|
-
shared: this.options.shared,
|
|
1132
|
-
origin: this
|
|
1133
|
-
});
|
|
1134
|
-
const { shareInfo: shareInfoRes } = loadShareRes;
|
|
1135
|
-
// Assert that shareInfoRes exists, if not, throw an error
|
|
1136
|
-
assert(shareInfoRes, `Cannot find ${pkgName} Share in the ${this.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
|
|
1137
|
-
// Retrieve from cache
|
|
1138
|
-
const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1139
|
-
const addUseIn = (shared)=>{
|
|
1140
|
-
if (!shared.useIn) {
|
|
1141
|
-
shared.useIn = [];
|
|
1142
|
-
}
|
|
1143
|
-
addUniqueItem(shared.useIn, this.options.name);
|
|
1144
|
-
};
|
|
1145
|
-
if (registeredShared && registeredShared.lib) {
|
|
1146
|
-
addUseIn(registeredShared);
|
|
1147
|
-
return registeredShared.lib;
|
|
1148
|
-
} else if (registeredShared && registeredShared.loading && !registeredShared.loaded) {
|
|
1149
|
-
const factory = await registeredShared.loading;
|
|
1150
|
-
registeredShared.loaded = true;
|
|
1151
|
-
if (!registeredShared.lib) {
|
|
1152
|
-
registeredShared.lib = factory;
|
|
1153
|
-
}
|
|
1154
|
-
addUseIn(registeredShared);
|
|
1155
|
-
return factory;
|
|
1156
|
-
} else if (registeredShared) {
|
|
1157
|
-
const asyncLoadProcess = async ()=>{
|
|
1158
|
-
const factory = await registeredShared.get();
|
|
1159
|
-
shareInfoRes.lib = factory;
|
|
1160
|
-
shareInfoRes.loaded = true;
|
|
1161
|
-
addUseIn(shareInfoRes);
|
|
1162
|
-
const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1163
|
-
if (gShared) {
|
|
1164
|
-
gShared.lib = factory;
|
|
1165
|
-
gShared.loaded = true;
|
|
1166
|
-
}
|
|
1167
|
-
return factory;
|
|
1168
|
-
};
|
|
1169
|
-
const loading = asyncLoadProcess();
|
|
1170
|
-
this.setShared({
|
|
1171
|
-
pkgName,
|
|
1172
|
-
loaded: false,
|
|
1173
|
-
shared: registeredShared,
|
|
1174
|
-
from: this.options.name,
|
|
1175
|
-
lib: null,
|
|
1176
|
-
loading
|
|
1177
|
-
});
|
|
1178
|
-
return loading;
|
|
1179
|
-
} else {
|
|
1180
|
-
if (customShareInfo) {
|
|
1181
|
-
return false;
|
|
1182
|
-
}
|
|
1183
|
-
const asyncLoadProcess = async ()=>{
|
|
1184
|
-
const factory = await shareInfoRes.get();
|
|
1185
|
-
shareInfoRes.lib = factory;
|
|
1186
|
-
shareInfoRes.loaded = true;
|
|
1187
|
-
addUseIn(shareInfoRes);
|
|
1188
|
-
const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1189
|
-
if (gShared) {
|
|
1190
|
-
gShared.lib = factory;
|
|
1191
|
-
gShared.loaded = true;
|
|
1192
|
-
}
|
|
1193
|
-
return factory;
|
|
1194
|
-
};
|
|
1195
|
-
const loading = asyncLoadProcess();
|
|
1196
|
-
this.setShared({
|
|
1197
|
-
pkgName,
|
|
1198
|
-
loaded: false,
|
|
1199
|
-
shared: shareInfoRes,
|
|
1200
|
-
from: this.options.name,
|
|
1201
|
-
lib: null,
|
|
1202
|
-
loading
|
|
1203
|
-
});
|
|
1204
|
-
return loading;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
// The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.
|
|
1208
|
-
// 1. If the loaded shared already exists globally, then it will be reused
|
|
1209
|
-
// 2. If lib exists in local shared, it will be used directly
|
|
1210
|
-
// 3. If the local get returns something other than Promise, then it will be used directly
|
|
1211
|
-
loadShareSync(pkgName, customShareInfo) {
|
|
1212
|
-
var _this_options_shared;
|
|
1213
|
-
const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
|
|
1214
|
-
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1215
|
-
shareInfo.scope.forEach((shareScope)=>{
|
|
1216
|
-
this.initializeSharing(shareScope, shareInfo.strategy);
|
|
1217
|
-
});
|
|
1218
|
-
}
|
|
1219
|
-
const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfo, this.hooks.lifecycle.resolveShare);
|
|
1220
|
-
const addUseIn = (shared)=>{
|
|
1221
|
-
if (!shared.useIn) {
|
|
1222
|
-
shared.useIn = [];
|
|
1223
|
-
}
|
|
1224
|
-
addUniqueItem(shared.useIn, this.options.name);
|
|
1225
|
-
};
|
|
1226
|
-
if (registeredShared) {
|
|
1227
|
-
if (typeof registeredShared.lib === 'function') {
|
|
1228
|
-
addUseIn(registeredShared);
|
|
1229
|
-
if (!registeredShared.loaded) {
|
|
1230
|
-
registeredShared.loaded = true;
|
|
1231
|
-
if (registeredShared.from === this.options.name) {
|
|
1232
|
-
shareInfo.loaded = true;
|
|
1233
|
-
}
|
|
1234
|
-
}
|
|
1235
|
-
return registeredShared.lib;
|
|
1236
|
-
}
|
|
1237
|
-
if (typeof registeredShared.get === 'function') {
|
|
1238
|
-
const module = registeredShared.get();
|
|
1239
|
-
if (!(module instanceof Promise)) {
|
|
1240
|
-
addUseIn(registeredShared);
|
|
1241
|
-
this.setShared({
|
|
1242
|
-
pkgName,
|
|
1243
|
-
loaded: true,
|
|
1244
|
-
from: this.options.name,
|
|
1245
|
-
lib: module,
|
|
1246
|
-
shared: registeredShared
|
|
1247
|
-
});
|
|
1248
|
-
return module;
|
|
1249
|
-
}
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
if (shareInfo.lib) {
|
|
1253
|
-
if (!shareInfo.loaded) {
|
|
1254
|
-
shareInfo.loaded = true;
|
|
1255
|
-
}
|
|
1256
|
-
return shareInfo.lib;
|
|
1257
|
-
}
|
|
1258
|
-
if (shareInfo.get) {
|
|
1259
|
-
const module = shareInfo.get();
|
|
1260
|
-
if (module instanceof Promise) {
|
|
1261
|
-
throw new Error(`
|
|
1262
|
-
The loadShareSync function was unable to load ${pkgName}. The ${pkgName} could not be found in ${this.options.name}.
|
|
1263
|
-
Possible reasons for failure: \n
|
|
1264
|
-
1. The ${pkgName} share was registered with the 'get' attribute, but loadShare was not used beforehand.\n
|
|
1265
|
-
2. The ${pkgName} share was not registered with the 'lib' attribute.\n
|
|
1266
|
-
`);
|
|
1267
|
-
}
|
|
1268
|
-
shareInfo.lib = module;
|
|
1269
|
-
this.setShared({
|
|
1270
|
-
pkgName,
|
|
1271
|
-
loaded: true,
|
|
1272
|
-
from: this.options.name,
|
|
1273
|
-
lib: shareInfo.lib,
|
|
1274
|
-
shared: shareInfo
|
|
1275
|
-
});
|
|
1276
|
-
return shareInfo.lib;
|
|
1277
|
-
}
|
|
1278
|
-
throw new Error(`
|
|
1279
|
-
The loadShareSync function was unable to load ${pkgName}. The ${pkgName} could not be found in ${this.options.name}.
|
|
1280
|
-
Possible reasons for failure: \n
|
|
1281
|
-
1. The ${pkgName} share was registered with the 'get' attribute, but loadShare was not used beforehand.\n
|
|
1282
|
-
2. The ${pkgName} share was not registered with the 'lib' attribute.\n
|
|
1283
|
-
`);
|
|
1284
|
-
}
|
|
1285
|
-
initRawContainer(name, url, container) {
|
|
1286
|
-
const remoteInfo = getRemoteInfo({
|
|
1287
|
-
name,
|
|
1288
|
-
entry: url
|
|
1289
|
-
});
|
|
1290
|
-
const module = new Module({
|
|
1291
|
-
host: this,
|
|
1292
|
-
remoteInfo
|
|
1293
|
-
});
|
|
1294
|
-
module.remoteEntryExports = container;
|
|
1295
|
-
this.moduleCache.set(name, module);
|
|
1296
|
-
return module;
|
|
1297
|
-
}
|
|
1298
|
-
async _getRemoteModuleAndOptions(id) {
|
|
1299
|
-
const loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({
|
|
1300
|
-
id,
|
|
1301
|
-
options: this.options,
|
|
1302
|
-
origin: this
|
|
1303
|
-
});
|
|
1304
|
-
const { id: idRes } = loadRemoteArgs;
|
|
1305
|
-
const remoteSplitInfo = matchRemoteWithNameAndExpose(this.options.remotes, idRes);
|
|
1306
|
-
assert(remoteSplitInfo, `
|
|
1307
|
-
Unable to locate ${idRes} in ${this.options.name}. Potential reasons for failure include:\n
|
|
1308
|
-
1. ${idRes} was not included in the 'remotes' parameter of ${this.options.name || 'the host'}.\n
|
|
1309
|
-
2. ${idRes} could not be found in the 'remotes' of ${this.options.name} with either 'name' or 'alias' attributes.
|
|
1310
|
-
3. ${idRes} is not online, injected, or loaded.
|
|
1311
|
-
4. ${idRes} cannot be accessed on the expected.
|
|
1312
|
-
5. The 'beforeRequest' hook was provided but did not return the correct 'remoteInfo' when attempting to load ${idRes}.
|
|
1313
|
-
`);
|
|
1314
|
-
const { remote: rawRemote } = remoteSplitInfo;
|
|
1315
|
-
const remoteInfo = getRemoteInfo(rawRemote);
|
|
1316
|
-
const matchInfo = await this.hooks.lifecycle.afterResolve.emit(_extends({
|
|
1317
|
-
id: idRes
|
|
1318
|
-
}, remoteSplitInfo, {
|
|
1319
|
-
options: this.options,
|
|
1320
|
-
origin: this,
|
|
1321
|
-
remoteInfo
|
|
1322
|
-
}));
|
|
1323
|
-
const { remote, expose } = matchInfo;
|
|
1324
|
-
assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
|
|
1325
|
-
let module = this.moduleCache.get(remote.name);
|
|
1326
|
-
const moduleOptions = {
|
|
1327
|
-
host: this,
|
|
1328
|
-
remoteInfo
|
|
1329
|
-
};
|
|
1330
|
-
if (!module) {
|
|
1331
|
-
module = new Module(moduleOptions);
|
|
1332
|
-
this.moduleCache.set(remote.name, module);
|
|
1333
|
-
}
|
|
1334
|
-
return {
|
|
1335
|
-
module,
|
|
1336
|
-
moduleOptions,
|
|
1337
|
-
remoteMatchInfo: matchInfo
|
|
1338
|
-
};
|
|
1339
|
-
}
|
|
1340
|
-
// eslint-disable-next-line max-lines-per-function
|
|
1341
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1342
|
-
async loadRemote(id, options) {
|
|
1343
|
-
try {
|
|
1344
|
-
const { loadFactory = true } = options || {
|
|
1345
|
-
loadFactory: true
|
|
1346
|
-
};
|
|
1347
|
-
// 1. Validate the parameters of the retrieved module. There are two module request methods: pkgName + expose and alias + expose.
|
|
1348
|
-
// 2. Request the snapshot information of the current host and globally store the obtained snapshot information. The retrieved module information is partially offline and partially online. The online module information will retrieve the modules used online.
|
|
1349
|
-
// 3. Retrieve the detailed information of the current module from global (remoteEntry address, expose resource address)
|
|
1350
|
-
// 4. After retrieving remoteEntry, call the init of the module, and then retrieve the exported content of the module through get
|
|
1351
|
-
// id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
|
|
1352
|
-
// id: alias(app1) + expose(button) = app1/button
|
|
1353
|
-
// id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort
|
|
1354
|
-
const { module, moduleOptions, remoteMatchInfo } = await this._getRemoteModuleAndOptions(id);
|
|
1355
|
-
const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
|
|
1356
|
-
const moduleOrFactory = await module.get(expose, options);
|
|
1357
|
-
const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
|
|
1358
|
-
id: idRes,
|
|
1359
|
-
pkgNameOrAlias,
|
|
1360
|
-
expose,
|
|
1361
|
-
exposeModule: loadFactory ? moduleOrFactory : undefined,
|
|
1362
|
-
exposeModuleFactory: loadFactory ? undefined : moduleOrFactory,
|
|
1363
|
-
remote,
|
|
1364
|
-
options: moduleOptions,
|
|
1365
|
-
moduleInstance: module,
|
|
1366
|
-
origin: this
|
|
1367
|
-
});
|
|
1368
|
-
if (typeof moduleWrapper === 'function') {
|
|
1369
|
-
return moduleWrapper;
|
|
1370
|
-
}
|
|
1371
|
-
return moduleOrFactory;
|
|
1372
|
-
} catch (error) {
|
|
1373
|
-
const { from = 'runtime' } = options || {
|
|
1374
|
-
from: 'runtime'
|
|
1375
|
-
};
|
|
1376
|
-
const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
1377
|
-
id,
|
|
1378
|
-
error,
|
|
1379
|
-
from,
|
|
1380
|
-
origin: this
|
|
1381
|
-
});
|
|
1382
|
-
if (!failOver) {
|
|
1383
|
-
throw error;
|
|
1384
|
-
}
|
|
1385
|
-
return failOver;
|
|
1386
|
-
}
|
|
1387
|
-
}
|
|
1388
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1389
|
-
async preloadRemote(preloadOptions) {
|
|
1390
|
-
await this.hooks.lifecycle.beforePreloadRemote.emit({
|
|
1391
|
-
preloadOptions,
|
|
1392
|
-
options: this.options,
|
|
1393
|
-
origin: this
|
|
1394
|
-
});
|
|
1395
|
-
const preloadOps = formatPreloadArgs(this.options.remotes, preloadOptions);
|
|
1396
|
-
await Promise.all(preloadOps.map(async (ops)=>{
|
|
1397
|
-
const { remote } = ops;
|
|
1398
|
-
const remoteInfo = getRemoteInfo(remote);
|
|
1399
|
-
const { globalSnapshot, remoteSnapshot } = await this.snapshotHandler.loadRemoteSnapshotInfo(remote);
|
|
1400
|
-
const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
|
|
1401
|
-
origin: this,
|
|
1402
|
-
preloadOptions: ops,
|
|
1403
|
-
remote,
|
|
1404
|
-
remoteInfo,
|
|
1405
|
-
globalSnapshot,
|
|
1406
|
-
remoteSnapshot
|
|
1407
|
-
});
|
|
1408
|
-
if (!assets) {
|
|
1409
|
-
return;
|
|
1410
|
-
}
|
|
1411
|
-
preloadAssets(remoteInfo, this, assets);
|
|
1412
|
-
}));
|
|
1413
|
-
}
|
|
1414
|
-
/**
|
|
1415
|
-
* This function initializes the sharing sequence (executed only once per share scope).
|
|
1416
|
-
* It accepts one argument, the name of the share scope.
|
|
1417
|
-
* If the share scope does not exist, it creates one.
|
|
1418
|
-
*/ // eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1419
|
-
initializeSharing(shareScopeName = DEFAULT_SCOPE, strategy) {
|
|
1420
|
-
const shareScope = this.shareScopeMap;
|
|
1421
|
-
const hostName = this.options.name;
|
|
1422
|
-
// Creates a new share scope if necessary
|
|
1423
|
-
if (!shareScope[shareScopeName]) {
|
|
1424
|
-
shareScope[shareScopeName] = {};
|
|
1425
|
-
}
|
|
1426
|
-
// Executes all initialization snippets from all accessible modules
|
|
1427
|
-
const scope = shareScope[shareScopeName];
|
|
1428
|
-
const register = (name, shared)=>{
|
|
1429
|
-
var _activeVersion_shareConfig;
|
|
1430
|
-
const { version, eager } = shared;
|
|
1431
|
-
scope[name] = scope[name] || {};
|
|
1432
|
-
const versions = scope[name];
|
|
1433
|
-
const activeVersion = versions[version];
|
|
1434
|
-
const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || ((_activeVersion_shareConfig = activeVersion.shareConfig) == null ? void 0 : _activeVersion_shareConfig.eager)));
|
|
1435
|
-
if (!activeVersion || activeVersion.strategy !== 'loaded-first' && !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
|
|
1436
|
-
versions[version] = shared;
|
|
1437
|
-
}
|
|
1438
|
-
};
|
|
1439
|
-
const promises = [];
|
|
1440
|
-
const initFn = (mod)=>mod && mod.init && mod.init(shareScope[shareScopeName]);
|
|
1441
|
-
const initRemoteModule = async (key)=>{
|
|
1442
|
-
const { module } = await this._getRemoteModuleAndOptions(key);
|
|
1443
|
-
if (module.getEntry) {
|
|
1444
|
-
const entry = await module.getEntry();
|
|
1445
|
-
if (!module.inited) {
|
|
1446
|
-
initFn(entry);
|
|
1447
|
-
module.inited = true;
|
|
1448
|
-
}
|
|
1449
|
-
}
|
|
1450
|
-
};
|
|
1451
|
-
Object.keys(this.options.shared).forEach((shareName)=>{
|
|
1452
|
-
const shared = this.options.shared[shareName];
|
|
1453
|
-
if (shared.scope.includes(shareScopeName)) {
|
|
1454
|
-
register(shareName, shared);
|
|
1455
|
-
}
|
|
1456
|
-
});
|
|
1457
|
-
if (strategy === 'version-first') {
|
|
1458
|
-
this.options.remotes.forEach((remote)=>{
|
|
1459
|
-
if (remote.shareScope === shareScopeName) {
|
|
1460
|
-
promises.push(initRemoteModule(remote.name));
|
|
1461
|
-
}
|
|
1462
|
-
});
|
|
1463
|
-
}
|
|
1464
|
-
return promises;
|
|
1465
|
-
}
|
|
1466
|
-
initShareScopeMap(scopeName, shareScope) {
|
|
1467
|
-
this.shareScopeMap[scopeName] = shareScope;
|
|
1468
|
-
this.hooks.lifecycle.initContainerShareScopeMap.emit({
|
|
1469
|
-
shareScope,
|
|
1470
|
-
options: this.options,
|
|
1471
|
-
origin: this
|
|
1472
|
-
});
|
|
1473
|
-
}
|
|
1474
|
-
formatOptions(globalOptions, userOptions) {
|
|
1475
|
-
const formatShareOptions = formatShareConfigs(userOptions.shared || {}, userOptions.name);
|
|
1476
|
-
const shared = _extends({}, globalOptions.shared, formatShareOptions);
|
|
1477
|
-
const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
|
|
1478
|
-
origin: this,
|
|
1479
|
-
userOptions,
|
|
1480
|
-
options: globalOptions,
|
|
1481
|
-
shareInfo: shared
|
|
1482
|
-
});
|
|
1483
|
-
const userRemotes = userOptionsRes.remotes || [];
|
|
1484
|
-
const remotes = userRemotes.reduce((res, remote)=>{
|
|
1485
|
-
this.registerRemote(remote, res, {
|
|
1486
|
-
force: false
|
|
1487
|
-
});
|
|
1488
|
-
return res;
|
|
1489
|
-
}, globalOptionsRes.remotes);
|
|
1490
|
-
// register shared in shareScopeMap
|
|
1491
|
-
const sharedKeys = Object.keys(formatShareOptions);
|
|
1492
|
-
sharedKeys.forEach((sharedKey)=>{
|
|
1493
|
-
const sharedVal = formatShareOptions[sharedKey];
|
|
1494
|
-
const registeredShared = getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
|
|
1495
|
-
if (!registeredShared && sharedVal && sharedVal.lib) {
|
|
1496
|
-
this.setShared({
|
|
1497
|
-
pkgName: sharedKey,
|
|
1498
|
-
lib: sharedVal.lib,
|
|
1499
|
-
get: sharedVal.get,
|
|
1500
|
-
loaded: true,
|
|
1501
|
-
shared: sharedVal,
|
|
1502
|
-
from: userOptions.name
|
|
1503
|
-
});
|
|
1504
|
-
}
|
|
1505
|
-
});
|
|
1506
|
-
const plugins = [
|
|
1507
|
-
...globalOptionsRes.plugins
|
|
1508
|
-
];
|
|
1509
|
-
if (userOptionsRes.plugins) {
|
|
1510
|
-
userOptionsRes.plugins.forEach((plugin)=>{
|
|
1511
|
-
if (!plugins.includes(plugin)) {
|
|
1512
|
-
plugins.push(plugin);
|
|
1513
|
-
}
|
|
1514
|
-
});
|
|
1515
|
-
}
|
|
1516
|
-
const optionsRes = _extends({}, globalOptions, userOptions, {
|
|
1517
|
-
plugins,
|
|
1518
|
-
remotes,
|
|
1519
|
-
shared
|
|
1520
|
-
});
|
|
1521
|
-
this.hooks.lifecycle.init.emit({
|
|
1522
|
-
origin: this,
|
|
1523
|
-
options: optionsRes
|
|
1524
|
-
});
|
|
1525
|
-
return optionsRes;
|
|
1526
|
-
}
|
|
1527
|
-
registerPlugins(plugins) {
|
|
1528
|
-
registerPlugins$1(plugins, [
|
|
1529
|
-
this.hooks,
|
|
1530
|
-
this.snapshotHandler.hooks,
|
|
1531
|
-
this.loaderHook
|
|
1532
|
-
]);
|
|
1533
|
-
}
|
|
1534
|
-
setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
|
|
1535
|
-
const { version, scope = 'default' } = shared, shareInfo = _object_without_properties_loose(shared, [
|
|
1536
|
-
"version",
|
|
1537
|
-
"scope"
|
|
1538
|
-
]);
|
|
1539
|
-
const scopes = Array.isArray(scope) ? scope : [
|
|
1540
|
-
scope
|
|
1541
|
-
];
|
|
1542
|
-
scopes.forEach((sc)=>{
|
|
1543
|
-
if (!this.shareScopeMap[sc]) {
|
|
1544
|
-
this.shareScopeMap[sc] = {};
|
|
1545
|
-
}
|
|
1546
|
-
if (!this.shareScopeMap[sc][pkgName]) {
|
|
1547
|
-
this.shareScopeMap[sc][pkgName] = {};
|
|
1548
|
-
}
|
|
1549
|
-
if (this.shareScopeMap[sc][pkgName][version]) {
|
|
1550
|
-
return;
|
|
1551
|
-
}
|
|
1552
|
-
this.shareScopeMap[sc][pkgName][version] = _extends({
|
|
1553
|
-
version,
|
|
1554
|
-
scope: [
|
|
1555
|
-
'default'
|
|
1556
|
-
]
|
|
1557
|
-
}, shareInfo, {
|
|
1558
|
-
lib,
|
|
1559
|
-
loaded,
|
|
1560
|
-
loading
|
|
1561
|
-
});
|
|
1562
|
-
if (get) {
|
|
1563
|
-
this.shareScopeMap[sc][pkgName][version].get = get;
|
|
1564
|
-
}
|
|
1565
|
-
});
|
|
1566
|
-
}
|
|
1567
|
-
removeRemote(remote) {
|
|
1568
|
-
const { name } = remote;
|
|
1569
|
-
const remoteIndex = this.options.remotes.findIndex((item)=>item.name === name);
|
|
1570
|
-
if (remoteIndex !== -1) {
|
|
1571
|
-
this.options.remotes.splice(remoteIndex, 1);
|
|
1572
|
-
}
|
|
1573
|
-
const loadedModule = this.moduleCache.get(remote.name);
|
|
1574
|
-
if (loadedModule) {
|
|
1575
|
-
const key = loadedModule.remoteInfo.entryGlobalName;
|
|
1576
|
-
if (globalThis[key]) {
|
|
1577
|
-
delete globalThis[key];
|
|
1578
|
-
}
|
|
1579
|
-
const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
|
|
1580
|
-
if (globalLoading[remoteEntryUniqueKey]) {
|
|
1581
|
-
delete globalLoading[remoteEntryUniqueKey];
|
|
1582
|
-
}
|
|
1583
|
-
this.moduleCache.delete(remote.name);
|
|
1584
|
-
}
|
|
1585
|
-
}
|
|
1586
|
-
registerRemote(remote, targetRemotes, options) {
|
|
1587
|
-
const normalizeRemote = ()=>{
|
|
1588
|
-
if (remote.alias) {
|
|
1589
|
-
// Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error
|
|
1590
|
-
// As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported
|
|
1591
|
-
const findEqual = targetRemotes.find((item)=>{
|
|
1592
|
-
var _item_alias;
|
|
1593
|
-
return remote.alias && (item.name.startsWith(remote.alias) || ((_item_alias = item.alias) == null ? void 0 : _item_alias.startsWith(remote.alias)));
|
|
1594
|
-
});
|
|
1595
|
-
assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
|
|
1596
|
-
}
|
|
1597
|
-
// Set the remote entry to a complete path
|
|
1598
|
-
if ('entry' in remote) {
|
|
1599
|
-
if (isBrowserEnv() && !remote.entry.startsWith('http')) {
|
|
1600
|
-
remote.entry = new URL(remote.entry, window.location.origin).href;
|
|
1601
|
-
}
|
|
1602
|
-
}
|
|
1603
|
-
if (!remote.shareScope) {
|
|
1604
|
-
remote.shareScope = DEFAULT_SCOPE;
|
|
1605
|
-
}
|
|
1606
|
-
if (!remote.type) {
|
|
1607
|
-
remote.type = DEFAULT_REMOTE_TYPE;
|
|
1608
|
-
}
|
|
1609
|
-
};
|
|
1610
|
-
const registeredRemote = targetRemotes.find((item)=>item.name === remote.name);
|
|
1611
|
-
if (!registeredRemote) {
|
|
1612
|
-
normalizeRemote();
|
|
1613
|
-
targetRemotes.push(remote);
|
|
1614
|
-
} else {
|
|
1615
|
-
const messages = [
|
|
1616
|
-
`The remote "${remote.name}" is already registered.`,
|
|
1617
|
-
(options == null ? void 0 : options.force) ? 'Hope you have known that OVERRIDE it may have some unexpected errors' : 'If you want to merge the remote, you can set "force: true".'
|
|
1618
|
-
];
|
|
1619
|
-
if (options == null ? void 0 : options.force) {
|
|
1620
|
-
// remove registered remote
|
|
1621
|
-
this.removeRemote(registeredRemote);
|
|
1622
|
-
normalizeRemote();
|
|
1623
|
-
targetRemotes.push(remote);
|
|
1624
|
-
}
|
|
1625
|
-
warn(messages.join(' '));
|
|
1626
|
-
}
|
|
1627
|
-
}
|
|
1628
|
-
registerRemotes(remotes, options) {
|
|
1629
|
-
remotes.forEach((remote)=>{
|
|
1630
|
-
this.registerRemote(remote, this.options.remotes, {
|
|
1631
|
-
force: options == null ? void 0 : options.force
|
|
1632
|
-
});
|
|
1633
|
-
});
|
|
1634
|
-
}
|
|
1635
|
-
constructor(userOptions){
|
|
1636
|
-
this.hooks = new PluginSystem({
|
|
1637
|
-
beforeInit: new SyncWaterfallHook('beforeInit'),
|
|
1638
|
-
init: new SyncHook(),
|
|
1639
|
-
beforeRequest: new AsyncWaterfallHook('beforeRequest'),
|
|
1640
|
-
afterResolve: new AsyncWaterfallHook('afterResolve'),
|
|
1641
|
-
// maybe will change, temporarily for internal use only
|
|
1642
|
-
beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
|
|
1643
|
-
// maybe will change, temporarily for internal use only
|
|
1644
|
-
initContainerShareScopeMap: new AsyncWaterfallHook('initContainer'),
|
|
1645
|
-
// maybe will change, temporarily for internal use only
|
|
1646
|
-
initContainer: new AsyncWaterfallHook('initContainer'),
|
|
1647
|
-
onLoad: new AsyncHook('onLoad'),
|
|
1648
|
-
handlePreloadModule: new SyncHook('handlePreloadModule'),
|
|
1649
|
-
errorLoadRemote: new AsyncHook('errorLoadRemote'),
|
|
1650
|
-
beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
|
|
1651
|
-
// not used yet
|
|
1652
|
-
loadShare: new AsyncHook(),
|
|
1653
|
-
resolveShare: new SyncWaterfallHook('resolveShare'),
|
|
1654
|
-
beforePreloadRemote: new AsyncHook(),
|
|
1655
|
-
generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
|
|
1656
|
-
// not used yet
|
|
1657
|
-
afterPreloadRemote: new AsyncHook()
|
|
1658
|
-
});
|
|
1659
|
-
this.version = "0.1.3";
|
|
1660
|
-
this.moduleCache = new Map();
|
|
1661
|
-
this.loaderHook = new PluginSystem({
|
|
1662
|
-
// FIXME: may not be suitable , not open to the public yet
|
|
1663
|
-
getModuleInfo: new SyncHook(),
|
|
1664
|
-
createScript: new SyncHook(),
|
|
1665
|
-
createLink: new SyncHook(),
|
|
1666
|
-
// only work for manifest , so not open to the public yet
|
|
1667
|
-
fetch: new AsyncHook('fetch')
|
|
1668
|
-
});
|
|
1669
|
-
// TODO: Validate the details of the options
|
|
1670
|
-
// Initialize options with default values
|
|
1671
|
-
const defaultOptions = {
|
|
1672
|
-
id: getBuilderId(),
|
|
1673
|
-
name: userOptions.name,
|
|
1674
|
-
plugins: [
|
|
1675
|
-
snapshotPlugin(),
|
|
1676
|
-
generatePreloadAssetsPlugin()
|
|
1677
|
-
],
|
|
1678
|
-
remotes: [],
|
|
1679
|
-
shared: {},
|
|
1680
|
-
inBrowser: isBrowserEnv()
|
|
1681
|
-
};
|
|
1682
|
-
this.name = userOptions.name;
|
|
1683
|
-
this.options = defaultOptions;
|
|
1684
|
-
this.shareScopeMap = {};
|
|
1685
|
-
this._setGlobalShareScopeMap();
|
|
1686
|
-
this.snapshotHandler = new SnapshotHandler(this);
|
|
1687
|
-
this.registerPlugins([
|
|
1688
|
-
...defaultOptions.plugins,
|
|
1689
|
-
...userOptions.plugins || []
|
|
1690
|
-
]);
|
|
1691
|
-
this.options = this.formatOptions(defaultOptions, userOptions);
|
|
1692
|
-
}
|
|
1693
|
-
}
|
|
1694
|
-
|
|
1695
|
-
let FederationInstance = null;
|
|
1696
|
-
function init(options) {
|
|
1697
|
-
// Retrieve the same instance with the same name
|
|
1698
|
-
const instance = getGlobalFederationInstance(options.name, options.version);
|
|
1699
|
-
if (!instance) {
|
|
1700
|
-
// Retrieve debug constructor
|
|
1701
|
-
const FederationConstructor = getGlobalFederationConstructor() || FederationHost;
|
|
1702
|
-
FederationInstance = new FederationConstructor(options);
|
|
1703
|
-
setGlobalFederationInstance(FederationInstance);
|
|
1704
|
-
return FederationInstance;
|
|
1705
|
-
} else {
|
|
1706
|
-
// Merge options
|
|
1707
|
-
instance.initOptions(options);
|
|
1708
|
-
if (!FederationInstance) {
|
|
1709
|
-
FederationInstance = instance;
|
|
1710
|
-
}
|
|
1711
|
-
return instance;
|
|
1712
|
-
}
|
|
1713
|
-
}
|
|
1714
|
-
function loadRemote(...args) {
|
|
1715
|
-
assert(FederationInstance, 'Please call init first');
|
|
1716
|
-
// eslint-disable-next-line prefer-spread
|
|
1717
|
-
return FederationInstance.loadRemote.apply(FederationInstance, args);
|
|
1718
|
-
}
|
|
1719
|
-
function loadShare(...args) {
|
|
1720
|
-
assert(FederationInstance, 'Please call init first');
|
|
1721
|
-
// eslint-disable-next-line prefer-spread
|
|
1722
|
-
return FederationInstance.loadShare.apply(FederationInstance, args);
|
|
1723
|
-
}
|
|
1724
|
-
function loadShareSync(...args) {
|
|
1725
|
-
assert(FederationInstance, 'Please call init first');
|
|
1726
|
-
// eslint-disable-next-line prefer-spread
|
|
1727
|
-
return FederationInstance.loadShareSync.apply(FederationInstance, args);
|
|
1728
|
-
}
|
|
1729
|
-
function preloadRemote(...args) {
|
|
1730
|
-
assert(FederationInstance, 'Please call init first');
|
|
1731
|
-
// eslint-disable-next-line prefer-spread
|
|
1732
|
-
return FederationInstance.preloadRemote.apply(FederationInstance, args);
|
|
1733
|
-
}
|
|
1734
|
-
function registerRemotes(...args) {
|
|
1735
|
-
assert(FederationInstance, 'Please call init first');
|
|
1736
|
-
// eslint-disable-next-line prefer-spread
|
|
1737
|
-
return FederationInstance.registerRemotes.apply(FederationInstance, args);
|
|
1738
|
-
}
|
|
1739
|
-
function registerPlugins(...args) {
|
|
1740
|
-
assert(FederationInstance, 'Please call init first');
|
|
1741
|
-
// eslint-disable-next-line prefer-spread
|
|
1742
|
-
return FederationInstance.registerPlugins.apply(FederationInstance, args);
|
|
1743
|
-
}
|
|
1744
|
-
// Inject for debug
|
|
1745
|
-
setGlobalFederationConstructor(FederationHost);
|
|
1746
|
-
|
|
1747
|
-
export { FederationHost, getRemoteEntry, getRemoteInfo, init, loadRemote, loadShare, loadShareSync, preloadRemote, registerPlugins, registerRemotes };
|