@module-federation/runtime-core 2.0.1 → 2.2.0

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 (209) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +19 -0
  2. package/dist/_virtual/_rolldown/runtime.js +18 -0
  3. package/dist/constant.cjs +9 -0
  4. package/dist/constant.cjs.map +1 -0
  5. package/dist/constant.js +7 -0
  6. package/dist/constant.js.map +1 -0
  7. package/dist/core.cjs +153 -0
  8. package/dist/core.cjs.map +1 -0
  9. package/dist/core.d.ts +129 -0
  10. package/dist/core.js +153 -0
  11. package/dist/core.js.map +1 -0
  12. package/dist/global.cjs +160 -0
  13. package/dist/global.cjs.map +1 -0
  14. package/dist/global.d.ts +45 -0
  15. package/dist/global.js +142 -0
  16. package/dist/global.js.map +1 -0
  17. package/dist/helpers.cjs +44 -0
  18. package/dist/helpers.cjs.map +1 -0
  19. package/dist/helpers.d.ts +30 -0
  20. package/dist/helpers.js +44 -0
  21. package/dist/helpers.js.map +1 -0
  22. package/dist/index.cjs +60 -0
  23. package/dist/index.cjs.map +1 -0
  24. package/dist/index.d.ts +29 -1
  25. package/dist/index.js +20 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/module/index.cjs +129 -0
  28. package/dist/module/index.cjs.map +1 -0
  29. package/dist/module/index.d.ts +31 -0
  30. package/dist/module/index.js +129 -0
  31. package/dist/module/index.js.map +1 -0
  32. package/dist/plugins/generate-preload-assets.cjs +182 -0
  33. package/dist/plugins/generate-preload-assets.cjs.map +1 -0
  34. package/dist/plugins/generate-preload-assets.js +182 -0
  35. package/dist/plugins/generate-preload-assets.js.map +1 -0
  36. package/dist/plugins/snapshot/SnapshotHandler.cjs +190 -0
  37. package/dist/plugins/snapshot/SnapshotHandler.cjs.map +1 -0
  38. package/dist/plugins/snapshot/SnapshotHandler.d.ts +66 -0
  39. package/dist/plugins/snapshot/SnapshotHandler.js +189 -0
  40. package/dist/plugins/snapshot/SnapshotHandler.js.map +1 -0
  41. package/dist/plugins/snapshot/index.cjs +63 -0
  42. package/dist/plugins/snapshot/index.cjs.map +1 -0
  43. package/dist/plugins/snapshot/index.js +62 -0
  44. package/dist/plugins/snapshot/index.js.map +1 -0
  45. package/dist/remote/index.cjs +304 -0
  46. package/dist/remote/index.cjs.map +1 -0
  47. package/dist/remote/index.d.ts +119 -0
  48. package/dist/remote/index.js +304 -0
  49. package/dist/remote/index.js.map +1 -0
  50. package/dist/shared/index.cjs +303 -0
  51. package/dist/shared/index.cjs.map +1 -0
  52. package/dist/shared/index.d.ts +82 -0
  53. package/dist/shared/index.js +303 -0
  54. package/dist/shared/index.js.map +1 -0
  55. package/dist/type/config.d.ts +130 -0
  56. package/dist/type/index.cjs +13 -0
  57. package/dist/type/index.cjs.map +1 -0
  58. package/dist/type/index.d.ts +11 -0
  59. package/dist/type/index.js +8 -0
  60. package/dist/type/index.js.map +1 -0
  61. package/dist/type/plugin.d.ts +27 -0
  62. package/dist/type/preload.d.ts +31 -0
  63. package/dist/types.cjs +0 -0
  64. package/dist/types.d.ts +4 -1
  65. package/dist/types.js +1 -0
  66. package/dist/utils/context.cjs +45 -0
  67. package/dist/utils/context.cjs.map +1 -0
  68. package/dist/utils/context.d.ts +1 -0
  69. package/dist/utils/context.js +44 -0
  70. package/dist/utils/context.js.map +1 -0
  71. package/dist/utils/env.cjs +10 -0
  72. package/dist/utils/env.cjs.map +1 -0
  73. package/dist/utils/env.d.ts +1 -0
  74. package/dist/utils/env.js +10 -0
  75. package/dist/utils/env.js.map +1 -0
  76. package/dist/utils/hooks/asyncHook.cjs +23 -0
  77. package/dist/utils/hooks/asyncHook.cjs.map +1 -0
  78. package/dist/utils/hooks/asyncHook.d.ts +10 -0
  79. package/dist/utils/hooks/asyncHook.js +23 -0
  80. package/dist/utils/hooks/asyncHook.js.map +1 -0
  81. package/dist/utils/hooks/asyncWaterfallHooks.cjs +42 -0
  82. package/dist/utils/hooks/asyncWaterfallHooks.cjs.map +1 -0
  83. package/dist/utils/hooks/asyncWaterfallHooks.d.ts +12 -0
  84. package/dist/utils/hooks/asyncWaterfallHooks.js +42 -0
  85. package/dist/utils/hooks/asyncWaterfallHooks.js.map +1 -0
  86. package/dist/utils/hooks/index.cjs +5 -0
  87. package/dist/utils/hooks/index.d.ts +5 -0
  88. package/dist/utils/hooks/index.js +7 -0
  89. package/dist/utils/hooks/pluginSystem.cjs +37 -0
  90. package/dist/utils/hooks/pluginSystem.cjs.map +1 -0
  91. package/dist/utils/hooks/pluginSystem.d.ts +19 -0
  92. package/dist/utils/hooks/pluginSystem.js +37 -0
  93. package/dist/utils/hooks/pluginSystem.js.map +1 -0
  94. package/dist/utils/hooks/syncHook.cjs +36 -0
  95. package/dist/utils/hooks/syncHook.cjs.map +1 -0
  96. package/dist/utils/hooks/syncHook.d.ts +16 -0
  97. package/dist/utils/hooks/syncHook.js +35 -0
  98. package/dist/utils/hooks/syncHook.js.map +1 -0
  99. package/dist/utils/hooks/syncWaterfallHook.cjs +39 -0
  100. package/dist/utils/hooks/syncWaterfallHook.cjs.map +1 -0
  101. package/dist/utils/hooks/syncWaterfallHook.d.ts +11 -0
  102. package/dist/utils/hooks/syncWaterfallHook.js +38 -0
  103. package/dist/utils/hooks/syncWaterfallHook.js.map +1 -0
  104. package/dist/utils/index.cjs +8 -0
  105. package/dist/utils/index.d.ts +4 -0
  106. package/dist/utils/index.js +9 -0
  107. package/dist/utils/load.cjs +179 -0
  108. package/dist/utils/load.cjs.map +1 -0
  109. package/dist/utils/load.d.ts +14 -0
  110. package/dist/utils/load.js +177 -0
  111. package/dist/utils/load.js.map +1 -0
  112. package/dist/utils/logger.cjs +34 -0
  113. package/dist/utils/logger.cjs.map +1 -0
  114. package/dist/utils/logger.d.ts +10 -0
  115. package/dist/utils/logger.js +31 -0
  116. package/dist/utils/logger.js.map +1 -0
  117. package/dist/utils/manifest.cjs +51 -0
  118. package/dist/utils/manifest.cjs.map +1 -0
  119. package/dist/utils/manifest.d.ts +10 -0
  120. package/dist/utils/manifest.js +49 -0
  121. package/dist/utils/manifest.js.map +1 -0
  122. package/dist/utils/plugin.cjs +27 -0
  123. package/dist/utils/plugin.cjs.map +1 -0
  124. package/dist/utils/plugin.d.ts +1 -0
  125. package/dist/utils/plugin.js +27 -0
  126. package/dist/utils/plugin.js.map +1 -0
  127. package/dist/utils/preload.cjs +146 -0
  128. package/dist/utils/preload.cjs.map +1 -0
  129. package/dist/utils/preload.d.ts +8 -0
  130. package/dist/utils/preload.js +143 -0
  131. package/dist/utils/preload.js.map +1 -0
  132. package/dist/utils/semver/compare.cjs +48 -0
  133. package/dist/utils/semver/compare.cjs.map +1 -0
  134. package/dist/utils/semver/compare.js +47 -0
  135. package/dist/utils/semver/compare.js.map +1 -0
  136. package/dist/utils/semver/constants.cjs +41 -0
  137. package/dist/utils/semver/constants.cjs.map +1 -0
  138. package/dist/utils/semver/constants.js +31 -0
  139. package/dist/utils/semver/constants.js.map +1 -0
  140. package/dist/utils/semver/index.cjs +68 -0
  141. package/dist/utils/semver/index.cjs.map +1 -0
  142. package/dist/utils/semver/index.d.ts +5 -0
  143. package/dist/utils/semver/index.js +68 -0
  144. package/dist/utils/semver/index.js.map +1 -0
  145. package/dist/utils/semver/parser.cjs +104 -0
  146. package/dist/utils/semver/parser.cjs.map +1 -0
  147. package/dist/utils/semver/parser.js +96 -0
  148. package/dist/utils/semver/parser.js.map +1 -0
  149. package/dist/utils/semver/utils.cjs +28 -0
  150. package/dist/utils/semver/utils.cjs.map +1 -0
  151. package/dist/utils/semver/utils.js +24 -0
  152. package/dist/utils/semver/utils.js.map +1 -0
  153. package/dist/utils/share.cjs +284 -0
  154. package/dist/utils/share.cjs.map +1 -0
  155. package/dist/utils/share.d.ts +23 -0
  156. package/dist/utils/share.js +278 -0
  157. package/dist/utils/share.js.map +1 -0
  158. package/dist/utils/tool.cjs +82 -0
  159. package/dist/utils/tool.cjs.map +1 -0
  160. package/dist/utils/tool.d.ts +8 -0
  161. package/dist/utils/tool.js +71 -0
  162. package/dist/utils/tool.js.map +1 -0
  163. package/package.json +16 -9
  164. package/dist/index.cjs.cjs +0 -3442
  165. package/dist/index.cjs.cjs.map +0 -1
  166. package/dist/index.cjs.d.ts +0 -1
  167. package/dist/index.esm.js +0 -3418
  168. package/dist/index.esm.js.map +0 -1
  169. package/dist/src/constant.d.ts +0 -2
  170. package/dist/src/core.d.ts +0 -119
  171. package/dist/src/global.d.ts +0 -42
  172. package/dist/src/helpers.d.ts +0 -38
  173. package/dist/src/index.d.ts +0 -14
  174. package/dist/src/module/index.d.ts +0 -25
  175. package/dist/src/plugins/generate-preload-assets.d.ts +0 -8
  176. package/dist/src/plugins/snapshot/SnapshotHandler.d.ts +0 -60
  177. package/dist/src/plugins/snapshot/index.d.ts +0 -5
  178. package/dist/src/remote/index.d.ts +0 -109
  179. package/dist/src/shared/index.d.ts +0 -75
  180. package/dist/src/type/config.d.ts +0 -128
  181. package/dist/src/type/index.d.ts +0 -3
  182. package/dist/src/type/plugin.d.ts +0 -35
  183. package/dist/src/type/preload.d.ts +0 -26
  184. package/dist/src/types.d.ts +0 -1
  185. package/dist/src/utils/env.d.ts +0 -3
  186. package/dist/src/utils/hooks/asyncHook.d.ts +0 -6
  187. package/dist/src/utils/hooks/asyncWaterfallHooks.d.ts +0 -10
  188. package/dist/src/utils/hooks/index.d.ts +0 -6
  189. package/dist/src/utils/hooks/pluginSystem.d.ts +0 -16
  190. package/dist/src/utils/hooks/syncHook.d.ts +0 -12
  191. package/dist/src/utils/hooks/syncWaterfallHook.d.ts +0 -9
  192. package/dist/src/utils/index.d.ts +0 -6
  193. package/dist/src/utils/load.d.ts +0 -11
  194. package/dist/src/utils/logger.d.ts +0 -6
  195. package/dist/src/utils/manifest.d.ts +0 -7
  196. package/dist/src/utils/plugin.d.ts +0 -3
  197. package/dist/src/utils/preload.d.ts +0 -6
  198. package/dist/src/utils/semver/compare.d.ts +0 -9
  199. package/dist/src/utils/semver/constants.d.ts +0 -10
  200. package/dist/src/utils/semver/index.d.ts +0 -2
  201. package/dist/src/utils/semver/parser.d.ts +0 -9
  202. package/dist/src/utils/semver/utils.d.ts +0 -11
  203. package/dist/src/utils/share.d.ts +0 -45
  204. package/dist/src/utils/tool.d.ts +0 -18
  205. package/dist/types.cjs.cjs +0 -3
  206. package/dist/types.cjs.cjs.map +0 -1
  207. package/dist/types.cjs.d.ts +0 -1
  208. package/dist/types.esm.js +0 -2
  209. package/dist/types.esm.js.map +0 -1
