@module-federation/runtime-core 0.0.0-feat-support-bridge-react-router-v7-20251015102312

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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/dist/LICENSE +21 -0
  4. package/dist/index.cjs.cjs +3240 -0
  5. package/dist/index.cjs.cjs.map +1 -0
  6. package/dist/index.cjs.d.ts +1 -0
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.esm.js +3216 -0
  9. package/dist/index.esm.js.map +1 -0
  10. package/dist/src/constant.d.ts +2 -0
  11. package/dist/src/core.d.ts +115 -0
  12. package/dist/src/global.d.ts +42 -0
  13. package/dist/src/helpers.d.ts +38 -0
  14. package/dist/src/index.d.ts +14 -0
  15. package/dist/src/module/index.d.ts +21 -0
  16. package/dist/src/plugins/generate-preload-assets.d.ts +8 -0
  17. package/dist/src/plugins/snapshot/SnapshotHandler.d.ts +60 -0
  18. package/dist/src/plugins/snapshot/index.d.ts +5 -0
  19. package/dist/src/remote/index.d.ts +109 -0
  20. package/dist/src/shared/index.d.ts +66 -0
  21. package/dist/src/type/config.d.ts +115 -0
  22. package/dist/src/type/index.d.ts +3 -0
  23. package/dist/src/type/plugin.d.ts +35 -0
  24. package/dist/src/type/preload.d.ts +26 -0
  25. package/dist/src/types.d.ts +1 -0
  26. package/dist/src/utils/env.d.ts +3 -0
  27. package/dist/src/utils/hooks/asyncHook.d.ts +6 -0
  28. package/dist/src/utils/hooks/asyncWaterfallHooks.d.ts +10 -0
  29. package/dist/src/utils/hooks/index.d.ts +6 -0
  30. package/dist/src/utils/hooks/pluginSystem.d.ts +16 -0
  31. package/dist/src/utils/hooks/syncHook.d.ts +12 -0
  32. package/dist/src/utils/hooks/syncWaterfallHook.d.ts +9 -0
  33. package/dist/src/utils/index.d.ts +6 -0
  34. package/dist/src/utils/load.d.ts +11 -0
  35. package/dist/src/utils/logger.d.ts +6 -0
  36. package/dist/src/utils/manifest.d.ts +7 -0
  37. package/dist/src/utils/plugin.d.ts +3 -0
  38. package/dist/src/utils/preload.d.ts +6 -0
  39. package/dist/src/utils/semver/compare.d.ts +9 -0
  40. package/dist/src/utils/semver/constants.d.ts +10 -0
  41. package/dist/src/utils/semver/index.d.ts +2 -0
  42. package/dist/src/utils/semver/parser.d.ts +9 -0
  43. package/dist/src/utils/semver/utils.d.ts +11 -0
  44. package/dist/src/utils/share.d.ts +27 -0
  45. package/dist/src/utils/tool.d.ts +18 -0
  46. package/dist/types.cjs.cjs +3 -0
  47. package/dist/types.cjs.cjs.map +1 -0
  48. package/dist/types.cjs.d.ts +1 -0
  49. package/dist/types.d.ts +1 -0
  50. package/dist/types.esm.js +2 -0
  51. package/dist/types.esm.js.map +1 -0
  52. package/package.json +58 -0
