@module-federation/webpack-bundler-runtime 0.0.0-next-20240823062237 → 0.0.0-next-20240824225724
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/dist/index.cjs.js +2 -9
- package/dist/index.esm.js +2 -9
- package/dist/package.json +7 -1
- package/dist/vendored.cjs.js +4799 -0
- package/dist/vendored.esm.js +3500 -0
- package/dist/vendored.js +3500 -0
- package/package.json +10 -4
package/dist/vendored.js
ADDED
|
@@ -0,0 +1,3500 @@
|
|
|
1
|
+
import { _ } from '@swc/helpers/_/_extends';
|
|
2
|
+
import { _ as _$1 } from '@swc/helpers/_/_object_without_properties_loose';
|
|
3
|
+
|
|
4
|
+
const MANIFEST_EXT = '.json';
|
|
5
|
+
const BROWSER_LOG_KEY = 'FEDERATION_DEBUG';
|
|
6
|
+
const BROWSER_LOG_VALUE = '1';
|
|
7
|
+
const NameTransformSymbol = {
|
|
8
|
+
AT: '@',
|
|
9
|
+
HYPHEN: '-',
|
|
10
|
+
SLASH: '/'
|
|
11
|
+
};
|
|
12
|
+
const NameTransformMap = {
|
|
13
|
+
[NameTransformSymbol.AT]: 'scope_',
|
|
14
|
+
[NameTransformSymbol.HYPHEN]: '_',
|
|
15
|
+
[NameTransformSymbol.SLASH]: '__'
|
|
16
|
+
};
|
|
17
|
+
const EncodedNameTransformMap = {
|
|
18
|
+
[NameTransformMap[NameTransformSymbol.AT]]: NameTransformSymbol.AT,
|
|
19
|
+
[NameTransformMap[NameTransformSymbol.HYPHEN]]: NameTransformSymbol.HYPHEN,
|
|
20
|
+
[NameTransformMap[NameTransformSymbol.SLASH]]: NameTransformSymbol.SLASH
|
|
21
|
+
};
|
|
22
|
+
const SEPARATOR = ':';
|
|
23
|
+
const ENCODE_NAME_PREFIX = 'ENCODE_NAME_PREFIX';
|
|
24
|
+
Object.freeze({
|
|
25
|
+
__proto__: null
|
|
26
|
+
});
|
|
27
|
+
Object.freeze({
|
|
28
|
+
__proto__: null
|
|
29
|
+
});
|
|
30
|
+
Object.freeze({
|
|
31
|
+
__proto__: null
|
|
32
|
+
});
|
|
33
|
+
Object.freeze({
|
|
34
|
+
__proto__: null
|
|
35
|
+
});
|
|
36
|
+
function isBrowserEnv() {
|
|
37
|
+
return typeof window !== 'undefined';
|
|
38
|
+
}
|
|
39
|
+
function isDebugMode() {
|
|
40
|
+
if (typeof process !== 'undefined' && process.env && process.env['FEDERATION_DEBUG']) {
|
|
41
|
+
return Boolean(process.env['FEDERATION_DEBUG']);
|
|
42
|
+
}
|
|
43
|
+
return typeof FEDERATION_DEBUG !== 'undefined' && Boolean(FEDERATION_DEBUG);
|
|
44
|
+
}
|
|
45
|
+
const DEBUG_LOG = '[ FEDERATION DEBUG ]';
|
|
46
|
+
function safeGetLocalStorageItem() {
|
|
47
|
+
try {
|
|
48
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
49
|
+
return localStorage.getItem(BROWSER_LOG_KEY) === BROWSER_LOG_VALUE;
|
|
50
|
+
}
|
|
51
|
+
} catch (error1) {
|
|
52
|
+
return typeof document !== 'undefined';
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
let Logger = class Logger {
|
|
57
|
+
info(msg, info) {
|
|
58
|
+
if (this.enable) {
|
|
59
|
+
const argsToString = safeToString(info) || '';
|
|
60
|
+
if (isBrowserEnv()) {
|
|
61
|
+
console.info(`%c ${this.identifier}: ${msg} ${argsToString}`, 'color:#3300CC');
|
|
62
|
+
} else {
|
|
63
|
+
console.info('\x1b[34m%s', `${this.identifier}: ${msg} ${argsToString ? `\n${argsToString}` : ''}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
logOriginalInfo(...args) {
|
|
68
|
+
if (this.enable) {
|
|
69
|
+
if (isBrowserEnv()) {
|
|
70
|
+
console.info(`%c ${this.identifier}: OriginalInfo`, 'color:#3300CC');
|
|
71
|
+
console.log(...args);
|
|
72
|
+
} else {
|
|
73
|
+
console.info(`%c ${this.identifier}: OriginalInfo`, 'color:#3300CC');
|
|
74
|
+
console.log(...args);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
constructor(identifier){
|
|
79
|
+
this.enable = false;
|
|
80
|
+
this.identifier = identifier || DEBUG_LOG;
|
|
81
|
+
if (isBrowserEnv() && safeGetLocalStorageItem()) {
|
|
82
|
+
this.enable = true;
|
|
83
|
+
} else if (isDebugMode()) {
|
|
84
|
+
this.enable = true;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const LOG_CATEGORY$1 = '[ Federation Runtime ]';
|
|
89
|
+
new Logger();
|
|
90
|
+
const composeKeyWithSeparator = function(...args) {
|
|
91
|
+
if (!args.length) {
|
|
92
|
+
return '';
|
|
93
|
+
}
|
|
94
|
+
return args.reduce((sum, cur)=>{
|
|
95
|
+
if (!cur) {
|
|
96
|
+
return sum;
|
|
97
|
+
}
|
|
98
|
+
if (!sum) {
|
|
99
|
+
return cur;
|
|
100
|
+
}
|
|
101
|
+
return `${sum}${SEPARATOR}${cur}`;
|
|
102
|
+
}, '');
|
|
103
|
+
};
|
|
104
|
+
const decodeName = function(name, prefix, withExt) {
|
|
105
|
+
try {
|
|
106
|
+
let decodedName = name;
|
|
107
|
+
if (prefix) {
|
|
108
|
+
if (!decodedName.startsWith(prefix)) {
|
|
109
|
+
return decodedName;
|
|
110
|
+
}
|
|
111
|
+
decodedName = decodedName.replace(new RegExp(prefix, 'g'), '');
|
|
112
|
+
}
|
|
113
|
+
decodedName = decodedName.replace(new RegExp(`${NameTransformMap[NameTransformSymbol.AT]}`, 'g'), EncodedNameTransformMap[NameTransformMap[NameTransformSymbol.AT]]).replace(new RegExp(`${NameTransformMap[NameTransformSymbol.SLASH]}`, 'g'), EncodedNameTransformMap[NameTransformMap[NameTransformSymbol.SLASH]]).replace(new RegExp(`${NameTransformMap[NameTransformSymbol.HYPHEN]}`, 'g'), EncodedNameTransformMap[NameTransformMap[NameTransformSymbol.HYPHEN]]);
|
|
114
|
+
if (withExt) {
|
|
115
|
+
decodedName = decodedName.replace('.js', '');
|
|
116
|
+
}
|
|
117
|
+
return decodedName;
|
|
118
|
+
} catch (err) {
|
|
119
|
+
throw err;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
const getResourceUrl = (module, sourceUrl)=>{
|
|
123
|
+
if ('getPublicPath' in module) {
|
|
124
|
+
let publicPath;
|
|
125
|
+
if (!module.getPublicPath.startsWith('function')) {
|
|
126
|
+
publicPath = new Function(module.getPublicPath)();
|
|
127
|
+
} else {
|
|
128
|
+
publicPath = new Function('return ' + module.getPublicPath)()();
|
|
129
|
+
}
|
|
130
|
+
return `${publicPath}${sourceUrl}`;
|
|
131
|
+
} else if ('publicPath' in module) {
|
|
132
|
+
return `${module.publicPath}${sourceUrl}`;
|
|
133
|
+
} else {
|
|
134
|
+
console.warn('Cannot get resource URL. If in debug mode, please ignore.', module, sourceUrl);
|
|
135
|
+
return '';
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const warn$1 = (msg)=>{
|
|
139
|
+
console.warn(`${LOG_CATEGORY$1}: ${msg}`);
|
|
140
|
+
};
|
|
141
|
+
function safeToString(info) {
|
|
142
|
+
try {
|
|
143
|
+
return JSON.stringify(info, null, 2);
|
|
144
|
+
} catch (e) {
|
|
145
|
+
return '';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const simpleJoinRemoteEntry = (rPath, rName)=>{
|
|
149
|
+
if (!rPath) {
|
|
150
|
+
return rName;
|
|
151
|
+
}
|
|
152
|
+
const transformPath = (str)=>{
|
|
153
|
+
if (str === '.') {
|
|
154
|
+
return '';
|
|
155
|
+
}
|
|
156
|
+
if (str.startsWith('./')) {
|
|
157
|
+
return str.replace('./', '');
|
|
158
|
+
}
|
|
159
|
+
if (str.startsWith('/')) {
|
|
160
|
+
const strWithoutSlash = str.slice(1);
|
|
161
|
+
if (strWithoutSlash.endsWith('/')) {
|
|
162
|
+
return strWithoutSlash.slice(0, -1);
|
|
163
|
+
}
|
|
164
|
+
return strWithoutSlash;
|
|
165
|
+
}
|
|
166
|
+
return str;
|
|
167
|
+
};
|
|
168
|
+
const transformedPath = transformPath(rPath);
|
|
169
|
+
if (!transformedPath) {
|
|
170
|
+
return rName;
|
|
171
|
+
}
|
|
172
|
+
if (transformedPath.endsWith('/')) {
|
|
173
|
+
return `${transformedPath}${rName}`;
|
|
174
|
+
}
|
|
175
|
+
return `${transformedPath}/${rName}`;
|
|
176
|
+
};
|
|
177
|
+
function inferAutoPublicPath(url) {
|
|
178
|
+
return url.replace(/#.*$/, '').replace(/\?.*$/, '').replace(/\/[^\/]+$/, '/');
|
|
179
|
+
}
|
|
180
|
+
function generateSnapshotFromManifest(manifest, options = {}) {
|
|
181
|
+
var _manifest_metaData, _manifest_metaData1;
|
|
182
|
+
const { remotes = {}, overrides = {}, version } = options;
|
|
183
|
+
let remoteSnapshot;
|
|
184
|
+
const getPublicPath = ()=>{
|
|
185
|
+
if ('publicPath' in manifest.metaData) {
|
|
186
|
+
if (manifest.metaData.publicPath === 'auto' && version) {
|
|
187
|
+
return inferAutoPublicPath(version);
|
|
188
|
+
}
|
|
189
|
+
return manifest.metaData.publicPath;
|
|
190
|
+
} else {
|
|
191
|
+
return manifest.metaData.getPublicPath;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const overridesKeys = Object.keys(overrides);
|
|
195
|
+
let remotesInfo = {};
|
|
196
|
+
if (!Object.keys(remotes).length) {
|
|
197
|
+
var _manifest_remotes;
|
|
198
|
+
remotesInfo = ((_manifest_remotes = manifest.remotes) == null ? void 0 : _manifest_remotes.reduce((res, next)=>{
|
|
199
|
+
let matchedVersion;
|
|
200
|
+
const name = next.federationContainerName;
|
|
201
|
+
if (overridesKeys.includes(name)) {
|
|
202
|
+
matchedVersion = overrides[name];
|
|
203
|
+
} else {
|
|
204
|
+
if ('version' in next) {
|
|
205
|
+
matchedVersion = next.version;
|
|
206
|
+
} else {
|
|
207
|
+
matchedVersion = next.entry;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
res[name] = {
|
|
211
|
+
matchedVersion
|
|
212
|
+
};
|
|
213
|
+
return res;
|
|
214
|
+
}, {})) || {};
|
|
215
|
+
}
|
|
216
|
+
Object.keys(remotes).forEach((key)=>remotesInfo[key] = {
|
|
217
|
+
matchedVersion: overridesKeys.includes(key) ? overrides[key] : remotes[key]
|
|
218
|
+
});
|
|
219
|
+
const { remoteEntry: { path: remoteEntryPath, name: remoteEntryName, type: remoteEntryType }, types: remoteTypes, buildInfo: { buildVersion }, globalName, ssrRemoteEntry } = manifest.metaData;
|
|
220
|
+
const { exposes } = manifest;
|
|
221
|
+
let basicRemoteSnapshot = {
|
|
222
|
+
version: version ? version : '',
|
|
223
|
+
buildVersion,
|
|
224
|
+
globalName,
|
|
225
|
+
remoteEntry: simpleJoinRemoteEntry(remoteEntryPath, remoteEntryName),
|
|
226
|
+
remoteEntryType,
|
|
227
|
+
remoteTypes: simpleJoinRemoteEntry(remoteTypes.path, remoteTypes.name),
|
|
228
|
+
remoteTypesZip: remoteTypes.zip || '',
|
|
229
|
+
remoteTypesAPI: remoteTypes.api || '',
|
|
230
|
+
remotesInfo,
|
|
231
|
+
shared: manifest == null ? void 0 : manifest.shared.map((item)=>({
|
|
232
|
+
assets: item.assets,
|
|
233
|
+
sharedName: item.name,
|
|
234
|
+
version: item.version
|
|
235
|
+
})),
|
|
236
|
+
modules: exposes == null ? void 0 : exposes.map((expose)=>({
|
|
237
|
+
moduleName: expose.name,
|
|
238
|
+
modulePath: expose.path,
|
|
239
|
+
assets: expose.assets
|
|
240
|
+
}))
|
|
241
|
+
};
|
|
242
|
+
if ((_manifest_metaData = manifest.metaData) == null ? void 0 : _manifest_metaData.prefetchInterface) {
|
|
243
|
+
const prefetchInterface = manifest.metaData.prefetchInterface;
|
|
244
|
+
basicRemoteSnapshot = _({}, basicRemoteSnapshot, {
|
|
245
|
+
prefetchInterface
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
if ((_manifest_metaData1 = manifest.metaData) == null ? void 0 : _manifest_metaData1.prefetchEntry) {
|
|
249
|
+
const { path, name, type } = manifest.metaData.prefetchEntry;
|
|
250
|
+
basicRemoteSnapshot = _({}, basicRemoteSnapshot, {
|
|
251
|
+
prefetchEntry: simpleJoinRemoteEntry(path, name),
|
|
252
|
+
prefetchEntryType: type
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
if ('publicPath' in manifest.metaData) {
|
|
256
|
+
remoteSnapshot = _({}, basicRemoteSnapshot, {
|
|
257
|
+
publicPath: getPublicPath()
|
|
258
|
+
});
|
|
259
|
+
} else {
|
|
260
|
+
remoteSnapshot = _({}, basicRemoteSnapshot, {
|
|
261
|
+
getPublicPath: getPublicPath()
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
if (ssrRemoteEntry) {
|
|
265
|
+
const fullSSRRemoteEntry = simpleJoinRemoteEntry(ssrRemoteEntry.path, ssrRemoteEntry.name);
|
|
266
|
+
remoteSnapshot.ssrRemoteEntry = fullSSRRemoteEntry;
|
|
267
|
+
remoteSnapshot.ssrRemoteEntryType = 'commonjs-module';
|
|
268
|
+
}
|
|
269
|
+
return remoteSnapshot;
|
|
270
|
+
}
|
|
271
|
+
function isManifestProvider(moduleInfo) {
|
|
272
|
+
if ('remoteEntry' in moduleInfo && moduleInfo.remoteEntry.includes(MANIFEST_EXT)) {
|
|
273
|
+
return true;
|
|
274
|
+
} else {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
async function safeWrapper(callback, disableWarn) {
|
|
279
|
+
try {
|
|
280
|
+
const res = await callback();
|
|
281
|
+
return res;
|
|
282
|
+
} catch (e) {
|
|
283
|
+
!disableWarn && warn$1(e);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function isStaticResourcesEqual(url1, url2) {
|
|
288
|
+
const REG_EXP = /^(https?:)?\/\//i;
|
|
289
|
+
const relativeUrl1 = url1.replace(REG_EXP, '').replace(/\/$/, '');
|
|
290
|
+
const relativeUrl2 = url2.replace(REG_EXP, '').replace(/\/$/, '');
|
|
291
|
+
return relativeUrl1 === relativeUrl2;
|
|
292
|
+
}
|
|
293
|
+
function createScript(info) {
|
|
294
|
+
let script = null;
|
|
295
|
+
let needAttach = true;
|
|
296
|
+
let timeout = 20000;
|
|
297
|
+
let timeoutId;
|
|
298
|
+
const scripts = document.getElementsByTagName('script');
|
|
299
|
+
for(let i = 0; i < scripts.length; i++){
|
|
300
|
+
const s = scripts[i];
|
|
301
|
+
const scriptSrc = s.getAttribute('src');
|
|
302
|
+
if (scriptSrc && isStaticResourcesEqual(scriptSrc, info.url)) {
|
|
303
|
+
script = s;
|
|
304
|
+
needAttach = false;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (!script) {
|
|
309
|
+
script = document.createElement('script');
|
|
310
|
+
script.type = 'text/javascript';
|
|
311
|
+
script.src = info.url;
|
|
312
|
+
let createScriptRes = undefined;
|
|
313
|
+
if (info.createScriptHook) {
|
|
314
|
+
createScriptRes = info.createScriptHook(info.url, info.attrs);
|
|
315
|
+
if (createScriptRes instanceof HTMLScriptElement) {
|
|
316
|
+
script = createScriptRes;
|
|
317
|
+
} else if (typeof createScriptRes === 'object') {
|
|
318
|
+
if ('script' in createScriptRes && createScriptRes.script) {
|
|
319
|
+
script = createScriptRes.script;
|
|
320
|
+
}
|
|
321
|
+
if ('timeout' in createScriptRes && createScriptRes.timeout) {
|
|
322
|
+
timeout = createScriptRes.timeout;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
const attrs = info.attrs;
|
|
327
|
+
if (attrs && !createScriptRes) {
|
|
328
|
+
Object.keys(attrs).forEach((name)=>{
|
|
329
|
+
if (script) {
|
|
330
|
+
if (name === 'async' || name === 'defer') {
|
|
331
|
+
script[name] = attrs[name];
|
|
332
|
+
} else if (!script.getAttribute(name)) {
|
|
333
|
+
script.setAttribute(name, attrs[name]);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
const onScriptComplete = (prev, event)=>{
|
|
340
|
+
var _info_cb;
|
|
341
|
+
clearTimeout(timeoutId);
|
|
342
|
+
if (script) {
|
|
343
|
+
script.onerror = null;
|
|
344
|
+
script.onload = null;
|
|
345
|
+
safeWrapper(()=>{
|
|
346
|
+
const { needDeleteScript = true } = info;
|
|
347
|
+
if (needDeleteScript) {
|
|
348
|
+
(script == null ? void 0 : script.parentNode) && script.parentNode.removeChild(script);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
if (prev) {
|
|
352
|
+
var _info_cb1;
|
|
353
|
+
const res = prev(event);
|
|
354
|
+
info == null ? void 0 : (_info_cb1 = info.cb) == null ? void 0 : _info_cb1.call(info);
|
|
355
|
+
return res;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
info == null ? void 0 : (_info_cb = info.cb) == null ? void 0 : _info_cb.call(info);
|
|
359
|
+
};
|
|
360
|
+
script.onerror = onScriptComplete.bind(null, script.onerror);
|
|
361
|
+
script.onload = onScriptComplete.bind(null, script.onload);
|
|
362
|
+
timeoutId = setTimeout(()=>{
|
|
363
|
+
onScriptComplete(null, new Error(`Remote script "${info.url}" time-outed.`));
|
|
364
|
+
}, timeout);
|
|
365
|
+
return {
|
|
366
|
+
script,
|
|
367
|
+
needAttach
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
function createLink(info) {
|
|
371
|
+
let link = null;
|
|
372
|
+
let needAttach = true;
|
|
373
|
+
const links = document.getElementsByTagName('link');
|
|
374
|
+
for(let i = 0; i < links.length; i++){
|
|
375
|
+
const l = links[i];
|
|
376
|
+
const linkHref = l.getAttribute('href');
|
|
377
|
+
const linkRef = l.getAttribute('ref');
|
|
378
|
+
if (linkHref && isStaticResourcesEqual(linkHref, info.url) && linkRef === info.attrs['ref']) {
|
|
379
|
+
link = l;
|
|
380
|
+
needAttach = false;
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (!link) {
|
|
385
|
+
link = document.createElement('link');
|
|
386
|
+
link.setAttribute('href', info.url);
|
|
387
|
+
let createLinkRes = undefined;
|
|
388
|
+
const attrs = info.attrs;
|
|
389
|
+
if (info.createLinkHook) {
|
|
390
|
+
createLinkRes = info.createLinkHook(info.url, attrs);
|
|
391
|
+
if (createLinkRes instanceof HTMLLinkElement) {
|
|
392
|
+
link = createLinkRes;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (attrs && !createLinkRes) {
|
|
396
|
+
Object.keys(attrs).forEach((name)=>{
|
|
397
|
+
if (link && !link.getAttribute(name)) {
|
|
398
|
+
link.setAttribute(name, attrs[name]);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const onLinkComplete = (prev, event)=>{
|
|
404
|
+
if (link) {
|
|
405
|
+
link.onerror = null;
|
|
406
|
+
link.onload = null;
|
|
407
|
+
safeWrapper(()=>{
|
|
408
|
+
const { needDeleteLink = true } = info;
|
|
409
|
+
if (needDeleteLink) {
|
|
410
|
+
(link == null ? void 0 : link.parentNode) && link.parentNode.removeChild(link);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
if (prev) {
|
|
414
|
+
const res = prev(event);
|
|
415
|
+
info.cb();
|
|
416
|
+
return res;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
info.cb();
|
|
420
|
+
};
|
|
421
|
+
link.onerror = onLinkComplete.bind(null, link.onerror);
|
|
422
|
+
link.onload = onLinkComplete.bind(null, link.onload);
|
|
423
|
+
return {
|
|
424
|
+
link,
|
|
425
|
+
needAttach
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
function loadScript(url, info) {
|
|
429
|
+
const { attrs = {}, createScriptHook } = info;
|
|
430
|
+
return new Promise((resolve, _reject)=>{
|
|
431
|
+
const { script, needAttach } = createScript({
|
|
432
|
+
url,
|
|
433
|
+
cb: resolve,
|
|
434
|
+
attrs: _({
|
|
435
|
+
fetchpriority: 'high'
|
|
436
|
+
}, attrs),
|
|
437
|
+
createScriptHook,
|
|
438
|
+
needDeleteScript: true
|
|
439
|
+
});
|
|
440
|
+
needAttach && document.head.appendChild(script);
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
function importNodeModule(name) {
|
|
444
|
+
if (!name) {
|
|
445
|
+
throw new Error('import specifier is required');
|
|
446
|
+
}
|
|
447
|
+
const importModule = new Function('name', `return import(name)`);
|
|
448
|
+
return importModule(name).then((res)=>res).catch((error1)=>{
|
|
449
|
+
console.error(`Error importing module ${name}:`, error1);
|
|
450
|
+
throw error1;
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
const loadNodeFetch = async ()=>{
|
|
454
|
+
const fetchModule = await importNodeModule('node-fetch');
|
|
455
|
+
return fetchModule.default || fetchModule;
|
|
456
|
+
};
|
|
457
|
+
const lazyLoaderHookFetch = async (input, init)=>{
|
|
458
|
+
const loaderHooks = __webpack_require__.federation.instance.loaderHook;
|
|
459
|
+
const hook = (url, init)=>{
|
|
460
|
+
return loaderHooks.lifecycle.fetch.emit(url, init);
|
|
461
|
+
};
|
|
462
|
+
const res = await hook(input, init || {});
|
|
463
|
+
if (!res || !(res instanceof Response)) {
|
|
464
|
+
const fetchFunction = typeof fetch === 'undefined' ? await loadNodeFetch() : fetch;
|
|
465
|
+
return fetchFunction(input, init || {});
|
|
466
|
+
}
|
|
467
|
+
return res;
|
|
468
|
+
};
|
|
469
|
+
function createScriptNode(url, cb, attrs, createScriptHook) {
|
|
470
|
+
if (createScriptHook) {
|
|
471
|
+
const hookResult = createScriptHook(url);
|
|
472
|
+
if (hookResult && typeof hookResult === 'object' && 'url' in hookResult) {
|
|
473
|
+
url = hookResult.url;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
let urlObj;
|
|
477
|
+
try {
|
|
478
|
+
urlObj = new URL(url);
|
|
479
|
+
} catch (e) {
|
|
480
|
+
console.error('Error constructing URL:', e);
|
|
481
|
+
cb(new Error(`Invalid URL: ${e}`));
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const getFetch = async ()=>{
|
|
485
|
+
if (typeof __webpack_require__ !== 'undefined') {
|
|
486
|
+
try {
|
|
487
|
+
const loaderHooks = __webpack_require__.federation.instance.loaderHook;
|
|
488
|
+
if (loaderHooks.lifecycle.fetch) {
|
|
489
|
+
return lazyLoaderHookFetch;
|
|
490
|
+
}
|
|
491
|
+
} catch (e) {
|
|
492
|
+
console.warn('federation.instance.loaderHook.lifecycle.fetch failed:', e);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return typeof fetch === 'undefined' ? loadNodeFetch() : fetch;
|
|
496
|
+
};
|
|
497
|
+
const handleScriptFetch = async (f, urlObj)=>{
|
|
498
|
+
try {
|
|
499
|
+
var _vm_constants;
|
|
500
|
+
const res = await f(urlObj.href);
|
|
501
|
+
const data = await res.text();
|
|
502
|
+
const [path, vm] = await Promise.all([
|
|
503
|
+
importNodeModule('path'),
|
|
504
|
+
importNodeModule('vm')
|
|
505
|
+
]);
|
|
506
|
+
const scriptContext = {
|
|
507
|
+
exports: {},
|
|
508
|
+
module: {
|
|
509
|
+
exports: {}
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
const urlDirname = urlObj.pathname.split('/').slice(0, -1).join('/');
|
|
513
|
+
const filename = path.basename(urlObj.pathname);
|
|
514
|
+
var _vm_constants_USE_MAIN_CONTEXT_DEFAULT_LOADER;
|
|
515
|
+
const script = new vm.Script(`(function(exports, module, require, __dirname, __filename) {${data}\n})`, {
|
|
516
|
+
filename,
|
|
517
|
+
importModuleDynamically: (_vm_constants_USE_MAIN_CONTEXT_DEFAULT_LOADER = (_vm_constants = vm.constants) == null ? void 0 : _vm_constants.USE_MAIN_CONTEXT_DEFAULT_LOADER) != null ? _vm_constants_USE_MAIN_CONTEXT_DEFAULT_LOADER : importNodeModule
|
|
518
|
+
});
|
|
519
|
+
script.runInThisContext()(scriptContext.exports, scriptContext.module, eval('require'), urlDirname, filename);
|
|
520
|
+
const exportedInterface = scriptContext.module.exports || scriptContext.exports;
|
|
521
|
+
if (attrs && exportedInterface && attrs['globalName']) {
|
|
522
|
+
const container = exportedInterface[attrs['globalName']] || exportedInterface;
|
|
523
|
+
cb(undefined, container);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
cb(undefined, exportedInterface);
|
|
527
|
+
} catch (e) {
|
|
528
|
+
cb(e instanceof Error ? e : new Error(`Script execution error: ${e}`));
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
getFetch().then((f)=>handleScriptFetch(f, urlObj)).catch((err)=>{
|
|
532
|
+
cb(err);
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
function loadScriptNode(url, info) {
|
|
536
|
+
return new Promise((resolve, reject)=>{
|
|
537
|
+
createScriptNode(url, (error1, scriptContext)=>{
|
|
538
|
+
if (error1) {
|
|
539
|
+
reject(error1);
|
|
540
|
+
} else {
|
|
541
|
+
var _info_attrs, _info_attrs1;
|
|
542
|
+
const remoteEntryKey = (info == null ? void 0 : (_info_attrs = info.attrs) == null ? void 0 : _info_attrs['globalName']) || `__FEDERATION_${info == null ? void 0 : (_info_attrs1 = info.attrs) == null ? void 0 : _info_attrs1['name']}:custom__`;
|
|
543
|
+
const entryExports = globalThis[remoteEntryKey] = scriptContext;
|
|
544
|
+
resolve(entryExports);
|
|
545
|
+
}
|
|
546
|
+
}, info.attrs, info.createScriptHook);
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function getBuilderId() {
|
|
551
|
+
return typeof FEDERATION_BUILD_IDENTIFIER !== 'undefined' ? FEDERATION_BUILD_IDENTIFIER : '';
|
|
552
|
+
}
|
|
553
|
+
const LOG_CATEGORY = '[ Federation Runtime ]';
|
|
554
|
+
function assert(condition, msg) {
|
|
555
|
+
if (!condition) {
|
|
556
|
+
error(msg);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
function error(msg) {
|
|
560
|
+
if (msg instanceof Error) {
|
|
561
|
+
msg.message = `${LOG_CATEGORY}: ${msg.message}`;
|
|
562
|
+
throw msg;
|
|
563
|
+
}
|
|
564
|
+
throw new Error(`${LOG_CATEGORY}: ${msg}`);
|
|
565
|
+
}
|
|
566
|
+
function warn(msg) {
|
|
567
|
+
if (msg instanceof Error) {
|
|
568
|
+
msg.message = `${LOG_CATEGORY}: ${msg.message}`;
|
|
569
|
+
console.warn(msg);
|
|
570
|
+
} else {
|
|
571
|
+
console.warn(`${LOG_CATEGORY}: ${msg}`);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
function addUniqueItem(arr, item) {
|
|
575
|
+
if (arr.findIndex((name)=>name === item) === -1) {
|
|
576
|
+
arr.push(item);
|
|
577
|
+
}
|
|
578
|
+
return arr;
|
|
579
|
+
}
|
|
580
|
+
function getFMId(remoteInfo) {
|
|
581
|
+
if ('version' in remoteInfo && remoteInfo.version) {
|
|
582
|
+
return `${remoteInfo.name}:${remoteInfo.version}`;
|
|
583
|
+
} else if ('entry' in remoteInfo && remoteInfo.entry) {
|
|
584
|
+
return `${remoteInfo.name}:${remoteInfo.entry}`;
|
|
585
|
+
} else {
|
|
586
|
+
return `${remoteInfo.name}`;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function isRemoteInfoWithEntry(remote) {
|
|
590
|
+
return typeof remote.entry !== 'undefined';
|
|
591
|
+
}
|
|
592
|
+
function isPureRemoteEntry(remote) {
|
|
593
|
+
return !remote.entry.includes('.json') && remote.entry.includes('.js');
|
|
594
|
+
}
|
|
595
|
+
function isObject(val) {
|
|
596
|
+
return val && typeof val === 'object';
|
|
597
|
+
}
|
|
598
|
+
const objectToString = Object.prototype.toString;
|
|
599
|
+
function isPlainObject(val) {
|
|
600
|
+
return objectToString.call(val) === '[object Object]';
|
|
601
|
+
}
|
|
602
|
+
function arrayOptions(options) {
|
|
603
|
+
return Array.isArray(options) ? options : [
|
|
604
|
+
options
|
|
605
|
+
];
|
|
606
|
+
}
|
|
607
|
+
function getRemoteEntryInfoFromSnapshot(snapshot) {
|
|
608
|
+
const defaultRemoteEntryInfo = {
|
|
609
|
+
url: '',
|
|
610
|
+
type: 'global',
|
|
611
|
+
globalName: ''
|
|
612
|
+
};
|
|
613
|
+
if (isBrowserEnv()) {
|
|
614
|
+
return 'remoteEntry' in snapshot ? {
|
|
615
|
+
url: snapshot.remoteEntry,
|
|
616
|
+
type: snapshot.remoteEntryType,
|
|
617
|
+
globalName: snapshot.globalName
|
|
618
|
+
} : defaultRemoteEntryInfo;
|
|
619
|
+
}
|
|
620
|
+
if ('ssrRemoteEntry' in snapshot) {
|
|
621
|
+
return {
|
|
622
|
+
url: snapshot.ssrRemoteEntry || defaultRemoteEntryInfo.url,
|
|
623
|
+
type: snapshot.ssrRemoteEntryType || defaultRemoteEntryInfo.type,
|
|
624
|
+
globalName: snapshot.globalName
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
return defaultRemoteEntryInfo;
|
|
628
|
+
}
|
|
629
|
+
const nativeGlobal = (()=>{
|
|
630
|
+
try {
|
|
631
|
+
return new Function('return this')();
|
|
632
|
+
} catch (e) {
|
|
633
|
+
return globalThis;
|
|
634
|
+
}
|
|
635
|
+
})();
|
|
636
|
+
const Global = nativeGlobal;
|
|
637
|
+
function definePropertyGlobalVal(target, key, val) {
|
|
638
|
+
Object.defineProperty(target, key, {
|
|
639
|
+
value: val,
|
|
640
|
+
configurable: false,
|
|
641
|
+
writable: true
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
function includeOwnProperty(target, key) {
|
|
645
|
+
return Object.hasOwnProperty.call(target, key);
|
|
646
|
+
}
|
|
647
|
+
if (!includeOwnProperty(globalThis, '__GLOBAL_LOADING_REMOTE_ENTRY__')) {
|
|
648
|
+
definePropertyGlobalVal(globalThis, '__GLOBAL_LOADING_REMOTE_ENTRY__', {});
|
|
649
|
+
}
|
|
650
|
+
const globalLoading = globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__;
|
|
651
|
+
function setGlobalDefaultVal(target) {
|
|
652
|
+
var _target___FEDERATION__, _target___FEDERATION__1, _target___FEDERATION__2, _target___FEDERATION__3, _target___FEDERATION__4, _target___FEDERATION__5;
|
|
653
|
+
if (includeOwnProperty(target, '__VMOK__') && !includeOwnProperty(target, '__FEDERATION__')) {
|
|
654
|
+
definePropertyGlobalVal(target, '__FEDERATION__', target.__VMOK__);
|
|
655
|
+
}
|
|
656
|
+
if (!includeOwnProperty(target, '__FEDERATION__')) {
|
|
657
|
+
definePropertyGlobalVal(target, '__FEDERATION__', {
|
|
658
|
+
__GLOBAL_PLUGIN__: [],
|
|
659
|
+
__INSTANCES__: [],
|
|
660
|
+
moduleInfo: {},
|
|
661
|
+
__SHARE__: {},
|
|
662
|
+
__MANIFEST_LOADING__: {},
|
|
663
|
+
__PRELOADED_MAP__: new Map()
|
|
664
|
+
});
|
|
665
|
+
definePropertyGlobalVal(target, '__VMOK__', target.__FEDERATION__);
|
|
666
|
+
}
|
|
667
|
+
var ___GLOBAL_PLUGIN__;
|
|
668
|
+
(___GLOBAL_PLUGIN__ = (_target___FEDERATION__ = target.__FEDERATION__).__GLOBAL_PLUGIN__) != null ? ___GLOBAL_PLUGIN__ : _target___FEDERATION__.__GLOBAL_PLUGIN__ = [];
|
|
669
|
+
var ___INSTANCES__;
|
|
670
|
+
(___INSTANCES__ = (_target___FEDERATION__1 = target.__FEDERATION__).__INSTANCES__) != null ? ___INSTANCES__ : _target___FEDERATION__1.__INSTANCES__ = [];
|
|
671
|
+
var _moduleInfo;
|
|
672
|
+
(_moduleInfo = (_target___FEDERATION__2 = target.__FEDERATION__).moduleInfo) != null ? _moduleInfo : _target___FEDERATION__2.moduleInfo = {};
|
|
673
|
+
var ___SHARE__;
|
|
674
|
+
(___SHARE__ = (_target___FEDERATION__3 = target.__FEDERATION__).__SHARE__) != null ? ___SHARE__ : _target___FEDERATION__3.__SHARE__ = {};
|
|
675
|
+
var ___MANIFEST_LOADING__;
|
|
676
|
+
(___MANIFEST_LOADING__ = (_target___FEDERATION__4 = target.__FEDERATION__).__MANIFEST_LOADING__) != null ? ___MANIFEST_LOADING__ : _target___FEDERATION__4.__MANIFEST_LOADING__ = {};
|
|
677
|
+
var ___PRELOADED_MAP__;
|
|
678
|
+
(___PRELOADED_MAP__ = (_target___FEDERATION__5 = target.__FEDERATION__).__PRELOADED_MAP__) != null ? ___PRELOADED_MAP__ : _target___FEDERATION__5.__PRELOADED_MAP__ = new Map();
|
|
679
|
+
}
|
|
680
|
+
setGlobalDefaultVal(globalThis);
|
|
681
|
+
setGlobalDefaultVal(nativeGlobal);
|
|
682
|
+
function getGlobalFederationInstance(name, version) {
|
|
683
|
+
const buildId = getBuilderId();
|
|
684
|
+
return globalThis.__FEDERATION__.__INSTANCES__.find((GMInstance)=>{
|
|
685
|
+
if (buildId && GMInstance.options.id === getBuilderId()) {
|
|
686
|
+
return true;
|
|
687
|
+
}
|
|
688
|
+
if (GMInstance.options.name === name && !GMInstance.options.version && !version) {
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
if (GMInstance.options.name === name && version && GMInstance.options.version === version) {
|
|
692
|
+
return true;
|
|
693
|
+
}
|
|
694
|
+
return false;
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
function setGlobalFederationInstance(FederationInstance) {
|
|
698
|
+
globalThis.__FEDERATION__.__INSTANCES__.push(FederationInstance);
|
|
699
|
+
}
|
|
700
|
+
function getGlobalFederationConstructor() {
|
|
701
|
+
return globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
|
|
702
|
+
}
|
|
703
|
+
function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
|
|
704
|
+
if (isDebug) {
|
|
705
|
+
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
|
|
706
|
+
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.5.1";
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
function getInfoWithoutType(target, key) {
|
|
710
|
+
if (typeof key === 'string') {
|
|
711
|
+
const keyRes = target[key];
|
|
712
|
+
if (keyRes) {
|
|
713
|
+
return {
|
|
714
|
+
value: target[key],
|
|
715
|
+
key: key
|
|
716
|
+
};
|
|
717
|
+
} else {
|
|
718
|
+
const targetKeys = Object.keys(target);
|
|
719
|
+
for (const targetKey of targetKeys){
|
|
720
|
+
const [targetTypeOrName, _] = targetKey.split(':');
|
|
721
|
+
const nKey = `${targetTypeOrName}:${key}`;
|
|
722
|
+
const typeWithKeyRes = target[nKey];
|
|
723
|
+
if (typeWithKeyRes) {
|
|
724
|
+
return {
|
|
725
|
+
value: typeWithKeyRes,
|
|
726
|
+
key: nKey
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
return {
|
|
731
|
+
value: undefined,
|
|
732
|
+
key: key
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
} else {
|
|
736
|
+
throw new Error('key must be string');
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
const getGlobalSnapshot = ()=>nativeGlobal.__FEDERATION__.moduleInfo;
|
|
740
|
+
const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot)=>{
|
|
741
|
+
const moduleKey = getFMId(moduleInfo);
|
|
742
|
+
const getModuleInfo = getInfoWithoutType(snapshot, moduleKey).value;
|
|
743
|
+
if (getModuleInfo && !getModuleInfo.version && 'version' in moduleInfo && moduleInfo['version']) {
|
|
744
|
+
getModuleInfo.version = moduleInfo['version'];
|
|
745
|
+
}
|
|
746
|
+
if (getModuleInfo) {
|
|
747
|
+
return getModuleInfo;
|
|
748
|
+
}
|
|
749
|
+
if ('version' in moduleInfo && moduleInfo['version']) {
|
|
750
|
+
const { version } = moduleInfo, resModuleInfo = _$1(moduleInfo, [
|
|
751
|
+
"version"
|
|
752
|
+
]);
|
|
753
|
+
const moduleKeyWithoutVersion = getFMId(resModuleInfo);
|
|
754
|
+
const getModuleInfoWithoutVersion = getInfoWithoutType(nativeGlobal.__FEDERATION__.moduleInfo, moduleKeyWithoutVersion).value;
|
|
755
|
+
if ((getModuleInfoWithoutVersion == null ? void 0 : getModuleInfoWithoutVersion.version) === version) {
|
|
756
|
+
return getModuleInfoWithoutVersion;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return;
|
|
760
|
+
};
|
|
761
|
+
const getGlobalSnapshotInfoByModuleInfo = (moduleInfo)=>getTargetSnapshotInfoByModuleInfo(moduleInfo, nativeGlobal.__FEDERATION__.moduleInfo);
|
|
762
|
+
const setGlobalSnapshotInfoByModuleInfo = (remoteInfo, moduleDetailInfo)=>{
|
|
763
|
+
const moduleKey = getFMId(remoteInfo);
|
|
764
|
+
nativeGlobal.__FEDERATION__.moduleInfo[moduleKey] = moduleDetailInfo;
|
|
765
|
+
return nativeGlobal.__FEDERATION__.moduleInfo;
|
|
766
|
+
};
|
|
767
|
+
const addGlobalSnapshot = (moduleInfos)=>{
|
|
768
|
+
nativeGlobal.__FEDERATION__.moduleInfo = _({}, nativeGlobal.__FEDERATION__.moduleInfo, moduleInfos);
|
|
769
|
+
return ()=>{
|
|
770
|
+
const keys = Object.keys(moduleInfos);
|
|
771
|
+
for (const key of keys){
|
|
772
|
+
delete nativeGlobal.__FEDERATION__.moduleInfo[key];
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
};
|
|
776
|
+
const getRemoteEntryExports = (name, globalName)=>{
|
|
777
|
+
const remoteEntryKey = globalName || `__FEDERATION_${name}:custom__`;
|
|
778
|
+
const entryExports = globalThis[remoteEntryKey];
|
|
779
|
+
return {
|
|
780
|
+
remoteEntryKey,
|
|
781
|
+
entryExports
|
|
782
|
+
};
|
|
783
|
+
};
|
|
784
|
+
const registerGlobalPlugins = (plugins)=>{
|
|
785
|
+
const { __GLOBAL_PLUGIN__ } = nativeGlobal.__FEDERATION__;
|
|
786
|
+
plugins.forEach((plugin)=>{
|
|
787
|
+
if (__GLOBAL_PLUGIN__.findIndex((p)=>p.name === plugin.name) === -1) {
|
|
788
|
+
__GLOBAL_PLUGIN__.push(plugin);
|
|
789
|
+
} else {
|
|
790
|
+
warn(`The plugin ${plugin.name} has been registered.`);
|
|
791
|
+
}
|
|
792
|
+
});
|
|
793
|
+
};
|
|
794
|
+
const getGlobalHostPlugins = ()=>nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;
|
|
795
|
+
const getPreloaded = (id)=>globalThis.__FEDERATION__.__PRELOADED_MAP__.get(id);
|
|
796
|
+
const setPreloaded = (id)=>globalThis.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
|
|
797
|
+
const DEFAULT_SCOPE = 'default';
|
|
798
|
+
const DEFAULT_REMOTE_TYPE = 'global';
|
|
799
|
+
const buildIdentifier = '[0-9A-Za-z-]+';
|
|
800
|
+
const build = `(?:\\+(${buildIdentifier}(?:\\.${buildIdentifier})*))`;
|
|
801
|
+
const numericIdentifier = '0|[1-9]\\d*';
|
|
802
|
+
const numericIdentifierLoose = '[0-9]+';
|
|
803
|
+
const nonNumericIdentifier = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
|
|
804
|
+
const preReleaseIdentifierLoose = `(?:${numericIdentifierLoose}|${nonNumericIdentifier})`;
|
|
805
|
+
const preReleaseLoose = `(?:-?(${preReleaseIdentifierLoose}(?:\\.${preReleaseIdentifierLoose})*))`;
|
|
806
|
+
const preReleaseIdentifier = `(?:${numericIdentifier}|${nonNumericIdentifier})`;
|
|
807
|
+
const preRelease = `(?:-(${preReleaseIdentifier}(?:\\.${preReleaseIdentifier})*))`;
|
|
808
|
+
const xRangeIdentifier = `${numericIdentifier}|x|X|\\*`;
|
|
809
|
+
const xRangePlain = `[v=\\s]*(${xRangeIdentifier})(?:\\.(${xRangeIdentifier})(?:\\.(${xRangeIdentifier})(?:${preRelease})?${build}?)?)?`;
|
|
810
|
+
const hyphenRange = `^\\s*(${xRangePlain})\\s+-\\s+(${xRangePlain})\\s*$`;
|
|
811
|
+
const mainVersionLoose = `(${numericIdentifierLoose})\\.(${numericIdentifierLoose})\\.(${numericIdentifierLoose})`;
|
|
812
|
+
const loosePlain = `[v=\\s]*${mainVersionLoose}${preReleaseLoose}?${build}?`;
|
|
813
|
+
const gtlt = '((?:<|>)?=?)';
|
|
814
|
+
const comparatorTrim = `(\\s*)${gtlt}\\s*(${loosePlain}|${xRangePlain})`;
|
|
815
|
+
const loneTilde = '(?:~>?)';
|
|
816
|
+
const tildeTrim = `(\\s*)${loneTilde}\\s+`;
|
|
817
|
+
const loneCaret = '(?:\\^)';
|
|
818
|
+
const caretTrim = `(\\s*)${loneCaret}\\s+`;
|
|
819
|
+
const star = '(<|>)?=?\\s*\\*';
|
|
820
|
+
const caret = `^${loneCaret}${xRangePlain}$`;
|
|
821
|
+
const mainVersion = `(${numericIdentifier})\\.(${numericIdentifier})\\.(${numericIdentifier})`;
|
|
822
|
+
const fullPlain = `v?${mainVersion}${preRelease}?${build}?`;
|
|
823
|
+
const tilde = `^${loneTilde}${xRangePlain}$`;
|
|
824
|
+
const xRange = `^${gtlt}\\s*${xRangePlain}$`;
|
|
825
|
+
const comparator = `^${gtlt}\\s*(${fullPlain})$|^$`;
|
|
826
|
+
const gte0 = '^\\s*>=\\s*0.0.0\\s*$';
|
|
827
|
+
function parseRegex(source) {
|
|
828
|
+
return new RegExp(source);
|
|
829
|
+
}
|
|
830
|
+
function isXVersion(version) {
|
|
831
|
+
return !version || version.toLowerCase() === 'x' || version === '*';
|
|
832
|
+
}
|
|
833
|
+
function pipe(...fns) {
|
|
834
|
+
return (x)=>fns.reduce((v, f)=>f(v), x);
|
|
835
|
+
}
|
|
836
|
+
function extractComparator(comparatorString) {
|
|
837
|
+
return comparatorString.match(parseRegex(comparator));
|
|
838
|
+
}
|
|
839
|
+
function combineVersion(major, minor, patch, preRelease) {
|
|
840
|
+
const mainVersion = `${major}.${minor}.${patch}`;
|
|
841
|
+
if (preRelease) {
|
|
842
|
+
return `${mainVersion}-${preRelease}`;
|
|
843
|
+
}
|
|
844
|
+
return mainVersion;
|
|
845
|
+
}
|
|
846
|
+
function parseHyphen(range) {
|
|
847
|
+
return range.replace(parseRegex(hyphenRange), (_range, from, fromMajor, fromMinor, fromPatch, _fromPreRelease, _fromBuild, to, toMajor, toMinor, toPatch, toPreRelease)=>{
|
|
848
|
+
if (isXVersion(fromMajor)) {
|
|
849
|
+
from = '';
|
|
850
|
+
} else if (isXVersion(fromMinor)) {
|
|
851
|
+
from = `>=${fromMajor}.0.0`;
|
|
852
|
+
} else if (isXVersion(fromPatch)) {
|
|
853
|
+
from = `>=${fromMajor}.${fromMinor}.0`;
|
|
854
|
+
} else {
|
|
855
|
+
from = `>=${from}`;
|
|
856
|
+
}
|
|
857
|
+
if (isXVersion(toMajor)) {
|
|
858
|
+
to = '';
|
|
859
|
+
} else if (isXVersion(toMinor)) {
|
|
860
|
+
to = `<${Number(toMajor) + 1}.0.0-0`;
|
|
861
|
+
} else if (isXVersion(toPatch)) {
|
|
862
|
+
to = `<${toMajor}.${Number(toMinor) + 1}.0-0`;
|
|
863
|
+
} else if (toPreRelease) {
|
|
864
|
+
to = `<=${toMajor}.${toMinor}.${toPatch}-${toPreRelease}`;
|
|
865
|
+
} else {
|
|
866
|
+
to = `<=${to}`;
|
|
867
|
+
}
|
|
868
|
+
return `${from} ${to}`.trim();
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
function parseComparatorTrim(range) {
|
|
872
|
+
return range.replace(parseRegex(comparatorTrim), '$1$2$3');
|
|
873
|
+
}
|
|
874
|
+
function parseTildeTrim(range) {
|
|
875
|
+
return range.replace(parseRegex(tildeTrim), '$1~');
|
|
876
|
+
}
|
|
877
|
+
function parseCaretTrim(range) {
|
|
878
|
+
return range.replace(parseRegex(caretTrim), '$1^');
|
|
879
|
+
}
|
|
880
|
+
function parseCarets(range) {
|
|
881
|
+
return range.trim().split(/\s+/).map((rangeVersion)=>rangeVersion.replace(parseRegex(caret), (_, major, minor, patch, preRelease)=>{
|
|
882
|
+
if (isXVersion(major)) {
|
|
883
|
+
return '';
|
|
884
|
+
} else if (isXVersion(minor)) {
|
|
885
|
+
return `>=${major}.0.0 <${Number(major) + 1}.0.0-0`;
|
|
886
|
+
} else if (isXVersion(patch)) {
|
|
887
|
+
if (major === '0') {
|
|
888
|
+
return `>=${major}.${minor}.0 <${major}.${Number(minor) + 1}.0-0`;
|
|
889
|
+
} else {
|
|
890
|
+
return `>=${major}.${minor}.0 <${Number(major) + 1}.0.0-0`;
|
|
891
|
+
}
|
|
892
|
+
} else if (preRelease) {
|
|
893
|
+
if (major === '0') {
|
|
894
|
+
if (minor === '0') {
|
|
895
|
+
return `>=${major}.${minor}.${patch}-${preRelease} <${major}.${minor}.${Number(patch) + 1}-0`;
|
|
896
|
+
} else {
|
|
897
|
+
return `>=${major}.${minor}.${patch}-${preRelease} <${major}.${Number(minor) + 1}.0-0`;
|
|
898
|
+
}
|
|
899
|
+
} else {
|
|
900
|
+
return `>=${major}.${minor}.${patch}-${preRelease} <${Number(major) + 1}.0.0-0`;
|
|
901
|
+
}
|
|
902
|
+
} else {
|
|
903
|
+
if (major === '0') {
|
|
904
|
+
if (minor === '0') {
|
|
905
|
+
return `>=${major}.${minor}.${patch} <${major}.${minor}.${Number(patch) + 1}-0`;
|
|
906
|
+
} else {
|
|
907
|
+
return `>=${major}.${minor}.${patch} <${major}.${Number(minor) + 1}.0-0`;
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
return `>=${major}.${minor}.${patch} <${Number(major) + 1}.0.0-0`;
|
|
911
|
+
}
|
|
912
|
+
})).join(' ');
|
|
913
|
+
}
|
|
914
|
+
function parseTildes(range) {
|
|
915
|
+
return range.trim().split(/\s+/).map((rangeVersion)=>rangeVersion.replace(parseRegex(tilde), (_, major, minor, patch, preRelease)=>{
|
|
916
|
+
if (isXVersion(major)) {
|
|
917
|
+
return '';
|
|
918
|
+
} else if (isXVersion(minor)) {
|
|
919
|
+
return `>=${major}.0.0 <${Number(major) + 1}.0.0-0`;
|
|
920
|
+
} else if (isXVersion(patch)) {
|
|
921
|
+
return `>=${major}.${minor}.0 <${major}.${Number(minor) + 1}.0-0`;
|
|
922
|
+
} else if (preRelease) {
|
|
923
|
+
return `>=${major}.${minor}.${patch}-${preRelease} <${major}.${Number(minor) + 1}.0-0`;
|
|
924
|
+
}
|
|
925
|
+
return `>=${major}.${minor}.${patch} <${major}.${Number(minor) + 1}.0-0`;
|
|
926
|
+
})).join(' ');
|
|
927
|
+
}
|
|
928
|
+
function parseXRanges(range) {
|
|
929
|
+
return range.split(/\s+/).map((rangeVersion)=>rangeVersion.trim().replace(parseRegex(xRange), (ret, gtlt, major, minor, patch, preRelease)=>{
|
|
930
|
+
const isXMajor = isXVersion(major);
|
|
931
|
+
const isXMinor = isXMajor || isXVersion(minor);
|
|
932
|
+
const isXPatch = isXMinor || isXVersion(patch);
|
|
933
|
+
if (gtlt === '=' && isXPatch) {
|
|
934
|
+
gtlt = '';
|
|
935
|
+
}
|
|
936
|
+
preRelease = '';
|
|
937
|
+
if (isXMajor) {
|
|
938
|
+
if (gtlt === '>' || gtlt === '<') {
|
|
939
|
+
return '<0.0.0-0';
|
|
940
|
+
} else {
|
|
941
|
+
return '*';
|
|
942
|
+
}
|
|
943
|
+
} else if (gtlt && isXPatch) {
|
|
944
|
+
if (isXMinor) {
|
|
945
|
+
minor = 0;
|
|
946
|
+
}
|
|
947
|
+
patch = 0;
|
|
948
|
+
if (gtlt === '>') {
|
|
949
|
+
gtlt = '>=';
|
|
950
|
+
if (isXMinor) {
|
|
951
|
+
major = Number(major) + 1;
|
|
952
|
+
minor = 0;
|
|
953
|
+
patch = 0;
|
|
954
|
+
} else {
|
|
955
|
+
minor = Number(minor) + 1;
|
|
956
|
+
patch = 0;
|
|
957
|
+
}
|
|
958
|
+
} else if (gtlt === '<=') {
|
|
959
|
+
gtlt = '<';
|
|
960
|
+
if (isXMinor) {
|
|
961
|
+
major = Number(major) + 1;
|
|
962
|
+
} else {
|
|
963
|
+
minor = Number(minor) + 1;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
if (gtlt === '<') {
|
|
967
|
+
preRelease = '-0';
|
|
968
|
+
}
|
|
969
|
+
return `${gtlt + major}.${minor}.${patch}${preRelease}`;
|
|
970
|
+
} else if (isXMinor) {
|
|
971
|
+
return `>=${major}.0.0${preRelease} <${Number(major) + 1}.0.0-0`;
|
|
972
|
+
} else if (isXPatch) {
|
|
973
|
+
return `>=${major}.${minor}.0${preRelease} <${major}.${Number(minor) + 1}.0-0`;
|
|
974
|
+
}
|
|
975
|
+
return ret;
|
|
976
|
+
})).join(' ');
|
|
977
|
+
}
|
|
978
|
+
function parseStar(range) {
|
|
979
|
+
return range.trim().replace(parseRegex(star), '');
|
|
980
|
+
}
|
|
981
|
+
function parseGTE0(comparatorString) {
|
|
982
|
+
return comparatorString.trim().replace(parseRegex(gte0), '');
|
|
983
|
+
}
|
|
984
|
+
function compareAtom(rangeAtom, versionAtom) {
|
|
985
|
+
rangeAtom = Number(rangeAtom) || rangeAtom;
|
|
986
|
+
versionAtom = Number(versionAtom) || versionAtom;
|
|
987
|
+
if (rangeAtom > versionAtom) {
|
|
988
|
+
return 1;
|
|
989
|
+
}
|
|
990
|
+
if (rangeAtom === versionAtom) {
|
|
991
|
+
return 0;
|
|
992
|
+
}
|
|
993
|
+
return -1;
|
|
994
|
+
}
|
|
995
|
+
function comparePreRelease(rangeAtom, versionAtom) {
|
|
996
|
+
const { preRelease: rangePreRelease } = rangeAtom;
|
|
997
|
+
const { preRelease: versionPreRelease } = versionAtom;
|
|
998
|
+
if (rangePreRelease === undefined && Boolean(versionPreRelease)) {
|
|
999
|
+
return 1;
|
|
1000
|
+
}
|
|
1001
|
+
if (Boolean(rangePreRelease) && versionPreRelease === undefined) {
|
|
1002
|
+
return -1;
|
|
1003
|
+
}
|
|
1004
|
+
if (rangePreRelease === undefined && versionPreRelease === undefined) {
|
|
1005
|
+
return 0;
|
|
1006
|
+
}
|
|
1007
|
+
for(let i = 0, n = rangePreRelease.length; i <= n; i++){
|
|
1008
|
+
const rangeElement = rangePreRelease[i];
|
|
1009
|
+
const versionElement = versionPreRelease[i];
|
|
1010
|
+
if (rangeElement === versionElement) {
|
|
1011
|
+
continue;
|
|
1012
|
+
}
|
|
1013
|
+
if (rangeElement === undefined && versionElement === undefined) {
|
|
1014
|
+
return 0;
|
|
1015
|
+
}
|
|
1016
|
+
if (!rangeElement) {
|
|
1017
|
+
return 1;
|
|
1018
|
+
}
|
|
1019
|
+
if (!versionElement) {
|
|
1020
|
+
return -1;
|
|
1021
|
+
}
|
|
1022
|
+
return compareAtom(rangeElement, versionElement);
|
|
1023
|
+
}
|
|
1024
|
+
return 0;
|
|
1025
|
+
}
|
|
1026
|
+
function compareVersion(rangeAtom, versionAtom) {
|
|
1027
|
+
return compareAtom(rangeAtom.major, versionAtom.major) || compareAtom(rangeAtom.minor, versionAtom.minor) || compareAtom(rangeAtom.patch, versionAtom.patch) || comparePreRelease(rangeAtom, versionAtom);
|
|
1028
|
+
}
|
|
1029
|
+
function eq(rangeAtom, versionAtom) {
|
|
1030
|
+
return rangeAtom.version === versionAtom.version;
|
|
1031
|
+
}
|
|
1032
|
+
function compare(rangeAtom, versionAtom) {
|
|
1033
|
+
switch(rangeAtom.operator){
|
|
1034
|
+
case '':
|
|
1035
|
+
case '=':
|
|
1036
|
+
return eq(rangeAtom, versionAtom);
|
|
1037
|
+
case '>':
|
|
1038
|
+
return compareVersion(rangeAtom, versionAtom) < 0;
|
|
1039
|
+
case '>=':
|
|
1040
|
+
return eq(rangeAtom, versionAtom) || compareVersion(rangeAtom, versionAtom) < 0;
|
|
1041
|
+
case '<':
|
|
1042
|
+
return compareVersion(rangeAtom, versionAtom) > 0;
|
|
1043
|
+
case '<=':
|
|
1044
|
+
return eq(rangeAtom, versionAtom) || compareVersion(rangeAtom, versionAtom) > 0;
|
|
1045
|
+
case undefined:
|
|
1046
|
+
{
|
|
1047
|
+
return true;
|
|
1048
|
+
}
|
|
1049
|
+
default:
|
|
1050
|
+
return false;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
function parseComparatorString(range) {
|
|
1054
|
+
return pipe(parseCarets, parseTildes, parseXRanges, parseStar)(range);
|
|
1055
|
+
}
|
|
1056
|
+
function parseRange(range) {
|
|
1057
|
+
return pipe(parseHyphen, parseComparatorTrim, parseTildeTrim, parseCaretTrim)(range.trim()).split(/\s+/).join(' ');
|
|
1058
|
+
}
|
|
1059
|
+
function satisfy(version, range) {
|
|
1060
|
+
if (!version) {
|
|
1061
|
+
return false;
|
|
1062
|
+
}
|
|
1063
|
+
const parsedRange = parseRange(range);
|
|
1064
|
+
const parsedComparator = parsedRange.split(' ').map((rangeVersion)=>parseComparatorString(rangeVersion)).join(' ');
|
|
1065
|
+
const comparators = parsedComparator.split(/\s+/).map((comparator)=>parseGTE0(comparator));
|
|
1066
|
+
const extractedVersion = extractComparator(version);
|
|
1067
|
+
if (!extractedVersion) {
|
|
1068
|
+
return false;
|
|
1069
|
+
}
|
|
1070
|
+
const [, versionOperator, , versionMajor, versionMinor, versionPatch, versionPreRelease] = extractedVersion;
|
|
1071
|
+
const versionAtom = {
|
|
1072
|
+
operator: versionOperator,
|
|
1073
|
+
version: combineVersion(versionMajor, versionMinor, versionPatch, versionPreRelease),
|
|
1074
|
+
major: versionMajor,
|
|
1075
|
+
minor: versionMinor,
|
|
1076
|
+
patch: versionPatch,
|
|
1077
|
+
preRelease: versionPreRelease == null ? void 0 : versionPreRelease.split('.')
|
|
1078
|
+
};
|
|
1079
|
+
for (const comparator of comparators){
|
|
1080
|
+
const extractedComparator = extractComparator(comparator);
|
|
1081
|
+
if (!extractedComparator) {
|
|
1082
|
+
return false;
|
|
1083
|
+
}
|
|
1084
|
+
const [, rangeOperator, , rangeMajor, rangeMinor, rangePatch, rangePreRelease] = extractedComparator;
|
|
1085
|
+
const rangeAtom = {
|
|
1086
|
+
operator: rangeOperator,
|
|
1087
|
+
version: combineVersion(rangeMajor, rangeMinor, rangePatch, rangePreRelease),
|
|
1088
|
+
major: rangeMajor,
|
|
1089
|
+
minor: rangeMinor,
|
|
1090
|
+
patch: rangePatch,
|
|
1091
|
+
preRelease: rangePreRelease == null ? void 0 : rangePreRelease.split('.')
|
|
1092
|
+
};
|
|
1093
|
+
if (!compare(rangeAtom, versionAtom)) {
|
|
1094
|
+
return false;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return true;
|
|
1098
|
+
}
|
|
1099
|
+
function formatShare(shareArgs, from, name, shareStrategy) {
|
|
1100
|
+
let get;
|
|
1101
|
+
if ('get' in shareArgs) {
|
|
1102
|
+
get = shareArgs.get;
|
|
1103
|
+
} else if ('lib' in shareArgs) {
|
|
1104
|
+
get = ()=>Promise.resolve(shareArgs.lib);
|
|
1105
|
+
} else {
|
|
1106
|
+
get = ()=>Promise.resolve(()=>{
|
|
1107
|
+
throw new Error(`Can not get shared '${name}'!`);
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
if (shareArgs.strategy) {
|
|
1111
|
+
warn(`"shared.strategy is deprecated, please set in initOptions.shareStrategy instead!"`);
|
|
1112
|
+
}
|
|
1113
|
+
var _shareArgs_version, _shareArgs_scope, _shareArgs_strategy;
|
|
1114
|
+
return _({
|
|
1115
|
+
deps: [],
|
|
1116
|
+
useIn: [],
|
|
1117
|
+
from,
|
|
1118
|
+
loading: null
|
|
1119
|
+
}, shareArgs, {
|
|
1120
|
+
shareConfig: _({
|
|
1121
|
+
requiredVersion: `^${shareArgs.version}`,
|
|
1122
|
+
singleton: false,
|
|
1123
|
+
eager: false,
|
|
1124
|
+
strictVersion: false
|
|
1125
|
+
}, shareArgs.shareConfig),
|
|
1126
|
+
get,
|
|
1127
|
+
loaded: (shareArgs == null ? void 0 : shareArgs.loaded) || 'lib' in shareArgs ? true : undefined,
|
|
1128
|
+
version: (_shareArgs_version = shareArgs.version) != null ? _shareArgs_version : '0',
|
|
1129
|
+
scope: Array.isArray(shareArgs.scope) ? shareArgs.scope : [
|
|
1130
|
+
(_shareArgs_scope = shareArgs.scope) != null ? _shareArgs_scope : 'default'
|
|
1131
|
+
],
|
|
1132
|
+
strategy: ((_shareArgs_strategy = shareArgs.strategy) != null ? _shareArgs_strategy : shareStrategy) || 'version-first'
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
function formatShareConfigs(globalOptions, userOptions) {
|
|
1136
|
+
const shareArgs = userOptions.shared || {};
|
|
1137
|
+
const from = userOptions.name;
|
|
1138
|
+
const shareInfos = Object.keys(shareArgs).reduce((res, pkgName)=>{
|
|
1139
|
+
const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
|
|
1140
|
+
res[pkgName] = res[pkgName] || [];
|
|
1141
|
+
arrayShareArgs.forEach((shareConfig)=>{
|
|
1142
|
+
res[pkgName].push(formatShare(shareConfig, from, pkgName, userOptions.shareStrategy));
|
|
1143
|
+
});
|
|
1144
|
+
return res;
|
|
1145
|
+
}, {});
|
|
1146
|
+
const shared = _({}, globalOptions.shared);
|
|
1147
|
+
Object.keys(shareInfos).forEach((shareKey)=>{
|
|
1148
|
+
if (!shared[shareKey]) {
|
|
1149
|
+
shared[shareKey] = shareInfos[shareKey];
|
|
1150
|
+
} else {
|
|
1151
|
+
shareInfos[shareKey].forEach((newUserSharedOptions)=>{
|
|
1152
|
+
const isSameVersion = shared[shareKey].find((sharedVal)=>sharedVal.version === newUserSharedOptions.version);
|
|
1153
|
+
if (!isSameVersion) {
|
|
1154
|
+
shared[shareKey].push(newUserSharedOptions);
|
|
1155
|
+
}
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
});
|
|
1159
|
+
return {
|
|
1160
|
+
shared,
|
|
1161
|
+
shareInfos
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
function versionLt(a, b) {
|
|
1165
|
+
const transformInvalidVersion = (version)=>{
|
|
1166
|
+
const isNumberVersion = !Number.isNaN(Number(version));
|
|
1167
|
+
if (isNumberVersion) {
|
|
1168
|
+
const splitArr = version.split('.');
|
|
1169
|
+
let validVersion = version;
|
|
1170
|
+
for(let i = 0; i < 3 - splitArr.length; i++){
|
|
1171
|
+
validVersion += '.0';
|
|
1172
|
+
}
|
|
1173
|
+
return validVersion;
|
|
1174
|
+
}
|
|
1175
|
+
return version;
|
|
1176
|
+
};
|
|
1177
|
+
if (satisfy(transformInvalidVersion(a), `<=${transformInvalidVersion(b)}`)) {
|
|
1178
|
+
return true;
|
|
1179
|
+
} else {
|
|
1180
|
+
return false;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
const findVersion = (shareVersionMap, cb)=>{
|
|
1184
|
+
const callback = cb || function(prev, cur) {
|
|
1185
|
+
return versionLt(prev, cur);
|
|
1186
|
+
};
|
|
1187
|
+
return Object.keys(shareVersionMap).reduce((prev, cur)=>{
|
|
1188
|
+
if (!prev) {
|
|
1189
|
+
return cur;
|
|
1190
|
+
}
|
|
1191
|
+
if (callback(prev, cur)) {
|
|
1192
|
+
return cur;
|
|
1193
|
+
}
|
|
1194
|
+
if (prev === '0') {
|
|
1195
|
+
return cur;
|
|
1196
|
+
}
|
|
1197
|
+
return prev;
|
|
1198
|
+
}, 0);
|
|
1199
|
+
};
|
|
1200
|
+
const isLoaded = (shared)=>{
|
|
1201
|
+
return Boolean(shared.loaded) || typeof shared.lib === 'function';
|
|
1202
|
+
};
|
|
1203
|
+
function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
|
|
1204
|
+
const versions = shareScopeMap[scope][pkgName];
|
|
1205
|
+
const callback = function(prev, cur) {
|
|
1206
|
+
return !isLoaded(versions[prev]) && versionLt(prev, cur);
|
|
1207
|
+
};
|
|
1208
|
+
return findVersion(shareScopeMap[scope][pkgName], callback);
|
|
1209
|
+
}
|
|
1210
|
+
function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
|
|
1211
|
+
const versions = shareScopeMap[scope][pkgName];
|
|
1212
|
+
const callback = function(prev, cur) {
|
|
1213
|
+
if (isLoaded(versions[cur])) {
|
|
1214
|
+
if (isLoaded(versions[prev])) {
|
|
1215
|
+
return Boolean(versionLt(prev, cur));
|
|
1216
|
+
} else {
|
|
1217
|
+
return true;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
if (isLoaded(versions[prev])) {
|
|
1221
|
+
return false;
|
|
1222
|
+
}
|
|
1223
|
+
return versionLt(prev, cur);
|
|
1224
|
+
};
|
|
1225
|
+
return findVersion(shareScopeMap[scope][pkgName], callback);
|
|
1226
|
+
}
|
|
1227
|
+
function getFindShareFunction(strategy) {
|
|
1228
|
+
if (strategy === 'loaded-first') {
|
|
1229
|
+
return findSingletonVersionOrderByLoaded;
|
|
1230
|
+
}
|
|
1231
|
+
return findSingletonVersionOrderByVersion;
|
|
1232
|
+
}
|
|
1233
|
+
function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare) {
|
|
1234
|
+
if (!localShareScopeMap) {
|
|
1235
|
+
return;
|
|
1236
|
+
}
|
|
1237
|
+
const { shareConfig, scope = DEFAULT_SCOPE, strategy } = shareInfo;
|
|
1238
|
+
const scopes = Array.isArray(scope) ? scope : [
|
|
1239
|
+
scope
|
|
1240
|
+
];
|
|
1241
|
+
for (const sc of scopes){
|
|
1242
|
+
if (shareConfig && localShareScopeMap[sc] && localShareScopeMap[sc][pkgName]) {
|
|
1243
|
+
const { requiredVersion } = shareConfig;
|
|
1244
|
+
const findShareFunction = getFindShareFunction(strategy);
|
|
1245
|
+
const maxOrSingletonVersion = findShareFunction(localShareScopeMap, sc, pkgName);
|
|
1246
|
+
const defaultResolver = ()=>{
|
|
1247
|
+
if (shareConfig.singleton) {
|
|
1248
|
+
if (typeof requiredVersion === 'string' && !satisfy(maxOrSingletonVersion, requiredVersion)) {
|
|
1249
|
+
const msg = `Version ${maxOrSingletonVersion} from ${maxOrSingletonVersion && localShareScopeMap[sc][pkgName][maxOrSingletonVersion].from} of shared singleton module ${pkgName} does not satisfy the requirement of ${shareInfo.from} which needs ${requiredVersion})`;
|
|
1250
|
+
if (shareConfig.strictVersion) {
|
|
1251
|
+
error(msg);
|
|
1252
|
+
} else {
|
|
1253
|
+
warn(msg);
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
|
|
1257
|
+
} else {
|
|
1258
|
+
if (requiredVersion === false || requiredVersion === '*') {
|
|
1259
|
+
return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
|
|
1260
|
+
}
|
|
1261
|
+
if (satisfy(maxOrSingletonVersion, requiredVersion)) {
|
|
1262
|
+
return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
|
|
1263
|
+
}
|
|
1264
|
+
for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])){
|
|
1265
|
+
if (satisfy(versionKey, requiredVersion)) {
|
|
1266
|
+
return versionValue;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
const params = {
|
|
1272
|
+
shareScopeMap: localShareScopeMap,
|
|
1273
|
+
scope: sc,
|
|
1274
|
+
pkgName,
|
|
1275
|
+
version: maxOrSingletonVersion,
|
|
1276
|
+
GlobalFederation: Global.__FEDERATION__,
|
|
1277
|
+
resolver: defaultResolver
|
|
1278
|
+
};
|
|
1279
|
+
const resolveShared = resolveShare.emit(params) || params;
|
|
1280
|
+
return resolveShared.resolver();
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
function getGlobalShareScope() {
|
|
1285
|
+
return Global.__FEDERATION__.__SHARE__;
|
|
1286
|
+
}
|
|
1287
|
+
function getTargetSharedOptions(options) {
|
|
1288
|
+
const { pkgName, extraOptions, shareInfos } = options;
|
|
1289
|
+
const defaultResolver = (sharedOptions)=>{
|
|
1290
|
+
if (!sharedOptions) {
|
|
1291
|
+
return undefined;
|
|
1292
|
+
}
|
|
1293
|
+
const shareVersionMap = {};
|
|
1294
|
+
sharedOptions.forEach((shared)=>{
|
|
1295
|
+
shareVersionMap[shared.version] = shared;
|
|
1296
|
+
});
|
|
1297
|
+
const callback = function(prev, cur) {
|
|
1298
|
+
return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
|
|
1299
|
+
};
|
|
1300
|
+
const maxVersion = findVersion(shareVersionMap, callback);
|
|
1301
|
+
return shareVersionMap[maxVersion];
|
|
1302
|
+
};
|
|
1303
|
+
var _extraOptions_resolver;
|
|
1304
|
+
const resolver = (_extraOptions_resolver = extraOptions == null ? void 0 : extraOptions.resolver) != null ? _extraOptions_resolver : defaultResolver;
|
|
1305
|
+
return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
function matchRemoteWithNameAndExpose(remotes, id) {
|
|
1309
|
+
for (const remote of remotes){
|
|
1310
|
+
const isNameMatched = id.startsWith(remote.name);
|
|
1311
|
+
let expose = id.replace(remote.name, '');
|
|
1312
|
+
if (isNameMatched) {
|
|
1313
|
+
if (expose.startsWith('/')) {
|
|
1314
|
+
const pkgNameOrAlias = remote.name;
|
|
1315
|
+
expose = `.${expose}`;
|
|
1316
|
+
return {
|
|
1317
|
+
pkgNameOrAlias,
|
|
1318
|
+
expose,
|
|
1319
|
+
remote
|
|
1320
|
+
};
|
|
1321
|
+
} else if (expose === '') {
|
|
1322
|
+
return {
|
|
1323
|
+
pkgNameOrAlias: remote.name,
|
|
1324
|
+
expose: '.',
|
|
1325
|
+
remote
|
|
1326
|
+
};
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
const isAliasMatched = remote.alias && id.startsWith(remote.alias);
|
|
1330
|
+
let exposeWithAlias = remote.alias && id.replace(remote.alias, '');
|
|
1331
|
+
if (remote.alias && isAliasMatched) {
|
|
1332
|
+
if (exposeWithAlias && exposeWithAlias.startsWith('/')) {
|
|
1333
|
+
const pkgNameOrAlias = remote.alias;
|
|
1334
|
+
exposeWithAlias = `.${exposeWithAlias}`;
|
|
1335
|
+
return {
|
|
1336
|
+
pkgNameOrAlias,
|
|
1337
|
+
expose: exposeWithAlias,
|
|
1338
|
+
remote
|
|
1339
|
+
};
|
|
1340
|
+
} else if (exposeWithAlias === '') {
|
|
1341
|
+
return {
|
|
1342
|
+
pkgNameOrAlias: remote.alias,
|
|
1343
|
+
expose: '.',
|
|
1344
|
+
remote
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
return;
|
|
1350
|
+
}
|
|
1351
|
+
function matchRemote(remotes, nameOrAlias) {
|
|
1352
|
+
for (const remote of remotes){
|
|
1353
|
+
const isNameMatched = nameOrAlias === remote.name;
|
|
1354
|
+
if (isNameMatched) {
|
|
1355
|
+
return remote;
|
|
1356
|
+
}
|
|
1357
|
+
const isAliasMatched = remote.alias && nameOrAlias === remote.alias;
|
|
1358
|
+
if (isAliasMatched) {
|
|
1359
|
+
return remote;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
function registerPlugins$1(plugins, hookInstances) {
|
|
1365
|
+
const globalPlugins = getGlobalHostPlugins();
|
|
1366
|
+
if (globalPlugins.length > 0) {
|
|
1367
|
+
globalPlugins.forEach((plugin)=>{
|
|
1368
|
+
if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
|
|
1369
|
+
plugins.push(plugin);
|
|
1370
|
+
}
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
if (plugins && plugins.length > 0) {
|
|
1374
|
+
plugins.forEach((plugin)=>{
|
|
1375
|
+
hookInstances.forEach((hookInstance)=>{
|
|
1376
|
+
hookInstance.applyPlugin(plugin);
|
|
1377
|
+
});
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
return plugins;
|
|
1381
|
+
}
|
|
1382
|
+
async function loadEsmEntry({ entry, remoteEntryExports }) {
|
|
1383
|
+
return new Promise((resolve, reject)=>{
|
|
1384
|
+
try {
|
|
1385
|
+
if (!remoteEntryExports) {
|
|
1386
|
+
new Function('callbacks', `import("${entry}").then(callbacks[0]).catch(callbacks[1])`)([
|
|
1387
|
+
resolve,
|
|
1388
|
+
reject
|
|
1389
|
+
]);
|
|
1390
|
+
} else {
|
|
1391
|
+
resolve(remoteEntryExports);
|
|
1392
|
+
}
|
|
1393
|
+
} catch (e) {
|
|
1394
|
+
reject(e);
|
|
1395
|
+
}
|
|
1396
|
+
});
|
|
1397
|
+
}
|
|
1398
|
+
async function loadSystemJsEntry({ entry, remoteEntryExports }) {
|
|
1399
|
+
return new Promise((resolve, reject)=>{
|
|
1400
|
+
try {
|
|
1401
|
+
if (!remoteEntryExports) {
|
|
1402
|
+
new Function('callbacks', `System.import("${entry}").then(callbacks[0]).catch(callbacks[1])`)([
|
|
1403
|
+
resolve,
|
|
1404
|
+
reject
|
|
1405
|
+
]);
|
|
1406
|
+
} else {
|
|
1407
|
+
resolve(remoteEntryExports);
|
|
1408
|
+
}
|
|
1409
|
+
} catch (e) {
|
|
1410
|
+
reject(e);
|
|
1411
|
+
}
|
|
1412
|
+
});
|
|
1413
|
+
}
|
|
1414
|
+
async function loadEntryScript({ name, globalName, entry, createScriptHook }) {
|
|
1415
|
+
const { entryExports: remoteEntryExports } = getRemoteEntryExports(name, globalName);
|
|
1416
|
+
if (remoteEntryExports) {
|
|
1417
|
+
return remoteEntryExports;
|
|
1418
|
+
}
|
|
1419
|
+
return loadScript(entry, {
|
|
1420
|
+
attrs: {},
|
|
1421
|
+
createScriptHook: (url, attrs)=>{
|
|
1422
|
+
const res = createScriptHook.emit({
|
|
1423
|
+
url,
|
|
1424
|
+
attrs
|
|
1425
|
+
});
|
|
1426
|
+
if (!res) return;
|
|
1427
|
+
if (res instanceof HTMLScriptElement) {
|
|
1428
|
+
return res;
|
|
1429
|
+
}
|
|
1430
|
+
if ('script' in res || 'timeout' in res) {
|
|
1431
|
+
return res;
|
|
1432
|
+
}
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
}).then(()=>{
|
|
1436
|
+
const { remoteEntryKey, entryExports } = getRemoteEntryExports(name, globalName);
|
|
1437
|
+
assert(entryExports, `
|
|
1438
|
+
Unable to use the ${name}'s '${entry}' URL with ${remoteEntryKey}'s globalName to get remoteEntry exports.
|
|
1439
|
+
Possible reasons could be:\n
|
|
1440
|
+
1. '${entry}' is not the correct URL, or the remoteEntry resource or name is incorrect.\n
|
|
1441
|
+
2. ${remoteEntryKey} cannot be used to get remoteEntry exports in the window object.
|
|
1442
|
+
`);
|
|
1443
|
+
return entryExports;
|
|
1444
|
+
}).catch((e)=>{
|
|
1445
|
+
throw e;
|
|
1446
|
+
});
|
|
1447
|
+
}
|
|
1448
|
+
async function loadEntryDom({ remoteInfo, remoteEntryExports, createScriptHook }) {
|
|
1449
|
+
const { entry, entryGlobalName: globalName, name, type } = remoteInfo;
|
|
1450
|
+
switch(type){
|
|
1451
|
+
case 'esm':
|
|
1452
|
+
case 'module':
|
|
1453
|
+
return loadEsmEntry({
|
|
1454
|
+
entry,
|
|
1455
|
+
remoteEntryExports
|
|
1456
|
+
});
|
|
1457
|
+
case 'system':
|
|
1458
|
+
return loadSystemJsEntry({
|
|
1459
|
+
entry,
|
|
1460
|
+
remoteEntryExports
|
|
1461
|
+
});
|
|
1462
|
+
default:
|
|
1463
|
+
return loadEntryScript({
|
|
1464
|
+
entry,
|
|
1465
|
+
globalName,
|
|
1466
|
+
name,
|
|
1467
|
+
createScriptHook
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
async function loadEntryNode({ remoteInfo, createScriptHook }) {
|
|
1472
|
+
const { entry, entryGlobalName: globalName, name } = remoteInfo;
|
|
1473
|
+
const { entryExports: remoteEntryExports } = getRemoteEntryExports(name, globalName);
|
|
1474
|
+
if (remoteEntryExports) {
|
|
1475
|
+
return remoteEntryExports;
|
|
1476
|
+
}
|
|
1477
|
+
return loadScriptNode(entry, {
|
|
1478
|
+
attrs: {
|
|
1479
|
+
name,
|
|
1480
|
+
globalName
|
|
1481
|
+
},
|
|
1482
|
+
createScriptHook: (url, attrs)=>{
|
|
1483
|
+
const res = createScriptHook.emit({
|
|
1484
|
+
url,
|
|
1485
|
+
attrs
|
|
1486
|
+
});
|
|
1487
|
+
if (!res) return;
|
|
1488
|
+
if ('url' in res) {
|
|
1489
|
+
return res;
|
|
1490
|
+
}
|
|
1491
|
+
return;
|
|
1492
|
+
}
|
|
1493
|
+
}).then(()=>{
|
|
1494
|
+
const { remoteEntryKey, entryExports } = getRemoteEntryExports(name, globalName);
|
|
1495
|
+
assert(entryExports, `
|
|
1496
|
+
Unable to use the ${name}'s '${entry}' URL with ${remoteEntryKey}'s globalName to get remoteEntry exports.
|
|
1497
|
+
Possible reasons could be:\n
|
|
1498
|
+
1. '${entry}' is not the correct URL, or the remoteEntry resource or name is incorrect.\n
|
|
1499
|
+
2. ${remoteEntryKey} cannot be used to get remoteEntry exports in the window object.
|
|
1500
|
+
`);
|
|
1501
|
+
return entryExports;
|
|
1502
|
+
}).catch((e)=>{
|
|
1503
|
+
throw e;
|
|
1504
|
+
});
|
|
1505
|
+
}
|
|
1506
|
+
function getRemoteEntryUniqueKey(remoteInfo) {
|
|
1507
|
+
const { entry, name } = remoteInfo;
|
|
1508
|
+
return composeKeyWithSeparator(name, entry);
|
|
1509
|
+
}
|
|
1510
|
+
async function getRemoteEntry({ origin, remoteEntryExports, remoteInfo }) {
|
|
1511
|
+
const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
|
|
1512
|
+
if (remoteEntryExports) {
|
|
1513
|
+
return remoteEntryExports;
|
|
1514
|
+
}
|
|
1515
|
+
if (!globalLoading[uniqueKey]) {
|
|
1516
|
+
const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;
|
|
1517
|
+
if (loadEntryHook.listeners.size) {
|
|
1518
|
+
globalLoading[uniqueKey] = loadEntryHook.emit({
|
|
1519
|
+
createScriptHook: origin.loaderHook.lifecycle.createScript,
|
|
1520
|
+
remoteInfo,
|
|
1521
|
+
remoteEntryExports
|
|
1522
|
+
}).then((res)=>res || undefined);
|
|
1523
|
+
} else {
|
|
1524
|
+
const createScriptHook = origin.loaderHook.lifecycle.createScript;
|
|
1525
|
+
if (!isBrowserEnv()) {
|
|
1526
|
+
globalLoading[uniqueKey] = loadEntryNode({
|
|
1527
|
+
remoteInfo,
|
|
1528
|
+
createScriptHook
|
|
1529
|
+
});
|
|
1530
|
+
} else {
|
|
1531
|
+
globalLoading[uniqueKey] = loadEntryDom({
|
|
1532
|
+
remoteInfo,
|
|
1533
|
+
remoteEntryExports,
|
|
1534
|
+
createScriptHook
|
|
1535
|
+
});
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
return globalLoading[uniqueKey];
|
|
1540
|
+
}
|
|
1541
|
+
function getRemoteInfo(remote) {
|
|
1542
|
+
return _({}, remote, {
|
|
1543
|
+
entry: 'entry' in remote ? remote.entry : '',
|
|
1544
|
+
type: remote.type || DEFAULT_REMOTE_TYPE,
|
|
1545
|
+
entryGlobalName: remote.entryGlobalName || remote.name,
|
|
1546
|
+
shareScope: remote.shareScope || DEFAULT_SCOPE
|
|
1547
|
+
});
|
|
1548
|
+
}
|
|
1549
|
+
let Module = class Module {
|
|
1550
|
+
async getEntry() {
|
|
1551
|
+
if (this.remoteEntryExports) {
|
|
1552
|
+
return this.remoteEntryExports;
|
|
1553
|
+
}
|
|
1554
|
+
const remoteEntryExports = await getRemoteEntry({
|
|
1555
|
+
origin: this.host,
|
|
1556
|
+
remoteInfo: this.remoteInfo,
|
|
1557
|
+
remoteEntryExports: this.remoteEntryExports
|
|
1558
|
+
});
|
|
1559
|
+
assert(remoteEntryExports, `remoteEntryExports is undefined \n ${safeToString(this.remoteInfo)}`);
|
|
1560
|
+
this.remoteEntryExports = remoteEntryExports;
|
|
1561
|
+
return this.remoteEntryExports;
|
|
1562
|
+
}
|
|
1563
|
+
async get(id, expose, options) {
|
|
1564
|
+
const { loadFactory = true } = options || {
|
|
1565
|
+
loadFactory: true
|
|
1566
|
+
};
|
|
1567
|
+
const remoteEntryExports = await this.getEntry();
|
|
1568
|
+
if (!this.inited) {
|
|
1569
|
+
const localShareScopeMap = this.host.shareScopeMap;
|
|
1570
|
+
const remoteShareScope = this.remoteInfo.shareScope || 'default';
|
|
1571
|
+
if (!localShareScopeMap[remoteShareScope]) {
|
|
1572
|
+
localShareScopeMap[remoteShareScope] = {};
|
|
1573
|
+
}
|
|
1574
|
+
const shareScope = localShareScopeMap[remoteShareScope];
|
|
1575
|
+
const initScope = [];
|
|
1576
|
+
const remoteEntryInitOptions = {
|
|
1577
|
+
version: this.remoteInfo.version || ''
|
|
1578
|
+
};
|
|
1579
|
+
Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
|
|
1580
|
+
value: localShareScopeMap,
|
|
1581
|
+
enumerable: false
|
|
1582
|
+
});
|
|
1583
|
+
const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
|
|
1584
|
+
shareScope,
|
|
1585
|
+
remoteEntryInitOptions,
|
|
1586
|
+
initScope,
|
|
1587
|
+
remoteInfo: this.remoteInfo,
|
|
1588
|
+
origin: this.host
|
|
1589
|
+
});
|
|
1590
|
+
if (typeof (remoteEntryExports == null ? void 0 : remoteEntryExports.init) === 'undefined') {
|
|
1591
|
+
console.error('The remote entry interface does not contain "init"', '\n', 'Ensure the name of this remote is not reserved or in use. Check if anything already exists on window[nameOfRemote]', '\n', 'Ensure that window[nameOfRemote] is returning a {get,init} object.');
|
|
1592
|
+
}
|
|
1593
|
+
await remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
|
|
1594
|
+
await this.host.hooks.lifecycle.initContainer.emit(_({}, initContainerOptions, {
|
|
1595
|
+
remoteEntryExports
|
|
1596
|
+
}));
|
|
1597
|
+
}
|
|
1598
|
+
this.lib = remoteEntryExports;
|
|
1599
|
+
this.inited = true;
|
|
1600
|
+
const moduleFactory = await remoteEntryExports.get(expose);
|
|
1601
|
+
assert(moduleFactory, `${getFMId(this.remoteInfo)} remote don't export ${expose}.`);
|
|
1602
|
+
const wrapModuleFactory = this.wraperFactory(moduleFactory, id);
|
|
1603
|
+
if (!loadFactory) {
|
|
1604
|
+
return wrapModuleFactory;
|
|
1605
|
+
}
|
|
1606
|
+
const exposeContent = await wrapModuleFactory();
|
|
1607
|
+
return exposeContent;
|
|
1608
|
+
}
|
|
1609
|
+
wraperFactory(moduleFactory, id) {
|
|
1610
|
+
function defineModuleId(res, id) {
|
|
1611
|
+
if (res && typeof res === 'object' && Object.isExtensible(res) && !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))) {
|
|
1612
|
+
Object.defineProperty(res, Symbol.for('mf_module_id'), {
|
|
1613
|
+
value: id,
|
|
1614
|
+
enumerable: false
|
|
1615
|
+
});
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
if (moduleFactory instanceof Promise) {
|
|
1619
|
+
return async ()=>{
|
|
1620
|
+
const res = await moduleFactory();
|
|
1621
|
+
defineModuleId(res, id);
|
|
1622
|
+
return res;
|
|
1623
|
+
};
|
|
1624
|
+
} else {
|
|
1625
|
+
return ()=>{
|
|
1626
|
+
const res = moduleFactory();
|
|
1627
|
+
defineModuleId(res, id);
|
|
1628
|
+
return res;
|
|
1629
|
+
};
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
constructor({ remoteInfo, host }){
|
|
1633
|
+
this.inited = false;
|
|
1634
|
+
this.lib = undefined;
|
|
1635
|
+
this.remoteInfo = remoteInfo;
|
|
1636
|
+
this.host = host;
|
|
1637
|
+
}
|
|
1638
|
+
};
|
|
1639
|
+
class SyncHook {
|
|
1640
|
+
on(fn) {
|
|
1641
|
+
if (typeof fn === 'function') {
|
|
1642
|
+
this.listeners.add(fn);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
once(fn) {
|
|
1646
|
+
const self = this;
|
|
1647
|
+
this.on(function wrapper(...args) {
|
|
1648
|
+
self.remove(wrapper);
|
|
1649
|
+
return fn.apply(null, args);
|
|
1650
|
+
});
|
|
1651
|
+
}
|
|
1652
|
+
emit(...data) {
|
|
1653
|
+
let result;
|
|
1654
|
+
if (this.listeners.size > 0) {
|
|
1655
|
+
this.listeners.forEach((fn)=>{
|
|
1656
|
+
result = fn(...data);
|
|
1657
|
+
});
|
|
1658
|
+
}
|
|
1659
|
+
return result;
|
|
1660
|
+
}
|
|
1661
|
+
remove(fn) {
|
|
1662
|
+
this.listeners.delete(fn);
|
|
1663
|
+
}
|
|
1664
|
+
removeAll() {
|
|
1665
|
+
this.listeners.clear();
|
|
1666
|
+
}
|
|
1667
|
+
constructor(type){
|
|
1668
|
+
this.type = '';
|
|
1669
|
+
this.listeners = new Set();
|
|
1670
|
+
if (type) {
|
|
1671
|
+
this.type = type;
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
class AsyncHook extends SyncHook {
|
|
1676
|
+
emit(...data) {
|
|
1677
|
+
let result;
|
|
1678
|
+
const ls = Array.from(this.listeners);
|
|
1679
|
+
if (ls.length > 0) {
|
|
1680
|
+
let i = 0;
|
|
1681
|
+
const call = (prev)=>{
|
|
1682
|
+
if (prev === false) {
|
|
1683
|
+
return false;
|
|
1684
|
+
} else if (i < ls.length) {
|
|
1685
|
+
return Promise.resolve(ls[i++].apply(null, data)).then(call);
|
|
1686
|
+
} else {
|
|
1687
|
+
return prev;
|
|
1688
|
+
}
|
|
1689
|
+
};
|
|
1690
|
+
result = call();
|
|
1691
|
+
}
|
|
1692
|
+
return Promise.resolve(result);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
function checkReturnData(originalData, returnedData) {
|
|
1696
|
+
if (!isObject(returnedData)) {
|
|
1697
|
+
return false;
|
|
1698
|
+
}
|
|
1699
|
+
if (originalData !== returnedData) {
|
|
1700
|
+
for(const key in originalData){
|
|
1701
|
+
if (!(key in returnedData)) {
|
|
1702
|
+
return false;
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
return true;
|
|
1707
|
+
}
|
|
1708
|
+
class SyncWaterfallHook extends SyncHook {
|
|
1709
|
+
emit(data) {
|
|
1710
|
+
if (!isObject(data)) {
|
|
1711
|
+
error(`The data for the "${this.type}" hook should be an object.`);
|
|
1712
|
+
}
|
|
1713
|
+
for (const fn of this.listeners){
|
|
1714
|
+
try {
|
|
1715
|
+
const tempData = fn(data);
|
|
1716
|
+
if (checkReturnData(data, tempData)) {
|
|
1717
|
+
data = tempData;
|
|
1718
|
+
} else {
|
|
1719
|
+
this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
|
|
1720
|
+
break;
|
|
1721
|
+
}
|
|
1722
|
+
} catch (e) {
|
|
1723
|
+
warn(e);
|
|
1724
|
+
this.onerror(e);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
return data;
|
|
1728
|
+
}
|
|
1729
|
+
constructor(type){
|
|
1730
|
+
super();
|
|
1731
|
+
this.onerror = error;
|
|
1732
|
+
this.type = type;
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
class AsyncWaterfallHook extends SyncHook {
|
|
1736
|
+
emit(data) {
|
|
1737
|
+
if (!isObject(data)) {
|
|
1738
|
+
error(`The response data for the "${this.type}" hook must be an object.`);
|
|
1739
|
+
}
|
|
1740
|
+
const ls = Array.from(this.listeners);
|
|
1741
|
+
if (ls.length > 0) {
|
|
1742
|
+
let i = 0;
|
|
1743
|
+
const processError = (e)=>{
|
|
1744
|
+
warn(e);
|
|
1745
|
+
this.onerror(e);
|
|
1746
|
+
return data;
|
|
1747
|
+
};
|
|
1748
|
+
const call = (prevData)=>{
|
|
1749
|
+
if (checkReturnData(data, prevData)) {
|
|
1750
|
+
data = prevData;
|
|
1751
|
+
if (i < ls.length) {
|
|
1752
|
+
try {
|
|
1753
|
+
return Promise.resolve(ls[i++](data)).then(call, processError);
|
|
1754
|
+
} catch (e) {
|
|
1755
|
+
return processError(e);
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
} else {
|
|
1759
|
+
this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
|
|
1760
|
+
}
|
|
1761
|
+
return data;
|
|
1762
|
+
};
|
|
1763
|
+
return Promise.resolve(call(data));
|
|
1764
|
+
}
|
|
1765
|
+
return Promise.resolve(data);
|
|
1766
|
+
}
|
|
1767
|
+
constructor(type){
|
|
1768
|
+
super();
|
|
1769
|
+
this.onerror = error;
|
|
1770
|
+
this.type = type;
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
class PluginSystem {
|
|
1774
|
+
applyPlugin(plugin) {
|
|
1775
|
+
assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
|
|
1776
|
+
const pluginName = plugin.name;
|
|
1777
|
+
assert(pluginName, 'A name must be provided by the plugin.');
|
|
1778
|
+
if (!this.registerPlugins[pluginName]) {
|
|
1779
|
+
this.registerPlugins[pluginName] = plugin;
|
|
1780
|
+
Object.keys(this.lifecycle).forEach((key)=>{
|
|
1781
|
+
const pluginLife = plugin[key];
|
|
1782
|
+
if (pluginLife) {
|
|
1783
|
+
this.lifecycle[key].on(pluginLife);
|
|
1784
|
+
}
|
|
1785
|
+
});
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
removePlugin(pluginName) {
|
|
1789
|
+
assert(pluginName, 'A name is required.');
|
|
1790
|
+
const plugin = this.registerPlugins[pluginName];
|
|
1791
|
+
assert(plugin, `The plugin "${pluginName}" is not registered.`);
|
|
1792
|
+
Object.keys(plugin).forEach((key)=>{
|
|
1793
|
+
if (key !== 'name') {
|
|
1794
|
+
this.lifecycle[key].remove(plugin[key]);
|
|
1795
|
+
}
|
|
1796
|
+
});
|
|
1797
|
+
}
|
|
1798
|
+
inherit({ lifecycle, registerPlugins }) {
|
|
1799
|
+
Object.keys(lifecycle).forEach((hookName)=>{
|
|
1800
|
+
assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
|
|
1801
|
+
this.lifecycle[hookName] = lifecycle[hookName];
|
|
1802
|
+
});
|
|
1803
|
+
Object.keys(registerPlugins).forEach((pluginName)=>{
|
|
1804
|
+
assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
|
|
1805
|
+
this.applyPlugin(registerPlugins[pluginName]);
|
|
1806
|
+
});
|
|
1807
|
+
}
|
|
1808
|
+
constructor(lifecycle){
|
|
1809
|
+
this.registerPlugins = {};
|
|
1810
|
+
this.lifecycle = lifecycle;
|
|
1811
|
+
this.lifecycleKeys = Object.keys(lifecycle);
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
function defaultPreloadArgs(preloadConfig) {
|
|
1815
|
+
return _({
|
|
1816
|
+
resourceCategory: 'sync',
|
|
1817
|
+
share: true,
|
|
1818
|
+
depsRemote: true,
|
|
1819
|
+
prefetchInterface: false
|
|
1820
|
+
}, preloadConfig);
|
|
1821
|
+
}
|
|
1822
|
+
function formatPreloadArgs(remotes, preloadArgs) {
|
|
1823
|
+
return preloadArgs.map((args)=>{
|
|
1824
|
+
const remoteInfo = matchRemote(remotes, args.nameOrAlias);
|
|
1825
|
+
assert(remoteInfo, `Unable to preload ${args.nameOrAlias} as it is not included in ${!remoteInfo && safeToString({
|
|
1826
|
+
remoteInfo,
|
|
1827
|
+
remotes
|
|
1828
|
+
})}`);
|
|
1829
|
+
return {
|
|
1830
|
+
remote: remoteInfo,
|
|
1831
|
+
preloadConfig: defaultPreloadArgs(args)
|
|
1832
|
+
};
|
|
1833
|
+
});
|
|
1834
|
+
}
|
|
1835
|
+
function normalizePreloadExposes(exposes) {
|
|
1836
|
+
if (!exposes) {
|
|
1837
|
+
return [];
|
|
1838
|
+
}
|
|
1839
|
+
return exposes.map((expose)=>{
|
|
1840
|
+
if (expose === '.') {
|
|
1841
|
+
return expose;
|
|
1842
|
+
}
|
|
1843
|
+
if (expose.startsWith('./')) {
|
|
1844
|
+
return expose.replace('./', '');
|
|
1845
|
+
}
|
|
1846
|
+
return expose;
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
function preloadAssets(remoteInfo, host, assets, useLinkPreload = true) {
|
|
1850
|
+
const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
|
|
1851
|
+
if (host.options.inBrowser) {
|
|
1852
|
+
entryAssets.forEach((asset)=>{
|
|
1853
|
+
const { moduleInfo } = asset;
|
|
1854
|
+
const module = host.moduleCache.get(remoteInfo.name);
|
|
1855
|
+
if (module) {
|
|
1856
|
+
getRemoteEntry({
|
|
1857
|
+
origin: host,
|
|
1858
|
+
remoteInfo: moduleInfo,
|
|
1859
|
+
remoteEntryExports: module.remoteEntryExports
|
|
1860
|
+
});
|
|
1861
|
+
} else {
|
|
1862
|
+
getRemoteEntry({
|
|
1863
|
+
origin: host,
|
|
1864
|
+
remoteInfo: moduleInfo,
|
|
1865
|
+
remoteEntryExports: undefined
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
});
|
|
1869
|
+
if (useLinkPreload) {
|
|
1870
|
+
const defaultAttrs = {
|
|
1871
|
+
rel: 'preload',
|
|
1872
|
+
as: 'style',
|
|
1873
|
+
crossorigin: 'anonymous'
|
|
1874
|
+
};
|
|
1875
|
+
cssAssets.forEach((cssUrl)=>{
|
|
1876
|
+
const { link: cssEl, needAttach } = createLink({
|
|
1877
|
+
url: cssUrl,
|
|
1878
|
+
cb: ()=>{},
|
|
1879
|
+
attrs: defaultAttrs,
|
|
1880
|
+
createLinkHook: (url, attrs)=>{
|
|
1881
|
+
const res = host.loaderHook.lifecycle.createLink.emit({
|
|
1882
|
+
url,
|
|
1883
|
+
attrs
|
|
1884
|
+
});
|
|
1885
|
+
if (res instanceof HTMLLinkElement) {
|
|
1886
|
+
return res;
|
|
1887
|
+
}
|
|
1888
|
+
return;
|
|
1889
|
+
}
|
|
1890
|
+
});
|
|
1891
|
+
needAttach && document.head.appendChild(cssEl);
|
|
1892
|
+
});
|
|
1893
|
+
} else {
|
|
1894
|
+
const defaultAttrs = {
|
|
1895
|
+
rel: 'stylesheet',
|
|
1896
|
+
type: 'text/css'
|
|
1897
|
+
};
|
|
1898
|
+
cssAssets.forEach((cssUrl)=>{
|
|
1899
|
+
const { link: cssEl, needAttach } = createLink({
|
|
1900
|
+
url: cssUrl,
|
|
1901
|
+
cb: ()=>{},
|
|
1902
|
+
attrs: defaultAttrs,
|
|
1903
|
+
createLinkHook: (url, attrs)=>{
|
|
1904
|
+
const res = host.loaderHook.lifecycle.createLink.emit({
|
|
1905
|
+
url,
|
|
1906
|
+
attrs
|
|
1907
|
+
});
|
|
1908
|
+
if (res instanceof HTMLLinkElement) {
|
|
1909
|
+
return res;
|
|
1910
|
+
}
|
|
1911
|
+
return;
|
|
1912
|
+
},
|
|
1913
|
+
needDeleteLink: false
|
|
1914
|
+
});
|
|
1915
|
+
needAttach && document.head.appendChild(cssEl);
|
|
1916
|
+
});
|
|
1917
|
+
}
|
|
1918
|
+
if (useLinkPreload) {
|
|
1919
|
+
const defaultAttrs = {
|
|
1920
|
+
rel: 'preload',
|
|
1921
|
+
as: 'script',
|
|
1922
|
+
crossorigin: 'anonymous'
|
|
1923
|
+
};
|
|
1924
|
+
jsAssetsWithoutEntry.forEach((jsUrl)=>{
|
|
1925
|
+
const { link: linkEl, needAttach } = createLink({
|
|
1926
|
+
url: jsUrl,
|
|
1927
|
+
cb: ()=>{},
|
|
1928
|
+
attrs: defaultAttrs,
|
|
1929
|
+
createLinkHook: (url, attrs)=>{
|
|
1930
|
+
const res = host.loaderHook.lifecycle.createLink.emit({
|
|
1931
|
+
url,
|
|
1932
|
+
attrs
|
|
1933
|
+
});
|
|
1934
|
+
if (res instanceof HTMLLinkElement) {
|
|
1935
|
+
return res;
|
|
1936
|
+
}
|
|
1937
|
+
return;
|
|
1938
|
+
}
|
|
1939
|
+
});
|
|
1940
|
+
needAttach && document.head.appendChild(linkEl);
|
|
1941
|
+
});
|
|
1942
|
+
} else {
|
|
1943
|
+
const defaultAttrs = {
|
|
1944
|
+
fetchpriority: 'high',
|
|
1945
|
+
type: (remoteInfo == null ? void 0 : remoteInfo.type) === 'module' ? 'module' : 'text/javascript'
|
|
1946
|
+
};
|
|
1947
|
+
jsAssetsWithoutEntry.forEach((jsUrl)=>{
|
|
1948
|
+
const { script: scriptEl, needAttach } = createScript({
|
|
1949
|
+
url: jsUrl,
|
|
1950
|
+
cb: ()=>{},
|
|
1951
|
+
attrs: defaultAttrs,
|
|
1952
|
+
createScriptHook: (url, attrs)=>{
|
|
1953
|
+
const res = host.loaderHook.lifecycle.createScript.emit({
|
|
1954
|
+
url,
|
|
1955
|
+
attrs
|
|
1956
|
+
});
|
|
1957
|
+
if (res instanceof HTMLScriptElement) {
|
|
1958
|
+
return res;
|
|
1959
|
+
}
|
|
1960
|
+
return;
|
|
1961
|
+
},
|
|
1962
|
+
needDeleteScript: true
|
|
1963
|
+
});
|
|
1964
|
+
needAttach && document.head.appendChild(scriptEl);
|
|
1965
|
+
});
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
function assignRemoteInfo(remoteInfo, remoteSnapshot) {
|
|
1970
|
+
const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);
|
|
1971
|
+
if (!remoteEntryInfo.url) {
|
|
1972
|
+
error(`The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`);
|
|
1973
|
+
}
|
|
1974
|
+
let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);
|
|
1975
|
+
if (!isBrowserEnv() && !entryUrl.startsWith('http')) {
|
|
1976
|
+
entryUrl = `https:${entryUrl}`;
|
|
1977
|
+
}
|
|
1978
|
+
remoteInfo.type = remoteEntryInfo.type;
|
|
1979
|
+
remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
|
|
1980
|
+
remoteInfo.entry = entryUrl;
|
|
1981
|
+
remoteInfo.version = remoteSnapshot.version;
|
|
1982
|
+
remoteInfo.buildVersion = remoteSnapshot.buildVersion;
|
|
1983
|
+
}
|
|
1984
|
+
function snapshotPlugin() {
|
|
1985
|
+
return {
|
|
1986
|
+
name: 'snapshot-plugin',
|
|
1987
|
+
async afterResolve (args) {
|
|
1988
|
+
const { remote, pkgNameOrAlias, expose, origin, remoteInfo } = args;
|
|
1989
|
+
if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {
|
|
1990
|
+
const { remoteSnapshot, globalSnapshot } = await origin.snapshotHandler.loadRemoteSnapshotInfo(remote);
|
|
1991
|
+
assignRemoteInfo(remoteInfo, remoteSnapshot);
|
|
1992
|
+
const preloadOptions = {
|
|
1993
|
+
remote,
|
|
1994
|
+
preloadConfig: {
|
|
1995
|
+
nameOrAlias: pkgNameOrAlias,
|
|
1996
|
+
exposes: [
|
|
1997
|
+
expose
|
|
1998
|
+
],
|
|
1999
|
+
resourceCategory: 'sync',
|
|
2000
|
+
share: false,
|
|
2001
|
+
depsRemote: false
|
|
2002
|
+
}
|
|
2003
|
+
};
|
|
2004
|
+
const assets = await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
|
|
2005
|
+
origin,
|
|
2006
|
+
preloadOptions,
|
|
2007
|
+
remoteInfo,
|
|
2008
|
+
remote,
|
|
2009
|
+
remoteSnapshot,
|
|
2010
|
+
globalSnapshot
|
|
2011
|
+
});
|
|
2012
|
+
if (assets) {
|
|
2013
|
+
preloadAssets(remoteInfo, origin, assets, false);
|
|
2014
|
+
}
|
|
2015
|
+
return _({}, args, {
|
|
2016
|
+
remoteSnapshot
|
|
2017
|
+
});
|
|
2018
|
+
}
|
|
2019
|
+
return args;
|
|
2020
|
+
}
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
2023
|
+
function splitId(id) {
|
|
2024
|
+
const splitInfo = id.split(':');
|
|
2025
|
+
if (splitInfo.length === 1) {
|
|
2026
|
+
return {
|
|
2027
|
+
name: splitInfo[0],
|
|
2028
|
+
version: undefined
|
|
2029
|
+
};
|
|
2030
|
+
} else if (splitInfo.length === 2) {
|
|
2031
|
+
return {
|
|
2032
|
+
name: splitInfo[0],
|
|
2033
|
+
version: splitInfo[1]
|
|
2034
|
+
};
|
|
2035
|
+
} else {
|
|
2036
|
+
return {
|
|
2037
|
+
name: splitInfo[1],
|
|
2038
|
+
version: splitInfo[2]
|
|
2039
|
+
};
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo = {}, remoteSnapshot) {
|
|
2043
|
+
const id = getFMId(remoteInfo);
|
|
2044
|
+
const { value: snapshotValue } = getInfoWithoutType(globalSnapshot, id);
|
|
2045
|
+
const effectiveRemoteSnapshot = remoteSnapshot || snapshotValue;
|
|
2046
|
+
if (effectiveRemoteSnapshot && !isManifestProvider(effectiveRemoteSnapshot)) {
|
|
2047
|
+
traverse(effectiveRemoteSnapshot, remoteInfo, isRoot);
|
|
2048
|
+
if (effectiveRemoteSnapshot.remotesInfo) {
|
|
2049
|
+
const remoteKeys = Object.keys(effectiveRemoteSnapshot.remotesInfo);
|
|
2050
|
+
for (const key of remoteKeys){
|
|
2051
|
+
if (memo[key]) {
|
|
2052
|
+
continue;
|
|
2053
|
+
}
|
|
2054
|
+
memo[key] = true;
|
|
2055
|
+
const subRemoteInfo = splitId(key);
|
|
2056
|
+
const remoteValue = effectiveRemoteSnapshot.remotesInfo[key];
|
|
2057
|
+
traverseModuleInfo(globalSnapshot, {
|
|
2058
|
+
name: subRemoteInfo.name,
|
|
2059
|
+
version: remoteValue.matchedVersion
|
|
2060
|
+
}, traverse, false, memo, undefined);
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, remoteSnapshot) {
|
|
2066
|
+
const cssAssets = [];
|
|
2067
|
+
const jsAssets = [];
|
|
2068
|
+
const entryAssets = [];
|
|
2069
|
+
const loadedSharedJsAssets = new Set();
|
|
2070
|
+
const loadedSharedCssAssets = new Set();
|
|
2071
|
+
const { options } = origin;
|
|
2072
|
+
const { preloadConfig: rootPreloadConfig } = preloadOptions;
|
|
2073
|
+
const { depsRemote } = rootPreloadConfig;
|
|
2074
|
+
const memo = {};
|
|
2075
|
+
traverseModuleInfo(globalSnapshot, remote, (moduleInfoSnapshot, remoteInfo, isRoot)=>{
|
|
2076
|
+
let preloadConfig;
|
|
2077
|
+
if (isRoot) {
|
|
2078
|
+
preloadConfig = rootPreloadConfig;
|
|
2079
|
+
} else {
|
|
2080
|
+
if (Array.isArray(depsRemote)) {
|
|
2081
|
+
const findPreloadConfig = depsRemote.find((remoteConfig)=>{
|
|
2082
|
+
if (remoteConfig.nameOrAlias === remoteInfo.name || remoteConfig.nameOrAlias === remoteInfo.alias) {
|
|
2083
|
+
return true;
|
|
2084
|
+
}
|
|
2085
|
+
return false;
|
|
2086
|
+
});
|
|
2087
|
+
if (!findPreloadConfig) {
|
|
2088
|
+
return;
|
|
2089
|
+
}
|
|
2090
|
+
preloadConfig = defaultPreloadArgs(findPreloadConfig);
|
|
2091
|
+
} else if (depsRemote === true) {
|
|
2092
|
+
preloadConfig = rootPreloadConfig;
|
|
2093
|
+
} else {
|
|
2094
|
+
return;
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
const remoteEntryUrl = getResourceUrl(moduleInfoSnapshot, getRemoteEntryInfoFromSnapshot(moduleInfoSnapshot).url);
|
|
2098
|
+
if (remoteEntryUrl) {
|
|
2099
|
+
entryAssets.push({
|
|
2100
|
+
name: remoteInfo.name,
|
|
2101
|
+
moduleInfo: {
|
|
2102
|
+
name: remoteInfo.name,
|
|
2103
|
+
entry: remoteEntryUrl,
|
|
2104
|
+
type: 'remoteEntryType' in moduleInfoSnapshot ? moduleInfoSnapshot.remoteEntryType : 'global',
|
|
2105
|
+
entryGlobalName: 'globalName' in moduleInfoSnapshot ? moduleInfoSnapshot.globalName : remoteInfo.name,
|
|
2106
|
+
shareScope: '',
|
|
2107
|
+
version: 'version' in moduleInfoSnapshot ? moduleInfoSnapshot.version : undefined
|
|
2108
|
+
},
|
|
2109
|
+
url: remoteEntryUrl
|
|
2110
|
+
});
|
|
2111
|
+
}
|
|
2112
|
+
let moduleAssetsInfo = 'modules' in moduleInfoSnapshot ? moduleInfoSnapshot.modules : [];
|
|
2113
|
+
const normalizedPreloadExposes = normalizePreloadExposes(preloadConfig.exposes);
|
|
2114
|
+
if (normalizedPreloadExposes.length && 'modules' in moduleInfoSnapshot) {
|
|
2115
|
+
var _moduleInfoSnapshot_modules;
|
|
2116
|
+
moduleAssetsInfo = moduleInfoSnapshot == null ? void 0 : (_moduleInfoSnapshot_modules = moduleInfoSnapshot.modules) == null ? void 0 : _moduleInfoSnapshot_modules.reduce((assets, moduleAssetInfo)=>{
|
|
2117
|
+
if ((normalizedPreloadExposes == null ? void 0 : normalizedPreloadExposes.indexOf(moduleAssetInfo.moduleName)) !== -1) {
|
|
2118
|
+
assets.push(moduleAssetInfo);
|
|
2119
|
+
}
|
|
2120
|
+
return assets;
|
|
2121
|
+
}, []);
|
|
2122
|
+
}
|
|
2123
|
+
function handleAssets(assets) {
|
|
2124
|
+
const assetsRes = assets.map((asset)=>getResourceUrl(moduleInfoSnapshot, asset));
|
|
2125
|
+
if (preloadConfig.filter) {
|
|
2126
|
+
return assetsRes.filter(preloadConfig.filter);
|
|
2127
|
+
}
|
|
2128
|
+
return assetsRes;
|
|
2129
|
+
}
|
|
2130
|
+
if (moduleAssetsInfo) {
|
|
2131
|
+
const assetsLength = moduleAssetsInfo.length;
|
|
2132
|
+
for(let index = 0; index < assetsLength; index++){
|
|
2133
|
+
const assetsInfo = moduleAssetsInfo[index];
|
|
2134
|
+
const exposeFullPath = `${remoteInfo.name}/${assetsInfo.moduleName}`;
|
|
2135
|
+
origin.remoteHandler.hooks.lifecycle.handlePreloadModule.emit({
|
|
2136
|
+
id: assetsInfo.moduleName === '.' ? remoteInfo.name : exposeFullPath,
|
|
2137
|
+
name: remoteInfo.name,
|
|
2138
|
+
remoteSnapshot: moduleInfoSnapshot,
|
|
2139
|
+
preloadConfig,
|
|
2140
|
+
remote: remoteInfo,
|
|
2141
|
+
origin
|
|
2142
|
+
});
|
|
2143
|
+
const preloaded = getPreloaded(exposeFullPath);
|
|
2144
|
+
if (preloaded) {
|
|
2145
|
+
continue;
|
|
2146
|
+
}
|
|
2147
|
+
if (preloadConfig.resourceCategory === 'all') {
|
|
2148
|
+
cssAssets.push(...handleAssets(assetsInfo.assets.css.async));
|
|
2149
|
+
cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
|
|
2150
|
+
jsAssets.push(...handleAssets(assetsInfo.assets.js.async));
|
|
2151
|
+
jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
|
|
2152
|
+
} else if (preloadConfig.resourceCategory = 'sync') {
|
|
2153
|
+
cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
|
|
2154
|
+
jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
|
|
2155
|
+
}
|
|
2156
|
+
setPreloaded(exposeFullPath);
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
}, true, memo, remoteSnapshot);
|
|
2160
|
+
if (remoteSnapshot.shared) {
|
|
2161
|
+
const collectSharedAssets = (shareInfo, snapshotShared)=>{
|
|
2162
|
+
const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare);
|
|
2163
|
+
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
2164
|
+
snapshotShared.assets.js.sync.forEach((asset)=>{
|
|
2165
|
+
loadedSharedJsAssets.add(asset);
|
|
2166
|
+
});
|
|
2167
|
+
snapshotShared.assets.css.sync.forEach((asset)=>{
|
|
2168
|
+
loadedSharedCssAssets.add(asset);
|
|
2169
|
+
});
|
|
2170
|
+
}
|
|
2171
|
+
};
|
|
2172
|
+
remoteSnapshot.shared.forEach((shared)=>{
|
|
2173
|
+
var _options_shared;
|
|
2174
|
+
const shareInfos = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
|
|
2175
|
+
if (!shareInfos) {
|
|
2176
|
+
return;
|
|
2177
|
+
}
|
|
2178
|
+
const sharedOptions = shared.version ? shareInfos.find((s)=>s.version === shared.version) : shareInfos;
|
|
2179
|
+
if (!sharedOptions) {
|
|
2180
|
+
return;
|
|
2181
|
+
}
|
|
2182
|
+
const arrayShareInfo = arrayOptions(sharedOptions);
|
|
2183
|
+
arrayShareInfo.forEach((s)=>{
|
|
2184
|
+
collectSharedAssets(s, shared);
|
|
2185
|
+
});
|
|
2186
|
+
});
|
|
2187
|
+
}
|
|
2188
|
+
const needPreloadJsAssets = jsAssets.filter((asset)=>!loadedSharedJsAssets.has(asset));
|
|
2189
|
+
const needPreloadCssAssets = cssAssets.filter((asset)=>!loadedSharedCssAssets.has(asset));
|
|
2190
|
+
return {
|
|
2191
|
+
cssAssets: needPreloadCssAssets,
|
|
2192
|
+
jsAssetsWithoutEntry: needPreloadJsAssets,
|
|
2193
|
+
entryAssets
|
|
2194
|
+
};
|
|
2195
|
+
}
|
|
2196
|
+
const generatePreloadAssetsPlugin = function() {
|
|
2197
|
+
return {
|
|
2198
|
+
name: 'generate-preload-assets-plugin',
|
|
2199
|
+
async generatePreloadAssets (args) {
|
|
2200
|
+
const { origin, preloadOptions, remoteInfo, remote, globalSnapshot, remoteSnapshot } = args;
|
|
2201
|
+
if (isRemoteInfoWithEntry(remote) && isPureRemoteEntry(remote)) {
|
|
2202
|
+
return {
|
|
2203
|
+
cssAssets: [],
|
|
2204
|
+
jsAssetsWithoutEntry: [],
|
|
2205
|
+
entryAssets: [
|
|
2206
|
+
{
|
|
2207
|
+
name: remote.name,
|
|
2208
|
+
url: remote.entry,
|
|
2209
|
+
moduleInfo: {
|
|
2210
|
+
name: remoteInfo.name,
|
|
2211
|
+
entry: remote.entry,
|
|
2212
|
+
type: 'global',
|
|
2213
|
+
entryGlobalName: '',
|
|
2214
|
+
shareScope: ''
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
]
|
|
2218
|
+
};
|
|
2219
|
+
}
|
|
2220
|
+
assignRemoteInfo(remoteInfo, remoteSnapshot);
|
|
2221
|
+
const assets = generatePreloadAssets(origin, preloadOptions, remoteInfo, globalSnapshot, remoteSnapshot);
|
|
2222
|
+
return assets;
|
|
2223
|
+
}
|
|
2224
|
+
};
|
|
2225
|
+
};
|
|
2226
|
+
function getGlobalRemoteInfo(moduleInfo, origin) {
|
|
2227
|
+
const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({
|
|
2228
|
+
name: origin.options.name,
|
|
2229
|
+
version: origin.options.version
|
|
2230
|
+
});
|
|
2231
|
+
const globalRemoteInfo = hostGlobalSnapshot && 'remotesInfo' in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;
|
|
2232
|
+
if (globalRemoteInfo && globalRemoteInfo.matchedVersion) {
|
|
2233
|
+
return {
|
|
2234
|
+
hostGlobalSnapshot,
|
|
2235
|
+
globalSnapshot: getGlobalSnapshot(),
|
|
2236
|
+
remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
|
|
2237
|
+
name: moduleInfo.name,
|
|
2238
|
+
version: globalRemoteInfo.matchedVersion
|
|
2239
|
+
})
|
|
2240
|
+
};
|
|
2241
|
+
}
|
|
2242
|
+
return {
|
|
2243
|
+
hostGlobalSnapshot: undefined,
|
|
2244
|
+
globalSnapshot: getGlobalSnapshot(),
|
|
2245
|
+
remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
|
|
2246
|
+
name: moduleInfo.name,
|
|
2247
|
+
version: 'version' in moduleInfo ? moduleInfo.version : undefined
|
|
2248
|
+
})
|
|
2249
|
+
};
|
|
2250
|
+
}
|
|
2251
|
+
class SnapshotHandler {
|
|
2252
|
+
async loadSnapshot(moduleInfo) {
|
|
2253
|
+
const { options } = this.HostInstance;
|
|
2254
|
+
const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } = this.getGlobalRemoteInfo(moduleInfo);
|
|
2255
|
+
const { remoteSnapshot: globalRemoteSnapshot, globalSnapshot: globalSnapshotRes } = await this.hooks.lifecycle.loadSnapshot.emit({
|
|
2256
|
+
options,
|
|
2257
|
+
moduleInfo,
|
|
2258
|
+
hostGlobalSnapshot,
|
|
2259
|
+
remoteSnapshot,
|
|
2260
|
+
globalSnapshot
|
|
2261
|
+
});
|
|
2262
|
+
return {
|
|
2263
|
+
remoteSnapshot: globalRemoteSnapshot,
|
|
2264
|
+
globalSnapshot: globalSnapshotRes
|
|
2265
|
+
};
|
|
2266
|
+
}
|
|
2267
|
+
async loadRemoteSnapshotInfo(moduleInfo) {
|
|
2268
|
+
const { options } = this.HostInstance;
|
|
2269
|
+
await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({
|
|
2270
|
+
options,
|
|
2271
|
+
moduleInfo
|
|
2272
|
+
});
|
|
2273
|
+
let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({
|
|
2274
|
+
name: this.HostInstance.options.name,
|
|
2275
|
+
version: this.HostInstance.options.version
|
|
2276
|
+
});
|
|
2277
|
+
if (!hostSnapshot) {
|
|
2278
|
+
hostSnapshot = {
|
|
2279
|
+
version: this.HostInstance.options.version || '',
|
|
2280
|
+
remoteEntry: '',
|
|
2281
|
+
remotesInfo: {}
|
|
2282
|
+
};
|
|
2283
|
+
addGlobalSnapshot({
|
|
2284
|
+
[this.HostInstance.options.name]: hostSnapshot
|
|
2285
|
+
});
|
|
2286
|
+
}
|
|
2287
|
+
if (hostSnapshot && 'remotesInfo' in hostSnapshot && !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
|
|
2288
|
+
if ('version' in moduleInfo || 'entry' in moduleInfo) {
|
|
2289
|
+
hostSnapshot.remotesInfo = _({}, hostSnapshot == null ? void 0 : hostSnapshot.remotesInfo, {
|
|
2290
|
+
[moduleInfo.name]: {
|
|
2291
|
+
matchedVersion: 'version' in moduleInfo ? moduleInfo.version : moduleInfo.entry
|
|
2292
|
+
}
|
|
2293
|
+
});
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } = this.getGlobalRemoteInfo(moduleInfo);
|
|
2297
|
+
const { remoteSnapshot: globalRemoteSnapshot, globalSnapshot: globalSnapshotRes } = await this.hooks.lifecycle.loadSnapshot.emit({
|
|
2298
|
+
options,
|
|
2299
|
+
moduleInfo,
|
|
2300
|
+
hostGlobalSnapshot,
|
|
2301
|
+
remoteSnapshot,
|
|
2302
|
+
globalSnapshot
|
|
2303
|
+
});
|
|
2304
|
+
if (globalRemoteSnapshot) {
|
|
2305
|
+
if (isManifestProvider(globalRemoteSnapshot)) {
|
|
2306
|
+
const remoteEntry = isBrowserEnv() ? globalRemoteSnapshot.remoteEntry : globalRemoteSnapshot.ssrRemoteEntry || globalRemoteSnapshot.remoteEntry || '';
|
|
2307
|
+
const moduleSnapshot = await this.getManifestJson(remoteEntry, moduleInfo, {});
|
|
2308
|
+
const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(_({}, moduleInfo, {
|
|
2309
|
+
entry: remoteEntry
|
|
2310
|
+
}), moduleSnapshot);
|
|
2311
|
+
return {
|
|
2312
|
+
remoteSnapshot: moduleSnapshot,
|
|
2313
|
+
globalSnapshot: globalSnapshotRes
|
|
2314
|
+
};
|
|
2315
|
+
} else {
|
|
2316
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
2317
|
+
options: this.HostInstance.options,
|
|
2318
|
+
moduleInfo,
|
|
2319
|
+
remoteSnapshot: globalRemoteSnapshot,
|
|
2320
|
+
from: 'global'
|
|
2321
|
+
});
|
|
2322
|
+
return {
|
|
2323
|
+
remoteSnapshot: remoteSnapshotRes,
|
|
2324
|
+
globalSnapshot: globalSnapshotRes
|
|
2325
|
+
};
|
|
2326
|
+
}
|
|
2327
|
+
} else {
|
|
2328
|
+
if (isRemoteInfoWithEntry(moduleInfo)) {
|
|
2329
|
+
const moduleSnapshot = await this.getManifestJson(moduleInfo.entry, moduleInfo, {});
|
|
2330
|
+
const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(moduleInfo, moduleSnapshot);
|
|
2331
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
2332
|
+
options: this.HostInstance.options,
|
|
2333
|
+
moduleInfo,
|
|
2334
|
+
remoteSnapshot: moduleSnapshot,
|
|
2335
|
+
from: 'global'
|
|
2336
|
+
});
|
|
2337
|
+
return {
|
|
2338
|
+
remoteSnapshot: remoteSnapshotRes,
|
|
2339
|
+
globalSnapshot: globalSnapshotRes
|
|
2340
|
+
};
|
|
2341
|
+
} else {
|
|
2342
|
+
error(`
|
|
2343
|
+
Cannot get remoteSnapshot with the name: '${moduleInfo.name}', version: '${moduleInfo.version}' from __FEDERATION__.moduleInfo. The following reasons may be causing the problem:\n
|
|
2344
|
+
1. The Deploy platform did not deliver the correct data. You can use __FEDERATION__.moduleInfo to check the remoteInfo.\n
|
|
2345
|
+
2. The remote '${moduleInfo.name}' version '${moduleInfo.version}' is not released.\n
|
|
2346
|
+
The transformed module info: ${JSON.stringify(globalSnapshotRes)}
|
|
2347
|
+
`);
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
getGlobalRemoteInfo(moduleInfo) {
|
|
2352
|
+
return getGlobalRemoteInfo(moduleInfo, this.HostInstance);
|
|
2353
|
+
}
|
|
2354
|
+
async getManifestJson(manifestUrl, moduleInfo, extraOptions) {
|
|
2355
|
+
const getManifest = async ()=>{
|
|
2356
|
+
let manifestJson = this.manifestCache.get(manifestUrl);
|
|
2357
|
+
if (manifestJson) {
|
|
2358
|
+
return manifestJson;
|
|
2359
|
+
}
|
|
2360
|
+
try {
|
|
2361
|
+
let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {});
|
|
2362
|
+
if (!res || !(res instanceof Response)) {
|
|
2363
|
+
res = await fetch(manifestUrl, {});
|
|
2364
|
+
}
|
|
2365
|
+
manifestJson = await res.json();
|
|
2366
|
+
assert(manifestJson.metaData && manifestJson.exposes && manifestJson.shared, `${manifestUrl} is not a federation manifest`);
|
|
2367
|
+
this.manifestCache.set(manifestUrl, manifestJson);
|
|
2368
|
+
return manifestJson;
|
|
2369
|
+
} catch (err) {
|
|
2370
|
+
delete this.manifestLoading[manifestUrl];
|
|
2371
|
+
error(`Failed to get manifestJson for ${moduleInfo.name}. The manifest URL is ${manifestUrl}. Please ensure that the manifestUrl is accessible.
|
|
2372
|
+
\n Error message:
|
|
2373
|
+
\n ${err}`);
|
|
2374
|
+
}
|
|
2375
|
+
};
|
|
2376
|
+
const asyncLoadProcess = async ()=>{
|
|
2377
|
+
const manifestJson = await getManifest();
|
|
2378
|
+
const remoteSnapshot = generateSnapshotFromManifest(manifestJson, {
|
|
2379
|
+
version: manifestUrl
|
|
2380
|
+
});
|
|
2381
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
2382
|
+
options: this.HostInstance.options,
|
|
2383
|
+
moduleInfo,
|
|
2384
|
+
manifestJson,
|
|
2385
|
+
remoteSnapshot,
|
|
2386
|
+
manifestUrl,
|
|
2387
|
+
from: 'manifest'
|
|
2388
|
+
});
|
|
2389
|
+
return remoteSnapshotRes;
|
|
2390
|
+
};
|
|
2391
|
+
if (!this.manifestLoading[manifestUrl]) {
|
|
2392
|
+
this.manifestLoading[manifestUrl] = asyncLoadProcess().then((res)=>res);
|
|
2393
|
+
}
|
|
2394
|
+
return this.manifestLoading[manifestUrl];
|
|
2395
|
+
}
|
|
2396
|
+
constructor(HostInstance){
|
|
2397
|
+
this.loadingHostSnapshot = null;
|
|
2398
|
+
this.manifestCache = new Map();
|
|
2399
|
+
this.hooks = new PluginSystem({
|
|
2400
|
+
beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
|
|
2401
|
+
loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
|
|
2402
|
+
loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot')
|
|
2403
|
+
});
|
|
2404
|
+
this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
|
|
2405
|
+
this.HostInstance = HostInstance;
|
|
2406
|
+
this.loaderHook = HostInstance.loaderHook;
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
class SharedHandler {
|
|
2410
|
+
registerShared(globalOptions, userOptions) {
|
|
2411
|
+
const { shareInfos, shared } = formatShareConfigs(globalOptions, userOptions);
|
|
2412
|
+
const sharedKeys = Object.keys(shareInfos);
|
|
2413
|
+
sharedKeys.forEach((sharedKey)=>{
|
|
2414
|
+
const sharedVals = shareInfos[sharedKey];
|
|
2415
|
+
sharedVals.forEach((sharedVal)=>{
|
|
2416
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
|
|
2417
|
+
if (!registeredShared && sharedVal && sharedVal.lib) {
|
|
2418
|
+
this.setShared({
|
|
2419
|
+
pkgName: sharedKey,
|
|
2420
|
+
lib: sharedVal.lib,
|
|
2421
|
+
get: sharedVal.get,
|
|
2422
|
+
loaded: true,
|
|
2423
|
+
shared: sharedVal,
|
|
2424
|
+
from: userOptions.name
|
|
2425
|
+
});
|
|
2426
|
+
}
|
|
2427
|
+
});
|
|
2428
|
+
});
|
|
2429
|
+
return {
|
|
2430
|
+
shareInfos,
|
|
2431
|
+
shared
|
|
2432
|
+
};
|
|
2433
|
+
}
|
|
2434
|
+
async loadShare(pkgName, extraOptions) {
|
|
2435
|
+
const { host } = this;
|
|
2436
|
+
const shareInfo = getTargetSharedOptions({
|
|
2437
|
+
pkgName,
|
|
2438
|
+
extraOptions,
|
|
2439
|
+
shareInfos: host.options.shared
|
|
2440
|
+
});
|
|
2441
|
+
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
2442
|
+
await Promise.all(shareInfo.scope.map(async (shareScope)=>{
|
|
2443
|
+
await Promise.all(this.initializeSharing(shareScope, shareInfo.strategy));
|
|
2444
|
+
return;
|
|
2445
|
+
}));
|
|
2446
|
+
}
|
|
2447
|
+
const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
|
|
2448
|
+
pkgName,
|
|
2449
|
+
shareInfo,
|
|
2450
|
+
shared: host.options.shared,
|
|
2451
|
+
origin: host
|
|
2452
|
+
});
|
|
2453
|
+
const { shareInfo: shareInfoRes } = loadShareRes;
|
|
2454
|
+
assert(shareInfoRes, `Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
|
|
2455
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
2456
|
+
const addUseIn = (shared)=>{
|
|
2457
|
+
if (!shared.useIn) {
|
|
2458
|
+
shared.useIn = [];
|
|
2459
|
+
}
|
|
2460
|
+
addUniqueItem(shared.useIn, host.options.name);
|
|
2461
|
+
};
|
|
2462
|
+
if (registeredShared && registeredShared.lib) {
|
|
2463
|
+
addUseIn(registeredShared);
|
|
2464
|
+
return registeredShared.lib;
|
|
2465
|
+
} else if (registeredShared && registeredShared.loading && !registeredShared.loaded) {
|
|
2466
|
+
const factory = await registeredShared.loading;
|
|
2467
|
+
registeredShared.loaded = true;
|
|
2468
|
+
if (!registeredShared.lib) {
|
|
2469
|
+
registeredShared.lib = factory;
|
|
2470
|
+
}
|
|
2471
|
+
addUseIn(registeredShared);
|
|
2472
|
+
return factory;
|
|
2473
|
+
} else if (registeredShared) {
|
|
2474
|
+
const asyncLoadProcess = async ()=>{
|
|
2475
|
+
const factory = await registeredShared.get();
|
|
2476
|
+
shareInfoRes.lib = factory;
|
|
2477
|
+
shareInfoRes.loaded = true;
|
|
2478
|
+
addUseIn(shareInfoRes);
|
|
2479
|
+
const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
2480
|
+
if (gShared) {
|
|
2481
|
+
gShared.lib = factory;
|
|
2482
|
+
gShared.loaded = true;
|
|
2483
|
+
}
|
|
2484
|
+
return factory;
|
|
2485
|
+
};
|
|
2486
|
+
const loading = asyncLoadProcess();
|
|
2487
|
+
this.setShared({
|
|
2488
|
+
pkgName,
|
|
2489
|
+
loaded: false,
|
|
2490
|
+
shared: registeredShared,
|
|
2491
|
+
from: host.options.name,
|
|
2492
|
+
lib: null,
|
|
2493
|
+
loading
|
|
2494
|
+
});
|
|
2495
|
+
return loading;
|
|
2496
|
+
} else {
|
|
2497
|
+
if (extraOptions == null ? void 0 : extraOptions.customShareInfo) {
|
|
2498
|
+
return false;
|
|
2499
|
+
}
|
|
2500
|
+
const asyncLoadProcess = async ()=>{
|
|
2501
|
+
const factory = await shareInfoRes.get();
|
|
2502
|
+
shareInfoRes.lib = factory;
|
|
2503
|
+
shareInfoRes.loaded = true;
|
|
2504
|
+
addUseIn(shareInfoRes);
|
|
2505
|
+
const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
2506
|
+
if (gShared) {
|
|
2507
|
+
gShared.lib = factory;
|
|
2508
|
+
gShared.loaded = true;
|
|
2509
|
+
}
|
|
2510
|
+
return factory;
|
|
2511
|
+
};
|
|
2512
|
+
const loading = asyncLoadProcess();
|
|
2513
|
+
this.setShared({
|
|
2514
|
+
pkgName,
|
|
2515
|
+
loaded: false,
|
|
2516
|
+
shared: shareInfoRes,
|
|
2517
|
+
from: host.options.name,
|
|
2518
|
+
lib: null,
|
|
2519
|
+
loading
|
|
2520
|
+
});
|
|
2521
|
+
return loading;
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
initializeSharing(shareScopeName = DEFAULT_SCOPE, strategy) {
|
|
2525
|
+
const { host } = this;
|
|
2526
|
+
const shareScope = this.shareScopeMap;
|
|
2527
|
+
const hostName = host.options.name;
|
|
2528
|
+
if (!shareScope[shareScopeName]) {
|
|
2529
|
+
shareScope[shareScopeName] = {};
|
|
2530
|
+
}
|
|
2531
|
+
const scope = shareScope[shareScopeName];
|
|
2532
|
+
const register = (name, shared)=>{
|
|
2533
|
+
var _activeVersion_shareConfig;
|
|
2534
|
+
const { version, eager } = shared;
|
|
2535
|
+
scope[name] = scope[name] || {};
|
|
2536
|
+
const versions = scope[name];
|
|
2537
|
+
const activeVersion = versions[version];
|
|
2538
|
+
const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || ((_activeVersion_shareConfig = activeVersion.shareConfig) == null ? void 0 : _activeVersion_shareConfig.eager)));
|
|
2539
|
+
if (!activeVersion || activeVersion.strategy !== 'loaded-first' && !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
|
|
2540
|
+
versions[version] = shared;
|
|
2541
|
+
}
|
|
2542
|
+
};
|
|
2543
|
+
const promises = [];
|
|
2544
|
+
const initFn = (mod)=>mod && mod.init && mod.init(shareScope[shareScopeName]);
|
|
2545
|
+
const initRemoteModule = async (key)=>{
|
|
2546
|
+
const { module } = await host.remoteHandler.getRemoteModuleAndOptions({
|
|
2547
|
+
id: key
|
|
2548
|
+
});
|
|
2549
|
+
if (module.getEntry) {
|
|
2550
|
+
const entry = await module.getEntry();
|
|
2551
|
+
if (!module.inited) {
|
|
2552
|
+
initFn(entry);
|
|
2553
|
+
module.inited = true;
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
};
|
|
2557
|
+
Object.keys(host.options.shared).forEach((shareName)=>{
|
|
2558
|
+
const sharedArr = host.options.shared[shareName];
|
|
2559
|
+
sharedArr.forEach((shared)=>{
|
|
2560
|
+
if (shared.scope.includes(shareScopeName)) {
|
|
2561
|
+
register(shareName, shared);
|
|
2562
|
+
}
|
|
2563
|
+
});
|
|
2564
|
+
});
|
|
2565
|
+
if (host.options.shareStrategy === 'version-first' || strategy === 'version-first') {
|
|
2566
|
+
host.options.remotes.forEach((remote)=>{
|
|
2567
|
+
if (remote.shareScope === shareScopeName) {
|
|
2568
|
+
promises.push(initRemoteModule(remote.name));
|
|
2569
|
+
}
|
|
2570
|
+
});
|
|
2571
|
+
}
|
|
2572
|
+
return promises;
|
|
2573
|
+
}
|
|
2574
|
+
loadShareSync(pkgName, extraOptions) {
|
|
2575
|
+
const { host } = this;
|
|
2576
|
+
const shareInfo = getTargetSharedOptions({
|
|
2577
|
+
pkgName,
|
|
2578
|
+
extraOptions,
|
|
2579
|
+
shareInfos: host.options.shared
|
|
2580
|
+
});
|
|
2581
|
+
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
2582
|
+
shareInfo.scope.forEach((shareScope)=>{
|
|
2583
|
+
this.initializeSharing(shareScope, shareInfo.strategy);
|
|
2584
|
+
});
|
|
2585
|
+
}
|
|
2586
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfo, this.hooks.lifecycle.resolveShare);
|
|
2587
|
+
const addUseIn = (shared)=>{
|
|
2588
|
+
if (!shared.useIn) {
|
|
2589
|
+
shared.useIn = [];
|
|
2590
|
+
}
|
|
2591
|
+
addUniqueItem(shared.useIn, host.options.name);
|
|
2592
|
+
};
|
|
2593
|
+
if (registeredShared) {
|
|
2594
|
+
if (typeof registeredShared.lib === 'function') {
|
|
2595
|
+
addUseIn(registeredShared);
|
|
2596
|
+
if (!registeredShared.loaded) {
|
|
2597
|
+
registeredShared.loaded = true;
|
|
2598
|
+
if (registeredShared.from === host.options.name) {
|
|
2599
|
+
shareInfo.loaded = true;
|
|
2600
|
+
}
|
|
2601
|
+
}
|
|
2602
|
+
return registeredShared.lib;
|
|
2603
|
+
}
|
|
2604
|
+
if (typeof registeredShared.get === 'function') {
|
|
2605
|
+
const module = registeredShared.get();
|
|
2606
|
+
if (!(module instanceof Promise)) {
|
|
2607
|
+
addUseIn(registeredShared);
|
|
2608
|
+
this.setShared({
|
|
2609
|
+
pkgName,
|
|
2610
|
+
loaded: true,
|
|
2611
|
+
from: host.options.name,
|
|
2612
|
+
lib: module,
|
|
2613
|
+
shared: registeredShared
|
|
2614
|
+
});
|
|
2615
|
+
return module;
|
|
2616
|
+
}
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2619
|
+
if (shareInfo.lib) {
|
|
2620
|
+
if (!shareInfo.loaded) {
|
|
2621
|
+
shareInfo.loaded = true;
|
|
2622
|
+
}
|
|
2623
|
+
return shareInfo.lib;
|
|
2624
|
+
}
|
|
2625
|
+
if (shareInfo.get) {
|
|
2626
|
+
const module = shareInfo.get();
|
|
2627
|
+
if (module instanceof Promise) {
|
|
2628
|
+
throw new Error(`
|
|
2629
|
+
The loadShareSync function was unable to load ${pkgName}. The ${pkgName} could not be found in ${host.options.name}.
|
|
2630
|
+
Possible reasons for failure: \n
|
|
2631
|
+
1. The ${pkgName} share was registered with the 'get' attribute, but loadShare was not used beforehand.\n
|
|
2632
|
+
2. The ${pkgName} share was not registered with the 'lib' attribute.\n
|
|
2633
|
+
`);
|
|
2634
|
+
}
|
|
2635
|
+
shareInfo.lib = module;
|
|
2636
|
+
this.setShared({
|
|
2637
|
+
pkgName,
|
|
2638
|
+
loaded: true,
|
|
2639
|
+
from: host.options.name,
|
|
2640
|
+
lib: shareInfo.lib,
|
|
2641
|
+
shared: shareInfo
|
|
2642
|
+
});
|
|
2643
|
+
return shareInfo.lib;
|
|
2644
|
+
}
|
|
2645
|
+
throw new Error(`
|
|
2646
|
+
The loadShareSync function was unable to load ${pkgName}. The ${pkgName} could not be found in ${host.options.name}.
|
|
2647
|
+
Possible reasons for failure: \n
|
|
2648
|
+
1. The ${pkgName} share was registered with the 'get' attribute, but loadShare was not used beforehand.\n
|
|
2649
|
+
2. The ${pkgName} share was not registered with the 'lib' attribute.\n
|
|
2650
|
+
`);
|
|
2651
|
+
}
|
|
2652
|
+
initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
|
|
2653
|
+
const { host } = this;
|
|
2654
|
+
this.shareScopeMap[scopeName] = shareScope;
|
|
2655
|
+
this.hooks.lifecycle.initContainerShareScopeMap.emit({
|
|
2656
|
+
shareScope,
|
|
2657
|
+
options: host.options,
|
|
2658
|
+
origin: host,
|
|
2659
|
+
scopeName,
|
|
2660
|
+
hostShareScopeMap: extraOptions.hostShareScopeMap
|
|
2661
|
+
});
|
|
2662
|
+
}
|
|
2663
|
+
setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
|
|
2664
|
+
const { version, scope = 'default' } = shared, shareInfo = _$1(shared, [
|
|
2665
|
+
"version",
|
|
2666
|
+
"scope"
|
|
2667
|
+
]);
|
|
2668
|
+
const scopes = Array.isArray(scope) ? scope : [
|
|
2669
|
+
scope
|
|
2670
|
+
];
|
|
2671
|
+
scopes.forEach((sc)=>{
|
|
2672
|
+
if (!this.shareScopeMap[sc]) {
|
|
2673
|
+
this.shareScopeMap[sc] = {};
|
|
2674
|
+
}
|
|
2675
|
+
if (!this.shareScopeMap[sc][pkgName]) {
|
|
2676
|
+
this.shareScopeMap[sc][pkgName] = {};
|
|
2677
|
+
}
|
|
2678
|
+
if (this.shareScopeMap[sc][pkgName][version]) {
|
|
2679
|
+
return;
|
|
2680
|
+
}
|
|
2681
|
+
this.shareScopeMap[sc][pkgName][version] = _({
|
|
2682
|
+
version,
|
|
2683
|
+
scope: [
|
|
2684
|
+
'default'
|
|
2685
|
+
]
|
|
2686
|
+
}, shareInfo, {
|
|
2687
|
+
lib,
|
|
2688
|
+
loaded,
|
|
2689
|
+
loading
|
|
2690
|
+
});
|
|
2691
|
+
if (get) {
|
|
2692
|
+
this.shareScopeMap[sc][pkgName][version].get = get;
|
|
2693
|
+
}
|
|
2694
|
+
});
|
|
2695
|
+
}
|
|
2696
|
+
_setGlobalShareScopeMap(hostOptions) {
|
|
2697
|
+
const globalShareScopeMap = getGlobalShareScope();
|
|
2698
|
+
const identifier = hostOptions.id || hostOptions.name;
|
|
2699
|
+
if (identifier && !globalShareScopeMap[identifier]) {
|
|
2700
|
+
globalShareScopeMap[identifier] = this.shareScopeMap;
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
constructor(host){
|
|
2704
|
+
this.hooks = new PluginSystem({
|
|
2705
|
+
afterResolve: new AsyncWaterfallHook('afterResolve'),
|
|
2706
|
+
beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
|
|
2707
|
+
loadShare: new AsyncHook(),
|
|
2708
|
+
resolveShare: new SyncWaterfallHook('resolveShare'),
|
|
2709
|
+
initContainerShareScopeMap: new SyncWaterfallHook('initContainerShareScopeMap')
|
|
2710
|
+
});
|
|
2711
|
+
this.host = host;
|
|
2712
|
+
this.shareScopeMap = {};
|
|
2713
|
+
this._setGlobalShareScopeMap(host.options);
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
class RemoteHandler {
|
|
2717
|
+
formatAndRegisterRemote(globalOptions, userOptions) {
|
|
2718
|
+
const userRemotes = userOptions.remotes || [];
|
|
2719
|
+
return userRemotes.reduce((res, remote)=>{
|
|
2720
|
+
this.registerRemote(remote, res, {
|
|
2721
|
+
force: false
|
|
2722
|
+
});
|
|
2723
|
+
return res;
|
|
2724
|
+
}, globalOptions.remotes);
|
|
2725
|
+
}
|
|
2726
|
+
setIdToRemoteMap(id, remoteMatchInfo) {
|
|
2727
|
+
const { remote, expose } = remoteMatchInfo;
|
|
2728
|
+
const { name, alias } = remote;
|
|
2729
|
+
this.idToRemoteMap[id] = {
|
|
2730
|
+
name: remote.name,
|
|
2731
|
+
expose
|
|
2732
|
+
};
|
|
2733
|
+
if (alias && id.startsWith(name)) {
|
|
2734
|
+
const idWithAlias = id.replace(name, alias);
|
|
2735
|
+
this.idToRemoteMap[idWithAlias] = {
|
|
2736
|
+
name: remote.name,
|
|
2737
|
+
expose
|
|
2738
|
+
};
|
|
2739
|
+
return;
|
|
2740
|
+
}
|
|
2741
|
+
if (alias && id.startsWith(alias)) {
|
|
2742
|
+
const idWithName = id.replace(alias, name);
|
|
2743
|
+
this.idToRemoteMap[idWithName] = {
|
|
2744
|
+
name: remote.name,
|
|
2745
|
+
expose
|
|
2746
|
+
};
|
|
2747
|
+
}
|
|
2748
|
+
}
|
|
2749
|
+
async loadRemote(id, options) {
|
|
2750
|
+
const { host } = this;
|
|
2751
|
+
try {
|
|
2752
|
+
const { loadFactory = true } = options || {
|
|
2753
|
+
loadFactory: true
|
|
2754
|
+
};
|
|
2755
|
+
const { module, moduleOptions, remoteMatchInfo } = await this.getRemoteModuleAndOptions({
|
|
2756
|
+
id
|
|
2757
|
+
});
|
|
2758
|
+
const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
|
|
2759
|
+
const moduleOrFactory = await module.get(idRes, expose, options);
|
|
2760
|
+
const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
|
|
2761
|
+
id: idRes,
|
|
2762
|
+
pkgNameOrAlias,
|
|
2763
|
+
expose,
|
|
2764
|
+
exposeModule: loadFactory ? moduleOrFactory : undefined,
|
|
2765
|
+
exposeModuleFactory: loadFactory ? undefined : moduleOrFactory,
|
|
2766
|
+
remote,
|
|
2767
|
+
options: moduleOptions,
|
|
2768
|
+
moduleInstance: module,
|
|
2769
|
+
origin: host
|
|
2770
|
+
});
|
|
2771
|
+
this.setIdToRemoteMap(id, remoteMatchInfo);
|
|
2772
|
+
if (typeof moduleWrapper === 'function') {
|
|
2773
|
+
return moduleWrapper;
|
|
2774
|
+
}
|
|
2775
|
+
return moduleOrFactory;
|
|
2776
|
+
} catch (error) {
|
|
2777
|
+
const { from = 'runtime' } = options || {
|
|
2778
|
+
from: 'runtime'
|
|
2779
|
+
};
|
|
2780
|
+
const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
2781
|
+
id,
|
|
2782
|
+
error,
|
|
2783
|
+
from,
|
|
2784
|
+
lifecycle: 'onLoad',
|
|
2785
|
+
origin: host
|
|
2786
|
+
});
|
|
2787
|
+
if (!failOver) {
|
|
2788
|
+
throw error;
|
|
2789
|
+
}
|
|
2790
|
+
return failOver;
|
|
2791
|
+
}
|
|
2792
|
+
}
|
|
2793
|
+
async preloadRemote(preloadOptions) {
|
|
2794
|
+
const { host } = this;
|
|
2795
|
+
await this.hooks.lifecycle.beforePreloadRemote.emit({
|
|
2796
|
+
preloadOps: preloadOptions,
|
|
2797
|
+
options: host.options,
|
|
2798
|
+
origin: host
|
|
2799
|
+
});
|
|
2800
|
+
const preloadOps = formatPreloadArgs(host.options.remotes, preloadOptions);
|
|
2801
|
+
await Promise.all(preloadOps.map(async (ops)=>{
|
|
2802
|
+
const { remote } = ops;
|
|
2803
|
+
const remoteInfo = getRemoteInfo(remote);
|
|
2804
|
+
const { globalSnapshot, remoteSnapshot } = await host.snapshotHandler.loadRemoteSnapshotInfo(remote);
|
|
2805
|
+
const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
|
|
2806
|
+
origin: host,
|
|
2807
|
+
preloadOptions: ops,
|
|
2808
|
+
remote,
|
|
2809
|
+
remoteInfo,
|
|
2810
|
+
globalSnapshot,
|
|
2811
|
+
remoteSnapshot
|
|
2812
|
+
});
|
|
2813
|
+
if (!assets) {
|
|
2814
|
+
return;
|
|
2815
|
+
}
|
|
2816
|
+
preloadAssets(remoteInfo, host, assets);
|
|
2817
|
+
}));
|
|
2818
|
+
}
|
|
2819
|
+
registerRemotes(remotes, options) {
|
|
2820
|
+
const { host } = this;
|
|
2821
|
+
remotes.forEach((remote)=>{
|
|
2822
|
+
this.registerRemote(remote, host.options.remotes, {
|
|
2823
|
+
force: options == null ? void 0 : options.force
|
|
2824
|
+
});
|
|
2825
|
+
});
|
|
2826
|
+
}
|
|
2827
|
+
async getRemoteModuleAndOptions(options) {
|
|
2828
|
+
const { host } = this;
|
|
2829
|
+
const { id } = options;
|
|
2830
|
+
let loadRemoteArgs;
|
|
2831
|
+
try {
|
|
2832
|
+
loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({
|
|
2833
|
+
id,
|
|
2834
|
+
options: host.options,
|
|
2835
|
+
origin: host
|
|
2836
|
+
});
|
|
2837
|
+
} catch (error) {
|
|
2838
|
+
loadRemoteArgs = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
2839
|
+
id,
|
|
2840
|
+
options: host.options,
|
|
2841
|
+
origin: host,
|
|
2842
|
+
from: 'runtime',
|
|
2843
|
+
error,
|
|
2844
|
+
lifecycle: 'beforeRequest'
|
|
2845
|
+
});
|
|
2846
|
+
if (!loadRemoteArgs) {
|
|
2847
|
+
throw error;
|
|
2848
|
+
}
|
|
2849
|
+
}
|
|
2850
|
+
const { id: idRes } = loadRemoteArgs;
|
|
2851
|
+
const remoteSplitInfo = matchRemoteWithNameAndExpose(host.options.remotes, idRes);
|
|
2852
|
+
assert(remoteSplitInfo, `
|
|
2853
|
+
Unable to locate ${idRes} in ${host.options.name}. Potential reasons for failure include:\n
|
|
2854
|
+
1. ${idRes} was not included in the 'remotes' parameter of ${host.options.name || 'the host'}.\n
|
|
2855
|
+
2. ${idRes} could not be found in the 'remotes' of ${host.options.name} with either 'name' or 'alias' attributes.
|
|
2856
|
+
3. ${idRes} is not online, injected, or loaded.
|
|
2857
|
+
4. ${idRes} cannot be accessed on the expected.
|
|
2858
|
+
5. The 'beforeRequest' hook was provided but did not return the correct 'remoteInfo' when attempting to load ${idRes}.
|
|
2859
|
+
`);
|
|
2860
|
+
const { remote: rawRemote } = remoteSplitInfo;
|
|
2861
|
+
const remoteInfo = getRemoteInfo(rawRemote);
|
|
2862
|
+
const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit(_({
|
|
2863
|
+
id: idRes
|
|
2864
|
+
}, remoteSplitInfo, {
|
|
2865
|
+
options: host.options,
|
|
2866
|
+
origin: host,
|
|
2867
|
+
remoteInfo
|
|
2868
|
+
}));
|
|
2869
|
+
const { remote, expose } = matchInfo;
|
|
2870
|
+
assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
|
|
2871
|
+
let module = host.moduleCache.get(remote.name);
|
|
2872
|
+
const moduleOptions = {
|
|
2873
|
+
host: host,
|
|
2874
|
+
remoteInfo
|
|
2875
|
+
};
|
|
2876
|
+
if (!module) {
|
|
2877
|
+
module = new Module(moduleOptions);
|
|
2878
|
+
host.moduleCache.set(remote.name, module);
|
|
2879
|
+
}
|
|
2880
|
+
return {
|
|
2881
|
+
module,
|
|
2882
|
+
moduleOptions,
|
|
2883
|
+
remoteMatchInfo: matchInfo
|
|
2884
|
+
};
|
|
2885
|
+
}
|
|
2886
|
+
registerRemote(remote, targetRemotes, options) {
|
|
2887
|
+
const { host } = this;
|
|
2888
|
+
const normalizeRemote = ()=>{
|
|
2889
|
+
if (remote.alias) {
|
|
2890
|
+
const findEqual = targetRemotes.find((item)=>{
|
|
2891
|
+
var _item_alias;
|
|
2892
|
+
return remote.alias && (item.name.startsWith(remote.alias) || ((_item_alias = item.alias) == null ? void 0 : _item_alias.startsWith(remote.alias)));
|
|
2893
|
+
});
|
|
2894
|
+
assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
|
|
2895
|
+
}
|
|
2896
|
+
if ('entry' in remote) {
|
|
2897
|
+
if (isBrowserEnv() && !remote.entry.startsWith('http')) {
|
|
2898
|
+
remote.entry = new URL(remote.entry, window.location.origin).href;
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
if (!remote.shareScope) {
|
|
2902
|
+
remote.shareScope = DEFAULT_SCOPE;
|
|
2903
|
+
}
|
|
2904
|
+
if (!remote.type) {
|
|
2905
|
+
remote.type = DEFAULT_REMOTE_TYPE;
|
|
2906
|
+
}
|
|
2907
|
+
};
|
|
2908
|
+
this.hooks.lifecycle.beforeRegisterRemote.emit({
|
|
2909
|
+
remote,
|
|
2910
|
+
origin: host
|
|
2911
|
+
});
|
|
2912
|
+
const registeredRemote = targetRemotes.find((item)=>item.name === remote.name);
|
|
2913
|
+
if (!registeredRemote) {
|
|
2914
|
+
normalizeRemote();
|
|
2915
|
+
targetRemotes.push(remote);
|
|
2916
|
+
this.hooks.lifecycle.registerRemote.emit({
|
|
2917
|
+
remote,
|
|
2918
|
+
origin: host
|
|
2919
|
+
});
|
|
2920
|
+
} else {
|
|
2921
|
+
const messages = [
|
|
2922
|
+
`The remote "${remote.name}" is already registered.`,
|
|
2923
|
+
(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".'
|
|
2924
|
+
];
|
|
2925
|
+
if (options == null ? void 0 : options.force) {
|
|
2926
|
+
this.removeRemote(registeredRemote);
|
|
2927
|
+
normalizeRemote();
|
|
2928
|
+
targetRemotes.push(remote);
|
|
2929
|
+
this.hooks.lifecycle.registerRemote.emit({
|
|
2930
|
+
remote,
|
|
2931
|
+
origin: host
|
|
2932
|
+
});
|
|
2933
|
+
}
|
|
2934
|
+
warn$1(messages.join(' '));
|
|
2935
|
+
}
|
|
2936
|
+
}
|
|
2937
|
+
removeRemote(remote) {
|
|
2938
|
+
try {
|
|
2939
|
+
const { host } = this;
|
|
2940
|
+
const { name } = remote;
|
|
2941
|
+
const remoteIndex = host.options.remotes.findIndex((item)=>item.name === name);
|
|
2942
|
+
if (remoteIndex !== -1) {
|
|
2943
|
+
host.options.remotes.splice(remoteIndex, 1);
|
|
2944
|
+
}
|
|
2945
|
+
const loadedModule = host.moduleCache.get(remote.name);
|
|
2946
|
+
if (loadedModule) {
|
|
2947
|
+
const remoteInfo = loadedModule.remoteInfo;
|
|
2948
|
+
const key = remoteInfo.entryGlobalName;
|
|
2949
|
+
if (globalThis[key]) {
|
|
2950
|
+
var _Object_getOwnPropertyDescriptor;
|
|
2951
|
+
if ((_Object_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(globalThis, key)) == null ? void 0 : _Object_getOwnPropertyDescriptor.configurable) {
|
|
2952
|
+
delete globalThis[key];
|
|
2953
|
+
} else {
|
|
2954
|
+
globalThis[key] = undefined;
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
|
|
2958
|
+
if (globalLoading[remoteEntryUniqueKey]) {
|
|
2959
|
+
delete globalLoading[remoteEntryUniqueKey];
|
|
2960
|
+
}
|
|
2961
|
+
host.snapshotHandler.manifestCache.delete(remoteInfo.entry);
|
|
2962
|
+
let remoteInsId = remoteInfo.buildVersion ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
|
|
2963
|
+
const remoteInsIndex = globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins)=>{
|
|
2964
|
+
if (remoteInfo.buildVersion) {
|
|
2965
|
+
return ins.options.id === remoteInsId;
|
|
2966
|
+
} else {
|
|
2967
|
+
return ins.name === remoteInsId;
|
|
2968
|
+
}
|
|
2969
|
+
});
|
|
2970
|
+
if (remoteInsIndex !== -1) {
|
|
2971
|
+
const remoteIns = globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
|
|
2972
|
+
remoteInsId = remoteIns.options.id || remoteInsId;
|
|
2973
|
+
const globalShareScopeMap = getGlobalShareScope();
|
|
2974
|
+
let isAllSharedNotUsed = true;
|
|
2975
|
+
const needDeleteKeys = [];
|
|
2976
|
+
Object.keys(globalShareScopeMap).forEach((instId)=>{
|
|
2977
|
+
const shareScopeMap = globalShareScopeMap[instId];
|
|
2978
|
+
shareScopeMap && Object.keys(shareScopeMap).forEach((shareScope)=>{
|
|
2979
|
+
const shareScopeVal = shareScopeMap[shareScope];
|
|
2980
|
+
shareScopeVal && Object.keys(shareScopeVal).forEach((shareName)=>{
|
|
2981
|
+
const sharedPkgs = shareScopeVal[shareName];
|
|
2982
|
+
sharedPkgs && Object.keys(sharedPkgs).forEach((shareVersion)=>{
|
|
2983
|
+
const shared = sharedPkgs[shareVersion];
|
|
2984
|
+
if (shared && typeof shared === 'object' && shared.from === remoteInfo.name) {
|
|
2985
|
+
if (shared.loaded || shared.loading) {
|
|
2986
|
+
shared.useIn = shared.useIn.filter((usedHostName)=>usedHostName !== remoteInfo.name);
|
|
2987
|
+
if (shared.useIn.length) {
|
|
2988
|
+
isAllSharedNotUsed = false;
|
|
2989
|
+
} else {
|
|
2990
|
+
needDeleteKeys.push([
|
|
2991
|
+
instId,
|
|
2992
|
+
shareScope,
|
|
2993
|
+
shareName,
|
|
2994
|
+
shareVersion
|
|
2995
|
+
]);
|
|
2996
|
+
}
|
|
2997
|
+
} else {
|
|
2998
|
+
needDeleteKeys.push([
|
|
2999
|
+
instId,
|
|
3000
|
+
shareScope,
|
|
3001
|
+
shareName,
|
|
3002
|
+
shareVersion
|
|
3003
|
+
]);
|
|
3004
|
+
}
|
|
3005
|
+
}
|
|
3006
|
+
});
|
|
3007
|
+
});
|
|
3008
|
+
});
|
|
3009
|
+
});
|
|
3010
|
+
if (isAllSharedNotUsed) {
|
|
3011
|
+
remoteIns.shareScopeMap = {};
|
|
3012
|
+
delete globalShareScopeMap[remoteInsId];
|
|
3013
|
+
}
|
|
3014
|
+
needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion])=>{
|
|
3015
|
+
var _globalShareScopeMap_insId_shareScope_shareName, _globalShareScopeMap_insId_shareScope, _globalShareScopeMap_insId;
|
|
3016
|
+
(_globalShareScopeMap_insId = globalShareScopeMap[insId]) == null ? true : (_globalShareScopeMap_insId_shareScope = _globalShareScopeMap_insId[shareScope]) == null ? true : (_globalShareScopeMap_insId_shareScope_shareName = _globalShareScopeMap_insId_shareScope[shareName]) == null ? true : delete _globalShareScopeMap_insId_shareScope_shareName[shareVersion];
|
|
3017
|
+
});
|
|
3018
|
+
globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
|
|
3019
|
+
}
|
|
3020
|
+
const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);
|
|
3021
|
+
if (hostGlobalSnapshot) {
|
|
3022
|
+
const remoteKey = hostGlobalSnapshot && 'remotesInfo' in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;
|
|
3023
|
+
if (remoteKey) {
|
|
3024
|
+
delete hostGlobalSnapshot.remotesInfo[remoteKey];
|
|
3025
|
+
if (Boolean(Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])) {
|
|
3026
|
+
delete Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];
|
|
3027
|
+
}
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
3030
|
+
host.moduleCache.delete(remote.name);
|
|
3031
|
+
}
|
|
3032
|
+
} catch (err) {
|
|
3033
|
+
console.log('removeRemote fail: ', err);
|
|
3034
|
+
}
|
|
3035
|
+
}
|
|
3036
|
+
constructor(host){
|
|
3037
|
+
this.hooks = new PluginSystem({
|
|
3038
|
+
beforeRegisterRemote: new SyncWaterfallHook('beforeRegisterRemote'),
|
|
3039
|
+
registerRemote: new SyncWaterfallHook('registerRemote'),
|
|
3040
|
+
beforeRequest: new AsyncWaterfallHook('beforeRequest'),
|
|
3041
|
+
onLoad: new AsyncHook('onLoad'),
|
|
3042
|
+
handlePreloadModule: new SyncHook('handlePreloadModule'),
|
|
3043
|
+
errorLoadRemote: new AsyncHook('errorLoadRemote'),
|
|
3044
|
+
beforePreloadRemote: new AsyncHook('beforePreloadRemote'),
|
|
3045
|
+
generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
|
|
3046
|
+
afterPreloadRemote: new AsyncHook(),
|
|
3047
|
+
loadEntry: new AsyncHook()
|
|
3048
|
+
});
|
|
3049
|
+
this.host = host;
|
|
3050
|
+
this.idToRemoteMap = {};
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
3053
|
+
class FederationHost {
|
|
3054
|
+
initOptions(userOptions) {
|
|
3055
|
+
this.registerPlugins(userOptions.plugins);
|
|
3056
|
+
const options = this.formatOptions(this.options, userOptions);
|
|
3057
|
+
this.options = options;
|
|
3058
|
+
return options;
|
|
3059
|
+
}
|
|
3060
|
+
async loadShare(pkgName, extraOptions) {
|
|
3061
|
+
return this.sharedHandler.loadShare(pkgName, extraOptions);
|
|
3062
|
+
}
|
|
3063
|
+
loadShareSync(pkgName, extraOptions) {
|
|
3064
|
+
return this.sharedHandler.loadShareSync(pkgName, extraOptions);
|
|
3065
|
+
}
|
|
3066
|
+
initializeSharing(shareScopeName = DEFAULT_SCOPE, strategy) {
|
|
3067
|
+
return this.sharedHandler.initializeSharing(shareScopeName, strategy);
|
|
3068
|
+
}
|
|
3069
|
+
initRawContainer(name, url, container) {
|
|
3070
|
+
const remoteInfo = getRemoteInfo({
|
|
3071
|
+
name,
|
|
3072
|
+
entry: url
|
|
3073
|
+
});
|
|
3074
|
+
const module = new Module({
|
|
3075
|
+
host: this,
|
|
3076
|
+
remoteInfo
|
|
3077
|
+
});
|
|
3078
|
+
module.remoteEntryExports = container;
|
|
3079
|
+
this.moduleCache.set(name, module);
|
|
3080
|
+
return module;
|
|
3081
|
+
}
|
|
3082
|
+
async loadRemote(id, options) {
|
|
3083
|
+
return this.remoteHandler.loadRemote(id, options);
|
|
3084
|
+
}
|
|
3085
|
+
async preloadRemote(preloadOptions) {
|
|
3086
|
+
return this.remoteHandler.preloadRemote(preloadOptions);
|
|
3087
|
+
}
|
|
3088
|
+
initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
|
|
3089
|
+
this.sharedHandler.initShareScopeMap(scopeName, shareScope, extraOptions);
|
|
3090
|
+
}
|
|
3091
|
+
formatOptions(globalOptions, userOptions) {
|
|
3092
|
+
const { shared } = formatShareConfigs(globalOptions, userOptions);
|
|
3093
|
+
const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
|
|
3094
|
+
origin: this,
|
|
3095
|
+
userOptions,
|
|
3096
|
+
options: globalOptions,
|
|
3097
|
+
shareInfo: shared
|
|
3098
|
+
});
|
|
3099
|
+
const remotes = this.remoteHandler.formatAndRegisterRemote(globalOptionsRes, userOptionsRes);
|
|
3100
|
+
const { shared: handledShared } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
|
|
3101
|
+
const plugins = [
|
|
3102
|
+
...globalOptionsRes.plugins
|
|
3103
|
+
];
|
|
3104
|
+
if (userOptionsRes.plugins) {
|
|
3105
|
+
userOptionsRes.plugins.forEach((plugin)=>{
|
|
3106
|
+
if (!plugins.includes(plugin)) {
|
|
3107
|
+
plugins.push(plugin);
|
|
3108
|
+
}
|
|
3109
|
+
});
|
|
3110
|
+
}
|
|
3111
|
+
const optionsRes = _({}, globalOptions, userOptions, {
|
|
3112
|
+
plugins,
|
|
3113
|
+
remotes,
|
|
3114
|
+
shared: handledShared
|
|
3115
|
+
});
|
|
3116
|
+
this.hooks.lifecycle.init.emit({
|
|
3117
|
+
origin: this,
|
|
3118
|
+
options: optionsRes
|
|
3119
|
+
});
|
|
3120
|
+
return optionsRes;
|
|
3121
|
+
}
|
|
3122
|
+
registerPlugins(plugins) {
|
|
3123
|
+
const pluginRes = registerPlugins$1(plugins, [
|
|
3124
|
+
this.hooks,
|
|
3125
|
+
this.remoteHandler.hooks,
|
|
3126
|
+
this.sharedHandler.hooks,
|
|
3127
|
+
this.snapshotHandler.hooks,
|
|
3128
|
+
this.loaderHook
|
|
3129
|
+
]);
|
|
3130
|
+
this.options.plugins = this.options.plugins.reduce((res, plugin)=>{
|
|
3131
|
+
if (!plugin) return res;
|
|
3132
|
+
if (res && !res.find((item)=>item.name === plugin.name)) {
|
|
3133
|
+
res.push(plugin);
|
|
3134
|
+
}
|
|
3135
|
+
return res;
|
|
3136
|
+
}, pluginRes || []);
|
|
3137
|
+
}
|
|
3138
|
+
registerRemotes(remotes, options) {
|
|
3139
|
+
return this.remoteHandler.registerRemotes(remotes, options);
|
|
3140
|
+
}
|
|
3141
|
+
constructor(userOptions){
|
|
3142
|
+
this.hooks = new PluginSystem({
|
|
3143
|
+
beforeInit: new SyncWaterfallHook('beforeInit'),
|
|
3144
|
+
init: new SyncHook(),
|
|
3145
|
+
beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
|
|
3146
|
+
initContainer: new AsyncWaterfallHook('initContainer')
|
|
3147
|
+
});
|
|
3148
|
+
this.version = "0.5.1";
|
|
3149
|
+
this.moduleCache = new Map();
|
|
3150
|
+
this.loaderHook = new PluginSystem({
|
|
3151
|
+
getModuleInfo: new SyncHook(),
|
|
3152
|
+
createScript: new SyncHook(),
|
|
3153
|
+
createLink: new SyncHook(),
|
|
3154
|
+
fetch: new AsyncHook()
|
|
3155
|
+
});
|
|
3156
|
+
const defaultOptions = {
|
|
3157
|
+
id: getBuilderId(),
|
|
3158
|
+
name: userOptions.name,
|
|
3159
|
+
plugins: [
|
|
3160
|
+
snapshotPlugin(),
|
|
3161
|
+
generatePreloadAssetsPlugin()
|
|
3162
|
+
],
|
|
3163
|
+
remotes: [],
|
|
3164
|
+
shared: {},
|
|
3165
|
+
inBrowser: isBrowserEnv()
|
|
3166
|
+
};
|
|
3167
|
+
this.name = userOptions.name;
|
|
3168
|
+
this.options = defaultOptions;
|
|
3169
|
+
this.snapshotHandler = new SnapshotHandler(this);
|
|
3170
|
+
this.sharedHandler = new SharedHandler(this);
|
|
3171
|
+
this.remoteHandler = new RemoteHandler(this);
|
|
3172
|
+
this.shareScopeMap = this.sharedHandler.shareScopeMap;
|
|
3173
|
+
this.registerPlugins([
|
|
3174
|
+
...defaultOptions.plugins,
|
|
3175
|
+
...userOptions.plugins || []
|
|
3176
|
+
]);
|
|
3177
|
+
this.options = this.formatOptions(defaultOptions, userOptions);
|
|
3178
|
+
}
|
|
3179
|
+
}
|
|
3180
|
+
let FederationInstance = null;
|
|
3181
|
+
function init(options) {
|
|
3182
|
+
const instance = getGlobalFederationInstance(options.name, options.version);
|
|
3183
|
+
if (!instance) {
|
|
3184
|
+
const FederationConstructor = getGlobalFederationConstructor() || FederationHost;
|
|
3185
|
+
FederationInstance = new FederationConstructor(options);
|
|
3186
|
+
setGlobalFederationInstance(FederationInstance);
|
|
3187
|
+
return FederationInstance;
|
|
3188
|
+
} else {
|
|
3189
|
+
instance.initOptions(options);
|
|
3190
|
+
if (!FederationInstance) {
|
|
3191
|
+
FederationInstance = instance;
|
|
3192
|
+
}
|
|
3193
|
+
return instance;
|
|
3194
|
+
}
|
|
3195
|
+
}
|
|
3196
|
+
function loadRemote(...args) {
|
|
3197
|
+
assert(FederationInstance, 'Please call init first');
|
|
3198
|
+
const loadRemote1 = FederationInstance.loadRemote;
|
|
3199
|
+
return loadRemote1.apply(FederationInstance, args);
|
|
3200
|
+
}
|
|
3201
|
+
function loadShare(...args) {
|
|
3202
|
+
assert(FederationInstance, 'Please call init first');
|
|
3203
|
+
const loadShare1 = FederationInstance.loadShare;
|
|
3204
|
+
return loadShare1.apply(FederationInstance, args);
|
|
3205
|
+
}
|
|
3206
|
+
function loadShareSync(...args) {
|
|
3207
|
+
assert(FederationInstance, 'Please call init first');
|
|
3208
|
+
const loadShareSync1 = FederationInstance.loadShareSync;
|
|
3209
|
+
return loadShareSync1.apply(FederationInstance, args);
|
|
3210
|
+
}
|
|
3211
|
+
function preloadRemote(...args) {
|
|
3212
|
+
assert(FederationInstance, 'Please call init first');
|
|
3213
|
+
return FederationInstance.preloadRemote.apply(FederationInstance, args);
|
|
3214
|
+
}
|
|
3215
|
+
function registerRemotes(...args) {
|
|
3216
|
+
assert(FederationInstance, 'Please call init first');
|
|
3217
|
+
return FederationInstance.registerRemotes.apply(FederationInstance, args);
|
|
3218
|
+
}
|
|
3219
|
+
function registerPlugins(...args) {
|
|
3220
|
+
assert(FederationInstance, 'Please call init first');
|
|
3221
|
+
return FederationInstance.registerPlugins.apply(FederationInstance, args);
|
|
3222
|
+
}
|
|
3223
|
+
function getInstance() {
|
|
3224
|
+
return FederationInstance;
|
|
3225
|
+
}
|
|
3226
|
+
setGlobalFederationConstructor(FederationHost);
|
|
3227
|
+
|
|
3228
|
+
var runtime = Object.freeze({
|
|
3229
|
+
__proto__: null,
|
|
3230
|
+
FederationHost: FederationHost,
|
|
3231
|
+
getInstance: getInstance,
|
|
3232
|
+
getRemoteEntry: getRemoteEntry,
|
|
3233
|
+
getRemoteInfo: getRemoteInfo,
|
|
3234
|
+
init: init,
|
|
3235
|
+
loadRemote: loadRemote,
|
|
3236
|
+
loadShare: loadShare,
|
|
3237
|
+
loadShareSync: loadShareSync,
|
|
3238
|
+
preloadRemote: preloadRemote,
|
|
3239
|
+
registerPlugins: registerPlugins,
|
|
3240
|
+
registerRemotes: registerRemotes,
|
|
3241
|
+
registerGlobalPlugins: registerGlobalPlugins,
|
|
3242
|
+
loadScript: loadScript,
|
|
3243
|
+
loadScriptNode: loadScriptNode
|
|
3244
|
+
});
|
|
3245
|
+
|
|
3246
|
+
function attachShareScopeMap(webpackRequire) {
|
|
3247
|
+
if (!webpackRequire.S || webpackRequire.federation.hasAttachShareScopeMap || !webpackRequire.federation.instance || !webpackRequire.federation.instance.shareScopeMap) {
|
|
3248
|
+
return;
|
|
3249
|
+
}
|
|
3250
|
+
webpackRequire.S = webpackRequire.federation.instance.shareScopeMap;
|
|
3251
|
+
webpackRequire.federation.hasAttachShareScopeMap = true;
|
|
3252
|
+
}
|
|
3253
|
+
|
|
3254
|
+
const FEDERATION_SUPPORTED_TYPES = [
|
|
3255
|
+
'script'
|
|
3256
|
+
];
|
|
3257
|
+
|
|
3258
|
+
function remotes(options) {
|
|
3259
|
+
const { chunkId, promises, chunkMapping, idToExternalAndNameMapping, webpackRequire, idToRemoteMap } = options;
|
|
3260
|
+
attachShareScopeMap(webpackRequire);
|
|
3261
|
+
if (webpackRequire.o(chunkMapping, chunkId)) {
|
|
3262
|
+
chunkMapping[chunkId].forEach((id)=>{
|
|
3263
|
+
let getScope = webpackRequire.R;
|
|
3264
|
+
if (!getScope) {
|
|
3265
|
+
getScope = [];
|
|
3266
|
+
}
|
|
3267
|
+
const data = idToExternalAndNameMapping[id];
|
|
3268
|
+
const remoteInfos = idToRemoteMap[id];
|
|
3269
|
+
if (getScope.indexOf(data) >= 0) {
|
|
3270
|
+
return;
|
|
3271
|
+
}
|
|
3272
|
+
getScope.push(data);
|
|
3273
|
+
if (data.p) {
|
|
3274
|
+
return promises.push(data.p);
|
|
3275
|
+
}
|
|
3276
|
+
const onError = (error)=>{
|
|
3277
|
+
if (!error) {
|
|
3278
|
+
error = new Error('Container missing');
|
|
3279
|
+
}
|
|
3280
|
+
if (typeof error.message === 'string') {
|
|
3281
|
+
error.message += `\nwhile loading "${data[1]}" from ${data[2]}`;
|
|
3282
|
+
}
|
|
3283
|
+
webpackRequire.m[id] = ()=>{
|
|
3284
|
+
throw error;
|
|
3285
|
+
};
|
|
3286
|
+
data.p = 0;
|
|
3287
|
+
};
|
|
3288
|
+
const handleFunction = (fn, arg1, arg2, d, next, first)=>{
|
|
3289
|
+
try {
|
|
3290
|
+
const promise = fn(arg1, arg2);
|
|
3291
|
+
if (promise && promise.then) {
|
|
3292
|
+
const p = promise.then((result)=>next(result, d), onError);
|
|
3293
|
+
if (first) {
|
|
3294
|
+
promises.push(data.p = p);
|
|
3295
|
+
} else {
|
|
3296
|
+
return p;
|
|
3297
|
+
}
|
|
3298
|
+
} else {
|
|
3299
|
+
return next(promise, d, first);
|
|
3300
|
+
}
|
|
3301
|
+
} catch (error) {
|
|
3302
|
+
onError(error);
|
|
3303
|
+
}
|
|
3304
|
+
};
|
|
3305
|
+
const onExternal = (external, _, first)=>external ? handleFunction(webpackRequire.I, data[0], 0, external, onInitialized, first) : onError();
|
|
3306
|
+
var onInitialized = (_, external, first)=>handleFunction(external.get, data[1], getScope, 0, onFactory, first);
|
|
3307
|
+
var onFactory = (factory)=>{
|
|
3308
|
+
data.p = 1;
|
|
3309
|
+
webpackRequire.m[id] = (module)=>{
|
|
3310
|
+
module.exports = factory();
|
|
3311
|
+
};
|
|
3312
|
+
};
|
|
3313
|
+
const onRemoteLoaded = ()=>{
|
|
3314
|
+
try {
|
|
3315
|
+
const remoteName = decodeName(remoteInfos[0].name, ENCODE_NAME_PREFIX);
|
|
3316
|
+
const remoteModuleName = remoteName + data[1].slice(1);
|
|
3317
|
+
return webpackRequire.federation.instance.loadRemote(remoteModuleName, {
|
|
3318
|
+
loadFactory: false,
|
|
3319
|
+
from: 'build'
|
|
3320
|
+
});
|
|
3321
|
+
} catch (error) {
|
|
3322
|
+
onError(error);
|
|
3323
|
+
}
|
|
3324
|
+
};
|
|
3325
|
+
const useRuntimeLoad = remoteInfos.length === 1 && FEDERATION_SUPPORTED_TYPES.includes(remoteInfos[0].externalType) && remoteInfos[0].name;
|
|
3326
|
+
if (useRuntimeLoad) {
|
|
3327
|
+
handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
|
|
3328
|
+
} else {
|
|
3329
|
+
handleFunction(webpackRequire, data[2], 0, 0, onExternal, 1);
|
|
3330
|
+
}
|
|
3331
|
+
});
|
|
3332
|
+
}
|
|
3333
|
+
}
|
|
3334
|
+
|
|
3335
|
+
function consumes(options) {
|
|
3336
|
+
const { chunkId, promises, chunkMapping, installedModules, moduleToHandlerMapping, webpackRequire } = options;
|
|
3337
|
+
attachShareScopeMap(webpackRequire);
|
|
3338
|
+
if (webpackRequire.o(chunkMapping, chunkId)) {
|
|
3339
|
+
chunkMapping[chunkId].forEach((id)=>{
|
|
3340
|
+
if (webpackRequire.o(installedModules, id)) {
|
|
3341
|
+
return promises.push(installedModules[id]);
|
|
3342
|
+
}
|
|
3343
|
+
const onFactory = (factory)=>{
|
|
3344
|
+
installedModules[id] = 0;
|
|
3345
|
+
webpackRequire.m[id] = (module)=>{
|
|
3346
|
+
delete webpackRequire.c[id];
|
|
3347
|
+
module.exports = factory();
|
|
3348
|
+
};
|
|
3349
|
+
};
|
|
3350
|
+
const onError = (error)=>{
|
|
3351
|
+
delete installedModules[id];
|
|
3352
|
+
webpackRequire.m[id] = (module)=>{
|
|
3353
|
+
delete webpackRequire.c[id];
|
|
3354
|
+
throw error;
|
|
3355
|
+
};
|
|
3356
|
+
};
|
|
3357
|
+
try {
|
|
3358
|
+
const federationInstance = webpackRequire.federation.instance;
|
|
3359
|
+
if (!federationInstance) {
|
|
3360
|
+
throw new Error('Federation instance not found!');
|
|
3361
|
+
}
|
|
3362
|
+
const { shareKey, getter, shareInfo } = moduleToHandlerMapping[id];
|
|
3363
|
+
const promise = federationInstance.loadShare(shareKey, {
|
|
3364
|
+
customShareInfo: shareInfo
|
|
3365
|
+
}).then((factory)=>{
|
|
3366
|
+
if (factory === false) {
|
|
3367
|
+
return getter();
|
|
3368
|
+
}
|
|
3369
|
+
return factory;
|
|
3370
|
+
});
|
|
3371
|
+
if (promise.then) {
|
|
3372
|
+
promises.push(installedModules[id] = promise.then(onFactory).catch(onError));
|
|
3373
|
+
} else {
|
|
3374
|
+
onFactory(promise);
|
|
3375
|
+
}
|
|
3376
|
+
} catch (e) {
|
|
3377
|
+
onError(e);
|
|
3378
|
+
}
|
|
3379
|
+
});
|
|
3380
|
+
}
|
|
3381
|
+
}
|
|
3382
|
+
|
|
3383
|
+
function initializeSharing({ shareScopeName, webpackRequire, initPromises, initTokens, initScope }) {
|
|
3384
|
+
if (!initScope) initScope = [];
|
|
3385
|
+
var initToken = initTokens[shareScopeName];
|
|
3386
|
+
if (!initToken) initToken = initTokens[shareScopeName] = {};
|
|
3387
|
+
if (initScope.indexOf(initToken) >= 0) return;
|
|
3388
|
+
initScope.push(initToken);
|
|
3389
|
+
const promise = initPromises[shareScopeName];
|
|
3390
|
+
if (promise) return promise;
|
|
3391
|
+
var warn = (msg)=>typeof console !== 'undefined' && console.warn && console.warn(msg);
|
|
3392
|
+
var initExternal = (id)=>{
|
|
3393
|
+
var handleError = (err)=>warn('Initialization of sharing external failed: ' + err);
|
|
3394
|
+
try {
|
|
3395
|
+
var module = webpackRequire(id);
|
|
3396
|
+
if (!module) return;
|
|
3397
|
+
var initFn = (module)=>module && module.init && module.init(webpackRequire.S[shareScopeName], initScope);
|
|
3398
|
+
if (module.then) return promises.push(module.then(initFn, handleError));
|
|
3399
|
+
var initResult = initFn(module);
|
|
3400
|
+
if (initResult && typeof initResult !== 'boolean' && initResult.then) return promises.push(initResult['catch'](handleError));
|
|
3401
|
+
} catch (err) {
|
|
3402
|
+
handleError(err);
|
|
3403
|
+
}
|
|
3404
|
+
};
|
|
3405
|
+
const promises = webpackRequire.federation.instance.initializeSharing(shareScopeName);
|
|
3406
|
+
attachShareScopeMap(webpackRequire);
|
|
3407
|
+
const bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes;
|
|
3408
|
+
if (bundlerRuntimeRemotesOptions) {
|
|
3409
|
+
Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach((moduleId)=>{
|
|
3410
|
+
const info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId];
|
|
3411
|
+
const externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2];
|
|
3412
|
+
if (info.length > 1) {
|
|
3413
|
+
initExternal(externalModuleId);
|
|
3414
|
+
} else if (info.length === 1) {
|
|
3415
|
+
const remoteInfo = info[0];
|
|
3416
|
+
if (!FEDERATION_SUPPORTED_TYPES.includes(remoteInfo.externalType)) {
|
|
3417
|
+
initExternal(externalModuleId);
|
|
3418
|
+
}
|
|
3419
|
+
}
|
|
3420
|
+
});
|
|
3421
|
+
}
|
|
3422
|
+
if (!promises.length) {
|
|
3423
|
+
return initPromises[shareScopeName] = true;
|
|
3424
|
+
}
|
|
3425
|
+
return initPromises[shareScopeName] = Promise.all(promises).then(()=>initPromises[shareScopeName] = true);
|
|
3426
|
+
}
|
|
3427
|
+
|
|
3428
|
+
function handleInitialConsumes(options) {
|
|
3429
|
+
const { moduleId, moduleToHandlerMapping, webpackRequire } = options;
|
|
3430
|
+
const federationInstance = webpackRequire.federation.instance;
|
|
3431
|
+
if (!federationInstance) {
|
|
3432
|
+
throw new Error('Federation instance not found!');
|
|
3433
|
+
}
|
|
3434
|
+
const { shareKey, shareInfo } = moduleToHandlerMapping[moduleId];
|
|
3435
|
+
try {
|
|
3436
|
+
return federationInstance.loadShareSync(shareKey, {
|
|
3437
|
+
customShareInfo: shareInfo
|
|
3438
|
+
});
|
|
3439
|
+
} catch (err) {
|
|
3440
|
+
console.error('loadShareSync failed! The function should not be called unless you set "eager:true". If you do not set it, and encounter this issue, you can check whether an async boundary is implemented.');
|
|
3441
|
+
console.error('The original error message is as follows: ');
|
|
3442
|
+
throw err;
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
function installInitialConsumes(options) {
|
|
3446
|
+
const { moduleToHandlerMapping, webpackRequire, installedModules, initialConsumes } = options;
|
|
3447
|
+
initialConsumes.forEach((id)=>{
|
|
3448
|
+
webpackRequire.m[id] = (module)=>{
|
|
3449
|
+
installedModules[id] = 0;
|
|
3450
|
+
delete webpackRequire.c[id];
|
|
3451
|
+
const factory = handleInitialConsumes({
|
|
3452
|
+
moduleId: id,
|
|
3453
|
+
moduleToHandlerMapping,
|
|
3454
|
+
webpackRequire
|
|
3455
|
+
});
|
|
3456
|
+
if (typeof factory !== 'function') {
|
|
3457
|
+
throw new Error(`Shared module is not available for eager consumption: ${id}`);
|
|
3458
|
+
}
|
|
3459
|
+
module.exports = factory();
|
|
3460
|
+
};
|
|
3461
|
+
});
|
|
3462
|
+
}
|
|
3463
|
+
|
|
3464
|
+
function initContainerEntry(options) {
|
|
3465
|
+
const { webpackRequire, shareScope, initScope, shareScopeKey, remoteEntryInitOptions } = options;
|
|
3466
|
+
if (!webpackRequire.S) return;
|
|
3467
|
+
if (!webpackRequire.federation || !webpackRequire.federation.instance || !webpackRequire.federation.initOptions) return;
|
|
3468
|
+
const federationInstance = webpackRequire.federation.instance;
|
|
3469
|
+
var name = shareScopeKey || 'default';
|
|
3470
|
+
federationInstance.initOptions({
|
|
3471
|
+
name: webpackRequire.federation.initOptions.name,
|
|
3472
|
+
remotes: [],
|
|
3473
|
+
...remoteEntryInitOptions
|
|
3474
|
+
});
|
|
3475
|
+
federationInstance.initShareScopeMap(name, shareScope, {
|
|
3476
|
+
hostShareScopeMap: remoteEntryInitOptions?.shareScopeMap || {}
|
|
3477
|
+
});
|
|
3478
|
+
if (webpackRequire.federation.attachShareScopeMap) {
|
|
3479
|
+
webpackRequire.federation.attachShareScopeMap(webpackRequire);
|
|
3480
|
+
}
|
|
3481
|
+
return webpackRequire.I(name, initScope);
|
|
3482
|
+
}
|
|
3483
|
+
|
|
3484
|
+
const federation = {
|
|
3485
|
+
runtime,
|
|
3486
|
+
instance: undefined,
|
|
3487
|
+
initOptions: undefined,
|
|
3488
|
+
bundlerRuntime: {
|
|
3489
|
+
remotes,
|
|
3490
|
+
consumes,
|
|
3491
|
+
I: initializeSharing,
|
|
3492
|
+
S: {},
|
|
3493
|
+
installInitialConsumes,
|
|
3494
|
+
initContainerEntry
|
|
3495
|
+
},
|
|
3496
|
+
attachShareScopeMap,
|
|
3497
|
+
bundlerRuntimeOptions: {}
|
|
3498
|
+
};
|
|
3499
|
+
|
|
3500
|
+
export { federation as default };
|