package/dist/index.esm.js DELETED
@@ -1,3418 +0,0 @@
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() || !('ssrRemoteEntry' in snapshot)) {
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__ = "2.0.1";
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
- if (shareArgs.shareConfig?.eager && shareArgs.treeShaking?.mode) {
811
- throw new Error('Can not set "eager:true" and "treeShaking" at the same time!');
812
- }
813
- return {
814
- deps: [],
815
- useIn: [],
816
- from,
817
- loading: null,
818
- ...shareArgs,
819
- shareConfig: {
820
- requiredVersion: `^${shareArgs.version}`,
821
- singleton: false,
822
- eager: false,
823
- strictVersion: false,
824
- ...shareArgs.shareConfig,
825
- },
826
- get,
827
- loaded: shareArgs?.loaded || 'lib' in shareArgs ? true : undefined,
828
- version: shareArgs.version ?? '0',
829
- scope: Array.isArray(shareArgs.scope)
830
- ? shareArgs.scope
831
- : [shareArgs.scope ?? 'default'],
832
- strategy: (shareArgs.strategy ?? shareStrategy) || 'version-first',
833
- treeShaking: shareArgs.treeShaking
834
- ? {
835
- ...shareArgs.treeShaking,
836
- mode: shareArgs.treeShaking.mode ?? 'server-calc',
837
- status: shareArgs.treeShaking.status ?? 1 /* TreeShakingStatus.UNKNOWN */,
838
- useIn: [],
839
- }
840
- : undefined,
841
- };
842
- }
843
- function formatShareConfigs(prevOptions, newOptions) {
844
- const shareArgs = newOptions.shared || {};
845
- const from = newOptions.name;
846
- const newShareInfos = Object.keys(shareArgs).reduce((res, pkgName) => {
847
- const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
848
- res[pkgName] = res[pkgName] || [];
849
- arrayShareArgs.forEach((shareConfig) => {
850
- res[pkgName].push(formatShare(shareConfig, from, pkgName, newOptions.shareStrategy));
851
- });
852
- return res;
853
- }, {});
854
- const allShareInfos = {
855
- ...prevOptions.shared,
856
- };
857
- Object.keys(newShareInfos).forEach((shareKey) => {
858
- if (!allShareInfos[shareKey]) {
859
- allShareInfos[shareKey] = newShareInfos[shareKey];
860
- }
861
- else {
862
- newShareInfos[shareKey].forEach((newUserSharedOptions) => {
863
- const isSameVersion = allShareInfos[shareKey].find((sharedVal) => sharedVal.version === newUserSharedOptions.version);
864
- if (!isSameVersion) {
865
- allShareInfos[shareKey].push(newUserSharedOptions);
866
- }
867
- });
868
- }
869
- });
870
- return { allShareInfos, newShareInfos };
871
- }
872
- function shouldUseTreeShaking(treeShaking, usedExports) {
873
- if (!treeShaking) {
874
- return false;
875
- }
876
- const { status, mode } = treeShaking;
877
- if (status === 0 /* TreeShakingStatus.NO_USE */) {
878
- return false;
879
- }
880
- if (status === 2 /* TreeShakingStatus.CALCULATED */) {
881
- return true;
882
- }
883
- if (mode === 'runtime-infer') {
884
- if (!usedExports) {
885
- return true;
886
- }
887
- return isMatchUsedExports(treeShaking, usedExports);
888
- }
889
- return false;
890
- }
891
- /**
892
- * compare version a and b, return true if a is less than b
893
- */
894
- function versionLt(a, b) {
895
- const transformInvalidVersion = (version) => {
896
- const isNumberVersion = !Number.isNaN(Number(version));
897
- if (isNumberVersion) {
898
- const splitArr = version.split('.');
899
- let validVersion = version;
900
- for (let i = 0; i < 3 - splitArr.length; i++) {
901
- validVersion += '.0';
902
- }
903
- return validVersion;
904
- }
905
- return version;
906
- };
907
- if (satisfy(transformInvalidVersion(a), `<=${transformInvalidVersion(b)}`)) {
908
- return true;
909
- }
910
- else {
911
- return false;
912
- }
913
- }
914
- const findVersion = (shareVersionMap, cb) => {
915
- const callback = cb ||
916
- function (prev, cur) {
917
- return versionLt(prev, cur);
918
- };
919
- return Object.keys(shareVersionMap).reduce((prev, cur) => {
920
- if (!prev) {
921
- return cur;
922
- }
923
- if (callback(prev, cur)) {
924
- return cur;
925
- }
926
- // default version is '0' https://github.com/webpack/webpack/blob/main/lib/sharing/ProvideSharedModule.js#L136
927
- if (prev === '0') {
928
- return cur;
929
- }
930
- return prev;
931
- }, 0);
932
- };
933
- const isLoaded = (shared) => {
934
- return Boolean(shared.loaded) || typeof shared.lib === 'function';
935
- };
936
- const isLoading = (shared) => {
937
- return Boolean(shared.loading);
938
- };
939
- const isMatchUsedExports = (treeShaking, usedExports) => {
940
- if (!treeShaking || !usedExports) {
941
- return false;
942
- }
943
- const { usedExports: treeShakingUsedExports } = treeShaking;
944
- if (!treeShakingUsedExports) {
945
- return false;
946
- }
947
- if (usedExports.every((e) => treeShakingUsedExports.includes(e))) {
948
- return true;
949
- }
950
- return false;
951
- };
952
- function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName, treeShaking) {
953
- const versions = shareScopeMap[scope][pkgName];
954
- let version = '';
955
- let useTreesShaking = shouldUseTreeShaking(treeShaking);
956
- // return false means use prev version
957
- const callback = function (prev, cur) {
958
- if (useTreesShaking) {
959
- if (!versions[prev].treeShaking) {
960
- return true;
961
- }
962
- if (!versions[cur].treeShaking) {
963
- return false;
964
- }
965
- return !isLoaded(versions[prev].treeShaking) && versionLt(prev, cur);
966
- }
967
- return !isLoaded(versions[prev]) && versionLt(prev, cur);
968
- };
969
- if (useTreesShaking) {
970
- version = findVersion(shareScopeMap[scope][pkgName], callback);
971
- if (version) {
972
- return {
973
- version,
974
- useTreesShaking,
975
- };
976
- }
977
- useTreesShaking = false;
978
- }
979
- return {
980
- version: findVersion(shareScopeMap[scope][pkgName], callback),
981
- useTreesShaking,
982
- };
983
- }
984
- const isLoadingOrLoaded = (shared) => {
985
- return isLoaded(shared) || isLoading(shared);
986
- };
987
- function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName, treeShaking) {
988
- const versions = shareScopeMap[scope][pkgName];
989
- let version = '';
990
- let useTreesShaking = shouldUseTreeShaking(treeShaking);
991
- // return false means use prev version
992
- const callback = function (prev, cur) {
993
- if (useTreesShaking) {
994
- if (!versions[prev].treeShaking) {
995
- return true;
996
- }
997
- if (!versions[cur].treeShaking) {
998
- return false;
999
- }
1000
- if (isLoadingOrLoaded(versions[cur].treeShaking)) {
1001
- if (isLoadingOrLoaded(versions[prev].treeShaking)) {
1002
- return Boolean(versionLt(prev, cur));
1003
- }
1004
- else {
1005
- return true;
1006
- }
1007
- }
1008
- if (isLoadingOrLoaded(versions[prev].treeShaking)) {
1009
- return false;
1010
- }
1011
- }
1012
- if (isLoadingOrLoaded(versions[cur])) {
1013
- if (isLoadingOrLoaded(versions[prev])) {
1014
- return Boolean(versionLt(prev, cur));
1015
- }
1016
- else {
1017
- return true;
1018
- }
1019
- }
1020
- if (isLoadingOrLoaded(versions[prev])) {
1021
- return false;
1022
- }
1023
- return versionLt(prev, cur);
1024
- };
1025
- if (useTreesShaking) {
1026
- version = findVersion(shareScopeMap[scope][pkgName], callback);
1027
- if (version) {
1028
- return {
1029
- version,
1030
- useTreesShaking,
1031
- };
1032
- }
1033
- useTreesShaking = false;
1034
- }
1035
- return {
1036
- version: findVersion(shareScopeMap[scope][pkgName], callback),
1037
- useTreesShaking,
1038
- };
1039
- }
1040
- function getFindShareFunction(strategy) {
1041
- if (strategy === 'loaded-first') {
1042
- return findSingletonVersionOrderByLoaded;
1043
- }
1044
- return findSingletonVersionOrderByVersion;
1045
- }
1046
- function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare) {
1047
- if (!localShareScopeMap) {
1048
- return;
1049
- }
1050
- const { shareConfig, scope = DEFAULT_SCOPE, strategy, treeShaking, } = shareInfo;
1051
- const scopes = Array.isArray(scope) ? scope : [scope];
1052
- for (const sc of scopes) {
1053
- if (shareConfig &&
1054
- localShareScopeMap[sc] &&
1055
- localShareScopeMap[sc][pkgName]) {
1056
- const { requiredVersion } = shareConfig;
1057
- const findShareFunction = getFindShareFunction(strategy);
1058
- const { version: maxOrSingletonVersion, useTreesShaking } = findShareFunction(localShareScopeMap, sc, pkgName, treeShaking);
1059
- const defaultResolver = () => {
1060
- const shared = localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1061
- if (shareConfig.singleton) {
1062
- if (typeof requiredVersion === 'string' &&
1063
- !satisfy(maxOrSingletonVersion, requiredVersion)) {
1064
- const msg = `Version ${maxOrSingletonVersion} from ${maxOrSingletonVersion && shared.from} of shared singleton module ${pkgName} does not satisfy the requirement of ${shareInfo.from} which needs ${requiredVersion})`;
1065
- if (shareConfig.strictVersion) {
1066
- error(msg);
1067
- }
1068
- else {
1069
- warn(msg);
1070
- }
1071
- }
1072
- return {
1073
- shared,
1074
- useTreesShaking,
1075
- };
1076
- }
1077
- else {
1078
- if (requiredVersion === false || requiredVersion === '*') {
1079
- return {
1080
- shared,
1081
- useTreesShaking,
1082
- };
1083
- }
1084
- if (satisfy(maxOrSingletonVersion, requiredVersion)) {
1085
- return {
1086
- shared,
1087
- useTreesShaking,
1088
- };
1089
- }
1090
- const _usedTreeShaking = shouldUseTreeShaking(treeShaking);
1091
- if (_usedTreeShaking) {
1092
- for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
1093
- if (!shouldUseTreeShaking(versionValue.treeShaking, treeShaking?.usedExports)) {
1094
- continue;
1095
- }
1096
- if (satisfy(versionKey, requiredVersion)) {
1097
- return {
1098
- shared: versionValue,
1099
- useTreesShaking: _usedTreeShaking,
1100
- };
1101
- }
1102
- }
1103
- }
1104
- for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
1105
- if (satisfy(versionKey, requiredVersion)) {
1106
- return {
1107
- shared: versionValue,
1108
- useTreesShaking: false,
1109
- };
1110
- }
1111
- }
1112
- }
1113
- return;
1114
- };
1115
- const params = {
1116
- shareScopeMap: localShareScopeMap,
1117
- scope: sc,
1118
- pkgName,
1119
- version: maxOrSingletonVersion,
1120
- GlobalFederation: Global.__FEDERATION__,
1121
- shareInfo,
1122
- resolver: defaultResolver,
1123
- };
1124
- const resolveShared = resolveShare.emit(params) || params;
1125
- return resolveShared.resolver();
1126
- }
1127
- }
1128
- }
1129
- function getGlobalShareScope() {
1130
- return Global.__FEDERATION__.__SHARE__;
1131
- }
1132
- function getTargetSharedOptions(options) {
1133
- const { pkgName, extraOptions, shareInfos } = options;
1134
- const defaultResolver = (sharedOptions) => {
1135
- if (!sharedOptions) {
1136
- return undefined;
1137
- }
1138
- const shareVersionMap = {};
1139
- sharedOptions.forEach((shared) => {
1140
- shareVersionMap[shared.version] = shared;
1141
- });
1142
- const callback = function (prev, cur) {
1143
- return (
1144
- // TODO: consider multiple treeShaking shared scenes
1145
- !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur));
1146
- };
1147
- const maxVersion = findVersion(shareVersionMap, callback);
1148
- return shareVersionMap[maxVersion];
1149
- };
1150
- const resolver = extraOptions?.resolver ?? defaultResolver;
1151
- const isPlainObject = (val) => {
1152
- return val !== null && typeof val === 'object' && !Array.isArray(val);
1153
- };
1154
- const merge = (...sources) => {
1155
- const out = {};
1156
- for (const src of sources) {
1157
- if (!src)
1158
- continue;
1159
- for (const [key, value] of Object.entries(src)) {
1160
- const prev = out[key];
1161
- if (isPlainObject(prev) && isPlainObject(value)) {
1162
- out[key] = merge(prev, value);
1163
- }
1164
- else if (value !== undefined) {
1165
- out[key] = value;
1166
- }
1167
- }
1168
- }
1169
- return out;
1170
- };
1171
- return merge(resolver(shareInfos[pkgName]), extraOptions?.customShareInfo);
1172
- }
1173
- const addUseIn = (shared, from) => {
1174
- if (!shared.useIn) {
1175
- shared.useIn = [];
1176
- }
1177
- addUniqueItem(shared.useIn, from);
1178
- };
1179
- function directShare(shared, useTreesShaking) {
1180
- if (useTreesShaking && shared.treeShaking) {
1181
- return shared.treeShaking;
1182
- }
1183
- return shared;
1184
- }
1185
-
1186
- function getBuilderId() {
1187
- //@ts-ignore
1188
- return typeof FEDERATION_BUILD_IDENTIFIER !== 'undefined'
1189
- ? //@ts-ignore
1190
- FEDERATION_BUILD_IDENTIFIER
1191
- : '';
1192
- }
1193
-
1194
- // Function to match a remote with its name and expose
1195
- // id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
1196
- // id: alias(app1) + expose(button) = app1/button
1197
- // id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort
1198
- function matchRemoteWithNameAndExpose(remotes, id) {
1199
- for (const remote of remotes) {
1200
- // match pkgName
1201
- const isNameMatched = id.startsWith(remote.name);
1202
- let expose = id.replace(remote.name, '');
1203
- if (isNameMatched) {
1204
- if (expose.startsWith('/')) {
1205
- const pkgNameOrAlias = remote.name;
1206
- expose = `.${expose}`;
1207
- return {
1208
- pkgNameOrAlias,
1209
- expose,
1210
- remote,
1211
- };
1212
- }
1213
- else if (expose === '') {
1214
- return {
1215
- pkgNameOrAlias: remote.name,
1216
- expose: '.',
1217
- remote,
1218
- };
1219
- }
1220
- }
1221
- // match alias
1222
- const isAliasMatched = remote.alias && id.startsWith(remote.alias);
1223
- let exposeWithAlias = remote.alias && id.replace(remote.alias, '');
1224
- if (remote.alias && isAliasMatched) {
1225
- if (exposeWithAlias && exposeWithAlias.startsWith('/')) {
1226
- const pkgNameOrAlias = remote.alias;
1227
- exposeWithAlias = `.${exposeWithAlias}`;
1228
- return {
1229
- pkgNameOrAlias,
1230
- expose: exposeWithAlias,
1231
- remote,
1232
- };
1233
- }
1234
- else if (exposeWithAlias === '') {
1235
- return {
1236
- pkgNameOrAlias: remote.alias,
1237
- expose: '.',
1238
- remote,
1239
- };
1240
- }
1241
- }
1242
- }
1243
- return;
1244
- }
1245
- // Function to match a remote with its name or alias
1246
- function matchRemote(remotes, nameOrAlias) {
1247
- for (const remote of remotes) {
1248
- const isNameMatched = nameOrAlias === remote.name;
1249
- if (isNameMatched) {
1250
- return remote;
1251
- }
1252
- const isAliasMatched = remote.alias && nameOrAlias === remote.alias;
1253
- if (isAliasMatched) {
1254
- return remote;
1255
- }
1256
- }
1257
- return;
1258
- }
1259
-
1260
- function registerPlugins(plugins, instance) {
1261
- const globalPlugins = getGlobalHostPlugins();
1262
- const hookInstances = [
1263
- instance.hooks,
1264
- instance.remoteHandler.hooks,
1265
- instance.sharedHandler.hooks,
1266
- instance.snapshotHandler.hooks,
1267
- instance.loaderHook,
1268
- instance.bridgeHook,
1269
- ];
1270
- // Incorporate global plugins
1271
- if (globalPlugins.length > 0) {
1272
- globalPlugins.forEach((plugin) => {
1273
- if (plugins?.find((item) => item.name !== plugin.name)) {
1274
- plugins.push(plugin);
1275
- }
1276
- });
1277
- }
1278
- if (plugins && plugins.length > 0) {
1279
- plugins.forEach((plugin) => {
1280
- hookInstances.forEach((hookInstance) => {
1281
- hookInstance.applyPlugin(plugin, instance);
1282
- });
1283
- });
1284
- }
1285
- return plugins;
1286
- }
1287
-
1288
- const importCallback = '.then(callbacks[0]).catch(callbacks[1])';
1289
- async function loadEsmEntry({ entry, remoteEntryExports, }) {
1290
- return new Promise((resolve, reject) => {
1291
- try {
1292
- if (!remoteEntryExports) {
1293
- if (typeof FEDERATION_ALLOW_NEW_FUNCTION !== 'undefined') {
1294
- new Function('callbacks', `import("${entry}")${importCallback}`)([
1295
- resolve,
1296
- reject,
1297
- ]);
1298
- }
1299
- else {
1300
- import(/* webpackIgnore: true */ /* @vite-ignore */ entry)
1301
- .then(resolve)
1302
- .catch(reject);
1303
- }
1304
- }
1305
- else {
1306
- resolve(remoteEntryExports);
1307
- }
1308
- }
1309
- catch (e) {
1310
- reject(e);
1311
- }
1312
- });
1313
- }
1314
- async function loadSystemJsEntry({ entry, remoteEntryExports, }) {
1315
- return new Promise((resolve, reject) => {
1316
- try {
1317
- if (!remoteEntryExports) {
1318
- //@ts-ignore
1319
- if (typeof __system_context__ === 'undefined') {
1320
- //@ts-ignore
1321
- System.import(entry).then(resolve).catch(reject);
1322
- }
1323
- else {
1324
- new Function('callbacks', `System.import("${entry}")${importCallback}`)([resolve, reject]);
1325
- }
1326
- }
1327
- else {
1328
- resolve(remoteEntryExports);
1329
- }
1330
- }
1331
- catch (e) {
1332
- reject(e);
1333
- }
1334
- });
1335
- }
1336
- function handleRemoteEntryLoaded(name, globalName, entry) {
1337
- const { remoteEntryKey, entryExports } = getRemoteEntryExports(name, globalName);
1338
- assert(entryExports, getShortErrorMsg(RUNTIME_001, runtimeDescMap, {
1339
- remoteName: name,
1340
- remoteEntryUrl: entry,
1341
- remoteEntryKey,
1342
- }));
1343
- return entryExports;
1344
- }
1345
- async function loadEntryScript({ name, globalName, entry, loaderHook, getEntryUrl, }) {
1346
- const { entryExports: remoteEntryExports } = getRemoteEntryExports(name, globalName);
1347
- if (remoteEntryExports) {
1348
- return remoteEntryExports;
1349
- }
1350
- // if getEntryUrl is passed, use the getEntryUrl to get the entry url
1351
- const url = getEntryUrl ? getEntryUrl(entry) : entry;
1352
- return loadScript(url, {
1353
- attrs: {},
1354
- createScriptHook: (url, attrs) => {
1355
- const res = loaderHook.lifecycle.createScript.emit({ url, attrs });
1356
- if (!res)
1357
- return;
1358
- if (res instanceof HTMLScriptElement) {
1359
- return res;
1360
- }
1361
- if ('script' in res || 'timeout' in res) {
1362
- return res;
1363
- }
1364
- return;
1365
- },
1366
- })
1367
- .then(() => {
1368
- return handleRemoteEntryLoaded(name, globalName, entry);
1369
- })
1370
- .catch((e) => {
1371
- assert(undefined, getShortErrorMsg(RUNTIME_008, runtimeDescMap, {
1372
- remoteName: name,
1373
- resourceUrl: entry,
1374
- }));
1375
- throw e;
1376
- });
1377
- }
1378
- async function loadEntryDom({ remoteInfo, remoteEntryExports, loaderHook, getEntryUrl, }) {
1379
- const { entry, entryGlobalName: globalName, name, type } = remoteInfo;
1380
- switch (type) {
1381
- case 'esm':
1382
- case 'module':
1383
- return loadEsmEntry({ entry, remoteEntryExports });
1384
- case 'system':
1385
- return loadSystemJsEntry({ entry, remoteEntryExports });
1386
- default:
1387
- return loadEntryScript({
1388
- entry,
1389
- globalName,
1390
- name,
1391
- loaderHook,
1392
- getEntryUrl,
1393
- });
1394
- }
1395
- }
1396
- async function loadEntryNode({ remoteInfo, loaderHook, }) {
1397
- const { entry, entryGlobalName: globalName, name, type } = remoteInfo;
1398
- const { entryExports: remoteEntryExports } = getRemoteEntryExports(name, globalName);
1399
- if (remoteEntryExports) {
1400
- return remoteEntryExports;
1401
- }
1402
- return loadScriptNode(entry, {
1403
- attrs: { name, globalName, type },
1404
- loaderHook: {
1405
- createScriptHook: (url, attrs = {}) => {
1406
- const res = loaderHook.lifecycle.createScript.emit({ url, attrs });
1407
- if (!res)
1408
- return;
1409
- if ('url' in res) {
1410
- return res;
1411
- }
1412
- return;
1413
- },
1414
- },
1415
- })
1416
- .then(() => {
1417
- return handleRemoteEntryLoaded(name, globalName, entry);
1418
- })
1419
- .catch((e) => {
1420
- throw e;
1421
- });
1422
- }
1423
- function getRemoteEntryUniqueKey(remoteInfo) {
1424
- const { entry, name } = remoteInfo;
1425
- return composeKeyWithSeparator(name, entry);
1426
- }
1427
- async function getRemoteEntry(params) {
1428
- const { origin, remoteEntryExports, remoteInfo, getEntryUrl, _inErrorHandling = false, } = params;
1429
- const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
1430
- if (remoteEntryExports) {
1431
- return remoteEntryExports;
1432
- }
1433
- if (!globalLoading[uniqueKey]) {
1434
- const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;
1435
- const loaderHook = origin.loaderHook;
1436
- globalLoading[uniqueKey] = loadEntryHook
1437
- .emit({
1438
- loaderHook,
1439
- remoteInfo,
1440
- remoteEntryExports,
1441
- })
1442
- .then((res) => {
1443
- if (res) {
1444
- return res;
1445
- }
1446
- // Use ENV_TARGET if defined, otherwise fallback to isBrowserEnv, must keep this
1447
- const isWebEnvironment = typeof ENV_TARGET !== 'undefined'
1448
- ? ENV_TARGET === 'web'
1449
- : isBrowserEnv();
1450
- return isWebEnvironment
1451
- ? loadEntryDom({
1452
- remoteInfo,
1453
- remoteEntryExports,
1454
- loaderHook,
1455
- getEntryUrl,
1456
- })
1457
- : loadEntryNode({ remoteInfo, loaderHook });
1458
- })
1459
- .catch(async (err) => {
1460
- const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);
1461
- const isScriptLoadError = err instanceof Error && err.message.includes(RUNTIME_008);
1462
- if (isScriptLoadError && !_inErrorHandling) {
1463
- const wrappedGetRemoteEntry = (params) => {
1464
- return getRemoteEntry({ ...params, _inErrorHandling: true });
1465
- };
1466
- const RemoteEntryExports = await origin.loaderHook.lifecycle.loadEntryError.emit({
1467
- getRemoteEntry: wrappedGetRemoteEntry,
1468
- origin,
1469
- remoteInfo: remoteInfo,
1470
- remoteEntryExports,
1471
- globalLoading,
1472
- uniqueKey,
1473
- });
1474
- if (RemoteEntryExports) {
1475
- return RemoteEntryExports;
1476
- }
1477
- }
1478
- throw err;
1479
- });
1480
- }
1481
- return globalLoading[uniqueKey];
1482
- }
1483
- function getRemoteInfo(remote) {
1484
- return {
1485
- ...remote,
1486
- entry: 'entry' in remote ? remote.entry : '',
1487
- type: remote.type || DEFAULT_REMOTE_TYPE,
1488
- entryGlobalName: remote.entryGlobalName || remote.name,
1489
- shareScope: remote.shareScope || DEFAULT_SCOPE,
1490
- };
1491
- }
1492
-
1493
- function defaultPreloadArgs(preloadConfig) {
1494
- return {
1495
- resourceCategory: 'sync',
1496
- share: true,
1497
- depsRemote: true,
1498
- prefetchInterface: false,
1499
- ...preloadConfig,
1500
- };
1501
- }
1502
- function formatPreloadArgs(remotes, preloadArgs) {
1503
- return preloadArgs.map((args) => {
1504
- const remoteInfo = matchRemote(remotes, args.nameOrAlias);
1505
- assert(remoteInfo, `Unable to preload ${args.nameOrAlias} as it is not included in ${!remoteInfo &&
1506
- safeToString({
1507
- remoteInfo,
1508
- remotes,
1509
- })}`);
1510
- return {
1511
- remote: remoteInfo,
1512
- preloadConfig: defaultPreloadArgs(args),
1513
- };
1514
- });
1515
- }
1516
- function normalizePreloadExposes(exposes) {
1517
- if (!exposes) {
1518
- return [];
1519
- }
1520
- return exposes.map((expose) => {
1521
- if (expose === '.') {
1522
- return expose;
1523
- }
1524
- if (expose.startsWith('./')) {
1525
- return expose.replace('./', '');
1526
- }
1527
- return expose;
1528
- });
1529
- }
1530
- function preloadAssets(remoteInfo, host, assets,
1531
- // It is used to distinguish preload from load remote parallel loading
1532
- useLinkPreload = true) {
1533
- const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
1534
- if (host.options.inBrowser) {
1535
- entryAssets.forEach((asset) => {
1536
- const { moduleInfo } = asset;
1537
- const module = host.moduleCache.get(remoteInfo.name);
1538
- if (module) {
1539
- getRemoteEntry({
1540
- origin: host,
1541
- remoteInfo: moduleInfo,
1542
- remoteEntryExports: module.remoteEntryExports,
1543
- });
1544
- }
1545
- else {
1546
- getRemoteEntry({
1547
- origin: host,
1548
- remoteInfo: moduleInfo,
1549
- remoteEntryExports: undefined,
1550
- });
1551
- }
1552
- });
1553
- if (useLinkPreload) {
1554
- const defaultAttrs = {
1555
- rel: 'preload',
1556
- as: 'style',
1557
- };
1558
- cssAssets.forEach((cssUrl) => {
1559
- const { link: cssEl, needAttach } = createLink({
1560
- url: cssUrl,
1561
- cb: () => {
1562
- // noop
1563
- },
1564
- attrs: defaultAttrs,
1565
- createLinkHook: (url, attrs) => {
1566
- const res = host.loaderHook.lifecycle.createLink.emit({
1567
- url,
1568
- attrs,
1569
- });
1570
- if (res instanceof HTMLLinkElement) {
1571
- return res;
1572
- }
1573
- return;
1574
- },
1575
- });
1576
- needAttach && document.head.appendChild(cssEl);
1577
- });
1578
- }
1579
- else {
1580
- const defaultAttrs = {
1581
- rel: 'stylesheet',
1582
- type: 'text/css',
1583
- };
1584
- cssAssets.forEach((cssUrl) => {
1585
- const { link: cssEl, needAttach } = createLink({
1586
- url: cssUrl,
1587
- cb: () => {
1588
- // noop
1589
- },
1590
- attrs: defaultAttrs,
1591
- createLinkHook: (url, attrs) => {
1592
- const res = host.loaderHook.lifecycle.createLink.emit({
1593
- url,
1594
- attrs,
1595
- });
1596
- if (res instanceof HTMLLinkElement) {
1597
- return res;
1598
- }
1599
- return;
1600
- },
1601
- needDeleteLink: false,
1602
- });
1603
- needAttach && document.head.appendChild(cssEl);
1604
- });
1605
- }
1606
- if (useLinkPreload) {
1607
- const defaultAttrs = {
1608
- rel: 'preload',
1609
- as: 'script',
1610
- };
1611
- jsAssetsWithoutEntry.forEach((jsUrl) => {
1612
- const { link: linkEl, needAttach } = createLink({
1613
- url: jsUrl,
1614
- cb: () => {
1615
- // noop
1616
- },
1617
- attrs: defaultAttrs,
1618
- createLinkHook: (url, attrs) => {
1619
- const res = host.loaderHook.lifecycle.createLink.emit({
1620
- url,
1621
- attrs,
1622
- });
1623
- if (res instanceof HTMLLinkElement) {
1624
- return res;
1625
- }
1626
- return;
1627
- },
1628
- });
1629
- needAttach && document.head.appendChild(linkEl);
1630
- });
1631
- }
1632
- else {
1633
- const defaultAttrs = {
1634
- fetchpriority: 'high',
1635
- type: remoteInfo?.type === 'module' ? 'module' : 'text/javascript',
1636
- };
1637
- jsAssetsWithoutEntry.forEach((jsUrl) => {
1638
- const { script: scriptEl, needAttach } = createScript({
1639
- url: jsUrl,
1640
- cb: () => {
1641
- // noop
1642
- },
1643
- attrs: defaultAttrs,
1644
- createScriptHook: (url, attrs) => {
1645
- const res = host.loaderHook.lifecycle.createScript.emit({
1646
- url,
1647
- attrs,
1648
- });
1649
- if (res instanceof HTMLScriptElement) {
1650
- return res;
1651
- }
1652
- return;
1653
- },
1654
- needDeleteScript: true,
1655
- });
1656
- needAttach && document.head.appendChild(scriptEl);
1657
- });
1658
- }
1659
- }
1660
- }
1661
-
1662
- const ShareUtils = {
1663
- getRegisteredShare,
1664
- getGlobalShareScope,
1665
- };
1666
- const GlobalUtils = {
1667
- Global,
1668
- nativeGlobal,
1669
- resetFederationGlobalInfo,
1670
- setGlobalFederationInstance,
1671
- getGlobalFederationConstructor,
1672
- setGlobalFederationConstructor,
1673
- getInfoWithoutType,
1674
- getGlobalSnapshot,
1675
- getTargetSnapshotInfoByModuleInfo,
1676
- getGlobalSnapshotInfoByModuleInfo,
1677
- setGlobalSnapshotInfoByModuleInfo,
1678
- addGlobalSnapshot,
1679
- getRemoteEntryExports,
1680
- registerGlobalPlugins,
1681
- getGlobalHostPlugins,
1682
- getPreloaded,
1683
- setPreloaded,
1684
- };
1685
- var helpers = {
1686
- global: GlobalUtils,
1687
- share: ShareUtils,
1688
- utils: {
1689
- matchRemoteWithNameAndExpose,
1690
- preloadAssets,
1691
- getRemoteInfo,
1692
- },
1693
- };
1694
-
1695
- function createRemoteEntryInitOptions(remoteInfo, hostShareScopeMap) {
1696
- const localShareScopeMap = hostShareScopeMap;
1697
- const shareScopeKeys = Array.isArray(remoteInfo.shareScope)
1698
- ? remoteInfo.shareScope
1699
- : [remoteInfo.shareScope];
1700
- if (!shareScopeKeys.length) {
1701
- shareScopeKeys.push('default');
1702
- }
1703
- shareScopeKeys.forEach((shareScopeKey) => {
1704
- if (!localShareScopeMap[shareScopeKey]) {
1705
- localShareScopeMap[shareScopeKey] = {};
1706
- }
1707
- });
1708
- const remoteEntryInitOptions = {
1709
- version: remoteInfo.version || '',
1710
- shareScopeKeys: Array.isArray(remoteInfo.shareScope)
1711
- ? shareScopeKeys
1712
- : remoteInfo.shareScope || 'default',
1713
- };
1714
- // Help to find host instance
1715
- Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
1716
- value: localShareScopeMap,
1717
- // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
1718
- enumerable: false,
1719
- });
1720
- // TODO: compate legacy init params, should use shareScopeMap if exist
1721
- const shareScope = localShareScopeMap[shareScopeKeys[0]];
1722
- const initScope = [];
1723
- return {
1724
- remoteEntryInitOptions,
1725
- shareScope,
1726
- initScope,
1727
- };
1728
- }
1729
- class Module {
1730
- constructor({ remoteInfo, host, }) {
1731
- this.inited = false;
1732
- this.initing = false;
1733
- this.lib = undefined;
1734
- this.remoteInfo = remoteInfo;
1735
- this.host = host;
1736
- }
1737
- async getEntry() {
1738
- if (this.remoteEntryExports) {
1739
- return this.remoteEntryExports;
1740
- }
1741
- const remoteEntryExports = await getRemoteEntry({
1742
- origin: this.host,
1743
- remoteInfo: this.remoteInfo,
1744
- remoteEntryExports: this.remoteEntryExports,
1745
- });
1746
- assert(remoteEntryExports, `remoteEntryExports is undefined \n ${safeToString(this.remoteInfo)}`);
1747
- this.remoteEntryExports = remoteEntryExports;
1748
- return this.remoteEntryExports;
1749
- }
1750
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1751
- async init(id, remoteSnapshot) {
1752
- // Get remoteEntry.js
1753
- const remoteEntryExports = await this.getEntry();
1754
- if (this.inited) {
1755
- return remoteEntryExports;
1756
- }
1757
- if (this.initPromise) {
1758
- await this.initPromise;
1759
- return remoteEntryExports;
1760
- }
1761
- this.initing = true;
1762
- this.initPromise = (async () => {
1763
- const { remoteEntryInitOptions, shareScope, initScope } = createRemoteEntryInitOptions(this.remoteInfo, this.host.shareScopeMap);
1764
- const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
1765
- shareScope,
1766
- // @ts-ignore shareScopeMap will be set by Object.defineProperty
1767
- remoteEntryInitOptions,
1768
- initScope,
1769
- remoteInfo: this.remoteInfo,
1770
- origin: this.host,
1771
- });
1772
- if (typeof remoteEntryExports?.init === 'undefined') {
1773
- error(getShortErrorMsg(RUNTIME_002, runtimeDescMap, {
1774
- hostName: this.host.name,
1775
- remoteName: this.remoteInfo.name,
1776
- remoteEntryUrl: this.remoteInfo.entry,
1777
- remoteEntryKey: this.remoteInfo.entryGlobalName,
1778
- }));
1779
- }
1780
- await remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
1781
- await this.host.hooks.lifecycle.initContainer.emit({
1782
- ...initContainerOptions,
1783
- id,
1784
- remoteSnapshot,
1785
- remoteEntryExports,
1786
- });
1787
- this.inited = true;
1788
- })();
1789
- try {
1790
- await this.initPromise;
1791
- }
1792
- finally {
1793
- this.initing = false;
1794
- this.initPromise = undefined;
1795
- }
1796
- return remoteEntryExports;
1797
- }
1798
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1799
- async get(id, expose, options, remoteSnapshot) {
1800
- const { loadFactory = true } = options || { loadFactory: true };
1801
- const remoteEntryExports = await this.init(id, remoteSnapshot);
1802
- this.lib = remoteEntryExports;
1803
- let moduleFactory;
1804
- moduleFactory = await this.host.loaderHook.lifecycle.getModuleFactory.emit({
1805
- remoteEntryExports,
1806
- expose,
1807
- moduleInfo: this.remoteInfo,
1808
- });
1809
- // get exposeGetter
1810
- if (!moduleFactory) {
1811
- moduleFactory = await remoteEntryExports.get(expose);
1812
- }
1813
- assert(moduleFactory, `${getFMId(this.remoteInfo)} remote don't export ${expose}.`);
1814
- // keep symbol for module name always one format
1815
- const symbolName = processModuleAlias(this.remoteInfo.name, expose);
1816
- const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);
1817
- if (!loadFactory) {
1818
- return wrapModuleFactory;
1819
- }
1820
- const exposeContent = await wrapModuleFactory();
1821
- return exposeContent;
1822
- }
1823
- wraperFactory(moduleFactory, id) {
1824
- function defineModuleId(res, id) {
1825
- if (res &&
1826
- typeof res === 'object' &&
1827
- Object.isExtensible(res) &&
1828
- !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))) {
1829
- Object.defineProperty(res, Symbol.for('mf_module_id'), {
1830
- value: id,
1831
- enumerable: false,
1832
- });
1833
- }
1834
- }
1835
- if (moduleFactory instanceof Promise) {
1836
- return async () => {
1837
- const res = await moduleFactory();
1838
- // This parameter is used for bridge debugging
1839
- defineModuleId(res, id);
1840
- return res;
1841
- };
1842
- }
1843
- else {
1844
- return () => {
1845
- const res = moduleFactory();
1846
- // This parameter is used for bridge debugging
1847
- defineModuleId(res, id);
1848
- return res;
1849
- };
1850
- }
1851
- }
1852
- }
1853
-
1854
- class SyncHook {
1855
- constructor(type) {
1856
- this.type = '';
1857
- this.listeners = new Set();
1858
- if (type) {
1859
- this.type = type;
1860
- }
1861
- }
1862
- on(fn) {
1863
- if (typeof fn === 'function') {
1864
- this.listeners.add(fn);
1865
- }
1866
- }
1867
- once(fn) {
1868
- // eslint-disable-next-line @typescript-eslint/no-this-alias
1869
- const self = this;
1870
- this.on(function wrapper(...args) {
1871
- self.remove(wrapper);
1872
- // eslint-disable-next-line prefer-spread
1873
- return fn.apply(null, args);
1874
- });
1875
- }
1876
- emit(...data) {
1877
- let result;
1878
- if (this.listeners.size > 0) {
1879
- // eslint-disable-next-line prefer-spread
1880
- this.listeners.forEach((fn) => {
1881
- result = fn(...data);
1882
- });
1883
- }
1884
- return result;
1885
- }
1886
- remove(fn) {
1887
- this.listeners.delete(fn);
1888
- }
1889
- removeAll() {
1890
- this.listeners.clear();
1891
- }
1892
- }
1893
-
1894
- class AsyncHook extends SyncHook {
1895
- emit(...data) {
1896
- let result;
1897
- const ls = Array.from(this.listeners);
1898
- if (ls.length > 0) {
1899
- let i = 0;
1900
- const call = (prev) => {
1901
- if (prev === false) {
1902
- return false; // Abort process
1903
- }
1904
- else if (i < ls.length) {
1905
- return Promise.resolve(ls[i++].apply(null, data)).then(call);
1906
- }
1907
- else {
1908
- return prev;
1909
- }
1910
- };
1911
- result = call();
1912
- }
1913
- return Promise.resolve(result);
1914
- }
1915
- }
1916
-
1917
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1918
- function checkReturnData(originalData, returnedData) {
1919
- if (!isObject(returnedData)) {
1920
- return false;
1921
- }
1922
- if (originalData !== returnedData) {
1923
- // eslint-disable-next-line no-restricted-syntax
1924
- for (const key in originalData) {
1925
- if (!(key in returnedData)) {
1926
- return false;
1927
- }
1928
- }
1929
- }
1930
- return true;
1931
- }
1932
- class SyncWaterfallHook extends SyncHook {
1933
- constructor(type) {
1934
- super();
1935
- this.onerror = error;
1936
- this.type = type;
1937
- }
1938
- emit(data) {
1939
- if (!isObject(data)) {
1940
- error(`The data for the "${this.type}" hook should be an object.`);
1941
- }
1942
- for (const fn of this.listeners) {
1943
- try {
1944
- const tempData = fn(data);
1945
- if (checkReturnData(data, tempData)) {
1946
- data = tempData;
1947
- }
1948
- else {
1949
- this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
1950
- break;
1951
- }
1952
- }
1953
- catch (e) {
1954
- warn(e);
1955
- this.onerror(e);
1956
- }
1957
- }
1958
- return data;
1959
- }
1960
- }
1961
-
1962
- class AsyncWaterfallHook extends SyncHook {
1963
- constructor(type) {
1964
- super();
1965
- this.onerror = error;
1966
- this.type = type;
1967
- }
1968
- emit(data) {
1969
- if (!isObject(data)) {
1970
- error(`The response data for the "${this.type}" hook must be an object.`);
1971
- }
1972
- const ls = Array.from(this.listeners);
1973
- if (ls.length > 0) {
1974
- let i = 0;
1975
- const processError = (e) => {
1976
- warn(e);
1977
- this.onerror(e);
1978
- return data;
1979
- };
1980
- const call = (prevData) => {
1981
- if (checkReturnData(data, prevData)) {
1982
- data = prevData;
1983
- if (i < ls.length) {
1984
- try {
1985
- return Promise.resolve(ls[i++](data)).then(call, processError);
1986
- }
1987
- catch (e) {
1988
- return processError(e);
1989
- }
1990
- }
1991
- }
1992
- else {
1993
- this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
1994
- }
1995
- return data;
1996
- };
1997
- return Promise.resolve(call(data));
1998
- }
1999
- return Promise.resolve(data);
2000
- }
2001
- }
2002
-
2003
- class PluginSystem {
2004
- constructor(lifecycle) {
2005
- this.registerPlugins = {};
2006
- this.lifecycle = lifecycle;
2007
- this.lifecycleKeys = Object.keys(lifecycle);
2008
- }
2009
- applyPlugin(plugin, instance) {
2010
- assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
2011
- // The plugin's name is mandatory and must be unique
2012
- const pluginName = plugin.name;
2013
- assert(pluginName, 'A name must be provided by the plugin.');
2014
- if (!this.registerPlugins[pluginName]) {
2015
- this.registerPlugins[pluginName] = plugin;
2016
- plugin.apply?.(instance);
2017
- Object.keys(this.lifecycle).forEach((key) => {
2018
- const pluginLife = plugin[key];
2019
- if (pluginLife) {
2020
- this.lifecycle[key].on(pluginLife);
2021
- }
2022
- });
2023
- }
2024
- }
2025
- removePlugin(pluginName) {
2026
- assert(pluginName, 'A name is required.');
2027
- const plugin = this.registerPlugins[pluginName];
2028
- assert(plugin, `The plugin "${pluginName}" is not registered.`);
2029
- Object.keys(plugin).forEach((key) => {
2030
- if (key !== 'name') {
2031
- this.lifecycle[key].remove(plugin[key]);
2032
- }
2033
- });
2034
- }
2035
- }
2036
-
2037
- function assignRemoteInfo(remoteInfo, remoteSnapshot) {
2038
- const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);
2039
- if (!remoteEntryInfo.url) {
2040
- error(`The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`);
2041
- }
2042
- let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);
2043
- if (!isBrowserEnv() && !entryUrl.startsWith('http')) {
2044
- entryUrl = `https:${entryUrl}`;
2045
- }
2046
- remoteInfo.type = remoteEntryInfo.type;
2047
- remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
2048
- remoteInfo.entry = entryUrl;
2049
- remoteInfo.version = remoteSnapshot.version;
2050
- remoteInfo.buildVersion = remoteSnapshot.buildVersion;
2051
- }
2052
- function snapshotPlugin() {
2053
- return {
2054
- name: 'snapshot-plugin',
2055
- async afterResolve(args) {
2056
- const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;
2057
- if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {
2058
- const { remoteSnapshot, globalSnapshot } = await origin.snapshotHandler.loadRemoteSnapshotInfo({
2059
- moduleInfo: remote,
2060
- id,
2061
- });
2062
- assignRemoteInfo(remoteInfo, remoteSnapshot);
2063
- // preloading assets
2064
- const preloadOptions = {
2065
- remote,
2066
- preloadConfig: {
2067
- nameOrAlias: pkgNameOrAlias,
2068
- exposes: [expose],
2069
- resourceCategory: 'sync',
2070
- share: false,
2071
- depsRemote: false,
2072
- },
2073
- };
2074
- const assets = await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
2075
- origin,
2076
- preloadOptions,
2077
- remoteInfo,
2078
- remote,
2079
- remoteSnapshot,
2080
- globalSnapshot,
2081
- });
2082
- if (assets) {
2083
- preloadAssets(remoteInfo, origin, assets, false);
2084
- }
2085
- return {
2086
- ...args,
2087
- remoteSnapshot,
2088
- };
2089
- }
2090
- return args;
2091
- },
2092
- };
2093
- }
2094
-
2095
- // name
2096
- // name:version
2097
- function splitId(id) {
2098
- const splitInfo = id.split(':');
2099
- if (splitInfo.length === 1) {
2100
- return {
2101
- name: splitInfo[0],
2102
- version: undefined,
2103
- };
2104
- }
2105
- else if (splitInfo.length === 2) {
2106
- return {
2107
- name: splitInfo[0],
2108
- version: splitInfo[1],
2109
- };
2110
- }
2111
- else {
2112
- return {
2113
- name: splitInfo[1],
2114
- version: splitInfo[2],
2115
- };
2116
- }
2117
- }
2118
- // Traverse all nodes in moduleInfo and traverse the entire snapshot
2119
- function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo = {}, remoteSnapshot) {
2120
- const id = getFMId(remoteInfo);
2121
- const { value: snapshotValue } = getInfoWithoutType(globalSnapshot, id);
2122
- const effectiveRemoteSnapshot = remoteSnapshot || snapshotValue;
2123
- if (effectiveRemoteSnapshot && !isManifestProvider(effectiveRemoteSnapshot)) {
2124
- traverse(effectiveRemoteSnapshot, remoteInfo, isRoot);
2125
- if (effectiveRemoteSnapshot.remotesInfo) {
2126
- const remoteKeys = Object.keys(effectiveRemoteSnapshot.remotesInfo);
2127
- for (const key of remoteKeys) {
2128
- if (memo[key]) {
2129
- continue;
2130
- }
2131
- memo[key] = true;
2132
- const subRemoteInfo = splitId(key);
2133
- const remoteValue = effectiveRemoteSnapshot.remotesInfo[key];
2134
- traverseModuleInfo(globalSnapshot, {
2135
- name: subRemoteInfo.name,
2136
- version: remoteValue.matchedVersion,
2137
- }, traverse, false, memo, undefined);
2138
- }
2139
- }
2140
- }
2141
- }
2142
- const isExisted = (type, url) => {
2143
- return document.querySelector(`${type}[${type === 'link' ? 'href' : 'src'}="${url}"]`);
2144
- };
2145
- // eslint-disable-next-line max-lines-per-function
2146
- function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, remoteSnapshot) {
2147
- const cssAssets = [];
2148
- const jsAssets = [];
2149
- const entryAssets = [];
2150
- const loadedSharedJsAssets = new Set();
2151
- const loadedSharedCssAssets = new Set();
2152
- const { options } = origin;
2153
- const { preloadConfig: rootPreloadConfig } = preloadOptions;
2154
- const { depsRemote } = rootPreloadConfig;
2155
- const memo = {};
2156
- traverseModuleInfo(globalSnapshot, remote, (moduleInfoSnapshot, remoteInfo, isRoot) => {
2157
- let preloadConfig;
2158
- if (isRoot) {
2159
- preloadConfig = rootPreloadConfig;
2160
- }
2161
- else {
2162
- if (Array.isArray(depsRemote)) {
2163
- // eslint-disable-next-line array-callback-return
2164
- const findPreloadConfig = depsRemote.find((remoteConfig) => {
2165
- if (remoteConfig.nameOrAlias === remoteInfo.name ||
2166
- remoteConfig.nameOrAlias === remoteInfo.alias) {
2167
- return true;
2168
- }
2169
- return false;
2170
- });
2171
- if (!findPreloadConfig) {
2172
- return;
2173
- }
2174
- preloadConfig = defaultPreloadArgs(findPreloadConfig);
2175
- }
2176
- else if (depsRemote === true) {
2177
- preloadConfig = rootPreloadConfig;
2178
- }
2179
- else {
2180
- return;
2181
- }
2182
- }
2183
- const remoteEntryUrl = getResourceUrl(moduleInfoSnapshot, getRemoteEntryInfoFromSnapshot(moduleInfoSnapshot).url);
2184
- if (remoteEntryUrl) {
2185
- entryAssets.push({
2186
- name: remoteInfo.name,
2187
- moduleInfo: {
2188
- name: remoteInfo.name,
2189
- entry: remoteEntryUrl,
2190
- type: 'remoteEntryType' in moduleInfoSnapshot
2191
- ? moduleInfoSnapshot.remoteEntryType
2192
- : 'global',
2193
- entryGlobalName: 'globalName' in moduleInfoSnapshot
2194
- ? moduleInfoSnapshot.globalName
2195
- : remoteInfo.name,
2196
- shareScope: '',
2197
- version: 'version' in moduleInfoSnapshot
2198
- ? moduleInfoSnapshot.version
2199
- : undefined,
2200
- },
2201
- url: remoteEntryUrl,
2202
- });
2203
- }
2204
- let moduleAssetsInfo = 'modules' in moduleInfoSnapshot ? moduleInfoSnapshot.modules : [];
2205
- const normalizedPreloadExposes = normalizePreloadExposes(preloadConfig.exposes);
2206
- if (normalizedPreloadExposes.length && 'modules' in moduleInfoSnapshot) {
2207
- moduleAssetsInfo = moduleInfoSnapshot?.modules?.reduce((assets, moduleAssetInfo) => {
2208
- if (normalizedPreloadExposes?.indexOf(moduleAssetInfo.moduleName) !==
2209
- -1) {
2210
- assets.push(moduleAssetInfo);
2211
- }
2212
- return assets;
2213
- }, []);
2214
- }
2215
- function handleAssets(assets) {
2216
- const assetsRes = assets.map((asset) => getResourceUrl(moduleInfoSnapshot, asset));
2217
- if (preloadConfig.filter) {
2218
- return assetsRes.filter(preloadConfig.filter);
2219
- }
2220
- return assetsRes;
2221
- }
2222
- if (moduleAssetsInfo) {
2223
- const assetsLength = moduleAssetsInfo.length;
2224
- for (let index = 0; index < assetsLength; index++) {
2225
- const assetsInfo = moduleAssetsInfo[index];
2226
- const exposeFullPath = `${remoteInfo.name}/${assetsInfo.moduleName}`;
2227
- origin.remoteHandler.hooks.lifecycle.handlePreloadModule.emit({
2228
- id: assetsInfo.moduleName === '.' ? remoteInfo.name : exposeFullPath,
2229
- name: remoteInfo.name,
2230
- remoteSnapshot: moduleInfoSnapshot,
2231
- preloadConfig,
2232
- remote: remoteInfo,
2233
- origin,
2234
- });
2235
- const preloaded = getPreloaded(exposeFullPath);
2236
- if (preloaded) {
2237
- continue;
2238
- }
2239
- if (preloadConfig.resourceCategory === 'all') {
2240
- cssAssets.push(...handleAssets(assetsInfo.assets.css.async));
2241
- cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
2242
- jsAssets.push(...handleAssets(assetsInfo.assets.js.async));
2243
- jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
2244
- // eslint-disable-next-line no-constant-condition
2245
- }
2246
- else if ((preloadConfig.resourceCategory = 'sync')) {
2247
- cssAssets.push(...handleAssets(assetsInfo.assets.css.sync));
2248
- jsAssets.push(...handleAssets(assetsInfo.assets.js.sync));
2249
- }
2250
- setPreloaded(exposeFullPath);
2251
- }
2252
- }
2253
- }, true, memo, remoteSnapshot);
2254
- if (remoteSnapshot.shared && remoteSnapshot.shared.length > 0) {
2255
- const collectSharedAssets = (shareInfo, snapshotShared) => {
2256
- const { shared: registeredShared } = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare) || {};
2257
- // 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.
2258
- if (registeredShared && typeof registeredShared.lib === 'function') {
2259
- snapshotShared.assets.js.sync.forEach((asset) => {
2260
- loadedSharedJsAssets.add(asset);
2261
- });
2262
- snapshotShared.assets.css.sync.forEach((asset) => {
2263
- loadedSharedCssAssets.add(asset);
2264
- });
2265
- }
2266
- };
2267
- remoteSnapshot.shared.forEach((shared) => {
2268
- const shareInfos = options.shared?.[shared.sharedName];
2269
- if (!shareInfos) {
2270
- return;
2271
- }
2272
- // if no version, preload all shared
2273
- const sharedOptions = shared.version
2274
- ? shareInfos.find((s) => s.version === shared.version)
2275
- : shareInfos;
2276
- if (!sharedOptions) {
2277
- return;
2278
- }
2279
- const arrayShareInfo = arrayOptions(sharedOptions);
2280
- arrayShareInfo.forEach((s) => {
2281
- collectSharedAssets(s, shared);
2282
- });
2283
- });
2284
- }
2285
- const needPreloadJsAssets = jsAssets.filter((asset) => !loadedSharedJsAssets.has(asset) && !isExisted('script', asset));
2286
- const needPreloadCssAssets = cssAssets.filter((asset) => !loadedSharedCssAssets.has(asset) && !isExisted('link', asset));
2287
- return {
2288
- cssAssets: needPreloadCssAssets,
2289
- jsAssetsWithoutEntry: needPreloadJsAssets,
2290
- entryAssets: entryAssets.filter((entry) => !isExisted('script', entry.url)),
2291
- };
2292
- }
2293
- const generatePreloadAssetsPlugin = function () {
2294
- return {
2295
- name: 'generate-preload-assets-plugin',
2296
- async generatePreloadAssets(args) {
2297
- const { origin, preloadOptions, remoteInfo, remote, globalSnapshot, remoteSnapshot, } = args;
2298
- if (!isBrowserEnv()) {
2299
- return {
2300
- cssAssets: [],
2301
- jsAssetsWithoutEntry: [],
2302
- entryAssets: [],
2303
- };
2304
- }
2305
- if (isRemoteInfoWithEntry(remote) && isPureRemoteEntry(remote)) {
2306
- return {
2307
- cssAssets: [],
2308
- jsAssetsWithoutEntry: [],
2309
- entryAssets: [
2310
- {
2311
- name: remote.name,
2312
- url: remote.entry,
2313
- moduleInfo: {
2314
- name: remoteInfo.name,
2315
- entry: remote.entry,
2316
- type: remoteInfo.type || 'global',
2317
- entryGlobalName: '',
2318
- shareScope: '',
2319
- },
2320
- },
2321
- ],
2322
- };
2323
- }
2324
- assignRemoteInfo(remoteInfo, remoteSnapshot);
2325
- const assets = generatePreloadAssets(origin, preloadOptions, remoteInfo, globalSnapshot, remoteSnapshot);
2326
- return assets;
2327
- },
2328
- };
2329
- };
2330
-
2331
- function getGlobalRemoteInfo(moduleInfo, origin) {
2332
- const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({
2333
- name: origin.name,
2334
- version: origin.options.version,
2335
- });
2336
- // get remote detail info from global
2337
- const globalRemoteInfo = hostGlobalSnapshot &&
2338
- 'remotesInfo' in hostGlobalSnapshot &&
2339
- hostGlobalSnapshot.remotesInfo &&
2340
- getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;
2341
- if (globalRemoteInfo && globalRemoteInfo.matchedVersion) {
2342
- return {
2343
- hostGlobalSnapshot,
2344
- globalSnapshot: getGlobalSnapshot(),
2345
- remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
2346
- name: moduleInfo.name,
2347
- version: globalRemoteInfo.matchedVersion,
2348
- }),
2349
- };
2350
- }
2351
- return {
2352
- hostGlobalSnapshot: undefined,
2353
- globalSnapshot: getGlobalSnapshot(),
2354
- remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
2355
- name: moduleInfo.name,
2356
- version: 'version' in moduleInfo ? moduleInfo.version : undefined,
2357
- }),
2358
- };
2359
- }
2360
- class SnapshotHandler {
2361
- constructor(HostInstance) {
2362
- this.loadingHostSnapshot = null;
2363
- this.manifestCache = new Map();
2364
- this.hooks = new PluginSystem({
2365
- beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
2366
- loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
2367
- loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot'),
2368
- afterLoadSnapshot: new AsyncWaterfallHook('afterLoadSnapshot'),
2369
- });
2370
- this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
2371
- this.HostInstance = HostInstance;
2372
- this.loaderHook = HostInstance.loaderHook;
2373
- }
2374
- // eslint-disable-next-line max-lines-per-function
2375
- async loadRemoteSnapshotInfo({ moduleInfo, id, expose, }) {
2376
- const { options } = this.HostInstance;
2377
- await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({
2378
- options,
2379
- moduleInfo,
2380
- });
2381
- let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({
2382
- name: this.HostInstance.options.name,
2383
- version: this.HostInstance.options.version,
2384
- });
2385
- if (!hostSnapshot) {
2386
- hostSnapshot = {
2387
- version: this.HostInstance.options.version || '',
2388
- remoteEntry: '',
2389
- remotesInfo: {},
2390
- };
2391
- addGlobalSnapshot({
2392
- [this.HostInstance.options.name]: hostSnapshot,
2393
- });
2394
- }
2395
- // 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.
2396
- // 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.
2397
- if (hostSnapshot &&
2398
- 'remotesInfo' in hostSnapshot &&
2399
- !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
2400
- if ('version' in moduleInfo || 'entry' in moduleInfo) {
2401
- hostSnapshot.remotesInfo = {
2402
- ...hostSnapshot?.remotesInfo,
2403
- [moduleInfo.name]: {
2404
- matchedVersion: 'version' in moduleInfo ? moduleInfo.version : moduleInfo.entry,
2405
- },
2406
- };
2407
- }
2408
- }
2409
- const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } = this.getGlobalRemoteInfo(moduleInfo);
2410
- const { remoteSnapshot: globalRemoteSnapshot, globalSnapshot: globalSnapshotRes, } = await this.hooks.lifecycle.loadSnapshot.emit({
2411
- options,
2412
- moduleInfo,
2413
- hostGlobalSnapshot,
2414
- remoteSnapshot,
2415
- globalSnapshot,
2416
- });
2417
- let mSnapshot;
2418
- let gSnapshot;
2419
- // global snapshot includes manifest or module info includes manifest
2420
- if (globalRemoteSnapshot) {
2421
- if (isManifestProvider(globalRemoteSnapshot)) {
2422
- const remoteEntry = isBrowserEnv()
2423
- ? globalRemoteSnapshot.remoteEntry
2424
- : globalRemoteSnapshot.ssrRemoteEntry ||
2425
- globalRemoteSnapshot.remoteEntry ||
2426
- '';
2427
- const moduleSnapshot = await this.getManifestJson(remoteEntry, moduleInfo, {});
2428
- // eslint-disable-next-line @typescript-eslint/no-shadow
2429
- const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo({
2430
- ...moduleInfo,
2431
- // The global remote may be overridden
2432
- // Therefore, set the snapshot key to the global address of the actual request
2433
- entry: remoteEntry,
2434
- }, moduleSnapshot);
2435
- mSnapshot = moduleSnapshot;
2436
- gSnapshot = globalSnapshotRes;
2437
- }
2438
- else {
2439
- const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
2440
- options: this.HostInstance.options,
2441
- moduleInfo,
2442
- remoteSnapshot: globalRemoteSnapshot,
2443
- from: 'global',
2444
- });
2445
- mSnapshot = remoteSnapshotRes;
2446
- gSnapshot = globalSnapshotRes;
2447
- }
2448
- }
2449
- else {
2450
- if (isRemoteInfoWithEntry(moduleInfo)) {
2451
- // get from manifest.json and merge remote info from remote server
2452
- const moduleSnapshot = await this.getManifestJson(moduleInfo.entry, moduleInfo, {});
2453
- // eslint-disable-next-line @typescript-eslint/no-shadow
2454
- const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(moduleInfo, moduleSnapshot);
2455
- const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
2456
- options: this.HostInstance.options,
2457
- moduleInfo,
2458
- remoteSnapshot: moduleSnapshot,
2459
- from: 'global',
2460
- });
2461
- mSnapshot = remoteSnapshotRes;
2462
- gSnapshot = globalSnapshotRes;
2463
- }
2464
- else {
2465
- error(getShortErrorMsg(RUNTIME_007, runtimeDescMap, {
2466
- hostName: moduleInfo.name,
2467
- hostVersion: moduleInfo.version,
2468
- globalSnapshot: JSON.stringify(globalSnapshotRes),
2469
- }));
2470
- }
2471
- }
2472
- await this.hooks.lifecycle.afterLoadSnapshot.emit({
2473
- id,
2474
- host: this.HostInstance,
2475
- options,
2476
- moduleInfo,
2477
- remoteSnapshot: mSnapshot,
2478
- });
2479
- return {
2480
- remoteSnapshot: mSnapshot,
2481
- globalSnapshot: gSnapshot,
2482
- };
2483
- }
2484
- getGlobalRemoteInfo(moduleInfo) {
2485
- return getGlobalRemoteInfo(moduleInfo, this.HostInstance);
2486
- }
2487
- async getManifestJson(manifestUrl, moduleInfo, extraOptions) {
2488
- const getManifest = async () => {
2489
- let manifestJson = this.manifestCache.get(manifestUrl);
2490
- if (manifestJson) {
2491
- return manifestJson;
2492
- }
2493
- try {
2494
- let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {});
2495
- if (!res || !(res instanceof Response)) {
2496
- res = await fetch(manifestUrl, {});
2497
- }
2498
- manifestJson = (await res.json());
2499
- }
2500
- catch (err) {
2501
- manifestJson =
2502
- (await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
2503
- id: manifestUrl,
2504
- error: err,
2505
- from: 'runtime',
2506
- lifecycle: 'afterResolve',
2507
- origin: this.HostInstance,
2508
- }));
2509
- if (!manifestJson) {
2510
- delete this.manifestLoading[manifestUrl];
2511
- error(getShortErrorMsg(RUNTIME_003, runtimeDescMap, {
2512
- manifestUrl,
2513
- moduleName: moduleInfo.name,
2514
- hostName: this.HostInstance.options.name,
2515
- }, `${err}`));
2516
- }
2517
- }
2518
- assert(manifestJson.metaData && manifestJson.exposes && manifestJson.shared, `${manifestUrl} is not a federation manifest`);
2519
- this.manifestCache.set(manifestUrl, manifestJson);
2520
- return manifestJson;
2521
- };
2522
- const asyncLoadProcess = async () => {
2523
- const manifestJson = await getManifest();
2524
- const remoteSnapshot = generateSnapshotFromManifest(manifestJson, {
2525
- version: manifestUrl,
2526
- });
2527
- const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
2528
- options: this.HostInstance.options,
2529
- moduleInfo,
2530
- manifestJson,
2531
- remoteSnapshot,
2532
- manifestUrl,
2533
- from: 'manifest',
2534
- });
2535
- return remoteSnapshotRes;
2536
- };
2537
- if (!this.manifestLoading[manifestUrl]) {
2538
- this.manifestLoading[manifestUrl] = asyncLoadProcess().then((res) => res);
2539
- }
2540
- return this.manifestLoading[manifestUrl];
2541
- }
2542
- }
2543
-
2544
- class SharedHandler {
2545
- constructor(host) {
2546
- this.hooks = new PluginSystem({
2547
- beforeRegisterShare: new SyncWaterfallHook('beforeRegisterShare'),
2548
- afterResolve: new AsyncWaterfallHook('afterResolve'),
2549
- beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
2550
- // not used yet
2551
- loadShare: new AsyncHook(),
2552
- resolveShare: new SyncWaterfallHook('resolveShare'),
2553
- // maybe will change, temporarily for internal use only
2554
- initContainerShareScopeMap: new SyncWaterfallHook('initContainerShareScopeMap'),
2555
- });
2556
- this.host = host;
2557
- this.shareScopeMap = {};
2558
- this.initTokens = {};
2559
- this._setGlobalShareScopeMap(host.options);
2560
- }
2561
- // register shared in shareScopeMap
2562
- registerShared(globalOptions, userOptions) {
2563
- const { newShareInfos, allShareInfos } = formatShareConfigs(globalOptions, userOptions);
2564
- const sharedKeys = Object.keys(newShareInfos);
2565
- sharedKeys.forEach((sharedKey) => {
2566
- const sharedVals = newShareInfos[sharedKey];
2567
- sharedVals.forEach((sharedVal) => {
2568
- sharedVal.scope.forEach((sc) => {
2569
- this.hooks.lifecycle.beforeRegisterShare.emit({
2570
- origin: this.host,
2571
- pkgName: sharedKey,
2572
- shared: sharedVal,
2573
- });
2574
- const registeredShared = this.shareScopeMap[sc]?.[sharedKey];
2575
- if (!registeredShared) {
2576
- this.setShared({
2577
- pkgName: sharedKey,
2578
- lib: sharedVal.lib,
2579
- get: sharedVal.get,
2580
- loaded: sharedVal.loaded || Boolean(sharedVal.lib),
2581
- shared: sharedVal,
2582
- from: userOptions.name,
2583
- });
2584
- }
2585
- });
2586
- });
2587
- });
2588
- return {
2589
- newShareInfos,
2590
- allShareInfos,
2591
- };
2592
- }
2593
- async loadShare(pkgName, extraOptions) {
2594
- const { host } = this;
2595
- // This function performs the following steps:
2596
- // 1. Checks if the currently loaded share already exists, if not, it throws an error
2597
- // 2. Searches globally for a matching share, if found, it uses it directly
2598
- // 3. If not found, it retrieves it from the current share and stores the obtained share globally.
2599
- const shareOptions = getTargetSharedOptions({
2600
- pkgName,
2601
- extraOptions,
2602
- shareInfos: host.options.shared,
2603
- });
2604
- if (shareOptions?.scope) {
2605
- await Promise.all(shareOptions.scope.map(async (shareScope) => {
2606
- await Promise.all(this.initializeSharing(shareScope, {
2607
- strategy: shareOptions.strategy,
2608
- }));
2609
- return;
2610
- }));
2611
- }
2612
- const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
2613
- pkgName,
2614
- shareInfo: shareOptions,
2615
- shared: host.options.shared,
2616
- origin: host,
2617
- });
2618
- const { shareInfo: shareOptionsRes } = loadShareRes;
2619
- // Assert that shareInfoRes exists, if not, throw an error
2620
- assert(shareOptionsRes, `Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
2621
- const { shared: registeredShared, useTreesShaking } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
2622
- if (registeredShared) {
2623
- const targetShared = directShare(registeredShared, useTreesShaking);
2624
- if (targetShared.lib) {
2625
- addUseIn(targetShared, host.options.name);
2626
- return targetShared.lib;
2627
- }
2628
- else if (targetShared.loading && !targetShared.loaded) {
2629
- const factory = await targetShared.loading;
2630
- targetShared.loaded = true;
2631
- if (!targetShared.lib) {
2632
- targetShared.lib = factory;
2633
- }
2634
- addUseIn(targetShared, host.options.name);
2635
- return factory;
2636
- }
2637
- else {
2638
- const asyncLoadProcess = async () => {
2639
- const factory = await targetShared.get();
2640
- addUseIn(targetShared, host.options.name);
2641
- targetShared.loaded = true;
2642
- targetShared.lib = factory;
2643
- return factory;
2644
- };
2645
- const loading = asyncLoadProcess();
2646
- this.setShared({
2647
- pkgName,
2648
- loaded: false,
2649
- shared: registeredShared,
2650
- from: host.options.name,
2651
- lib: null,
2652
- loading,
2653
- treeShaking: useTreesShaking
2654
- ? targetShared
2655
- : undefined,
2656
- });
2657
- return loading;
2658
- }
2659
- }
2660
- else {
2661
- if (extraOptions?.customShareInfo) {
2662
- return false;
2663
- }
2664
- const _useTreeShaking = shouldUseTreeShaking(shareOptionsRes.treeShaking);
2665
- const targetShared = directShare(shareOptionsRes, _useTreeShaking);
2666
- const asyncLoadProcess = async () => {
2667
- const factory = await targetShared.get();
2668
- targetShared.lib = factory;
2669
- targetShared.loaded = true;
2670
- addUseIn(targetShared, host.options.name);
2671
- const { shared: gShared, useTreesShaking: gUseTreeShaking } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
2672
- if (gShared) {
2673
- const targetGShared = directShare(gShared, gUseTreeShaking);
2674
- targetGShared.lib = factory;
2675
- targetGShared.loaded = true;
2676
- gShared.from = shareOptionsRes.from;
2677
- }
2678
- return factory;
2679
- };
2680
- const loading = asyncLoadProcess();
2681
- this.setShared({
2682
- pkgName,
2683
- loaded: false,
2684
- shared: shareOptionsRes,
2685
- from: host.options.name,
2686
- lib: null,
2687
- loading,
2688
- treeShaking: _useTreeShaking
2689
- ? targetShared
2690
- : undefined,
2691
- });
2692
- return loading;
2693
- }
2694
- }
2695
- /**
2696
- * This function initializes the sharing sequence (executed only once per share scope).
2697
- * It accepts one argument, the name of the share scope.
2698
- * If the share scope does not exist, it creates one.
2699
- */
2700
- // eslint-disable-next-line @typescript-eslint/member-ordering
2701
- initializeSharing(shareScopeName = DEFAULT_SCOPE, extraOptions) {
2702
- const { host } = this;
2703
- const from = extraOptions?.from;
2704
- const strategy = extraOptions?.strategy;
2705
- let initScope = extraOptions?.initScope;
2706
- const promises = [];
2707
- if (from !== 'build') {
2708
- const { initTokens } = this;
2709
- if (!initScope)
2710
- initScope = [];
2711
- let initToken = initTokens[shareScopeName];
2712
- if (!initToken)
2713
- initToken = initTokens[shareScopeName] = { from: this.host.name };
2714
- if (initScope.indexOf(initToken) >= 0)
2715
- return promises;
2716
- initScope.push(initToken);
2717
- }
2718
- const shareScope = this.shareScopeMap;
2719
- const hostName = host.options.name;
2720
- // Creates a new share scope if necessary
2721
- if (!shareScope[shareScopeName]) {
2722
- shareScope[shareScopeName] = {};
2723
- }
2724
- // Executes all initialization snippets from all accessible modules
2725
- const scope = shareScope[shareScopeName];
2726
- const register = (name, shared) => {
2727
- const { version, eager } = shared;
2728
- scope[name] = scope[name] || {};
2729
- const versions = scope[name];
2730
- const activeVersion = versions[version] && directShare(versions[version]);
2731
- const activeVersionEager = Boolean(activeVersion &&
2732
- (('eager' in activeVersion && activeVersion.eager) ||
2733
- ('shareConfig' in activeVersion &&
2734
- activeVersion.shareConfig?.eager)));
2735
- if (!activeVersion ||
2736
- (activeVersion.strategy !== 'loaded-first' &&
2737
- !activeVersion.loaded &&
2738
- (Boolean(!eager) !== !activeVersionEager
2739
- ? eager
2740
- : hostName > versions[version].from))) {
2741
- versions[version] = shared;
2742
- }
2743
- };
2744
- const initRemoteModule = async (key) => {
2745
- const { module } = await host.remoteHandler.getRemoteModuleAndOptions({
2746
- id: key,
2747
- });
2748
- let remoteEntryExports = undefined;
2749
- try {
2750
- remoteEntryExports = await module.getEntry();
2751
- }
2752
- catch (error) {
2753
- remoteEntryExports =
2754
- (await host.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
2755
- id: key,
2756
- error,
2757
- from: 'runtime',
2758
- lifecycle: 'beforeLoadShare',
2759
- origin: host,
2760
- }));
2761
- }
2762
- finally {
2763
- if (remoteEntryExports?.init) {
2764
- module.remoteEntryExports = remoteEntryExports;
2765
- await module.init();
2766
- }
2767
- }
2768
- };
2769
- Object.keys(host.options.shared).forEach((shareName) => {
2770
- const sharedArr = host.options.shared[shareName];
2771
- sharedArr.forEach((shared) => {
2772
- if (shared.scope.includes(shareScopeName)) {
2773
- register(shareName, shared);
2774
- }
2775
- });
2776
- });
2777
- // TODO: strategy==='version-first' need to be removed in the future
2778
- if (host.options.shareStrategy === 'version-first' ||
2779
- strategy === 'version-first') {
2780
- host.options.remotes.forEach((remote) => {
2781
- if (remote.shareScope === shareScopeName) {
2782
- promises.push(initRemoteModule(remote.name));
2783
- }
2784
- });
2785
- }
2786
- return promises;
2787
- }
2788
- // The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.
2789
- // 1. If the loaded shared already exists globally, then it will be reused
2790
- // 2. If lib exists in local shared, it will be used directly
2791
- // 3. If the local get returns something other than Promise, then it will be used directly
2792
- loadShareSync(pkgName, extraOptions) {
2793
- const { host } = this;
2794
- const shareOptions = getTargetSharedOptions({
2795
- pkgName,
2796
- extraOptions,
2797
- shareInfos: host.options.shared,
2798
- });
2799
- if (shareOptions?.scope) {
2800
- shareOptions.scope.forEach((shareScope) => {
2801
- this.initializeSharing(shareScope, { strategy: shareOptions.strategy });
2802
- });
2803
- }
2804
- const { shared: registeredShared, useTreesShaking } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare) || {};
2805
- if (registeredShared) {
2806
- if (typeof registeredShared.lib === 'function') {
2807
- addUseIn(registeredShared, host.options.name);
2808
- if (!registeredShared.loaded) {
2809
- registeredShared.loaded = true;
2810
- if (registeredShared.from === host.options.name) {
2811
- shareOptions.loaded = true;
2812
- }
2813
- }
2814
- return registeredShared.lib;
2815
- }
2816
- if (typeof registeredShared.get === 'function') {
2817
- const module = registeredShared.get();
2818
- if (!(module instanceof Promise)) {
2819
- addUseIn(registeredShared, host.options.name);
2820
- this.setShared({
2821
- pkgName,
2822
- loaded: true,
2823
- from: host.options.name,
2824
- lib: module,
2825
- shared: registeredShared,
2826
- });
2827
- return module;
2828
- }
2829
- }
2830
- }
2831
- if (shareOptions.lib) {
2832
- if (!shareOptions.loaded) {
2833
- shareOptions.loaded = true;
2834
- }
2835
- return shareOptions.lib;
2836
- }
2837
- if (shareOptions.get) {
2838
- const module = shareOptions.get();
2839
- if (module instanceof Promise) {
2840
- const errorCode = extraOptions?.from === 'build' ? RUNTIME_005 : RUNTIME_006;
2841
- throw new Error(getShortErrorMsg(errorCode, runtimeDescMap, {
2842
- hostName: host.options.name,
2843
- sharedPkgName: pkgName,
2844
- }));
2845
- }
2846
- shareOptions.lib = module;
2847
- this.setShared({
2848
- pkgName,
2849
- loaded: true,
2850
- from: host.options.name,
2851
- lib: shareOptions.lib,
2852
- shared: shareOptions,
2853
- });
2854
- return shareOptions.lib;
2855
- }
2856
- throw new Error(getShortErrorMsg(RUNTIME_006, runtimeDescMap, {
2857
- hostName: host.options.name,
2858
- sharedPkgName: pkgName,
2859
- }));
2860
- }
2861
- initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
2862
- const { host } = this;
2863
- this.shareScopeMap[scopeName] = shareScope;
2864
- this.hooks.lifecycle.initContainerShareScopeMap.emit({
2865
- shareScope,
2866
- options: host.options,
2867
- origin: host,
2868
- scopeName,
2869
- hostShareScopeMap: extraOptions.hostShareScopeMap,
2870
- });
2871
- }
2872
- setShared({ pkgName, shared, from, lib, loading, loaded, get, treeShaking, }) {
2873
- const { version, scope = 'default', ...shareInfo } = shared;
2874
- const scopes = Array.isArray(scope) ? scope : [scope];
2875
- const mergeAttrs = (shared) => {
2876
- const merge = (s, key, val) => {
2877
- if (val && !s[key]) {
2878
- s[key] = val;
2879
- }
2880
- };
2881
- const targetShared = (treeShaking ? shared.treeShaking : shared);
2882
- merge(targetShared, 'loaded', loaded);
2883
- merge(targetShared, 'loading', loading);
2884
- merge(targetShared, 'get', get);
2885
- };
2886
- scopes.forEach((sc) => {
2887
- if (!this.shareScopeMap[sc]) {
2888
- this.shareScopeMap[sc] = {};
2889
- }
2890
- if (!this.shareScopeMap[sc][pkgName]) {
2891
- this.shareScopeMap[sc][pkgName] = {};
2892
- }
2893
- if (!this.shareScopeMap[sc][pkgName][version]) {
2894
- this.shareScopeMap[sc][pkgName][version] = {
2895
- version,
2896
- scope: [sc],
2897
- ...shareInfo,
2898
- lib,
2899
- };
2900
- }
2901
- const registeredShared = this.shareScopeMap[sc][pkgName][version];
2902
- mergeAttrs(registeredShared);
2903
- if (from && registeredShared.from !== from) {
2904
- registeredShared.from = from;
2905
- }
2906
- });
2907
- }
2908
- _setGlobalShareScopeMap(hostOptions) {
2909
- const globalShareScopeMap = getGlobalShareScope();
2910
- const identifier = hostOptions.id || hostOptions.name;
2911
- if (identifier && !globalShareScopeMap[identifier]) {
2912
- globalShareScopeMap[identifier] = this.shareScopeMap;
2913
- }
2914
- }
2915
- }
2916
-
2917
- class RemoteHandler {
2918
- constructor(host) {
2919
- this.hooks = new PluginSystem({
2920
- beforeRegisterRemote: new SyncWaterfallHook('beforeRegisterRemote'),
2921
- registerRemote: new SyncWaterfallHook('registerRemote'),
2922
- beforeRequest: new AsyncWaterfallHook('beforeRequest'),
2923
- onLoad: new AsyncHook('onLoad'),
2924
- handlePreloadModule: new SyncHook('handlePreloadModule'),
2925
- errorLoadRemote: new AsyncHook('errorLoadRemote'),
2926
- beforePreloadRemote: new AsyncHook('beforePreloadRemote'),
2927
- generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
2928
- // not used yet
2929
- afterPreloadRemote: new AsyncHook(),
2930
- // TODO: Move to loaderHook
2931
- loadEntry: new AsyncHook(),
2932
- });
2933
- this.host = host;
2934
- this.idToRemoteMap = {};
2935
- }
2936
- formatAndRegisterRemote(globalOptions, userOptions) {
2937
- const userRemotes = userOptions.remotes || [];
2938
- return userRemotes.reduce((res, remote) => {
2939
- this.registerRemote(remote, res, { force: false });
2940
- return res;
2941
- }, globalOptions.remotes);
2942
- }
2943
- setIdToRemoteMap(id, remoteMatchInfo) {
2944
- const { remote, expose } = remoteMatchInfo;
2945
- const { name, alias } = remote;
2946
- this.idToRemoteMap[id] = { name: remote.name, expose };
2947
- if (alias && id.startsWith(name)) {
2948
- const idWithAlias = id.replace(name, alias);
2949
- this.idToRemoteMap[idWithAlias] = { name: remote.name, expose };
2950
- return;
2951
- }
2952
- if (alias && id.startsWith(alias)) {
2953
- const idWithName = id.replace(alias, name);
2954
- this.idToRemoteMap[idWithName] = { name: remote.name, expose };
2955
- }
2956
- }
2957
- // eslint-disable-next-line max-lines-per-function
2958
- // eslint-disable-next-line @typescript-eslint/member-ordering
2959
- async loadRemote(id, options) {
2960
- const { host } = this;
2961
- try {
2962
- const { loadFactory = true } = options || {
2963
- loadFactory: true,
2964
- };
2965
- // 1. Validate the parameters of the retrieved module. There are two module request methods: pkgName + expose and alias + expose.
2966
- // 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.
2967
- // 3. Retrieve the detailed information of the current module from global (remoteEntry address, expose resource address)
2968
- // 4. After retrieving remoteEntry, call the init of the module, and then retrieve the exported content of the module through get
2969
- // id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
2970
- // id: alias(app1) + expose(button) = app1/button
2971
- // id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort
2972
- const { module, moduleOptions, remoteMatchInfo } = await this.getRemoteModuleAndOptions({
2973
- id,
2974
- });
2975
- const { pkgNameOrAlias, remote, expose, id: idRes, remoteSnapshot, } = remoteMatchInfo;
2976
- const moduleOrFactory = (await module.get(idRes, expose, options, remoteSnapshot));
2977
- const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
2978
- id: idRes,
2979
- pkgNameOrAlias,
2980
- expose,
2981
- exposeModule: loadFactory ? moduleOrFactory : undefined,
2982
- exposeModuleFactory: loadFactory ? undefined : moduleOrFactory,
2983
- remote,
2984
- options: moduleOptions,
2985
- moduleInstance: module,
2986
- origin: host,
2987
- });
2988
- this.setIdToRemoteMap(id, remoteMatchInfo);
2989
- if (typeof moduleWrapper === 'function') {
2990
- return moduleWrapper;
2991
- }
2992
- return moduleOrFactory;
2993
- }
2994
- catch (error) {
2995
- const { from = 'runtime' } = options || { from: 'runtime' };
2996
- const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({
2997
- id,
2998
- error,
2999
- from,
3000
- lifecycle: 'onLoad',
3001
- origin: host,
3002
- });
3003
- if (!failOver) {
3004
- throw error;
3005
- }
3006
- return failOver;
3007
- }
3008
- }
3009
- // eslint-disable-next-line @typescript-eslint/member-ordering
3010
- async preloadRemote(preloadOptions) {
3011
- const { host } = this;
3012
- await this.hooks.lifecycle.beforePreloadRemote.emit({
3013
- preloadOps: preloadOptions,
3014
- options: host.options,
3015
- origin: host,
3016
- });
3017
- const preloadOps = formatPreloadArgs(host.options.remotes, preloadOptions);
3018
- await Promise.all(preloadOps.map(async (ops) => {
3019
- const { remote } = ops;
3020
- const remoteInfo = getRemoteInfo(remote);
3021
- const { globalSnapshot, remoteSnapshot } = await host.snapshotHandler.loadRemoteSnapshotInfo({
3022
- moduleInfo: remote,
3023
- });
3024
- const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
3025
- origin: host,
3026
- preloadOptions: ops,
3027
- remote,
3028
- remoteInfo,
3029
- globalSnapshot,
3030
- remoteSnapshot,
3031
- });
3032
- if (!assets) {
3033
- return;
3034
- }
3035
- preloadAssets(remoteInfo, host, assets);
3036
- }));
3037
- }
3038
- registerRemotes(remotes, options) {
3039
- const { host } = this;
3040
- remotes.forEach((remote) => {
3041
- this.registerRemote(remote, host.options.remotes, {
3042
- force: options?.force,
3043
- });
3044
- });
3045
- }
3046
- async getRemoteModuleAndOptions(options) {
3047
- const { host } = this;
3048
- const { id } = options;
3049
- let loadRemoteArgs;
3050
- try {
3051
- loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({
3052
- id,
3053
- options: host.options,
3054
- origin: host,
3055
- });
3056
- }
3057
- catch (error) {
3058
- loadRemoteArgs = (await this.hooks.lifecycle.errorLoadRemote.emit({
3059
- id,
3060
- options: host.options,
3061
- origin: host,
3062
- from: 'runtime',
3063
- error,
3064
- lifecycle: 'beforeRequest',
3065
- }));
3066
- if (!loadRemoteArgs) {
3067
- throw error;
3068
- }
3069
- }
3070
- const { id: idRes } = loadRemoteArgs;
3071
- const remoteSplitInfo = matchRemoteWithNameAndExpose(host.options.remotes, idRes);
3072
- assert(remoteSplitInfo, getShortErrorMsg(RUNTIME_004, runtimeDescMap, {
3073
- hostName: host.options.name,
3074
- requestId: idRes,
3075
- }));
3076
- const { remote: rawRemote } = remoteSplitInfo;
3077
- const remoteInfo = getRemoteInfo(rawRemote);
3078
- const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit({
3079
- id: idRes,
3080
- ...remoteSplitInfo,
3081
- options: host.options,
3082
- origin: host,
3083
- remoteInfo,
3084
- });
3085
- const { remote, expose } = matchInfo;
3086
- assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
3087
- let module = host.moduleCache.get(remote.name);
3088
- const moduleOptions = {
3089
- host: host,
3090
- remoteInfo,
3091
- };
3092
- if (!module) {
3093
- module = new Module(moduleOptions);
3094
- host.moduleCache.set(remote.name, module);
3095
- }
3096
- return {
3097
- module,
3098
- moduleOptions,
3099
- remoteMatchInfo: matchInfo,
3100
- };
3101
- }
3102
- registerRemote(remote, targetRemotes, options) {
3103
- const { host } = this;
3104
- const normalizeRemote = () => {
3105
- if (remote.alias) {
3106
- // Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error
3107
- // As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported
3108
- const findEqual = targetRemotes.find((item) => remote.alias &&
3109
- (item.name.startsWith(remote.alias) ||
3110
- item.alias?.startsWith(remote.alias)));
3111
- assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
3112
- }
3113
- // Set the remote entry to a complete path
3114
- if ('entry' in remote) {
3115
- if (isBrowserEnv() && !remote.entry.startsWith('http')) {
3116
- remote.entry = new URL(remote.entry, window.location.origin).href;
3117
- }
3118
- }
3119
- if (!remote.shareScope) {
3120
- remote.shareScope = DEFAULT_SCOPE;
3121
- }
3122
- if (!remote.type) {
3123
- remote.type = DEFAULT_REMOTE_TYPE;
3124
- }
3125
- };
3126
- this.hooks.lifecycle.beforeRegisterRemote.emit({ remote, origin: host });
3127
- const registeredRemote = targetRemotes.find((item) => item.name === remote.name);
3128
- if (!registeredRemote) {
3129
- normalizeRemote();
3130
- targetRemotes.push(remote);
3131
- this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });
3132
- }
3133
- else {
3134
- const messages = [
3135
- `The remote "${remote.name}" is already registered.`,
3136
- 'Please note that overriding it may cause unexpected errors.',
3137
- ];
3138
- if (options?.force) {
3139
- // remove registered remote
3140
- this.removeRemote(registeredRemote);
3141
- normalizeRemote();
3142
- targetRemotes.push(remote);
3143
- this.hooks.lifecycle.registerRemote.emit({ remote, origin: host });
3144
- warn$1(messages.join(' '));
3145
- }
3146
- }
3147
- }
3148
- removeRemote(remote) {
3149
- try {
3150
- const { host } = this;
3151
- const { name } = remote;
3152
- const remoteIndex = host.options.remotes.findIndex((item) => item.name === name);
3153
- if (remoteIndex !== -1) {
3154
- host.options.remotes.splice(remoteIndex, 1);
3155
- }
3156
- const loadedModule = host.moduleCache.get(remote.name);
3157
- if (loadedModule) {
3158
- const remoteInfo = loadedModule.remoteInfo;
3159
- const key = remoteInfo.entryGlobalName;
3160
- if (CurrentGlobal[key]) {
3161
- if (Object.getOwnPropertyDescriptor(CurrentGlobal, key)?.configurable) {
3162
- delete CurrentGlobal[key];
3163
- }
3164
- else {
3165
- // @ts-ignore
3166
- CurrentGlobal[key] = undefined;
3167
- }
3168
- }
3169
- const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
3170
- if (globalLoading[remoteEntryUniqueKey]) {
3171
- delete globalLoading[remoteEntryUniqueKey];
3172
- }
3173
- host.snapshotHandler.manifestCache.delete(remoteInfo.entry);
3174
- // delete unloaded shared and instance
3175
- let remoteInsId = remoteInfo.buildVersion
3176
- ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion)
3177
- : remoteInfo.name;
3178
- const remoteInsIndex = CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {
3179
- if (remoteInfo.buildVersion) {
3180
- return ins.options.id === remoteInsId;
3181
- }
3182
- else {
3183
- return ins.name === remoteInsId;
3184
- }
3185
- });
3186
- if (remoteInsIndex !== -1) {
3187
- const remoteIns = CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];
3188
- remoteInsId = remoteIns.options.id || remoteInsId;
3189
- const globalShareScopeMap = getGlobalShareScope();
3190
- let isAllSharedNotUsed = true;
3191
- const needDeleteKeys = [];
3192
- Object.keys(globalShareScopeMap).forEach((instId) => {
3193
- const shareScopeMap = globalShareScopeMap[instId];
3194
- shareScopeMap &&
3195
- Object.keys(shareScopeMap).forEach((shareScope) => {
3196
- const shareScopeVal = shareScopeMap[shareScope];
3197
- shareScopeVal &&
3198
- Object.keys(shareScopeVal).forEach((shareName) => {
3199
- const sharedPkgs = shareScopeVal[shareName];
3200
- sharedPkgs &&
3201
- Object.keys(sharedPkgs).forEach((shareVersion) => {
3202
- const shared = sharedPkgs[shareVersion];
3203
- if (shared &&
3204
- typeof shared === 'object' &&
3205
- shared.from === remoteInfo.name) {
3206
- if (shared.loaded || shared.loading) {
3207
- shared.useIn = shared.useIn.filter((usedHostName) => usedHostName !== remoteInfo.name);
3208
- if (shared.useIn.length) {
3209
- isAllSharedNotUsed = false;
3210
- }
3211
- else {
3212
- needDeleteKeys.push([
3213
- instId,
3214
- shareScope,
3215
- shareName,
3216
- shareVersion,
3217
- ]);
3218
- }
3219
- }
3220
- else {
3221
- needDeleteKeys.push([
3222
- instId,
3223
- shareScope,
3224
- shareName,
3225
- shareVersion,
3226
- ]);
3227
- }
3228
- }
3229
- });
3230
- });
3231
- });
3232
- });
3233
- if (isAllSharedNotUsed) {
3234
- remoteIns.shareScopeMap = {};
3235
- delete globalShareScopeMap[remoteInsId];
3236
- }
3237
- needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion]) => {
3238
- delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[shareVersion];
3239
- });
3240
- CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
3241
- }
3242
- const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);
3243
- if (hostGlobalSnapshot) {
3244
- const remoteKey = hostGlobalSnapshot &&
3245
- 'remotesInfo' in hostGlobalSnapshot &&
3246
- hostGlobalSnapshot.remotesInfo &&
3247
- getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;
3248
- if (remoteKey) {
3249
- delete hostGlobalSnapshot.remotesInfo[remoteKey];
3250
- if (
3251
- //eslint-disable-next-line no-extra-boolean-cast
3252
- Boolean(Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])) {
3253
- delete Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];
3254
- }
3255
- }
3256
- }
3257
- host.moduleCache.delete(remote.name);
3258
- }
3259
- }
3260
- catch (err) {
3261
- logger.log('removeRemote fail: ', err);
3262
- }
3263
- }
3264
- }
3265
-
3266
- const USE_SNAPSHOT = typeof FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN === 'boolean'
3267
- ? !FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN
3268
- : true; // Default to true (use snapshot) when not explicitly defined
3269
- class ModuleFederation {
3270
- constructor(userOptions) {
3271
- this.hooks = new PluginSystem({
3272
- beforeInit: new SyncWaterfallHook('beforeInit'),
3273
- init: new SyncHook(),
3274
- // maybe will change, temporarily for internal use only
3275
- beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
3276
- // maybe will change, temporarily for internal use only
3277
- initContainer: new AsyncWaterfallHook('initContainer'),
3278
- });
3279
- this.version = "2.0.1";
3280
- this.moduleCache = new Map();
3281
- this.loaderHook = new PluginSystem({
3282
- // FIXME: may not be suitable , not open to the public yet
3283
- getModuleInfo: new SyncHook(),
3284
- createScript: new SyncHook(),
3285
- createLink: new SyncHook(),
3286
- fetch: new AsyncHook(),
3287
- loadEntryError: new AsyncHook(),
3288
- getModuleFactory: new AsyncHook(),
3289
- });
3290
- this.bridgeHook = new PluginSystem({
3291
- beforeBridgeRender: new SyncHook(),
3292
- afterBridgeRender: new SyncHook(),
3293
- beforeBridgeDestroy: new SyncHook(),
3294
- afterBridgeDestroy: new SyncHook(),
3295
- });
3296
- const plugins = USE_SNAPSHOT
3297
- ? [snapshotPlugin(), generatePreloadAssetsPlugin()]
3298
- : [];
3299
- // TODO: Validate the details of the options
3300
- // Initialize options with default values
3301
- const defaultOptions = {
3302
- id: getBuilderId(),
3303
- name: userOptions.name,
3304
- plugins,
3305
- remotes: [],
3306
- shared: {},
3307
- inBrowser: isBrowserEnv(),
3308
- };
3309
- this.name = userOptions.name;
3310
- this.options = defaultOptions;
3311
- this.snapshotHandler = new SnapshotHandler(this);
3312
- this.sharedHandler = new SharedHandler(this);
3313
- this.remoteHandler = new RemoteHandler(this);
3314
- this.shareScopeMap = this.sharedHandler.shareScopeMap;
3315
- this.registerPlugins([
3316
- ...defaultOptions.plugins,
3317
- ...(userOptions.plugins || []),
3318
- ]);
3319
- this.options = this.formatOptions(defaultOptions, userOptions);
3320
- }
3321
- initOptions(userOptions) {
3322
- this.registerPlugins(userOptions.plugins);
3323
- const options = this.formatOptions(this.options, userOptions);
3324
- this.options = options;
3325
- return options;
3326
- }
3327
- async loadShare(pkgName, extraOptions) {
3328
- return this.sharedHandler.loadShare(pkgName, extraOptions);
3329
- }
3330
- // The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.
3331
- // 1. If the loaded shared already exists globally, then it will be reused
3332
- // 2. If lib exists in local shared, it will be used directly
3333
- // 3. If the local get returns something other than Promise, then it will be used directly
3334
- loadShareSync(pkgName, extraOptions) {
3335
- return this.sharedHandler.loadShareSync(pkgName, extraOptions);
3336
- }
3337
- initializeSharing(shareScopeName = DEFAULT_SCOPE, extraOptions) {
3338
- return this.sharedHandler.initializeSharing(shareScopeName, extraOptions);
3339
- }
3340
- initRawContainer(name, url, container) {
3341
- const remoteInfo = getRemoteInfo({ name, entry: url });
3342
- const module = new Module({ host: this, remoteInfo });
3343
- module.remoteEntryExports = container;
3344
- this.moduleCache.set(name, module);
3345
- return module;
3346
- }
3347
- // eslint-disable-next-line max-lines-per-function
3348
- // eslint-disable-next-line @typescript-eslint/member-ordering
3349
- async loadRemote(id, options) {
3350
- return this.remoteHandler.loadRemote(id, options);
3351
- }
3352
- // eslint-disable-next-line @typescript-eslint/member-ordering
3353
- async preloadRemote(preloadOptions) {
3354
- return this.remoteHandler.preloadRemote(preloadOptions);
3355
- }
3356
- initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
3357
- this.sharedHandler.initShareScopeMap(scopeName, shareScope, extraOptions);
3358
- }
3359
- formatOptions(globalOptions, userOptions) {
3360
- const { allShareInfos: shared} = formatShareConfigs(globalOptions, userOptions);
3361
- const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
3362
- origin: this,
3363
- userOptions,
3364
- options: globalOptions,
3365
- shareInfo: shared,
3366
- });
3367
- const remotes = this.remoteHandler.formatAndRegisterRemote(globalOptionsRes, userOptionsRes);
3368
- const { allShareInfos } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
3369
- const plugins = [...globalOptionsRes.plugins];
3370
- if (userOptionsRes.plugins) {
3371
- userOptionsRes.plugins.forEach((plugin) => {
3372
- if (!plugins.includes(plugin)) {
3373
- plugins.push(plugin);
3374
- }
3375
- });
3376
- }
3377
- const optionsRes = {
3378
- ...globalOptions,
3379
- ...userOptions,
3380
- plugins,
3381
- remotes,
3382
- shared: allShareInfos,
3383
- };
3384
- this.hooks.lifecycle.init.emit({
3385
- origin: this,
3386
- options: optionsRes,
3387
- });
3388
- return optionsRes;
3389
- }
3390
- registerPlugins(plugins) {
3391
- const pluginRes = registerPlugins(plugins, this);
3392
- // Merge plugin
3393
- this.options.plugins = this.options.plugins.reduce((res, plugin) => {
3394
- if (!plugin)
3395
- return res;
3396
- if (res && !res.find((item) => item.name === plugin.name)) {
3397
- res.push(plugin);
3398
- }
3399
- return res;
3400
- }, pluginRes || []);
3401
- }
3402
- registerRemotes(remotes, options) {
3403
- return this.remoteHandler.registerRemotes(remotes, options);
3404
- }
3405
- registerShared(shared) {
3406
- this.sharedHandler.registerShared(this.options, {
3407
- ...this.options,
3408
- shared,
3409
- });
3410
- }
3411
- }
3412
-
3413
- var index = /*#__PURE__*/Object.freeze({
3414
- __proto__: null
3415
- });
3416
-
3417
- 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 };
3418
- //# sourceMappingURL=index.esm.js.map