@@ -0,0 +1,3216 @@
1
+ import { createLogger, isBrowserEnv, isReactNativeEnv, isDebugMode, composeKeyWithSeparator, loadScriptNode, loadScript, createLink, createScript, safeToString, getResourceUrl, isManifestProvider, generateSnapshotFromManifest, warn as warn$1 } from '@module-federation/sdk';
2
+ export { loadScript, loadScriptNode } from '@module-federation/sdk';
3
+ import { RUNTIME_008, getShortErrorMsg, runtimeDescMap, RUNTIME_001, RUNTIME_002, RUNTIME_007, RUNTIME_003, RUNTIME_005, RUNTIME_006, RUNTIME_004 } from '@module-federation/error-codes';
4
+
5
+ const LOG_CATEGORY = '[ Federation Runtime ]';
6
+ // FIXME: pre-bundle ?
7
+ const logger = createLogger(LOG_CATEGORY);
8
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
9
+ function assert(condition, msg) {
10
+ if (!condition) {
11
+ error(msg);
12
+ }
13
+ }
14
+ function error(msg) {
15
+ if (msg instanceof Error) {
16
+ // Check if the message already starts with the log category to avoid duplication
17
+ if (!msg.message.startsWith(LOG_CATEGORY)) {
18
+ msg.message = `${LOG_CATEGORY}: ${msg.message}`;
19
+ }
20
+ throw msg;
21
+ }
22
+ throw new Error(`${LOG_CATEGORY}: ${msg}`);
23
+ }
24
+ function warn(msg) {
25
+ if (msg instanceof Error) {
26
+ // Check if the message already starts with the log category to avoid duplication
27
+ if (!msg.message.startsWith(LOG_CATEGORY)) {
28
+ msg.message = `${LOG_CATEGORY}: ${msg.message}`;
29
+ }
30
+ logger.warn(msg);
31
+ }
32
+ else {
33
+ logger.warn(msg);
34
+ }
35
+ }
36
+
37
+ function addUniqueItem(arr, item) {
38
+ if (arr.findIndex((name) => name === item) === -1) {
39
+ arr.push(item);
40
+ }
41
+ return arr;
42
+ }
43
+ function getFMId(remoteInfo) {
44
+ if ('version' in remoteInfo && remoteInfo.version) {
45
+ return `${remoteInfo.name}:${remoteInfo.version}`;
46
+ }
47
+ else if ('entry' in remoteInfo && remoteInfo.entry) {
48
+ return `${remoteInfo.name}:${remoteInfo.entry}`;
49
+ }
50
+ else {
51
+ return `${remoteInfo.name}`;
52
+ }
53
+ }
54
+ function isRemoteInfoWithEntry(remote) {
55
+ return typeof remote.entry !== 'undefined';
56
+ }
57
+ function isPureRemoteEntry(remote) {
58
+ return !remote.entry.includes('.json');
59
+ }
60
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
+ async function safeWrapper(callback, disableWarn) {
62
+ try {
63
+ const res = await callback();
64
+ return res;
65
+ }
66
+ catch (e) {
67
+ !disableWarn && warn(e);
68
+ return;
69
+ }
70
+ }
71
+ function isObject(val) {
72
+ return val && typeof val === 'object';
73
+ }
74
+ const objectToString = Object.prototype.toString;
75
+ // eslint-disable-next-line @typescript-eslint/ban-types
76
+ function isPlainObject(val) {
77
+ return objectToString.call(val) === '[object Object]';
78
+ }
79
+ function isStaticResourcesEqual(url1, url2) {
80
+ const REG_EXP = /^(https?:)?\/\//i;
81
+ // Transform url1 and url2 into relative paths
82
+ const relativeUrl1 = url1.replace(REG_EXP, '').replace(/\/$/, '');
83
+ const relativeUrl2 = url2.replace(REG_EXP, '').replace(/\/$/, '');
84
+ // Check if the relative paths are identical
85
+ return relativeUrl1 === relativeUrl2;
86
+ }
87
+ function arrayOptions(options) {
88
+ return Array.isArray(options) ? options : [options];
89
+ }
90
+ function getRemoteEntryInfoFromSnapshot(snapshot) {
91
+ const defaultRemoteEntryInfo = {
92
+ url: '',
93
+ type: 'global',
94
+ globalName: '',
95
+ };
96
+ if (isBrowserEnv() || isReactNativeEnv()) {
97
+ return 'remoteEntry' in snapshot
98
+ ? {
99
+ url: snapshot.remoteEntry,
100
+ type: snapshot.remoteEntryType,
101
+ globalName: snapshot.globalName,
102
+ }
103
+ : defaultRemoteEntryInfo;
104
+ }
105
+ if ('ssrRemoteEntry' in snapshot) {
106
+ return {
107
+ url: snapshot.ssrRemoteEntry || defaultRemoteEntryInfo.url,
108
+ type: snapshot.ssrRemoteEntryType || defaultRemoteEntryInfo.type,
109
+ globalName: snapshot.globalName,
110
+ };
111
+ }
112
+ return defaultRemoteEntryInfo;
113
+ }
114
+ const processModuleAlias = (name, subPath) => {
115
+ // @host/ ./button -> @host/button
116
+ let moduleName;
117
+ if (name.endsWith('/')) {
118
+ moduleName = name.slice(0, -1);
119
+ }
120
+ else {
121
+ moduleName = name;
122
+ }
123
+ if (subPath.startsWith('.')) {
124
+ subPath = subPath.slice(1);
125
+ }
126
+ moduleName = moduleName + subPath;
127
+ return moduleName;
128
+ };
129
+
130
+ const CurrentGlobal = typeof globalThis === 'object' ? globalThis : window;
131
+ const nativeGlobal = (() => {
132
+ try {
133
+ // get real window (incase of sandbox)
134
+ return document.defaultView;
135
+ }
136
+ catch {
137
+ // node env
138
+ return CurrentGlobal;
139
+ }
140
+ })();
141
+ const Global = nativeGlobal;
142
+ function definePropertyGlobalVal(target, key, val) {
143
+ Object.defineProperty(target, key, {
144
+ value: val,
145
+ configurable: false,
146
+ writable: true,
147
+ });
148
+ }
149
+ function includeOwnProperty(target, key) {
150
+ return Object.hasOwnProperty.call(target, key);
151
+ }
152
+ // This section is to prevent encapsulation by certain microfrontend frameworks. Due to reuse policies, sandbox escapes.
153
+ // The sandbox in the microfrontend does not replicate the value of 'configurable'.
154
+ // If there is no loading content on the global object, this section defines the loading object.
155
+ if (!includeOwnProperty(CurrentGlobal, '__GLOBAL_LOADING_REMOTE_ENTRY__')) {
156
+ definePropertyGlobalVal(CurrentGlobal, '__GLOBAL_LOADING_REMOTE_ENTRY__', {});
157
+ }
158
+ const globalLoading = CurrentGlobal.__GLOBAL_LOADING_REMOTE_ENTRY__;
159
+ function setGlobalDefaultVal(target) {
160
+ if (includeOwnProperty(target, '__VMOK__') &&
161
+ !includeOwnProperty(target, '__FEDERATION__')) {
162
+ definePropertyGlobalVal(target, '__FEDERATION__', target.__VMOK__);
163
+ }
164
+ if (!includeOwnProperty(target, '__FEDERATION__')) {
165
+ definePropertyGlobalVal(target, '__FEDERATION__', {
166
+ __GLOBAL_PLUGIN__: [],
167
+ __INSTANCES__: [],
168
+ moduleInfo: {},
169
+ __SHARE__: {},
170
+ __MANIFEST_LOADING__: {},
171
+ __PRELOADED_MAP__: new Map(),
172
+ });
173
+ definePropertyGlobalVal(target, '__VMOK__', target.__FEDERATION__);
174
+ }
175
+ target.__FEDERATION__.__GLOBAL_PLUGIN__ ??= [];
176
+ target.__FEDERATION__.__INSTANCES__ ??= [];
177
+ target.__FEDERATION__.moduleInfo ??= {};
178
+ target.__FEDERATION__.__SHARE__ ??= {};
179
+ target.__FEDERATION__.__MANIFEST_LOADING__ ??= {};
180
+ target.__FEDERATION__.__PRELOADED_MAP__ ??= new Map();
181
+ }
182
+ setGlobalDefaultVal(CurrentGlobal);
183
+ setGlobalDefaultVal(nativeGlobal);
184
+ function resetFederationGlobalInfo() {
185
+ CurrentGlobal.__FEDERATION__.__GLOBAL_PLUGIN__ = [];
186
+ CurrentGlobal.__FEDERATION__.__INSTANCES__ = [];
187
+ CurrentGlobal.__FEDERATION__.moduleInfo = {};
188
+ CurrentGlobal.__FEDERATION__.__SHARE__ = {};
189
+ CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};
190
+ Object.keys(globalLoading).forEach((key) => {
191
+ delete globalLoading[key];
192
+ });
193
+ }
194
+ function setGlobalFederationInstance(FederationInstance) {
195
+ CurrentGlobal.__FEDERATION__.__INSTANCES__.push(FederationInstance);
196
+ }
197
+ function getGlobalFederationConstructor() {
198
+ return CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
199
+ }
200
+ function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
201
+ if (isDebug) {
202
+ CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
203
+ CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.0-feat-support-bridge-react-router-v7-20251015102312";
204
+ }
205
+ }
206
+ // eslint-disable-next-line @typescript-eslint/ban-types
207
+ function getInfoWithoutType(target, key) {
208
+ if (typeof key === 'string') {
209
+ const keyRes = target[key];
210
+ if (keyRes) {
211
+ return {
212
+ value: target[key],
213
+ key: key,
214
+ };
215
+ }
216
+ else {
217
+ const targetKeys = Object.keys(target);
218
+ for (const targetKey of targetKeys) {
219
+ const [targetTypeOrName, _] = targetKey.split(':');
220
+ const nKey = `${targetTypeOrName}:${key}`;
221
+ const typeWithKeyRes = target[nKey];
222
+ if (typeWithKeyRes) {
223
+ return {
224
+ value: typeWithKeyRes,
225
+ key: nKey,
226
+ };
227
+ }
228
+ }
229
+ return {
230
+ value: undefined,
231
+ key: key,
232
+ };
233
+ }
234
+ }
235
+ else {
236
+ throw new Error('key must be string');
237
+ }
238
+ }
239
+ const getGlobalSnapshot = () => nativeGlobal.__FEDERATION__.moduleInfo;
240
+ const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot) => {
241
+ // Check if the remote is included in the hostSnapshot
242
+ const moduleKey = getFMId(moduleInfo);
243
+ const getModuleInfo = getInfoWithoutType(snapshot, moduleKey).value;
244
+ // The remoteSnapshot might not include a version
245
+ if (getModuleInfo &&
246
+ !getModuleInfo.version &&
247
+ 'version' in moduleInfo &&
248
+ moduleInfo['version']) {
249
+ getModuleInfo.version = moduleInfo['version'];
250
+ }
251
+ if (getModuleInfo) {
252
+ return getModuleInfo;
253
+ }
254
+ // If the remote is not included in the hostSnapshot, deploy a micro app snapshot
255
+ if ('version' in moduleInfo && moduleInfo['version']) {
256
+ const { version, ...resModuleInfo } = moduleInfo;
257
+ const moduleKeyWithoutVersion = getFMId(resModuleInfo);
258
+ const getModuleInfoWithoutVersion = getInfoWithoutType(nativeGlobal.__FEDERATION__.moduleInfo, moduleKeyWithoutVersion).value;
259
+ if (getModuleInfoWithoutVersion?.version === version) {
260
+ return getModuleInfoWithoutVersion;
261
+ }
262
+ }
263
+ return;
264
+ };
265
+ const getGlobalSnapshotInfoByModuleInfo = (moduleInfo) => getTargetSnapshotInfoByModuleInfo(moduleInfo, nativeGlobal.__FEDERATION__.moduleInfo);
266
+ const setGlobalSnapshotInfoByModuleInfo = (remoteInfo, moduleDetailInfo) => {
267
+ const moduleKey = getFMId(remoteInfo);
268
+ nativeGlobal.__FEDERATION__.moduleInfo[moduleKey] = moduleDetailInfo;
269
+ return nativeGlobal.__FEDERATION__.moduleInfo;
270
+ };
271
+ const addGlobalSnapshot = (moduleInfos) => {
272
+ nativeGlobal.__FEDERATION__.moduleInfo = {
273
+ ...nativeGlobal.__FEDERATION__.moduleInfo,
274
+ ...moduleInfos,
275
+ };
276
+ return () => {
277
+ const keys = Object.keys(moduleInfos);
278
+ for (const key of keys) {
279
+ delete nativeGlobal.__FEDERATION__.moduleInfo[key];
280
+ }
281
+ };
282
+ };
283
+ const getRemoteEntryExports = (name, globalName) => {
284
+ const remoteEntryKey = globalName || `__FEDERATION_${name}:custom__`;
285
+ const entryExports = CurrentGlobal[remoteEntryKey];
286
+ return {
287
+ remoteEntryKey,
288
+ entryExports,
289
+ };
290
+ };
291
+ // This function is used to register global plugins.
292
+ // It iterates over the provided plugins and checks if they are already registered.
293
+ // If a plugin is not registered, it is added to the global plugins.
294
+ // If a plugin is already registered, a warning message is logged.
295
+ const registerGlobalPlugins = (plugins) => {
296
+ const { __GLOBAL_PLUGIN__ } = nativeGlobal.__FEDERATION__;
297
+ plugins.forEach((plugin) => {
298
+ if (__GLOBAL_PLUGIN__.findIndex((p) => p.name === plugin.name) === -1) {
299
+ __GLOBAL_PLUGIN__.push(plugin);
300
+ }
301
+ else {
302
+ warn(`The plugin ${plugin.name} has been registered.`);
303
+ }
304
+ });
305
+ };
306
+ const getGlobalHostPlugins = () => nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;
307
+ const getPreloaded = (id) => CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.get(id);
308
+ const setPreloaded = (id) => CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
309
+
310
+ const DEFAULT_SCOPE = 'default';
311
+ const DEFAULT_REMOTE_TYPE = 'global';
312
+
313
+ // fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
314
+ // those constants are based on https://www.rubydoc.info/gems/semantic_range/3.0.0/SemanticRange#BUILDIDENTIFIER-constant
315
+ // Copyright (c)
316
+ // vite-plugin-federation is licensed under Mulan PSL v2.
317
+ // You can use this software according to the terms and conditions of the Mulan PSL v2.
318
+ // You may obtain a copy of Mulan PSL v2 at:
319
+ // http://license.coscl.org.cn/MulanPSL2
320
+ // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
321
+ // See the Mulan PSL v2 for more details.
322
+ const buildIdentifier = '[0-9A-Za-z-]+';
323
+ const build = `(?:\\+(${buildIdentifier}(?:\\.${buildIdentifier})*))`;
324
+ const numericIdentifier = '0|[1-9]\\d*';
325
+ const numericIdentifierLoose = '[0-9]+';
326
+ const nonNumericIdentifier = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
327
+ const preReleaseIdentifierLoose = `(?:${numericIdentifierLoose}|${nonNumericIdentifier})`;
328
+ const preReleaseLoose = `(?:-?(${preReleaseIdentifierLoose}(?:\\.${preReleaseIdentifierLoose})*))`;
329
+ const preReleaseIdentifier = `(?:${numericIdentifier}|${nonNumericIdentifier})`;
330
+ const preRelease = `(?:-(${preReleaseIdentifier}(?:\\.${preReleaseIdentifier})*))`;
331
+ const xRangeIdentifier = `${numericIdentifier}|x|X|\\*`;
332
+ const xRangePlain = `[v=\\s]*(${xRangeIdentifier})(?:\\.(${xRangeIdentifier})(?:\\.(${xRangeIdentifier})(?:${preRelease})?${build}?)?)?`;
333
+ const hyphenRange = `^\\s*(${xRangePlain})\\s+-\\s+(${xRangePlain})\\s*$`;
334
+ const mainVersionLoose = `(${numericIdentifierLoose})\\.(${numericIdentifierLoose})\\.(${numericIdentifierLoose})`;
335
+ const loosePlain = `[v=\\s]*${mainVersionLoose}${preReleaseLoose}?${build}?`;
336
+ const gtlt = '((?:<|>)?=?)';
337
+ const comparatorTrim = `(\\s*)${gtlt}\\s*(${loosePlain}|${xRangePlain})`;
338
+ const loneTilde = '(?:~>?)';
339
+ const tildeTrim = `(\\s*)${loneTilde}\\s+`;
340
+ const loneCaret = '(?:\\^)';
341
+ const caretTrim = `(\\s*)${loneCaret}\\s+`;
342
+ const star = '(<|>)?=?\\s*\\*';
343
+ const caret = `^${loneCaret}${xRangePlain}$`;
344
+ const mainVersion = `(${numericIdentifier})\\.(${numericIdentifier})\\.(${numericIdentifier})`;
345
+ const fullPlain = `v?${mainVersion}${preRelease}?${build}?`;
346
+ const tilde = `^${loneTilde}${xRangePlain}$`;
347
+ const xRange = `^${gtlt}\\s*${xRangePlain}$`;
348
+ const comparator = `^${gtlt}\\s*(${fullPlain})$|^$`;
349
+ // copy from semver package
350
+ const gte0 = '^\\s*>=\\s*0.0.0\\s*$';
351
+
352
+ // fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
353
+ // Copyright (c)
354
+ // vite-plugin-federation is licensed under Mulan PSL v2.
355
+ // You can use this software according to the terms and conditions of the Mulan PSL v2.
356
+ // You may obtain a copy of Mulan PSL v2 at:
357
+ // http://license.coscl.org.cn/MulanPSL2
358
+ // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
359
+ // See the Mulan PSL v2 for more details.
360
+ function parseRegex(source) {
361
+ return new RegExp(source);
362
+ }
363
+ function isXVersion(version) {
364
+ return !version || version.toLowerCase() === 'x' || version === '*';
365
+ }
366
+ function pipe(...fns) {
367
+ return (x) => fns.reduce((v, f) => f(v), x);
368
+ }
369
+ function extractComparator(comparatorString) {
370
+ return comparatorString.match(parseRegex(comparator));
371
+ }
372
+ function combineVersion(major, minor, patch, preRelease) {
373
+ const mainVersion = `${major}.${minor}.${patch}`;
374
+ if (preRelease) {
375
+ return `${mainVersion}-${preRelease}`;
376
+ }
377
+ return mainVersion;
378
+ }
379
+
380
+ // fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
381
+ // Copyright (c)
382
+ // vite-plugin-federation is licensed under Mulan PSL v2.
383
+ // You can use this software according to the terms and conditions of the Mulan PSL v2.
384
+ // You may obtain a copy of Mulan PSL v2 at:
385
+ // http://license.coscl.org.cn/MulanPSL2
386
+ // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
387
+ // See the Mulan PSL v2 for more details.
388
+ function parseHyphen(range) {
389
+ return range.replace(parseRegex(hyphenRange), (_range, from, fromMajor, fromMinor, fromPatch, _fromPreRelease, _fromBuild, to, toMajor, toMinor, toPatch, toPreRelease) => {
390
+ if (isXVersion(fromMajor)) {
391
+ from = '';
392
+ }
393
+ else if (isXVersion(fromMinor)) {
394
+ from = `>=${fromMajor}.0.0`;
395
+ }
396
+ else if (isXVersion(fromPatch)) {
397
+ from = `>=${fromMajor}.${fromMinor}.0`;
398
+ }
399
+ else {
400
+ from = `>=${from}`;
401
+ }
402
+ if (isXVersion(toMajor)) {
403
+ to = '';
404
+ }
405
+ else if (isXVersion(toMinor)) {
406
+ to = `<${Number(toMajor) + 1}.0.0-0`;
407
+ }
408
+ else if (isXVersion(toPatch)) {
409
+ to = `<${toMajor}.${Number(toMinor) + 1}.0-0`;
410
+ }
411
+ else if (toPreRelease) {
412
+ to = `<=${toMajor}.${toMinor}.${toPatch}-${toPreRelease}`;
413
+ }
414
+ else {
415
+ to = `<=${to}`;
416
+ }
417
+ return `${from} ${to}`.trim();
418
+ });
419
+ }
420
+ function parseComparatorTrim(range) {
421
+ return range.replace(parseRegex(comparatorTrim), '$1$2$3');
422
+ }
423
+ function parseTildeTrim(range) {
424
+ return range.replace(parseRegex(tildeTrim), '$1~');
425
+ }
426
+ function parseCaretTrim(range) {
427
+ return range.replace(parseRegex(caretTrim), '$1^');
428
+ }
429
+ function parseCarets(range) {
430
+ return range
431
+ .trim()
432
+ .split(/\s+/)
433
+ .map((rangeVersion) => rangeVersion.replace(parseRegex(caret), (_, major, minor, patch, preRelease) => {
434
+ if (isXVersion(major)) {
435
+ return '';
436
+ }
437
+ else if (isXVersion(minor)) {
438
+ return `>=${major}.0.0 <${Number(major) + 1}.0.0-0`;
439
+ }
440
+ else if (isXVersion(patch)) {
441
+ if (major === '0') {
442
+ return `>=${major}.${minor}.0 <${major}.${Number(minor) + 1}.0-0`;
443
+ }
444
+ else {
445
+ return `>=${major}.${minor}.0 <${Number(major) + 1}.0.0-0`;
446
+ }
447
+ }
448
+ else if (preRelease) {
449
+ if (major === '0') {
450
+ if (minor === '0') {
451
+ return `>=${major}.${minor}.${patch}-${preRelease} <${major}.${minor}.${Number(patch) + 1}-0`;
452
+ }
453
+ else {
454
+ return `>=${major}.${minor}.${patch}-${preRelease} <${major}.${Number(minor) + 1}.0-0`;
455
+ }
456
+ }
457
+ else {
458
+ return `>=${major}.${minor}.${patch}-${preRelease} <${Number(major) + 1}.0.0-0`;
459
+ }
460
+ }
461
+ else {
462
+ if (major === '0') {
463
+ if (minor === '0') {
464
+ return `>=${major}.${minor}.${patch} <${major}.${minor}.${Number(patch) + 1}-0`;
465
+ }
466
+ else {
467
+ return `>=${major}.${minor}.${patch} <${major}.${Number(minor) + 1}.0-0`;
468
+ }
469
+ }
470
+ return `>=${major}.${minor}.${patch} <${Number(major) + 1}.0.0-0`;
471
+ }
472
+ }))
473
+ .join(' ');
474
+ }
475
+ function parseTildes(range) {
476
+ return range
477
+ .trim()
478
+ .split(/\s+/)
479
+ .map((rangeVersion) => rangeVersion.replace(parseRegex(tilde), (_, major, minor, patch, preRelease) => {
480
+ if (isXVersion(major)) {
481
+ return '';
482
+ }
483
+ else if (isXVersion(minor)) {
484
+ return `>=${major}.0.0 <${Number(major) + 1}.0.0-0`;
485
+ }
486
+ else if (isXVersion(patch)) {
487
+ return `>=${major}.${minor}.0 <${major}.${Number(minor) + 1}.0-0`;
488
+ }
489
+ else if (preRelease) {
490
+ return `>=${major}.${minor}.${patch}-${preRelease} <${major}.${Number(minor) + 1}.0-0`;
491
+ }
492
+ return `>=${major}.${minor}.${patch} <${major}.${Number(minor) + 1}.0-0`;
493
+ }))
494
+ .join(' ');
495
+ }
496
+ function parseXRanges(range) {
497
+ return range
498
+ .split(/\s+/)
499
+ .map((rangeVersion) => rangeVersion
500
+ .trim()
501
+ .replace(parseRegex(xRange), (ret, gtlt, major, minor, patch, preRelease) => {
502
+ const isXMajor = isXVersion(major);
503
+ const isXMinor = isXMajor || isXVersion(minor);
504
+ const isXPatch = isXMinor || isXVersion(patch);
505
+ if (gtlt === '=' && isXPatch) {
506
+ gtlt = '';
507
+ }
508
+ preRelease = '';
509
+ if (isXMajor) {
510
+ if (gtlt === '>' || gtlt === '<') {
511
+ // nothing is allowed
512
+ return '<0.0.0-0';
513
+ }
514
+ else {
515
+ // nothing is forbidden
516
+ return '*';
517
+ }
518
+ }
519
+ else if (gtlt && isXPatch) {
520
+ // replace X with 0
521
+ if (isXMinor) {
522
+ minor = 0;
523
+ }
524
+ patch = 0;
525
+ if (gtlt === '>') {
526
+ // >1 => >=2.0.0
527
+ // >1.2 => >=1.3.0
528
+ gtlt = '>=';
529
+ if (isXMinor) {
530
+ major = Number(major) + 1;
531
+ minor = 0;
532
+ patch = 0;
533
+ }
534
+ else {
535
+ minor = Number(minor) + 1;
536
+ patch = 0;
537
+ }
538
+ }
539
+ else if (gtlt === '<=') {
540
+ // <=0.7.x is actually <0.8.0, since any 0.7.x should pass
541
+ // Similarly, <=7.x is actually <8.0.0, etc.
542
+ gtlt = '<';
543
+ if (isXMinor) {
544
+ major = Number(major) + 1;
545
+ }
546
+ else {
547
+ minor = Number(minor) + 1;
548
+ }
549
+ }
550
+ if (gtlt === '<') {
551
+ preRelease = '-0';
552
+ }
553
+ return `${gtlt + major}.${minor}.${patch}${preRelease}`;
554
+ }
555
+ else if (isXMinor) {
556
+ return `>=${major}.0.0${preRelease} <${Number(major) + 1}.0.0-0`;
557
+ }
558
+ else if (isXPatch) {
559
+ return `>=${major}.${minor}.0${preRelease} <${major}.${Number(minor) + 1}.0-0`;
560
+ }
561
+ return ret;
562
+ }))
563
+ .join(' ');
564
+ }
565
+ function parseStar(range) {
566
+ return range.trim().replace(parseRegex(star), '');
567
+ }
568
+ function parseGTE0(comparatorString) {
569
+ return comparatorString.trim().replace(parseRegex(gte0), '');
570
+ }
571
+
572
+ // fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
573
+ // Copyright (c)
574
+ // vite-plugin-federation is licensed under Mulan PSL v2.
575
+ // You can use this software according to the terms and conditions of the Mulan PSL v2.
576
+ // You may obtain a copy of Mulan PSL v2 at:
577
+ // http://license.coscl.org.cn/MulanPSL2
578
+ // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
579
+ // See the Mulan PSL v2 for more details.
580
+ function compareAtom(rangeAtom, versionAtom) {
581
+ rangeAtom = Number(rangeAtom) || rangeAtom;
582
+ versionAtom = Number(versionAtom) || versionAtom;
583
+ if (rangeAtom > versionAtom) {
584
+ return 1;
585
+ }
586
+ if (rangeAtom === versionAtom) {
587
+ return 0;
588
+ }
589
+ return -1;
590
+ }
591
+ function comparePreRelease(rangeAtom, versionAtom) {
592
+ const { preRelease: rangePreRelease } = rangeAtom;
593
+ const { preRelease: versionPreRelease } = versionAtom;
594
+ if (rangePreRelease === undefined && Boolean(versionPreRelease)) {
595
+ return 1;
596
+ }
597
+ if (Boolean(rangePreRelease) && versionPreRelease === undefined) {
598
+ return -1;
599
+ }
600
+ if (rangePreRelease === undefined && versionPreRelease === undefined) {
601
+ return 0;
602
+ }
603
+ for (let i = 0, n = rangePreRelease.length; i <= n; i++) {
604
+ const rangeElement = rangePreRelease[i];
605
+ const versionElement = versionPreRelease[i];
606
+ if (rangeElement === versionElement) {
607
+ continue;
608
+ }
609
+ if (rangeElement === undefined && versionElement === undefined) {
610
+ return 0;
611
+ }
612
+ if (!rangeElement) {
613
+ return 1;
614
+ }
615
+ if (!versionElement) {
616
+ return -1;
617
+ }
618
+ return compareAtom(rangeElement, versionElement);
619
+ }
620
+ return 0;
621
+ }
622
+ function compareVersion(rangeAtom, versionAtom) {
623
+ return (compareAtom(rangeAtom.major, versionAtom.major) ||
624
+ compareAtom(rangeAtom.minor, versionAtom.minor) ||
625
+ compareAtom(rangeAtom.patch, versionAtom.patch) ||
626
+ comparePreRelease(rangeAtom, versionAtom));
627
+ }
628
+ function eq(rangeAtom, versionAtom) {
629
+ return rangeAtom.version === versionAtom.version;
630
+ }
631
+ function compare(rangeAtom, versionAtom) {
632
+ switch (rangeAtom.operator) {
633
+ case '':
634
+ case '=':
635
+ return eq(rangeAtom, versionAtom);
636
+ case '>':
637
+ return compareVersion(rangeAtom, versionAtom) < 0;
638
+ case '>=':
639
+ return (eq(rangeAtom, versionAtom) || compareVersion(rangeAtom, versionAtom) < 0);
640
+ case '<':
641
+ return compareVersion(rangeAtom, versionAtom) > 0;
642
+ case '<=':
643
+ return (eq(rangeAtom, versionAtom) || compareVersion(rangeAtom, versionAtom) > 0);
644
+ case undefined: {
645
+ // mean * or x -> all versions
646
+ return true;
647
+ }
648
+ default:
649
+ return false;
650
+ }
651
+ }
652
+
653
+ // fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
654
+ // Copyright (c)
655
+ // vite-plugin-federation is licensed under Mulan PSL v2.
656
+ // You can use this software according to the terms and conditions of the Mulan PSL v2.
657
+ // You may obtain a copy of Mulan PSL v2 at:
658
+ // http://license.coscl.org.cn/MulanPSL2
659
+ // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
660
+ // See the Mulan PSL v2 for more details.
661
+ function parseComparatorString(range) {
662
+ return pipe(
663
+ // handle caret
664
+ // ^ --> * (any, kinda silly)
665
+ // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0-0
666
+ // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0-0
667
+ // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0-0
668
+ // ^1.2.3 --> >=1.2.3 <2.0.0-0
669
+ // ^1.2.0 --> >=1.2.0 <2.0.0-0
670
+ parseCarets,
671
+ // handle tilde
672
+ // ~, ~> --> * (any, kinda silly)
673
+ // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0-0
674
+ // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0-0
675
+ // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0-0
676
+ // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0-0
677
+ // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0-0
678
+ parseTildes, parseXRanges, parseStar)(range);
679
+ }
680
+ function parseRange(range) {
681
+ return pipe(
682
+ // handle hyphenRange
683
+ // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
684
+ parseHyphen,
685
+ // handle trim comparator
686
+ // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
687
+ parseComparatorTrim,
688
+ // handle trim tilde
689
+ // `~ 1.2.3` => `~1.2.3`
690
+ parseTildeTrim,
691
+ // handle trim caret
692
+ // `^ 1.2.3` => `^1.2.3`
693
+ parseCaretTrim)(range.trim())
694
+ .split(/\s+/)
695
+ .join(' ');
696
+ }
697
+ function satisfy(version, range) {
698
+ if (!version) {
699
+ return false;
700
+ }
701
+ // Extract version details once
702
+ const extractedVersion = extractComparator(version);
703
+ if (!extractedVersion) {
704
+ // If the version string is invalid, it can't satisfy any range
705
+ return false;
706
+ }
707
+ const [, versionOperator, , versionMajor, versionMinor, versionPatch, versionPreRelease,] = extractedVersion;
708
+ const versionAtom = {
709
+ operator: versionOperator,
710
+ version: combineVersion(versionMajor, versionMinor, versionPatch, versionPreRelease), // exclude build atom
711
+ major: versionMajor,
712
+ minor: versionMinor,
713
+ patch: versionPatch,
714
+ preRelease: versionPreRelease?.split('.'),
715
+ };
716
+ // Split the range by || to handle OR conditions
717
+ const orRanges = range.split('||');
718
+ for (const orRange of orRanges) {
719
+ const trimmedOrRange = orRange.trim();
720
+ if (!trimmedOrRange) {
721
+ // An empty range string signifies wildcard *, satisfy any valid version
722
+ // (We already checked if the version itself is valid)
723
+ return true;
724
+ }
725
+ // Handle simple wildcards explicitly before complex parsing
726
+ if (trimmedOrRange === '*' || trimmedOrRange === 'x') {
727
+ return true;
728
+ }
729
+ try {
730
+ // Apply existing parsing logic to the current OR sub-range
731
+ const parsedSubRange = parseRange(trimmedOrRange); // Handles hyphens, trims etc.
732
+ // Check if the result of initial parsing is empty, which can happen
733
+ // for some wildcard cases handled by parseRange/parseComparatorString.
734
+ // E.g. `parseStar` used in `parseComparatorString` returns ''.
735
+ if (!parsedSubRange.trim()) {
736
+ // If parsing results in empty string, treat as wildcard match
737
+ return true;
738
+ }
739
+ const parsedComparatorString = parsedSubRange
740
+ .split(' ')
741
+ .map((rangeVersion) => parseComparatorString(rangeVersion)) // Expands ^, ~
742
+ .join(' ');
743
+ // Check again if the comparator string became empty after specific parsing like ^ or ~
744
+ if (!parsedComparatorString.trim()) {
745
+ return true;
746
+ }
747
+ // Split the sub-range by space for implicit AND conditions
748
+ const comparators = parsedComparatorString
749
+ .split(/\s+/)
750
+ .map((comparator) => parseGTE0(comparator))
751
+ // Filter out empty strings that might result from multiple spaces
752
+ .filter(Boolean);
753
+ // If a sub-range becomes empty after parsing (e.g., invalid characters),
754
+ // it cannot be satisfied. This check might be redundant now but kept for safety.
755
+ if (comparators.length === 0) {
756
+ continue;
757
+ }
758
+ let subRangeSatisfied = true;
759
+ for (const comparator of comparators) {
760
+ const extractedComparator = extractComparator(comparator);
761
+ // If any part of the AND sub-range is invalid, the sub-range is not satisfied
762
+ if (!extractedComparator) {
763
+ subRangeSatisfied = false;
764
+ break;
765
+ }
766
+ const [, rangeOperator, , rangeMajor, rangeMinor, rangePatch, rangePreRelease,] = extractedComparator;
767
+ const rangeAtom = {
768
+ operator: rangeOperator,
769
+ version: combineVersion(rangeMajor, rangeMinor, rangePatch, rangePreRelease),
770
+ major: rangeMajor,
771
+ minor: rangeMinor,
772
+ patch: rangePatch,
773
+ preRelease: rangePreRelease?.split('.'),
774
+ };
775
+ // Check if the version satisfies this specific comparator in the AND chain
776
+ if (!compare(rangeAtom, versionAtom)) {
777
+ subRangeSatisfied = false; // This part of the AND condition failed
778
+ break; // No need to check further comparators in this sub-range
779
+ }
780
+ }
781
+ // If all AND conditions within this OR sub-range were met, the overall range is satisfied
782
+ if (subRangeSatisfied) {
783
+ return true;
784
+ }
785
+ }
786
+ catch (e) {
787
+ // Log error and treat this sub-range as unsatisfied
788
+ console.error(`[semver] Error processing range part "${trimmedOrRange}":`, e);
789
+ continue;
790
+ }
791
+ }
792
+ // If none of the OR sub-ranges were satisfied
793
+ return false;
794
+ }
795
+
796
+ function formatShare(shareArgs, from, name, shareStrategy) {
797
+ let get;
798
+ if ('get' in shareArgs) {
799
+ // eslint-disable-next-line prefer-destructuring
800
+ get = shareArgs.get;
801
+ }
802
+ else if ('lib' in shareArgs) {
803
+ get = () => Promise.resolve(shareArgs.lib);
804
+ }
805
+ else {
806
+ get = () => Promise.resolve(() => {
807
+ throw new Error(`Can not get shared '${name}'!`);
808
+ });
809
+ }
810
+ return {
811
+ deps: [],
812
+ useIn: [],
813
+ from,
814
+ loading: null,
815
+ ...shareArgs,
816
+ shareConfig: {
817
+ requiredVersion: `^${shareArgs.version}`,
818
+ singleton: false,
819
+ eager: false,
820
+ strictVersion: false,
821
+ ...shareArgs.shareConfig,
822
+ },
823
+ get,
824
+ loaded: shareArgs?.loaded || 'lib' in shareArgs ? true : undefined,
825
+ version: shareArgs.version ?? '0',
826
+ scope: Array.isArray(shareArgs.scope)
827
+ ? shareArgs.scope
828
+ : [shareArgs.scope ?? 'default'],
829
+ strategy: (shareArgs.strategy ?? shareStrategy) || 'version-first',
830
+ };
831
+ }
832
+ function formatShareConfigs(globalOptions, userOptions) {
833
+ const shareArgs = userOptions.shared || {};
834
+ const from = userOptions.name;
835
+ const shareInfos = Object.keys(shareArgs).reduce((res, pkgName) => {
836
+ const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
837
+ res[pkgName] = res[pkgName] || [];
838
+ arrayShareArgs.forEach((shareConfig) => {
839
+ res[pkgName].push(formatShare(shareConfig, from, pkgName, userOptions.shareStrategy));
840
+ });
841
+ return res;
842
+ }, {});
843
+ const shared = {
844
+ ...globalOptions.shared,
845
+ };
846
+ Object.keys(shareInfos).forEach((shareKey) => {
847
+ if (!shared[shareKey]) {
848
+ shared[shareKey] = shareInfos[shareKey];
849
+ }
850
+ else {
851
+ shareInfos[shareKey].forEach((newUserSharedOptions) => {
852
+ const isSameVersion = shared[shareKey].find((sharedVal) => sharedVal.version === newUserSharedOptions.version);
853
+ if (!isSameVersion) {
854
+ shared[shareKey].push(newUserSharedOptions);
855
+ }
856
+ });
857
+ }
858
+ });
859
+ return { shared, shareInfos };
860
+ }
861
+ function versionLt(a, b) {
862
+ const transformInvalidVersion = (version) => {
863
+ const isNumberVersion = !Number.isNaN(Number(version));
864
+ if (isNumberVersion) {
865
+ const splitArr = version.split('.');
866
+ let validVersion = version;
867
+ for (let i = 0; i < 3 - splitArr.length; i++) {
868
+ validVersion += '.0';
869
+ }
870
+ return validVersion;
871
+ }
872
+ return version;
873
+ };
874
+ if (satisfy(transformInvalidVersion(a), `<=${transformInvalidVersion(b)}`)) {
875
+ return true;
876
+ }
877
+ else {
878
+ return false;
879
+ }
880
+ }
881
+ const findVersion = (shareVersionMap, cb) => {
882
+ const callback = cb ||
883
+ function (prev, cur) {
884
+ return versionLt(prev, cur);
885
+ };
886
+ return Object.keys(shareVersionMap).reduce((prev, cur) => {
887
+ if (!prev) {
888
+ return cur;
889
+ }
890
+ if (callback(prev, cur)) {
891
+ return cur;
892
+ }
893
+ // default version is '0' https://github.com/webpack/webpack/blob/main/lib/sharing/ProvideSharedModule.js#L136
894
+ if (prev === '0') {
895
+ return cur;
896
+ }
897
+ return prev;
898
+ }, 0);
899
+ };
900
+ const isLoaded = (shared) => {
901
+ return Boolean(shared.loaded) || typeof shared.lib === 'function';
902
+ };
903
+ const isLoading = (shared) => {
904
+ return Boolean(shared.loading);
905
+ };
906
+ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
907
+ const versions = shareScopeMap[scope][pkgName];
908
+ const callback = function (prev, cur) {
909
+ return !isLoaded(versions[prev]) && versionLt(prev, cur);
910
+ };
911
+ return findVersion(shareScopeMap[scope][pkgName], callback);
912
+ }
913
+ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
914
+ const versions = shareScopeMap[scope][pkgName];
915
+ const callback = function (prev, cur) {
916
+ const isLoadingOrLoaded = (shared) => {
917
+ return isLoaded(shared) || isLoading(shared);
918
+ };
919
+ if (isLoadingOrLoaded(versions[cur])) {
920
+ if (isLoadingOrLoaded(versions[prev])) {
921
+ return Boolean(versionLt(prev, cur));
922
+ }
923
+ else {
924
+ return true;
925
+ }
926
+ }
927
+ if (isLoadingOrLoaded(versions[prev])) {
928
+ return false;
929
+ }
930
+ return versionLt(prev, cur);
931
+ };
932
+ return findVersion(shareScopeMap[scope][pkgName], callback);
933
+ }
934
+ function getFindShareFunction(strategy) {
935
+ if (strategy === 'loaded-first') {
936
+ return findSingletonVersionOrderByLoaded;
937
+ }
938
+ return findSingletonVersionOrderByVersion;
939
+ }
940
+ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare) {
941
+ if (!localShareScopeMap) {
942
+ return;
943
+ }
944
+ const { shareConfig, scope = DEFAULT_SCOPE, strategy } = shareInfo;
945
+ const scopes = Array.isArray(scope) ? scope : [scope];
946
+ for (const sc of scopes) {
947
+ if (shareConfig &&
948
+ localShareScopeMap[sc] &&
949
+ localShareScopeMap[sc][pkgName]) {
950
+ const { requiredVersion } = shareConfig;
951
+ const findShareFunction = getFindShareFunction(strategy);
952
+ const maxOrSingletonVersion = findShareFunction(localShareScopeMap, sc, pkgName);
953
+ //@ts-ignore
954
+ const defaultResolver = () => {
955
+ if (shareConfig.singleton) {
956
+ if (typeof requiredVersion === 'string' &&
957
+ !satisfy(maxOrSingletonVersion, requiredVersion)) {
958
+ const msg = `Version ${maxOrSingletonVersion} from ${maxOrSingletonVersion &&
959
+ localShareScopeMap[sc][pkgName][maxOrSingletonVersion].from} of shared singleton module ${pkgName} does not satisfy the requirement of ${shareInfo.from} which needs ${requiredVersion})`;
960
+ if (shareConfig.strictVersion) {
961
+ error(msg);
962
+ }
963
+ else {
964
+ warn(msg);
965
+ }
966
+ }
967
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
968
+ }
969
+ else {
970
+ if (requiredVersion === false || requiredVersion === '*') {
971
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
972
+ }
973
+ if (satisfy(maxOrSingletonVersion, requiredVersion)) {
974
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
975
+ }
976
+ for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
977
+ if (satisfy(versionKey, requiredVersion)) {
978
+ return versionValue;
979
+ }
980
+ }
981
+ }
982
+ };
983
+ const params = {
984
+ shareScopeMap: localShareScopeMap,
985
+ scope: sc,
986
+ pkgName,
987
+ version: maxOrSingletonVersion,
988
+ GlobalFederation: Global.__FEDERATION__,
989
+ resolver: defaultResolver,
990
+ };
991
+ const resolveShared = resolveShare.emit(params) || params;
992
+ return resolveShared.resolver();
993
+ }
994
+ }
995
+ }
996
+ function getGlobalShareScope() {
997
+ return Global.__FEDERATION__.__SHARE__;
998
+ }
999
+ function getTargetSharedOptions(options) {
1000
+ const { pkgName, extraOptions, shareInfos } = options;
1001
+ const defaultResolver = (sharedOptions) => {
1002
+ if (!sharedOptions) {
1003
+ return undefined;
1004
+ }
1005
+ const shareVersionMap = {};
1006
+ sharedOptions.forEach((shared) => {
1007
+ shareVersionMap[shared.version] = shared;
1008
+ });
1009
+ const callback = function (prev, cur) {
1010
+ return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
1011
+ };
1012
+ const maxVersion = findVersion(shareVersionMap, callback);
1013
+ return shareVersionMap[maxVersion];
1014
+ };
1015
+ const resolver = extraOptions?.resolver ?? defaultResolver;
1016
+ return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions?.customShareInfo);
1017
+ }
1018
+
1019
+ function getBuilderId() {
1020
+ //@ts-ignore
1021
+ return typeof FEDERATION_BUILD_IDENTIFIER !== 'undefined'
1022
+ ? //@ts-ignore
1023
+ FEDERATION_BUILD_IDENTIFIER
1024
+ : '';
1025
+ }
1026
+
1027
+ // Function to match a remote with its name and expose
1028
+ // id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
1029
+ // id: alias(app1) + expose(button) = app1/button
1030
+ // id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort
1031
+ function matchRemoteWithNameAndExpose(remotes, id) {
1032
+ for (const remote of remotes) {
1033
+ // match pkgName
1034
+ const isNameMatched = id.startsWith(remote.name);
1035
+ let expose = id.replace(remote.name, '');
1036
+ if (isNameMatched) {
1037
+ if (expose.startsWith('/')) {
1038
+ const pkgNameOrAlias = remote.name;
1039
+ expose = `.${expose}`;
1040
+ return {
1041
+ pkgNameOrAlias,
1042
+ expose,
1043
+ remote,
1044
+ };
1045
+ }
1046
+ else if (expose === '') {
1047
+ return {
1048
+ pkgNameOrAlias: remote.name,
1049
+ expose: '.',
1050
+ remote,
1051
+ };
1052
+ }
1053
+ }
1054
+ // match alias
1055
+ const isAliasMatched = remote.alias && id.startsWith(remote.alias);
1056
+ let exposeWithAlias = remote.alias && id.replace(remote.alias, '');
1057
+ if (remote.alias && isAliasMatched) {
1058
+ if (exposeWithAlias && exposeWithAlias.startsWith('/')) {
1059
+ const pkgNameOrAlias = remote.alias;
1060
+ exposeWithAlias = `.${exposeWithAlias}`;
1061
+ return {
1062
+ pkgNameOrAlias,
1063
+ expose: exposeWithAlias,
1064
+ remote,
1065
+ };
1066
+ }
1067
+ else if (exposeWithAlias === '') {
1068
+ return {
1069
+ pkgNameOrAlias: remote.alias,
1070
+ expose: '.',
1071
+ remote,
1072
+ };
1073
+ }
1074
+ }
1075
+ }
1076
+ return;
1077
+ }
1078
+ // Function to match a remote with its name or alias
1079
+ function matchRemote(remotes, nameOrAlias) {
1080
+ for (const remote of remotes) {
1081
+ const isNameMatched = nameOrAlias === remote.name;
1082
+ if (isNameMatched) {
1083
+ return remote;
1084
+ }
1085
+ const isAliasMatched = remote.alias && nameOrAlias === remote.alias;
1086
+ if (isAliasMatched) {
1087
+ return remote;
1088
+ }
1089
+ }
1090
+ return;
1091
+ }
1092
+
1093
+ function registerPlugins(plugins, instance) {
1094
+ const globalPlugins = getGlobalHostPlugins();
1095
+ const hookInstances = [
1096
+ instance.hooks,
1097
+ instance.remoteHandler.hooks,
1098
+ instance.sharedHandler.hooks,
1099
+ instance.snapshotHandler.hooks,
1100
+ instance.loaderHook,
1101
+ instance.bridgeHook,
1102
+ ];
1103
+ // Incorporate global plugins
1104
+ if (globalPlugins.length > 0) {
1105
+ globalPlugins.forEach((plugin) => {
1106
+ if (plugins?.find((item) => item.name !== plugin.name)) {
1107
+ plugins.push(plugin);
1108
+ }
1109
+ });
1110
+ }
1111
+ if (plugins && plugins.length > 0) {
1112
+ plugins.forEach((plugin) => {
1113
+ hookInstances.forEach((hookInstance) => {
1114
+ hookInstance.applyPlugin(plugin, instance);
1115
+ });
1116
+ });
1117
+ }
1118
+ return plugins;
1119
+ }
1120
+
1121
+ const importCallback = '.then(callbacks[0]).catch(callbacks[1])';
1122
+ async function loadEsmEntry({ entry, remoteEntryExports, }) {
1123
+ return new Promise((resolve, reject) => {
1124
+ try {
1125
+ if (!remoteEntryExports) {
1126
+ if (typeof FEDERATION_ALLOW_NEW_FUNCTION !== 'undefined') {
1127
+ new Function('callbacks', `import("${entry}")${importCallback}`)([
1128
+ resolve,
1129
+ reject,
1130
+ ]);
1131
+ }
1132
+ else {
1133
+ import(/* webpackIgnore: true */ /* @vite-ignore */ entry)
1134
+ .then(resolve)
1135
+ .catch(reject);
1136
+ }
1137
+ }
1138
+ else {
1139
+ resolve(remoteEntryExports);
1140
+ }
1141
+ }
1142
+ catch (e) {
1143
+ reject(e);
1144
+ }
1145
+ });
1146
+ }
1147
+ async function loadSystemJsEntry({ entry, remoteEntryExports, }) {
1148
+ return new Promise((resolve, reject) => {
1149
+ try {
1150
+ if (!remoteEntryExports) {
1151
+ //@ts-ignore
1152
+ if (typeof __system_context__ === 'undefined') {
1153
+ //@ts-ignore
1154
+ System.import(entry).then(resolve).catch(reject);
1155
+ }
1156
+ else {
1157
+ new Function('callbacks', `System.import("${entry}")${importCallback}`)([resolve, reject]);
1158
+ }
1159
+ }
1160
+ else {
1161
+ resolve(remoteEntryExports);
1162
+ }
1163
+ }
1164
+ catch (e) {
1165
+ reject(e);
1166
+ }
1167
+ });
1168
+ }
1169
+ function handleRemoteEntryLoaded(name, globalName, entry) {
1170
+ const { remoteEntryKey, entryExports } = getRemoteEntryExports(name, globalName);
1171
+ assert(entryExports, getShortErrorMsg(RUNTIME_001, runtimeDescMap, {
1172
+ remoteName: name,
1173
+ remoteEntryUrl: entry,
1174
+ remoteEntryKey,
1175
+ }));
1176
+ return entryExports;
1177
+ }
1178
+ async function loadEntryScript({ name, globalName, entry, loaderHook, getEntryUrl, }) {
1179
+ const { entryExports: remoteEntryExports } = getRemoteEntryExports(name, globalName);
1180
+ if (remoteEntryExports) {
1181
+ return remoteEntryExports;
1182
+ }
1183
+ // if getEntryUrl is passed, use the getEntryUrl to get the entry url
1184
+ const url = getEntryUrl ? getEntryUrl(entry) : entry;
1185
+ return loadScript(url, {
1186
+ attrs: {},
1187
+ createScriptHook: (url, attrs) => {
1188
+ const res = loaderHook.lifecycle.createScript.emit({ url, attrs });
1189
+ if (!res)
1190
+ return;
1191
+ if (res instanceof HTMLScriptElement) {
1192
+ return res;
1193
+ }
1194
+ if ('script' in res || 'timeout' in res) {
1195
+ return res;
1196
+ }
1197
+ return;
1198
+ },
1199
+ })
1200
+ .then(() => {
1201
+ return handleRemoteEntryLoaded(name, globalName, entry);
1202
+ })
1203
+ .catch((e) => {
1204
+ assert(undefined, getShortErrorMsg(RUNTIME_008, runtimeDescMap, {
1205
+ remoteName: name,
1206
+ resourceUrl: entry,
1207
+ }));
1208
+ throw e;
1209
+ });
1210
+ }
1211
+ async function loadEntryDom({ remoteInfo, remoteEntryExports, loaderHook, getEntryUrl, }) {
1212
+ const { entry, entryGlobalName: globalName, name, type } = remoteInfo;
1213
+ switch (type) {
1214
+ case 'esm':
1215
+ case 'module':
1216
+ return loadEsmEntry({ entry, remoteEntryExports });
1217
+ case 'system':
1218
+ return loadSystemJsEntry({ entry, remoteEntryExports });
1219
+ default:
1220
+ return loadEntryScript({
1221
+ entry,
1222
+ globalName,
1223
+ name,
1224
+ loaderHook,
1225
+ getEntryUrl,
1226
+ });
1227
+ }
1228
+ }
1229
+ async function loadEntryNode({ remoteInfo, loaderHook, }) {
1230
+ const { entry, entryGlobalName: globalName, name, type } = remoteInfo;
1231
+ const { entryExports: remoteEntryExports } = getRemoteEntryExports(name, globalName);
1232
+ if (remoteEntryExports) {
1233
+ return remoteEntryExports;
1234
+ }
1235
+ return loadScriptNode(entry, {
1236
+ attrs: { name, globalName, type },
1237
+ loaderHook: {
1238
+ createScriptHook: (url, attrs = {}) => {
1239
+ const res = loaderHook.lifecycle.createScript.emit({ url, attrs });
1240
+ if (!res)
1241
+ return;
1242
+ if ('url' in res) {
1243
+ return res;
1244
+ }
1245
+ return;
1246
+ },
1247
+ },
1248
+ })
1249
+ .then(() => {
1250
+ return handleRemoteEntryLoaded(name, globalName, entry);
1251
+ })
1252
+ .catch((e) => {
1253
+ throw e;
1254
+ });
1255
+ }
1256
+ function getRemoteEntryUniqueKey(remoteInfo) {
1257
+ const { entry, name } = remoteInfo;
1258
+ return composeKeyWithSeparator(name, entry);
1259
+ }
1260
+ async function getRemoteEntry(params) {
1261
+ const { origin, remoteEntryExports, remoteInfo, getEntryUrl, _inErrorHandling = false, } = params;
1262
+ const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
1263
+ if (remoteEntryExports) {
1264
+ return remoteEntryExports;
1265
+ }
1266
+ if (!globalLoading[uniqueKey]) {
1267
+ const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;
1268
+ const loaderHook = origin.loaderHook;
1269
+ globalLoading[uniqueKey] = loadEntryHook
1270
+ .emit({
1271
+ loaderHook,
1272
+ remoteInfo,
1273
+ remoteEntryExports,
1274
+ })
1275
+ .then((res) => {
1276
+ if (res) {
1277
+ return res;
1278
+ }
1279
+ // Use ENV_TARGET if defined, otherwise fallback to isBrowserEnv, must keep this
1280
+ const isWebEnvironment = typeof ENV_TARGET !== 'undefined'
1281
+ ? ENV_TARGET === 'web'
1282
+ : isBrowserEnv();
1283
+ return isWebEnvironment
1284
+ ? loadEntryDom({
1285
+ remoteInfo,
1286
+ remoteEntryExports,
1287
+ loaderHook,
1288
+ getEntryUrl,
1289
+ })
1290
+ : loadEntryNode({ remoteInfo, loaderHook });
1291
+ })
1292
+ .catch(async (err) => {
1293
+ const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
1294
+ const isScriptLoadError = err instanceof Error && err.message.includes(RUNTIME_008);
1295
+ if (isScriptLoadError && !_inErrorHandling) {
1296
+ const wrappedGetRemoteEntry = (params) => {
1297
+ return getRemoteEntry({ ...params, _inErrorHandling: true });
1298
+ };
1299
+ const RemoteEntryExports = await origin.loaderHook.lifecycle.loadEntryError.emit({
1300
+ getRemoteEntry: wrappedGetRemoteEntry,
1301
+ origin,
1302
+ remoteInfo: remoteInfo,
1303
+ remoteEntryExports,
1304
+ globalLoading,
1305
+ uniqueKey,
1306
+ });
1307
+ if (RemoteEntryExports) {
1308
+ return RemoteEntryExports;
1309
+ }
1310
+ }
1311
+ throw err;
1312
+ });
1313
+ }
1314
+ return globalLoading[uniqueKey];
1315
+ }
1316
+ function getRemoteInfo(remote) {
1317
+ return {
1318
+ ...remote,
1319
+ entry: 'entry' in remote ? remote.entry : '',
1320
+ type: remote.type || DEFAULT_REMOTE_TYPE,
1321
+ entryGlobalName: remote.entryGlobalName || remote.name,
1322
+ shareScope: remote.shareScope || DEFAULT_SCOPE,
1323
+ };
1324
+ }
1325
+
1326
+ function defaultPreloadArgs(preloadConfig) {
1327
+ return {
1328
+ resourceCategory: 'sync',
1329
+ share: true,
1330
+ depsRemote: true,
1331
+ prefetchInterface: false,
1332
+ ...preloadConfig,
1333
+ };
1334
+ }
1335
+ function formatPreloadArgs(remotes, preloadArgs) {
1336
+ return preloadArgs.map((args) => {
1337
+ const remoteInfo = matchRemote(remotes, args.nameOrAlias);
1338
+ assert(remoteInfo, `Unable to preload ${args.nameOrAlias} as it is not included in ${!remoteInfo &&
1339
+ safeToString({
1340
+ remoteInfo,
1341
+ remotes,
1342
+ })}`);
1343
+ return {
1344
+ remote: remoteInfo,
1345
+ preloadConfig: defaultPreloadArgs(args),
1346
+ };
1347
+ });
1348
+ }
1349
+ function normalizePreloadExposes(exposes) {
1350
+ if (!exposes) {
1351
+ return [];
1352
+ }
1353
+ return exposes.map((expose) => {
1354
+ if (expose === '.') {
1355
+ return expose;
1356
+ }
1357
+ if (expose.startsWith('./')) {
1358
+ return expose.replace('./', '');
1359
+ }
1360
+ return expose;
1361
+ });
1362
+ }
1363
+ function preloadAssets(remoteInfo, host, assets,
1364
+ // It is used to distinguish preload from load remote parallel loading
1365
+ useLinkPreload = true) {
1366
+ const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
1367
+ if (host.options.inBrowser) {
1368
+ entryAssets.forEach((asset) => {
1369
+ const { moduleInfo } = asset;
1370
+ const module = host.moduleCache.get(remoteInfo.name);
1371
+ if (module) {
1372
+ getRemoteEntry({
1373
+ origin: host,
1374
+ remoteInfo: moduleInfo,
1375
+ remoteEntryExports: module.remoteEntryExports,
1376
+ });
1377
+ }
1378
+ else {
1379
+ getRemoteEntry({
1380
+ origin: host,
1381
+ remoteInfo: moduleInfo,
1382
+ remoteEntryExports: undefined,
1383
+ });
1384
+ }
1385
+ });
1386
+ if (useLinkPreload) {
1387
+ const defaultAttrs = {
1388
+ rel: 'preload',
1389
+ as: 'style',
1390
+ };
1391
+ cssAssets.forEach((cssUrl) => {
1392
+ const { link: cssEl, needAttach } = createLink({
1393
+ url: cssUrl,
1394
+ cb: () => {
1395
+ // noop
1396
+ },
1397
+ attrs: defaultAttrs,
1398
+ createLinkHook: (url, attrs) => {
1399
+ const res = host.loaderHook.lifecycle.createLink.emit({
1400
+ url,
1401
+ attrs,
1402
+ });
1403
+ if (res instanceof HTMLLinkElement) {
1404
+ return res;
1405
+ }
1406
+ return;
1407
+ },
1408
+ });
1409
+ needAttach && document.head.appendChild(cssEl);
1410
+ });
1411
+ }
1412
+ else {
1413
+ const defaultAttrs = {
1414
+ rel: 'stylesheet',
1415
+ type: 'text/css',
1416
+ };
1417
+ cssAssets.forEach((cssUrl) => {
1418
+ const { link: cssEl, needAttach } = createLink({
1419
+ url: cssUrl,
1420
+ cb: () => {
1421
+ // noop
1422
+ },
1423
+ attrs: defaultAttrs,
1424
+ createLinkHook: (url, attrs) => {
1425
+ const res = host.loaderHook.lifecycle.createLink.emit({
1426
+ url,
1427
+ attrs,
1428
+ });
1429
+ if (res instanceof HTMLLinkElement) {
1430
+ return res;
1431
+ }
1432
+ return;
1433
+ },
1434
+ needDeleteLink: false,
1435
+ });
1436
+ needAttach && document.head.appendChild(cssEl);
1437
+ });
1438
+ }
1439
+ if (useLinkPreload) {
1440
+ const defaultAttrs = {
1441
+ rel: 'preload',
1442
+ as: 'script',
1443
+ };
1444
+ jsAssetsWithoutEntry.forEach((jsUrl) => {
1445
+ const { link: linkEl, needAttach } = createLink({
1446
+ url: jsUrl,
1447
+ cb: () => {
1448
+ // noop
1449
+ },
1450
+ attrs: defaultAttrs,
1451
+ createLinkHook: (url, attrs) => {
1452
+ const res = host.loaderHook.lifecycle.createLink.emit({
1453
+ url,
1454
+ attrs,
1455
+ });
1456
+ if (res instanceof HTMLLinkElement) {
1457
+ return res;
1458
+ }
1459
+ return;
1460
+ },
1461
+ });
1462
+ needAttach && document.head.appendChild(linkEl);
1463
+ });
1464
+ }
1465
+ else {
1466
+ const defaultAttrs = {
1467
+ fetchpriority: 'high',
1468
+ type: remoteInfo?.type === 'module' ? 'module' : 'text/javascript',
1469
+ };
1470
+ jsAssetsWithoutEntry.forEach((jsUrl) => {
1471
+ const { script: scriptEl, needAttach } = createScript({
1472
+ url: jsUrl,
1473
+ cb: () => {
1474
+ // noop
1475
+ },
1476
+ attrs: defaultAttrs,
1477
+ createScriptHook: (url, attrs) => {
1478
+ const res = host.loaderHook.lifecycle.createScript.emit({
1479
+ url,
1480
+ attrs,
1481
+ });
1482
+ if (res instanceof HTMLScriptElement) {
1483
+ return res;
1484
+ }
1485
+ return;
1486
+ },
1487
+ needDeleteScript: true,
1488
+ });
1489
+ needAttach && document.head.appendChild(scriptEl);
1490
+ });
1491
+ }
1492
+ }
1493
+ }
1494
+
1495
+ const ShareUtils = {
1496
+ getRegisteredShare,
1497
+ getGlobalShareScope,
1498
+ };
1499
+ const GlobalUtils = {
1500
+ Global,
1501
+ nativeGlobal,
1502
+ resetFederationGlobalInfo,
1503
+ setGlobalFederationInstance,
1504
+ getGlobalFederationConstructor,
1505
+ setGlobalFederationConstructor,
1506
+ getInfoWithoutType,
1507
+ getGlobalSnapshot,
1508
+ getTargetSnapshotInfoByModuleInfo,
1509
+ getGlobalSnapshotInfoByModuleInfo,
1510
+ setGlobalSnapshotInfoByModuleInfo,
1511
+ addGlobalSnapshot,
1512
+ getRemoteEntryExports,
1513
+ registerGlobalPlugins,
1514
+ getGlobalHostPlugins,
1515
+ getPreloaded,
1516
+ setPreloaded,
1517
+ };
1518
+ var helpers = {
1519
+ global: GlobalUtils,
1520
+ share: ShareUtils,
1521
+ utils: {
1522
+ matchRemoteWithNameAndExpose,
1523
+ preloadAssets,
1524
+ getRemoteInfo,
1525
+ },
1526
+ };
1527
+
1528
+ class Module {
1529
+ constructor({ remoteInfo, host, }) {
1530
+ this.inited = false;
1531
+ this.lib = undefined;
1532
+ this.remoteInfo = remoteInfo;
1533
+ this.host = host;
1534
+ }
1535
+ async getEntry() {
1536
+ if (this.remoteEntryExports) {
1537
+ return this.remoteEntryExports;
1538
+ }
1539
+ let remoteEntryExports;
1540
+ remoteEntryExports = await getRemoteEntry({
1541
+ origin: this.host,
1542
+ remoteInfo: this.remoteInfo,
1543
+ remoteEntryExports: this.remoteEntryExports,
1544
+ });
1545
+ assert(remoteEntryExports, `remoteEntryExports is undefined \n ${safeToString(this.remoteInfo)}`);
1546
+ this.remoteEntryExports = remoteEntryExports;
1547
+ return this.remoteEntryExports;
1548
+ }
1549
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1550
+ async get(id, expose, options, remoteSnapshot) {
1551
+ const { loadFactory = true } = options || { loadFactory: true };
1552
+ // Get remoteEntry.js
1553
+ const remoteEntryExports = await this.getEntry();
1554
+ if (!this.inited) {
1555
+ const localShareScopeMap = this.host.shareScopeMap;
1556
+ const shareScopeKeys = Array.isArray(this.remoteInfo.shareScope)
1557
+ ? this.remoteInfo.shareScope
1558
+ : [this.remoteInfo.shareScope];
1559
+ if (!shareScopeKeys.length) {
1560
+ shareScopeKeys.push('default');
1561
+ }
1562
+ shareScopeKeys.forEach((shareScopeKey) => {
1563
+ if (!localShareScopeMap[shareScopeKey]) {
1564
+ localShareScopeMap[shareScopeKey] = {};
1565
+ }
1566
+ });
1567
+ // TODO: compate legacy init params, should use shareScopeMap if exist
1568
+ const shareScope = localShareScopeMap[shareScopeKeys[0]];
1569
+ const initScope = [];
1570
+ const remoteEntryInitOptions = {
1571
+ version: this.remoteInfo.version || '',
1572
+ shareScopeKeys: Array.isArray(this.remoteInfo.shareScope)
1573
+ ? shareScopeKeys
1574
+ : this.remoteInfo.shareScope || 'default',
1575
+ };
1576
+ // Help to find host instance
1577
+ Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
1578
+ value: localShareScopeMap,
1579
+ // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
1580
+ enumerable: false,
1581
+ });
1582
+ const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
1583
+ shareScope,
1584
+ // @ts-ignore shareScopeMap will be set by Object.defineProperty
1585
+ remoteEntryInitOptions,
1586
+ initScope,
1587
+ remoteInfo: this.remoteInfo,
1588
+ origin: this.host,
1589
+ });
1590
+ if (typeof remoteEntryExports?.init === 'undefined') {
1591
+ error(getShortErrorMsg(RUNTIME_002, runtimeDescMap, {
1592
+ hostName: this.host.name,
1593
+ remoteName: this.remoteInfo.name,
1594
+ remoteEntryUrl: this.remoteInfo.entry,
1595
+ remoteEntryKey: this.remoteInfo.entryGlobalName,
1596
+ }));
1597
+ }
1598
+ await remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
1599
+ await this.host.hooks.lifecycle.initContainer.emit({
1600
+ ...initContainerOptions,
1601
+ id,
1602
+ remoteSnapshot,
1603
+ remoteEntryExports,
1604
+ });
1605
+ }
1606
+ this.lib = remoteEntryExports;
1607
+ this.inited = true;
1608
+ let moduleFactory;
1609
+ moduleFactory = await this.host.loaderHook.lifecycle.getModuleFactory.emit({
1610
+ remoteEntryExports,
1611
+ expose,
1612
+ moduleInfo: this.remoteInfo,
1613
+ });
1614
+ // get exposeGetter
1615
+ if (!moduleFactory) {
1616
+ moduleFactory = await remoteEntryExports.get(expose);
1617
+ }
1618
+ assert(moduleFactory, `${getFMId(this.remoteInfo)} remote don't export ${expose}.`);
1619
+ // keep symbol for module name always one format
1620
+ const symbolName = processModuleAlias(this.remoteInfo.name, expose);
1621
+ const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);
1622
+ if (!loadFactory) {
1623
+ return wrapModuleFactory;
1624
+ }
1625
+ const exposeContent = await wrapModuleFactory();
1626
+ return exposeContent;
1627
+ }
1628
+ wraperFactory(moduleFactory, id) {
1629
+ function defineModuleId(res, id) {
1630
+ if (res &&
1631
+ typeof res === 'object' &&
1632
+ Object.isExtensible(res) &&
1633
+ !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))) {
1634
+ Object.defineProperty(res, Symbol.for('mf_module_id'), {
1635
+ value: id,
1636
+ enumerable: false,
1637
+ });
1638
+ }
1639
+ }
1640
+ if (moduleFactory instanceof Promise) {
1641
+ return async () => {
1642
+ const res = await moduleFactory();
1643
+ // This parameter is used for bridge debugging
1644
+ defineModuleId(res, id);
1645
+ return res;
1646
+ };
1647
+ }
1648
+ else {
1649
+ return () => {
1650
+ const res = moduleFactory();
1651
+ // This parameter is used for bridge debugging
1652
+ defineModuleId(res, id);
1653
+ return res;
1654
+ };
1655
+ }
1656
+ }
1657
+ }
1658
+
1659
+ class SyncHook {
1660
+ constructor(type) {
1661
+ this.type = '';
1662
+ this.listeners = new Set();
1663
+ if (type) {
1664
+ this.type = type;
1665
+ }
1666
+ }
1667
+ on(fn) {
1668
+ if (typeof fn === 'function') {
1669
+ this.listeners.add(fn);
1670
+ }
1671
+ }
1672
+ once(fn) {
1673
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
1674
+ const self = this;
1675
+ this.on(function wrapper(...args) {
1676
+ self.remove(wrapper);
1677
+ // eslint-disable-next-line prefer-spread
1678
+ return fn.apply(null, args);
1679
+ });
1680
+ }
1681
+ emit(...data) {
1682
+ let result;
1683
+ if (this.listeners.size > 0) {
1684
+ // eslint-disable-next-line prefer-spread
1685
+ this.listeners.forEach((fn) => {
1686
+ result = fn(...data);
1687
+ });
1688
+ }
1689
+ return result;
1690
+ }
1691
+ remove(fn) {
1692
+ this.listeners.delete(fn);
1693
+ }
1694
+ removeAll() {
1695
+ this.listeners.clear();
1696
+ }
1697
+ }
1698
+
1699
+ class AsyncHook extends SyncHook {
1700
+ emit(...data) {
1701
+ let result;
1702
+ const ls = Array.from(this.listeners);
1703
+ if (ls.length > 0) {
1704
+ let i = 0;
1705
+ const call = (prev) => {
1706
+ if (prev === false) {
1707
+ return false; // Abort process
1708
+ }
1709
+ else if (i < ls.length) {
1710
+ return Promise.resolve(ls[i++].apply(null, data)).then(call);
1711
+ }
1712
+ else {
1713
+ return prev;
1714
+ }
1715
+ };
1716
+ result = call();
1717
+ }
1718
+ return Promise.resolve(result);
1719
+ }
1720
+ }
1721
+
1722
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1723
+ function checkReturnData(originalData, returnedData) {
1724
+ if (!isObject(returnedData)) {
1725
+ return false;
1726
+ }
1727
+ if (originalData !== returnedData) {
1728
+ // eslint-disable-next-line no-restricted-syntax
1729
+ for (const key in originalData) {
1730
+ if (!(key in returnedData)) {
1731
+ return false;
1732
+ }
1733
+ }
1734
+ }
1735
+ return true;
1736
+ }
1737
+ class SyncWaterfallHook extends SyncHook {
1738
+ constructor(type) {
1739
+ super();
1740
+ this.onerror = error;
1741
+ this.type = type;
1742
+ }
1743
+ emit(data) {
1744
+ if (!isObject(data)) {
1745
+ error(`The data for the "${this.type}" hook should be an object.`);
1746
+ }
1747
+ for (const fn of this.listeners) {
1748
+ try {
1749
+ const tempData = fn(data);
1750
+ if (checkReturnData(data, tempData)) {
1751
+ data = tempData;
1752
+ }
1753
+ else {
1754
+ this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
1755
+ break;
1756
+ }
1757
+ }
1758
+ catch (e) {
1759
+ warn(e);
1760
+ this.onerror(e);
1761
+ }
1762
+ }
1763
+ return data;
1764
+ }
1765
+ }
1766
+
1767
+ class AsyncWaterfallHook extends SyncHook {
1768
+ constructor(type) {
1769
+ super();
1770
+ this.onerror = error;
1771
+ this.type = type;
1772
+ }
1773
+ emit(data) {
1774
+ if (!isObject(data)) {
1775
+ error(`The response data for the "${this.type}" hook must be an object.`);
1776
+ }
1777
+ const ls = Array.from(this.listeners);
1778
+ if (ls.length > 0) {
1779
+ let i = 0;
1780
+ const processError = (e) => {
1781
+ warn(e);
1782
+ this.onerror(e);
1783
+ return data;
1784
+ };
1785
+ const call = (prevData) => {
1786
+ if (checkReturnData(data, prevData)) {
1787
+ data = prevData;
1788
+ if (i < ls.length) {
1789
+ try {
1790
+ return Promise.resolve(ls[i++](data)).then(call, processError);
1791
+ }
1792
+ catch (e) {
1793
+ return processError(e);
1794
+ }
1795
+ }
1796
+ }
1797
+ else {
1798
+ this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
1799
+ }
1800
+ return data;
1801
+ };
1802
+ return Promise.resolve(call(data));
1803
+ }
1804
+ return Promise.resolve(data);
1805
+ }
1806
+ }
1807
+
1808
+ class PluginSystem {
1809
+ constructor(lifecycle) {
1810
+ this.registerPlugins = {};
1811
+ this.lifecycle = lifecycle;
1812
+ this.lifecycleKeys = Object.keys(lifecycle);
1813
+ }
1814
+ applyPlugin(plugin, instance) {
1815
+ assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
1816
+ // The plugin's name is mandatory and must be unique
1817
+ const pluginName = plugin.name;
1818
+ assert(pluginName, 'A name must be provided by the plugin.');
1819
+ if (!this.registerPlugins[pluginName]) {
1820
+ this.registerPlugins[pluginName] = plugin;
1821
+ plugin.apply?.(instance);
1822
+ Object.keys(this.lifecycle).forEach((key) => {
1823
+ const pluginLife = plugin[key];
1824
+ if (pluginLife) {
1825
+ this.lifecycle[key].on(pluginLife);
1826
+ }
1827
+ });
1828
+ }
1829
+ }
1830
+ removePlugin(pluginName) {
1831
+ assert(pluginName, 'A name is required.');
1832
+ const plugin = this.registerPlugins[pluginName];
1833
+ assert(plugin, `The plugin "${pluginName}" is not registered.`);
1834
+ Object.keys(plugin).forEach((key) => {
1835
+ if (key !== 'name') {
1836
+ this.lifecycle[key].remove(plugin[key]);
1837
+ }
1838
+ });
1839
+ }
1840
+ }
1841
+
1842
+ function assignRemoteInfo(remoteInfo, remoteSnapshot) {
1843
+ const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);
1844
+ if (!remoteEntryInfo.url) {
1845
+ error(`The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`);
1846
+ }
1847
+ let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);
1848
+ if (!isBrowserEnv() && !entryUrl.startsWith('http')) {
1849
+ entryUrl = `https:${entryUrl}`;
1850
+ }
1851
+ remoteInfo.type = remoteEntryInfo.type;
1852
+ remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
1853
+ remoteInfo.entry = entryUrl;
1854
+ remoteInfo.version = remoteSnapshot.version;
1855
+ remoteInfo.buildVersion = remoteSnapshot.buildVersion;
1856
+ }
1857
+ function snapshotPlugin() {
1858
+ return {
1859
+ name: 'snapshot-plugin',
1860
+ async afterResolve(args) {
1861
+ const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;
1862
+ if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {
1863
+ const { remoteSnapshot, globalSnapshot } = await origin.snapshotHandler.loadRemoteSnapshotInfo({
1864
+ moduleInfo: remote,
1865
+ id,
1866
+ });
1867
+ assignRemoteInfo(remoteInfo, remoteSnapshot);
1868
+ // preloading assets
1869
+ const preloadOptions = {
1870
+ remote,
1871
+ preloadConfig: {
1872
+ nameOrAlias: pkgNameOrAlias,
1873
+ exposes: [expose],
1874
+ resourceCategory: 'sync',
1875
+ share: false,
1876
+ depsRemote: false,
1877
+ },
1878
+ };
1879
+ const assets = await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
1880
+ origin,
1881
+ preloadOptions,
1882
+ remoteInfo,
1883
+ remote,
1884
+ remoteSnapshot,
1885
+ globalSnapshot,
1886
+ });
1887
+ if (assets) {
1888
+ preloadAssets(remoteInfo, origin, assets, false);
1889
+ }
1890
+ return {
1891
+ ...args,
1892
+ remoteSnapshot,
1893
+ };
1894
+ }
1895
+ return args;
1896
+ },
1897
+ };
1898
+ }
1899
+
1900
+ // name
1901
+ // name:version
1902
+ function splitId(id) {
1903
+ const splitInfo = id.split(':');
1904
+ if (splitInfo.length === 1) {
1905
+ return {
1906
+ name: splitInfo[0],
1907
+ version: undefined,
1908
+ };
1909
+ }
1910
+ else if (splitInfo.length === 2) {
1911
+ return {
1912
+ name: splitInfo[0],
1913
+ version: splitInfo[1],
1914
+ };
1915
+ }
1916
+ else {
1917
+ return {
1918
+ name: splitInfo[1],
1919
+ version: splitInfo[2],
1920
+ };
1921
+ }
1922
+ }
1923
+ // Traverse all nodes in moduleInfo and traverse the entire snapshot
1924
+ function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo = {}, remoteSnapshot) {
1925
+ const id = getFMId(remoteInfo);
1926
+ const { value: snapshotValue } = getInfoWithoutType(globalSnapshot, id);
1927
+ const effectiveRemoteSnapshot = remoteSnapshot || snapshotValue;
1928
+ if (effectiveRemoteSnapshot && !isManifestProvider(effectiveRemoteSnapshot)) {
1929
+ traverse(effectiveRemoteSnapshot, remoteInfo, isRoot);
1930
+ if (effectiveRemoteSnapshot.remotesInfo) {
1931
+ const remoteKeys = Object.keys(effectiveRemoteSnapshot.remotesInfo);
1932
+ for (const key of remoteKeys) {
1933
+ if (memo[key]) {
1934
+ continue;
1935
+ }
1936
+ memo[key] = true;
1937
+ const subRemoteInfo = splitId(key);
1938
+ const remoteValue = effectiveRemoteSnapshot.remotesInfo[key];
1939
+ traverseModuleInfo(globalSnapshot, {
1940
+ name: subRemoteInfo.name,
1941
+ version: remoteValue.matchedVersion,
1942
+ }, traverse, false, memo, undefined);
1943
+ }
1944
+ }
1945
+ }
1946
+ }
1947
+ const isExisted = (type, url) => {
1948
+ return document.querySelector(`${type}[${type === 'link' ? 'href' : 'src'}="${url}"]`);
1949
+ };
1950
+ // eslint-disable-next-line max-lines-per-function
1951
+ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, remoteSnapshot) {
1952
+ const cssAssets = [];
1953
+ const jsAssets = [];
1954
+ const entryAssets = [];
1955
+ const loadedSharedJsAssets = new Set();
1956
+ const loadedSharedCssAssets = new Set();
1957
+ const { options } = origin;
1958
+ const { preloadConfig: rootPreloadConfig } = preloadOptions;
1959
+ const { depsRemote } = rootPreloadConfig;
1960
+ const memo = {};
1961
+ traverseModuleInfo(globalSnapshot, remote, (moduleInfoSnapshot, remoteInfo, isRoot) => {
1962
+ let preloadConfig;
1963
+ if (isRoot) {
1964
+ preloadConfig = rootPreloadConfig;
1965
+ }
1966
+ else {
1967
+ if (Array.isArray(depsRemote)) {
1968
+ // eslint-disable-next-line array-callback-return
1969
+ const findPreloadConfig = depsRemote.find((remoteConfig) => {
1970
+ if (remoteConfig.nameOrAlias === remoteInfo.name ||
1971
+ remoteConfig.nameOrAlias === remoteInfo.alias) {
1972
+ return true;
1973
+ }
1974
+ return false;
1975
+ });
1976
+ if (!findPreloadConfig) {
1977
+ return;
1978
+ }
1979
+ preloadConfig = defaultPreloadArgs(findPreloadConfig);
1980
+ }
1981
+ else if (depsRemote === true) {
1982
+ preloadConfig = rootPreloadConfig;
1983
+ }
1984
+ else {
1985
+ return;
1986
+ }
1987
+ }
1988
+ const remoteEntryUrl = getResourceUrl(moduleInfoSnapshot, getRemoteEntryInfoFromSnapshot(moduleInfoSnapshot).url);
1989
+ if (remoteEntryUrl) {
1990
+ entryAssets.push({
1991
+ name: remoteInfo.name,
1992
+ moduleInfo: {
1993
+ name: remoteInfo.name,
1994
+ entry: remoteEntryUrl,
1995
+ type: 'remoteEntryType' in moduleInfoSnapshot
1996
+ ? moduleInfoSnapshot.remoteEntryType
1997
+ : 'global',
1998
+ entryGlobalName: 'globalName' in moduleInfoSnapshot
1999
+ ? moduleInfoSnapshot.globalName
2000
+ : remoteInfo.name,
2001
+ shareScope: '',
2002
+ version: 'version' in moduleInfoSnapshot
2003
+ ? moduleInfoSnapshot.version
2004
+ : undefined,
2005
+ },
2006
+ url: remoteEntryUrl,
2007
+ });
2008
+ }
2009
+ let moduleAssetsInfo = 'modules' in moduleInfoSnapshot ? moduleInfoSnapshot.modules : [];
2010
+ const normalizedPreloadExposes = normalizePreloadExposes(preloadConfig.exposes);
2011
+ if (normalizedPreloadExposes.length && 'modules' in moduleInfoSnapshot) {
2012
+ moduleAssetsInfo = moduleInfoSnapshot?.modules?.reduce((assets, moduleAssetInfo) => {
2013
+ if (normalizedPreloadExposes?.indexOf(moduleAssetInfo.moduleName) !==
2014
+ -1) {
2015
+ assets.push(moduleAssetInfo);
2016
+ }
2017
+ return assets;
2018
+ }, []);
2019
+ }
2020
+ function handleAssets(assets) {
2021
+ const assetsRes = assets.map((asset) => getResourceUrl(moduleInfoSnapshot, asset));
2022
+ if (preloadConfig.filter) {
2023
+ return assetsRes.filter(preloadConfig.filter);
2024
+ }
2025
+ return assetsRes;
2026
+ }
2027
+ if (moduleAssetsInfo) {
2028
+ const assetsLength = moduleAssetsInfo.length;
2029
+ for (let index = 0; index < assetsLength; index++) {
2030
+ const assetsInfo = moduleAssetsInfo[index];
2031
+ const exposeFullPath = `${remoteInfo.name}/${assetsInfo.moduleName}`;
2032
+ origin.remoteHandler.hooks.lifecycle.handlePreloadModule.emit({
2033
+ id: assetsInfo.moduleName === '.' ? remoteInfo.name : exposeFullPath,
2034
+ name: remoteInfo.name,
2035
+ remoteSnapshot: moduleInfoSnapshot,
2036
+ preloadConfig,
2037
+ remote: remoteInfo,
2038
+ origin,
2039
+ });
2040
+ const preloaded = getPreloaded(exposeFullPath);
2041
+ if (preloaded) {
2042
+ continue;
2043
+ }
2044
+ if (preloadConfig.resourceCategory === 'all') {
2045
+ cssAssets.push(...handleAssets(assetsInfo.assets.css.async));
2046
+ cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
2047
+ jsAssets.push(...handleAssets(assetsInfo.assets.js.async));
2048
+ jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
2049
+ // eslint-disable-next-line no-constant-condition
2050
+ }
2051
+ else if ((preloadConfig.resourceCategory = 'sync')) {
2052
+ cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
2053
+ jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
2054
+ }
2055
+ setPreloaded(exposeFullPath);
2056
+ }
2057
+ }
2058
+ }, true, memo, remoteSnapshot);
2059
+ if (remoteSnapshot.shared && remoteSnapshot.shared.length > 0) {
2060
+ const collectSharedAssets = (shareInfo, snapshotShared) => {
2061
+ const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare);
2062
+ // If the global share does not exist, or the lib function does not exist, it means that the shared has not been loaded yet and can be preloaded.
2063
+ if (registeredShared && typeof registeredShared.lib === 'function') {
2064
+ snapshotShared.assets.js.sync.forEach((asset) => {
2065
+ loadedSharedJsAssets.add(asset);
2066
+ });
2067
+ snapshotShared.assets.css.sync.forEach((asset) => {
2068
+ loadedSharedCssAssets.add(asset);
2069
+ });
2070
+ }
2071
+ };
2072
+ remoteSnapshot.shared.forEach((shared) => {
2073
+ const shareInfos = options.shared?.[shared.sharedName];
2074
+ if (!shareInfos) {
2075
+ return;
2076
+ }
2077
+ // if no version, preload all shared
2078
+ const sharedOptions = shared.version
2079
+ ? shareInfos.find((s) => s.version === shared.version)
2080
+ : shareInfos;
2081
+ if (!sharedOptions) {
2082
+ return;
2083
+ }
2084
+ const arrayShareInfo = arrayOptions(sharedOptions);
2085
+ arrayShareInfo.forEach((s) => {
2086
+ collectSharedAssets(s, shared);
2087
+ });
2088
+ });
2089
+ }
2090
+ const needPreloadJsAssets = jsAssets.filter((asset) => !loadedSharedJsAssets.has(asset) && !isExisted('script', asset));
2091
+ const needPreloadCssAssets = cssAssets.filter((asset) => !loadedSharedCssAssets.has(asset) && !isExisted('link', asset));
2092
+ return {
2093
+ cssAssets: needPreloadCssAssets,
2094
+ jsAssetsWithoutEntry: needPreloadJsAssets,
2095
+ entryAssets: entryAssets.filter((entry) => !isExisted('script', entry.url)),
2096
+ };
2097
+ }
2098
+ const generatePreloadAssetsPlugin = function () {
2099
+ return {
2100
+ name: 'generate-preload-assets-plugin',
2101
+ async generatePreloadAssets(args) {
2102
+ const { origin, preloadOptions, remoteInfo, remote, globalSnapshot, remoteSnapshot, } = args;
2103
+ if (!isBrowserEnv()) {
2104
+ return {
2105
+ cssAssets: [],
2106
+ jsAssetsWithoutEntry: [],
2107
+ entryAssets: [],
2108
+ };
2109
+ }
2110
+ if (isRemoteInfoWithEntry(remote) && isPureRemoteEntry(remote)) {
2111
+ return {
2112
+ cssAssets: [],
2113
+ jsAssetsWithoutEntry: [],
2114
+ entryAssets: [
2115
+ {
2116
+ name: remote.name,
2117
+ url: remote.entry,
2118
+ moduleInfo: {
2119
+ name: remoteInfo.name,
2120
+ entry: remote.entry,
2121
+ type: remoteInfo.type || 'global',
2122
+ entryGlobalName: '',
2123
+ shareScope: '',
2124
+ },
2125
+ },
2126
+ ],
2127
+ };
2128
+ }
2129
+ assignRemoteInfo(remoteInfo, remoteSnapshot);
2130
+ const assets = generatePreloadAssets(origin, preloadOptions, remoteInfo, globalSnapshot, remoteSnapshot);
2131
+ return assets;
2132
+ },
2133
+ };
2134
+ };
2135
+
2136
+ function getGlobalRemoteInfo(moduleInfo, origin) {
2137
+ const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({
2138
+ name: origin.name,
2139
+ version: origin.options.version,
2140
+ });
2141
+ // get remote detail info from global
2142
+ const globalRemoteInfo = hostGlobalSnapshot &&
2143
+ 'remotesInfo' in hostGlobalSnapshot &&
2144
+ hostGlobalSnapshot.remotesInfo &&
2145
+ getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;
2146
+ if (globalRemoteInfo && globalRemoteInfo.matchedVersion) {
2147
+ return {
2148
+ hostGlobalSnapshot,
2149
+ globalSnapshot: getGlobalSnapshot(),
2150
+ remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
2151
+ name: moduleInfo.name,
2152
+ version: globalRemoteInfo.matchedVersion,
2153
+ }),
2154
+ };
2155
+ }
2156
+ return {
2157
+ hostGlobalSnapshot: undefined,
2158
+ globalSnapshot: getGlobalSnapshot(),
2159
+ remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
2160
+ name: moduleInfo.name,
2161
+ version: 'version' in moduleInfo ? moduleInfo.version : undefined,
2162
+ }),
2163
+ };
2164
+ }
2165
+ class SnapshotHandler {
2166
+ constructor(HostInstance) {
2167
+ this.loadingHostSnapshot = null;
2168
+ this.manifestCache = new Map();
2169
+ this.hooks = new PluginSystem({
2170
+ beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
2171
+ loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
2172
+ loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot'),
2173
+ afterLoadSnapshot: new AsyncWaterfallHook('afterLoadSnapshot'),
2174
+ });
2175
+ this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
2176
+ this.HostInstance = HostInstance;
2177
+ this.loaderHook = HostInstance.loaderHook;
2178
+ }
2179
+ // eslint-disable-next-line max-lines-per-function
2180
+ async loadRemoteSnapshotInfo({ moduleInfo, id, expose, }) {
2181
+ const { options } = this.HostInstance;
2182
+ await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({
2183
+ options,
2184
+ moduleInfo,
2185
+ });
2186
+ let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({
2187
+ name: this.HostInstance.options.name,
2188
+ version: this.HostInstance.options.version,
2189
+ });
2190
+ if (!hostSnapshot) {
2191
+ hostSnapshot = {
2192
+ version: this.HostInstance.options.version || '',
2193
+ remoteEntry: '',
2194
+ remotesInfo: {},
2195
+ };
2196
+ addGlobalSnapshot({
2197
+ [this.HostInstance.options.name]: hostSnapshot,
2198
+ });
2199
+ }
2200
+ // In dynamic loadRemote scenarios, incomplete remotesInfo delivery may occur. In such cases, the remotesInfo in the host needs to be completed in the snapshot at runtime.
2201
+ // This ensures the snapshot's integrity and helps the chrome plugin correctly identify all producer modules, ensuring that proxyable producer modules will not be missing.
2202
+ if (hostSnapshot &&
2203
+ 'remotesInfo' in hostSnapshot &&
2204
+ !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
2205
+ if ('version' in moduleInfo || 'entry' in moduleInfo) {
2206
+ hostSnapshot.remotesInfo = {
2207
+ ...hostSnapshot?.remotesInfo,
2208
+ [moduleInfo.name]: {
2209
+ matchedVersion: 'version' in moduleInfo ? moduleInfo.version : moduleInfo.entry,
2210
+ },
2211
+ };
2212
+ }
2213
+ }
2214
+ const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } = this.getGlobalRemoteInfo(moduleInfo);
2215
+ const { remoteSnapshot: globalRemoteSnapshot, globalSnapshot: globalSnapshotRes, } = await this.hooks.lifecycle.loadSnapshot.emit({
2216
+ options,
2217
+ moduleInfo,
2218
+ hostGlobalSnapshot,
2219
+ remoteSnapshot,
2220
+ globalSnapshot,
2221
+ });
2222
+ let mSnapshot;
2223
+ let gSnapshot;
2224
+ // global snapshot includes manifest or module info includes manifest
2225
+ if (globalRemoteSnapshot) {
2226
+ if (isManifestProvider(globalRemoteSnapshot)) {
2227
+ const remoteEntry = isBrowserEnv()
2228
+ ? globalRemoteSnapshot.remoteEntry
2229
+ : globalRemoteSnapshot.ssrRemoteEntry ||
2230
+ globalRemoteSnapshot.remoteEntry ||
2231
+ '';
2232
+ const moduleSnapshot = await this.getManifestJson(remoteEntry, moduleInfo, {});
2233
+ // eslint-disable-next-line @typescript-eslint/no-shadow
2234
+ const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo({
2235
+ ...moduleInfo,
2236
+ // The global remote may be overridden
2237
+ // Therefore, set the snapshot key to the global address of the actual request
2238
+ entry: remoteEntry,
2239
+ }, moduleSnapshot);
2240
+ mSnapshot = moduleSnapshot;
2241
+ gSnapshot = globalSnapshotRes;
2242
+ }
2243
+ else {
2244
+ const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
2245
+ options: this.HostInstance.options,
2246
+ moduleInfo,
2247
+ remoteSnapshot: globalRemoteSnapshot,
2248
+ from: 'global',
2249
+ });
2250
+ mSnapshot = remoteSnapshotRes;
2251
+ gSnapshot = globalSnapshotRes;
2252
+ }
2253
+ }
2254
+ else {
2255
+ if (isRemoteInfoWithEntry(moduleInfo)) {
2256
+ // get from manifest.json and merge remote info from remote server
2257
+ const moduleSnapshot = await this.getManifestJson(moduleInfo.entry, moduleInfo, {});
2258
+ // eslint-disable-next-line @typescript-eslint/no-shadow
2259
+ const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(moduleInfo, moduleSnapshot);
2260
+ const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
2261
+ options: this.HostInstance.options,
2262
+ moduleInfo,
2263
+ remoteSnapshot: moduleSnapshot,
2264
+ from: 'global',
2265
+ });
2266
+ mSnapshot = remoteSnapshotRes;
2267
+ gSnapshot = globalSnapshotRes;
2268
+ }
2269
+ else {
2270
+ error(getShortErrorMsg(RUNTIME_007, runtimeDescMap, {
2271
+ hostName: moduleInfo.name,
2272
+ hostVersion: moduleInfo.version,
2273
+ globalSnapshot: JSON.stringify(globalSnapshotRes),
2274
+ }));
2275
+ }
2276
+ }
2277
+ await this.hooks.lifecycle.afterLoadSnapshot.emit({
2278
+ id,
2279
+ host: this.HostInstance,
2280
+ options,
2281
+ moduleInfo,
2282
+ remoteSnapshot: mSnapshot,
2283
+ });
2284
+ return {
2285
+ remoteSnapshot: mSnapshot,
2286
+ globalSnapshot: gSnapshot,
2287
+ };
2288
+ }
2289
+ getGlobalRemoteInfo(moduleInfo) {
2290
+ return getGlobalRemoteInfo(moduleInfo, this.HostInstance);
2291
+ }
2292
+ async getManifestJson(manifestUrl, moduleInfo, extraOptions) {
2293
+ const getManifest = async () => {
2294
+ let manifestJson = this.manifestCache.get(manifestUrl);
2295
+ if (manifestJson) {
2296
+ return manifestJson;
2297
+ }
2298
+ try {
2299
+ let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {});
2300
+ if (!res || !(res instanceof Response)) {
2301
+ res = await fetch(manifestUrl, {});
2302
+ }
2303
+ manifestJson = (await res.json());
2304
+ }
2305
+ catch (err) {
2306
+ manifestJson =
2307
+ (await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
2308
+ id: manifestUrl,
2309
+ error: err,
2310
+ from: 'runtime',
2311
+ lifecycle: 'afterResolve',
2312
+ origin: this.HostInstance,
2313
+ }));
2314
+ if (!manifestJson) {
2315
+ delete this.manifestLoading[manifestUrl];
2316
+ error(getShortErrorMsg(RUNTIME_003, runtimeDescMap, {
2317
+ manifestUrl,
2318
+ moduleName: moduleInfo.name,
2319
+ hostName: this.HostInstance.options.name,
2320
+ }, `${err}`));
2321
+ }
2322
+ }
2323
+ assert(manifestJson.metaData && manifestJson.exposes && manifestJson.shared, `${manifestUrl} is not a federation manifest`);
2324
+ this.manifestCache.set(manifestUrl, manifestJson);
2325
+ return manifestJson;
2326
+ };
2327
+ const asyncLoadProcess = async () => {
2328
+ const manifestJson = await getManifest();
2329
+ const remoteSnapshot = generateSnapshotFromManifest(manifestJson, {
2330
+ version: manifestUrl,
2331
+ });
2332
+ const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
2333
+ options: this.HostInstance.options,
2334
+ moduleInfo,
2335
+ manifestJson,
2336
+ remoteSnapshot,
2337
+ manifestUrl,
2338
+ from: 'manifest',
2339
+ });
2340
+ return remoteSnapshotRes;
2341
+ };
2342
+ if (!this.manifestLoading[manifestUrl]) {
2343
+ this.manifestLoading[manifestUrl] = asyncLoadProcess().then((res) => res);
2344
+ }
2345
+ return this.manifestLoading[manifestUrl];
2346
+ }
2347
+ }
2348
+
2349
+ class SharedHandler {
2350
+ constructor(host) {
2351
+ this.hooks = new PluginSystem({
2352
+ afterResolve: new AsyncWaterfallHook('afterResolve'),
2353
+ beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
2354
+ // not used yet
2355
+ loadShare: new AsyncHook(),
2356
+ resolveShare: new SyncWaterfallHook('resolveShare'),
2357
+ // maybe will change, temporarily for internal use only
2358
+ initContainerShareScopeMap: new SyncWaterfallHook('initContainerShareScopeMap'),
2359
+ });
2360
+ this.host = host;
2361
+ this.shareScopeMap = {};
2362
+ this.initTokens = {};
2363
+ this._setGlobalShareScopeMap(host.options);
2364
+ }
2365
+ // register shared in shareScopeMap
2366
+ registerShared(globalOptions, userOptions) {
2367
+ const { shareInfos, shared } = formatShareConfigs(globalOptions, userOptions);
2368
+ const sharedKeys = Object.keys(shareInfos);
2369
+ sharedKeys.forEach((sharedKey) => {
2370
+ const sharedVals = shareInfos[sharedKey];
2371
+ sharedVals.forEach((sharedVal) => {
2372
+ const registeredShared = getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
2373
+ if (!registeredShared && sharedVal && sharedVal.lib) {
2374
+ this.setShared({
2375
+ pkgName: sharedKey,
2376
+ lib: sharedVal.lib,
2377
+ get: sharedVal.get,
2378
+ loaded: true,
2379
+ shared: sharedVal,
2380
+ from: userOptions.name,
2381
+ });
2382
+ }
2383
+ });
2384
+ });
2385
+ return {
2386
+ shareInfos,
2387
+ shared,
2388
+ };
2389
+ }
2390
+ async loadShare(pkgName, extraOptions) {
2391
+ const { host } = this;
2392
+ // This function performs the following steps:
2393
+ // 1. Checks if the currently loaded share already exists, if not, it throws an error
2394
+ // 2. Searches globally for a matching share, if found, it uses it directly
2395
+ // 3. If not found, it retrieves it from the current share and stores the obtained share globally.
2396
+ const shareOptions = getTargetSharedOptions({
2397
+ pkgName,
2398
+ extraOptions,
2399
+ shareInfos: host.options.shared,
2400
+ });
2401
+ if (shareOptions?.scope) {
2402
+ await Promise.all(shareOptions.scope.map(async (shareScope) => {
2403
+ await Promise.all(this.initializeSharing(shareScope, {
2404
+ strategy: shareOptions.strategy,
2405
+ }));
2406
+ return;
2407
+ }));
2408
+ }
2409
+ const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
2410
+ pkgName,
2411
+ shareInfo: shareOptions,
2412
+ shared: host.options.shared,
2413
+ origin: host,
2414
+ });
2415
+ const { shareInfo: shareOptionsRes } = loadShareRes;
2416
+ // Assert that shareInfoRes exists, if not, throw an error
2417
+ assert(shareOptionsRes, `Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
2418
+ // Retrieve from cache
2419
+ const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
2420
+ const addUseIn = (shared) => {
2421
+ if (!shared.useIn) {
2422
+ shared.useIn = [];
2423
+ }
2424
+ addUniqueItem(shared.useIn, host.options.name);
2425
+ };
2426
+ if (registeredShared && registeredShared.lib) {
2427
+ addUseIn(registeredShared);
2428
+ return registeredShared.lib;
2429
+ }
2430
+ else if (registeredShared &&
2431
+ registeredShared.loading &&
2432
+ !registeredShared.loaded) {
2433
+ const factory = await registeredShared.loading;
2434
+ registeredShared.loaded = true;
2435
+ if (!registeredShared.lib) {
2436
+ registeredShared.lib = factory;
2437
+ }
2438
+ addUseIn(registeredShared);
2439
+ return factory;
2440
+ }
2441
+ else if (registeredShared) {
2442
+ const asyncLoadProcess = async () => {
2443
+ const factory = await registeredShared.get();
2444
+ addUseIn(registeredShared);
2445
+ registeredShared.loaded = true;
2446
+ registeredShared.lib = factory;
2447
+ return factory;
2448
+ };
2449
+ const loading = asyncLoadProcess();
2450
+ this.setShared({
2451
+ pkgName,
2452
+ loaded: false,
2453
+ shared: registeredShared,
2454
+ from: host.options.name,
2455
+ lib: null,
2456
+ loading,
2457
+ });
2458
+ return loading;
2459
+ }
2460
+ else {
2461
+ if (extraOptions?.customShareInfo) {
2462
+ return false;
2463
+ }
2464
+ const asyncLoadProcess = async () => {
2465
+ const factory = await shareOptionsRes.get();
2466
+ shareOptionsRes.lib = factory;
2467
+ shareOptionsRes.loaded = true;
2468
+ addUseIn(shareOptionsRes);
2469
+ const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
2470
+ if (gShared) {
2471
+ gShared.lib = factory;
2472
+ gShared.loaded = true;
2473
+ gShared.from = shareOptionsRes.from;
2474
+ }
2475
+ return factory;
2476
+ };
2477
+ const loading = asyncLoadProcess();
2478
+ this.setShared({
2479
+ pkgName,
2480
+ loaded: false,
2481
+ shared: shareOptionsRes,
2482
+ from: host.options.name,
2483
+ lib: null,
2484
+ loading,
2485
+ });
2486
+ return loading;
2487
+ }
2488
+ }
2489
+ /**
2490
+ * This function initializes the sharing sequence (executed only once per share scope).
2491
+ * It accepts one argument, the name of the share scope.
2492
+ * If the share scope does not exist, it creates one.
2493
+ */
2494
+ // eslint-disable-next-line @typescript-eslint/member-ordering
2495
+ initializeSharing(shareScopeName = DEFAULT_SCOPE, extraOptions) {
2496
+ const { host } = this;
2497
+ const from = extraOptions?.from;
2498
+ const strategy = extraOptions?.strategy;
2499
+ let initScope = extraOptions?.initScope;
2500
+ const promises = [];
2501
+ if (from !== 'build') {
2502
+ const { initTokens } = this;
2503
+ if (!initScope)
2504
+ initScope = [];
2505
+ let initToken = initTokens[shareScopeName];
2506
+ if (!initToken)
2507
+ initToken = initTokens[shareScopeName] = { from: this.host.name };
2508
+ if (initScope.indexOf(initToken) >= 0)
2509
+ return promises;
2510
+ initScope.push(initToken);
2511
+ }
2512
+ const shareScope = this.shareScopeMap;
2513
+ const hostName = host.options.name;
2514
+ // Creates a new share scope if necessary
2515
+ if (!shareScope[shareScopeName]) {
2516
+ shareScope[shareScopeName] = {};
2517
+ }
2518
+ // Executes all initialization snippets from all accessible modules
2519
+ const scope = shareScope[shareScopeName];
2520
+ const register = (name, shared) => {
2521
+ const { version, eager } = shared;
2522
+ scope[name] = scope[name] || {};
2523
+ const versions = scope[name];
2524
+ const activeVersion = versions[version];
2525
+ const activeVersionEager = Boolean(activeVersion &&
2526
+ (activeVersion.eager || activeVersion.shareConfig?.eager));
2527
+ if (!activeVersion ||
2528
+ (activeVersion.strategy !== 'loaded-first' &&
2529
+ !activeVersion.loaded &&
2530
+ (Boolean(!eager) !== !activeVersionEager
2531
+ ? eager
2532
+ : hostName > activeVersion.from))) {
2533
+ versions[version] = shared;
2534
+ }
2535
+ };
2536
+ const initFn = (mod) => mod && mod.init && mod.init(shareScope[shareScopeName], initScope);
2537
+ const initRemoteModule = async (key) => {
2538
+ const { module } = await host.remoteHandler.getRemoteModuleAndOptions({
2539
+ id: key,
2540
+ });
2541
+ if (module.getEntry) {
2542
+ let remoteEntryExports;
2543
+ try {
2544
+ remoteEntryExports = await module.getEntry();
2545
+ }
2546
+ catch (error) {
2547
+ remoteEntryExports =
2548
+ (await host.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
2549
+ id: key,
2550
+ error,
2551
+ from: 'runtime',
2552
+ lifecycle: 'beforeLoadShare',
2553
+ origin: host,
2554
+ }));
2555
+ }
2556
+ if (!module.inited) {
2557
+ await initFn(remoteEntryExports);
2558
+ module.inited = true;
2559
+ }
2560
+ }
2561
+ };
2562
+ Object.keys(host.options.shared).forEach((shareName) => {
2563
+ const sharedArr = host.options.shared[shareName];
2564
+ sharedArr.forEach((shared) => {
2565
+ if (shared.scope.includes(shareScopeName)) {
2566
+ register(shareName, shared);
2567
+ }
2568
+ });
2569
+ });
2570
+ // TODO: strategy==='version-first' need to be removed in the future
2571
+ if (host.options.shareStrategy === 'version-first' ||
2572
+ strategy === 'version-first') {
2573
+ host.options.remotes.forEach((remote) => {
2574
+ if (remote.shareScope === shareScopeName) {
2575
+ promises.push(initRemoteModule(remote.name));
2576
+ }
2577
+ });
2578
+ }
2579
+ return promises;
2580
+ }
2581
+ // The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.
2582
+ // 1. If the loaded shared already exists globally, then it will be reused
2583
+ // 2. If lib exists in local shared, it will be used directly
2584
+ // 3. If the local get returns something other than Promise, then it will be used directly
2585
+ loadShareSync(pkgName, extraOptions) {
2586
+ const { host } = this;
2587
+ const shareOptions = getTargetSharedOptions({
2588
+ pkgName,
2589
+ extraOptions,
2590
+ shareInfos: host.options.shared,
2591
+ });
2592
+ if (shareOptions?.scope) {
2593
+ shareOptions.scope.forEach((shareScope) => {
2594
+ this.initializeSharing(shareScope, { strategy: shareOptions.strategy });
2595
+ });
2596
+ }
2597
+ const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare);
2598
+ const addUseIn = (shared) => {
2599
+ if (!shared.useIn) {
2600
+ shared.useIn = [];
2601
+ }
2602
+ addUniqueItem(shared.useIn, host.options.name);
2603
+ };
2604
+ if (registeredShared) {
2605
+ if (typeof registeredShared.lib === 'function') {
2606
+ addUseIn(registeredShared);
2607
+ if (!registeredShared.loaded) {
2608
+ registeredShared.loaded = true;
2609
+ if (registeredShared.from === host.options.name) {
2610
+ shareOptions.loaded = true;
2611
+ }
2612
+ }
2613
+ return registeredShared.lib;
2614
+ }
2615
+ if (typeof registeredShared.get === 'function') {
2616
+ const module = registeredShared.get();
2617
+ if (!(module instanceof Promise)) {
2618
+ addUseIn(registeredShared);
2619
+ this.setShared({
2620
+ pkgName,
2621
+ loaded: true,
2622
+ from: host.options.name,
2623
+ lib: module,
2624
+ shared: registeredShared,
2625
+ });
2626
+ return module;
2627
+ }
2628
+ }
2629
+ }
2630
+ if (shareOptions.lib) {
2631
+ if (!shareOptions.loaded) {
2632
+ shareOptions.loaded = true;
2633
+ }
2634
+ return shareOptions.lib;
2635
+ }
2636
+ if (shareOptions.get) {
2637
+ const module = shareOptions.get();
2638
+ if (module instanceof Promise) {
2639
+ const errorCode = extraOptions?.from === 'build' ? RUNTIME_005 : RUNTIME_006;
2640
+ throw new Error(getShortErrorMsg(errorCode, runtimeDescMap, {
2641
+ hostName: host.options.name,
2642
+ sharedPkgName: pkgName,
2643
+ }));
2644
+ }
2645
+ shareOptions.lib = module;
2646
+ this.setShared({
2647
+ pkgName,
2648
+ loaded: true,
2649
+ from: host.options.name,
2650
+ lib: shareOptions.lib,
2651
+ shared: shareOptions,
2652
+ });
2653
+ return shareOptions.lib;
2654
+ }
2655
+ throw new Error(getShortErrorMsg(RUNTIME_006, runtimeDescMap, {
2656
+ hostName: host.options.name,
2657
+ sharedPkgName: pkgName,
2658
+ }));
2659
+ }
2660
+ initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
2661
+ const { host } = this;
2662
+ this.shareScopeMap[scopeName] = shareScope;
2663
+ this.hooks.lifecycle.initContainerShareScopeMap.emit({
2664
+ shareScope,
2665
+ options: host.options,
2666
+ origin: host,
2667
+ scopeName,
2668
+ hostShareScopeMap: extraOptions.hostShareScopeMap,
2669
+ });
2670
+ }
2671
+ setShared({ pkgName, shared, from, lib, loading, loaded, get, }) {
2672
+ const { version, scope = 'default', ...shareInfo } = shared;
2673
+ const scopes = Array.isArray(scope) ? scope : [scope];
2674
+ scopes.forEach((sc) => {
2675
+ if (!this.shareScopeMap[sc]) {
2676
+ this.shareScopeMap[sc] = {};
2677
+ }
2678
+ if (!this.shareScopeMap[sc][pkgName]) {
2679
+ this.shareScopeMap[sc][pkgName] = {};
2680
+ }
2681
+ if (!this.shareScopeMap[sc][pkgName][version]) {
2682
+ this.shareScopeMap[sc][pkgName][version] = {
2683
+ version,
2684
+ scope: ['default'],
2685
+ ...shareInfo,
2686
+ lib,
2687
+ loaded,
2688
+ loading,
2689
+ };
2690
+ if (get) {
2691
+ this.shareScopeMap[sc][pkgName][version].get = get;
2692
+ }
2693
+ return;
2694
+ }
2695
+ const registeredShared = this.shareScopeMap[sc][pkgName][version];
2696
+ if (loading && !registeredShared.loading) {
2697
+ registeredShared.loading = loading;
2698
+ }
2699
+ if (loaded && !registeredShared.loaded) {
2700
+ registeredShared.loaded = loaded;
2701
+ }
2702
+ if (from && registeredShared.from !== from) {
2703
+ registeredShared.from = from;
2704
+ }
2705
+ });
2706
+ }
2707
+ _setGlobalShareScopeMap(hostOptions) {
2708
+ const globalShareScopeMap = getGlobalShareScope();
2709
+ const identifier = hostOptions.id || hostOptions.name;
2710
+ if (identifier && !globalShareScopeMap[identifier]) {
2711
+ globalShareScopeMap[identifier] = this.shareScopeMap;
2712
+ }
2713
+ }
2714
+ }
2715
+
2716
+ class RemoteHandler {
2717
+ constructor(host) {
2718
+ this.hooks = new PluginSystem({
2719
+ beforeRegisterRemote: new SyncWaterfallHook('beforeRegisterRemote'),
2720
+ registerRemote: new SyncWaterfallHook('registerRemote'),
2721
+ beforeRequest: new AsyncWaterfallHook('beforeRequest'),
2722
+ onLoad: new AsyncHook('onLoad'),
2723
+ handlePreloadModule: new SyncHook('handlePreloadModule'),
2724
+ errorLoadRemote: new AsyncHook('errorLoadRemote'),
2725
+ beforePreloadRemote: new AsyncHook('beforePreloadRemote'),
2726
+ generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
2727
+ // not used yet
2728
+ afterPreloadRemote: new AsyncHook(),
2729
+ loadEntry: new AsyncHook(),
2730
+ });
2731
+ this.host = host;
2732
+ this.idToRemoteMap = {};
2733
+ }
2734
+ formatAndRegisterRemote(globalOptions, userOptions) {
2735
+ const userRemotes = userOptions.remotes || [];
2736
+ return userRemotes.reduce((res, remote) => {
2737
+ this.registerRemote(remote, res, { force: false });
2738
+ return res;
2739
+ }, globalOptions.remotes);
2740
+ }
2741
+ setIdToRemoteMap(id, remoteMatchInfo) {
2742
+ const { remote, expose } = remoteMatchInfo;
2743
+ const { name, alias } = remote;
2744
+ this.idToRemoteMap[id] = { name: remote.name, expose };
2745
+ if (alias && id.startsWith(name)) {
2746
+ const idWithAlias = id.replace(name, alias);
2747
+ this.idToRemoteMap[idWithAlias] = { name: remote.name, expose };
2748
+ return;
2749
+ }
2750
+ if (alias && id.startsWith(alias)) {
2751
+ const idWithName = id.replace(alias, name);
2752
+ this.idToRemoteMap[idWithName] = { name: remote.name, expose };
2753
+ }
2754
+ }
2755
+ // eslint-disable-next-line max-lines-per-function
2756
+ // eslint-disable-next-line @typescript-eslint/member-ordering
2757
+ async loadRemote(id, options) {
2758
+ const { host } = this;
2759
+ try {
2760
+ const { loadFactory = true } = options || {
2761
+ loadFactory: true,
2762
+ };
2763
+ // 1. Validate the parameters of the retrieved module. There are two module request methods: pkgName + expose and alias + expose.
2764
+ // 2. Request the snapshot information of the current host and globally store the obtained snapshot information. The retrieved module information is partially offline and partially online. The online module information will retrieve the modules used online.
2765
+ // 3. Retrieve the detailed information of the current module from global (remoteEntry address, expose resource address)
2766
+ // 4. After retrieving remoteEntry, call the init of the module, and then retrieve the exported content of the module through get
2767
+ // id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
2768
+ // id: alias(app1) + expose(button) = app1/button
2769
+ // id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort
2770
+ const { module, moduleOptions, remoteMatchInfo } = await this.getRemoteModuleAndOptions({
2771
+ id,
2772
+ });
2773
+ const { pkgNameOrAlias, remote, expose, id: idRes, remoteSnapshot, } = remoteMatchInfo;
2774
+ const moduleOrFactory = (await module.get(idRes, expose, options, remoteSnapshot));
2775
+ const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
2776
+ id: idRes,
2777
+ pkgNameOrAlias,
2778
+ expose,
2779
+ exposeModule: loadFactory ? moduleOrFactory : undefined,
2780
+ exposeModuleFactory: loadFactory ? undefined : moduleOrFactory,
2781
+ remote,
2782
+ options: moduleOptions,
2783
+ moduleInstance: module,
2784
+ origin: host,
2785
+ });
2786
+ this.setIdToRemoteMap(id, remoteMatchInfo);
2787
+ if (typeof moduleWrapper === 'function') {
2788
+ return moduleWrapper;
2789
+ }
2790
+ return moduleOrFactory;
2791
+ }
2792
+ catch (error) {
2793
+ const { from = 'runtime' } = options || { from: 'runtime' };
2794
+ const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({
2795
+ id,
2796
+ error,
2797
+ from,
2798
+ lifecycle: 'onLoad',
2799
+ origin: host,
2800
+ });
2801
+ if (!failOver) {
2802
+ throw error;
2803
+ }
2804
+ return failOver;
2805
+ }
2806
+ }
2807
+ // eslint-disable-next-line @typescript-eslint/member-ordering
2808
+ async preloadRemote(preloadOptions) {
2809
+ const { host } = this;
2810
+ await this.hooks.lifecycle.beforePreloadRemote.emit({
2811
+ preloadOps: preloadOptions,
2812
+ options: host.options,
2813
+ origin: host,
2814
+ });
2815
+ const preloadOps = formatPreloadArgs(host.options.remotes, preloadOptions);
2816
+ await Promise.all(preloadOps.map(async (ops) => {
2817
+ const { remote } = ops;
2818
+ const remoteInfo = getRemoteInfo(remote);
2819
+ const { globalSnapshot, remoteSnapshot } = await host.snapshotHandler.loadRemoteSnapshotInfo({
2820
+ moduleInfo: remote,
2821
+ });
2822
+ const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
2823
+ origin: host,
2824
+ preloadOptions: ops,
2825
+ remote,
2826
+ remoteInfo,
2827
+ globalSnapshot,
2828
+ remoteSnapshot,
2829
+ });
2830
+ if (!assets) {
2831
+ return;
2832
+ }
2833
+ preloadAssets(remoteInfo, host, assets);
2834
+ }));
2835
+ }
2836
+ registerRemotes(remotes, options) {
2837
+ const { host } = this;
2838
+ remotes.forEach((remote) => {
2839
+ this.registerRemote(remote, host.options.remotes, {
2840
+ force: options?.force,
2841
+ });
2842
+ });
2843
+ }
2844
+ async getRemoteModuleAndOptions(options) {
2845
+ const { host } = this;
2846
+ const { id } = options;
2847
+ let loadRemoteArgs;
2848
+ try {
2849
+ loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({
2850
+ id,
2851
+ options: host.options,
2852
+ origin: host,
2853
+ });
2854
+ }
2855
+ catch (error) {
2856
+ loadRemoteArgs = (await this.hooks.lifecycle.errorLoadRemote.emit({
2857
+ id,
2858
+ options: host.options,
2859
+ origin: host,
2860
+ from: 'runtime',
2861
+ error,
2862
+ lifecycle: 'beforeRequest',
2863
+ }));
2864
+ if (!loadRemoteArgs) {
2865
+ throw error;
2866
+ }
2867
+ }
2868
+ const { id: idRes } = loadRemoteArgs;
2869
+ const remoteSplitInfo = matchRemoteWithNameAndExpose(host.options.remotes, idRes);
2870
+ assert(remoteSplitInfo, getShortErrorMsg(RUNTIME_004, runtimeDescMap, {
2871
+ hostName: host.options.name,
2872
+ requestId: idRes,
2873
+ }));
2874
+ const { remote: rawRemote } = remoteSplitInfo;
2875
+ const remoteInfo = getRemoteInfo(rawRemote);
2876
+ const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit({
2877
+ id: idRes,
2878
+ ...remoteSplitInfo,
2879
+ options: host.options,
2880
+ origin: host,
2881
+ remoteInfo,
2882
+ });
2883
+ const { remote, expose } = matchInfo;
2884
+ assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
2885
+ let module = host.moduleCache.get(remote.name);
2886
+ const moduleOptions = {
2887
+ host: host,
2888
+ remoteInfo,
2889
+ };
2890
+ if (!module) {
2891
+ module = new Module(moduleOptions);
2892
+ host.moduleCache.set(remote.name, module);
2893
+ }
2894
+ return {
2895
+ module,
2896
+ moduleOptions,
2897
+ remoteMatchInfo: matchInfo,
2898
+ };
2899
+ }
2900
+ registerRemote(remote, targetRemotes, options) {
2901
+ const { host } = this;
2902
+ const normalizeRemote = () => {
2903
+ if (remote.alias) {
2904
+ // Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error
2905
+ // As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported
2906
+ const findEqual = targetRemotes.find((item) => remote.alias &&
2907
+ (item.name.startsWith(remote.alias) ||
2908
+ item.alias?.startsWith(remote.alias)));
2909
+ assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
2910
+ }
2911
+ // Set the remote entry to a complete path
2912
+ if ('entry' in remote) {
2913
+ if (isBrowserEnv() && !remote.entry.startsWith('http')) {
2914
+ remote.entry = new URL(remote.entry, window.location.origin).href;
2915
+ }
2916
+ }
2917
+ if (!remote.shareScope) {
2918
+ remote.shareScope = DEFAULT_SCOPE;
2919
+ }
2920
+ if (!remote.type) {
2921
+ remote.type = DEFAULT_REMOTE_TYPE;
2922
+ }
2923
+ };
2924
+ this.hooks.lifecycle.beforeRegisterRemote.emit({ remote, origin: host });
2925
+ const registeredRemote = targetRemotes.find((item) => item.name === remote.name);
2926
+ if (!registeredRemote) {
2927
+ normalizeRemote();
2928
+ targetRemotes.push(remote);
2929
+ this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });
2930
+ }
2931
+ else {
2932
+ const messages = [
2933
+ `The remote "${remote.name}" is already registered.`,
2934
+ 'Please note that overriding it may cause unexpected errors.',
2935
+ ];
2936
+ if (options?.force) {
2937
+ // remove registered remote
2938
+ this.removeRemote(registeredRemote);
2939
+ normalizeRemote();
2940
+ targetRemotes.push(remote);
2941
+ this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });
2942
+ warn$1(messages.join(' '));
2943
+ }
2944
+ }
2945
+ }
2946
+ removeRemote(remote) {
2947
+ try {
2948
+ const { host } = this;
2949
+ const { name } = remote;
2950
+ const remoteIndex = host.options.remotes.findIndex((item) => item.name === name);
2951
+ if (remoteIndex !== -1) {
2952
+ host.options.remotes.splice(remoteIndex, 1);
2953
+ }
2954
+ const loadedModule = host.moduleCache.get(remote.name);
2955
+ if (loadedModule) {
2956
+ const remoteInfo = loadedModule.remoteInfo;
2957
+ const key = remoteInfo.entryGlobalName;
2958
+ if (CurrentGlobal[key]) {
2959
+ if (Object.getOwnPropertyDescriptor(CurrentGlobal, key)?.configurable) {
2960
+ delete CurrentGlobal[key];
2961
+ }
2962
+ else {
2963
+ // @ts-ignore
2964
+ CurrentGlobal[key] = undefined;
2965
+ }
2966
+ }
2967
+ const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
2968
+ if (globalLoading[remoteEntryUniqueKey]) {
2969
+ delete globalLoading[remoteEntryUniqueKey];
2970
+ }
2971
+ host.snapshotHandler.manifestCache.delete(remoteInfo.entry);
2972
+ // delete unloaded shared and instance
2973
+ let remoteInsId = remoteInfo.buildVersion
2974
+ ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion)
2975
+ : remoteInfo.name;
2976
+ const remoteInsIndex = CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {
2977
+ if (remoteInfo.buildVersion) {
2978
+ return ins.options.id === remoteInsId;
2979
+ }
2980
+ else {
2981
+ return ins.name === remoteInsId;
2982
+ }
2983
+ });
2984
+ if (remoteInsIndex !== -1) {
2985
+ const remoteIns = CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];
2986
+ remoteInsId = remoteIns.options.id || remoteInsId;
2987
+ const globalShareScopeMap = getGlobalShareScope();
2988
+ let isAllSharedNotUsed = true;
2989
+ const needDeleteKeys = [];
2990
+ Object.keys(globalShareScopeMap).forEach((instId) => {
2991
+ const shareScopeMap = globalShareScopeMap[instId];
2992
+ shareScopeMap &&
2993
+ Object.keys(shareScopeMap).forEach((shareScope) => {
2994
+ const shareScopeVal = shareScopeMap[shareScope];
2995
+ shareScopeVal &&
2996
+ Object.keys(shareScopeVal).forEach((shareName) => {
2997
+ const sharedPkgs = shareScopeVal[shareName];
2998
+ sharedPkgs &&
2999
+ Object.keys(sharedPkgs).forEach((shareVersion) => {
3000
+ const shared = sharedPkgs[shareVersion];
3001
+ if (shared &&
3002
+ typeof shared === 'object' &&
3003
+ shared.from === remoteInfo.name) {
3004
+ if (shared.loaded || shared.loading) {
3005
+ shared.useIn = shared.useIn.filter((usedHostName) => usedHostName !== remoteInfo.name);
3006
+ if (shared.useIn.length) {
3007
+ isAllSharedNotUsed = false;
3008
+ }
3009
+ else {
3010
+ needDeleteKeys.push([
3011
+ instId,
3012
+ shareScope,
3013
+ shareName,
3014
+ shareVersion,
3015
+ ]);
3016
+ }
3017
+ }
3018
+ else {
3019
+ needDeleteKeys.push([
3020
+ instId,
3021
+ shareScope,
3022
+ shareName,
3023
+ shareVersion,
3024
+ ]);
3025
+ }
3026
+ }
3027
+ });
3028
+ });
3029
+ });
3030
+ });
3031
+ if (isAllSharedNotUsed) {
3032
+ remoteIns.shareScopeMap = {};
3033
+ delete globalShareScopeMap[remoteInsId];
3034
+ }
3035
+ needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion]) => {
3036
+ delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[shareVersion];
3037
+ });
3038
+ CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
3039
+ }
3040
+ const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);
3041
+ if (hostGlobalSnapshot) {
3042
+ const remoteKey = hostGlobalSnapshot &&
3043
+ 'remotesInfo' in hostGlobalSnapshot &&
3044
+ hostGlobalSnapshot.remotesInfo &&
3045
+ getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;
3046
+ if (remoteKey) {
3047
+ delete hostGlobalSnapshot.remotesInfo[remoteKey];
3048
+ if (
3049
+ //eslint-disable-next-line no-extra-boolean-cast
3050
+ Boolean(Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])) {
3051
+ delete Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];
3052
+ }
3053
+ }
3054
+ }
3055
+ host.moduleCache.delete(remote.name);
3056
+ }
3057
+ }
3058
+ catch (err) {
3059
+ logger.log('removeRemote fail: ', err);
3060
+ }
3061
+ }
3062
+ }
3063
+
3064
+ const USE_SNAPSHOT = typeof FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN === 'boolean'
3065
+ ? !FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN
3066
+ : true; // Default to true (use snapshot) when not explicitly defined
3067
+ class ModuleFederation {
3068
+ constructor(userOptions) {
3069
+ this.hooks = new PluginSystem({
3070
+ beforeInit: new SyncWaterfallHook('beforeInit'),
3071
+ init: new SyncHook(),
3072
+ // maybe will change, temporarily for internal use only
3073
+ beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
3074
+ // maybe will change, temporarily for internal use only
3075
+ initContainer: new AsyncWaterfallHook('initContainer'),
3076
+ });
3077
+ this.version = "0.0.0-feat-support-bridge-react-router-v7-20251015102312";
3078
+ this.moduleCache = new Map();
3079
+ this.loaderHook = new PluginSystem({
3080
+ // FIXME: may not be suitable , not open to the public yet
3081
+ getModuleInfo: new SyncHook(),
3082
+ createScript: new SyncHook(),
3083
+ createLink: new SyncHook(),
3084
+ fetch: new AsyncHook(),
3085
+ loadEntryError: new AsyncHook(),
3086
+ getModuleFactory: new AsyncHook(),
3087
+ });
3088
+ this.bridgeHook = new PluginSystem({
3089
+ beforeBridgeRender: new SyncHook(),
3090
+ afterBridgeRender: new SyncHook(),
3091
+ beforeBridgeDestroy: new SyncHook(),
3092
+ afterBridgeDestroy: new SyncHook(),
3093
+ });
3094
+ const plugins = USE_SNAPSHOT
3095
+ ? [snapshotPlugin(), generatePreloadAssetsPlugin()]
3096
+ : [];
3097
+ // TODO: Validate the details of the options
3098
+ // Initialize options with default values
3099
+ const defaultOptions = {
3100
+ id: getBuilderId(),
3101
+ name: userOptions.name,
3102
+ plugins,
3103
+ remotes: [],
3104
+ shared: {},
3105
+ inBrowser: isBrowserEnv(),
3106
+ };
3107
+ this.name = userOptions.name;
3108
+ this.options = defaultOptions;
3109
+ this.snapshotHandler = new SnapshotHandler(this);
3110
+ this.sharedHandler = new SharedHandler(this);
3111
+ this.remoteHandler = new RemoteHandler(this);
3112
+ this.shareScopeMap = this.sharedHandler.shareScopeMap;
3113
+ this.registerPlugins([
3114
+ ...defaultOptions.plugins,
3115
+ ...(userOptions.plugins || []),
3116
+ ]);
3117
+ this.options = this.formatOptions(defaultOptions, userOptions);
3118
+ }
3119
+ initOptions(userOptions) {
3120
+ this.registerPlugins(userOptions.plugins);
3121
+ const options = this.formatOptions(this.options, userOptions);
3122
+ this.options = options;
3123
+ return options;
3124
+ }
3125
+ async loadShare(pkgName, extraOptions) {
3126
+ return this.sharedHandler.loadShare(pkgName, extraOptions);
3127
+ }
3128
+ // The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.
3129
+ // 1. If the loaded shared already exists globally, then it will be reused
3130
+ // 2. If lib exists in local shared, it will be used directly
3131
+ // 3. If the local get returns something other than Promise, then it will be used directly
3132
+ loadShareSync(pkgName, extraOptions) {
3133
+ return this.sharedHandler.loadShareSync(pkgName, extraOptions);
3134
+ }
3135
+ initializeSharing(shareScopeName = DEFAULT_SCOPE, extraOptions) {
3136
+ return this.sharedHandler.initializeSharing(shareScopeName, extraOptions);
3137
+ }
3138
+ initRawContainer(name, url, container) {
3139
+ const remoteInfo = getRemoteInfo({ name, entry: url });
3140
+ const module = new Module({ host: this, remoteInfo });
3141
+ module.remoteEntryExports = container;
3142
+ this.moduleCache.set(name, module);
3143
+ return module;
3144
+ }
3145
+ // eslint-disable-next-line max-lines-per-function
3146
+ // eslint-disable-next-line @typescript-eslint/member-ordering
3147
+ async loadRemote(id, options) {
3148
+ return this.remoteHandler.loadRemote(id, options);
3149
+ }
3150
+ // eslint-disable-next-line @typescript-eslint/member-ordering
3151
+ async preloadRemote(preloadOptions) {
3152
+ return this.remoteHandler.preloadRemote(preloadOptions);
3153
+ }
3154
+ initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
3155
+ this.sharedHandler.initShareScopeMap(scopeName, shareScope, extraOptions);
3156
+ }
3157
+ formatOptions(globalOptions, userOptions) {
3158
+ const { shared } = formatShareConfigs(globalOptions, userOptions);
3159
+ const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
3160
+ origin: this,
3161
+ userOptions,
3162
+ options: globalOptions,
3163
+ shareInfo: shared,
3164
+ });
3165
+ const remotes = this.remoteHandler.formatAndRegisterRemote(globalOptionsRes, userOptionsRes);
3166
+ const { shared: handledShared } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
3167
+ const plugins = [...globalOptionsRes.plugins];
3168
+ if (userOptionsRes.plugins) {
3169
+ userOptionsRes.plugins.forEach((plugin) => {
3170
+ if (!plugins.includes(plugin)) {
3171
+ plugins.push(plugin);
3172
+ }
3173
+ });
3174
+ }
3175
+ const optionsRes = {
3176
+ ...globalOptions,
3177
+ ...userOptions,
3178
+ plugins,
3179
+ remotes,
3180
+ shared: handledShared,
3181
+ };
3182
+ this.hooks.lifecycle.init.emit({
3183
+ origin: this,
3184
+ options: optionsRes,
3185
+ });
3186
+ return optionsRes;
3187
+ }
3188
+ registerPlugins(plugins) {
3189
+ const pluginRes = registerPlugins(plugins, this);
3190
+ // Merge plugin
3191
+ this.options.plugins = this.options.plugins.reduce((res, plugin) => {
3192
+ if (!plugin)
3193
+ return res;
3194
+ if (res && !res.find((item) => item.name === plugin.name)) {
3195
+ res.push(plugin);
3196
+ }
3197
+ return res;
3198
+ }, pluginRes || []);
3199
+ }
3200
+ registerRemotes(remotes, options) {
3201
+ return this.remoteHandler.registerRemotes(remotes, options);
3202
+ }
3203
+ registerShared(shared) {
3204
+ this.sharedHandler.registerShared(this.options, {
3205
+ ...this.options,
3206
+ shared,
3207
+ });
3208
+ }
3209
+ }
3210
+
3211
+ var index = /*#__PURE__*/Object.freeze({
3212
+ __proto__: null
3213
+ });
3214
+
3215
+ export { CurrentGlobal, Global, Module, ModuleFederation, addGlobalSnapshot, assert, getGlobalFederationConstructor, getGlobalSnapshot, getInfoWithoutType, getRegisteredShare, getRemoteEntry, getRemoteInfo, helpers, isStaticResourcesEqual, matchRemoteWithNameAndExpose, registerGlobalPlugins, resetFederationGlobalInfo, safeWrapper, satisfy, setGlobalFederationConstructor, setGlobalFederationInstance, index as types };
3216
+ //# sourceMappingURL=index.esm.js.map