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