@module-federation/webpack-bundler-runtime 2.0.1 → 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 (107) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  2. package/dist/attachShareScopeMap.cjs +11 -0
  3. package/dist/attachShareScopeMap.cjs.map +1 -0
  4. package/dist/attachShareScopeMap.d.ts +7 -0
  5. package/dist/attachShareScopeMap.js +10 -0
  6. package/dist/attachShareScopeMap.js.map +1 -0
  7. package/dist/constant.cjs +8 -0
  8. package/dist/constant.cjs.map +1 -0
  9. package/dist/constant.d.ts +5 -1
  10. package/dist/constant.js +6 -0
  11. package/dist/constant.js.map +1 -0
  12. package/dist/consumes.cjs +55 -0
  13. package/dist/consumes.cjs.map +1 -0
  14. package/dist/consumes.js +55 -0
  15. package/dist/consumes.js.map +1 -0
  16. package/dist/getSharedFallbackGetter.cjs +27 -0
  17. package/dist/getSharedFallbackGetter.cjs.map +1 -0
  18. package/dist/getSharedFallbackGetter.js +26 -0
  19. package/dist/getSharedFallbackGetter.js.map +1 -0
  20. package/dist/getUsedExports.cjs +11 -0
  21. package/dist/getUsedExports.cjs.map +1 -0
  22. package/dist/getUsedExports.js +10 -0
  23. package/dist/getUsedExports.js.map +1 -0
  24. package/dist/index.cjs +50 -0
  25. package/dist/index.cjs.map +1 -0
  26. package/dist/index.d.ts +48 -2
  27. package/dist/index.js +36 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/init.cjs +82 -0
  30. package/dist/init.cjs.map +1 -0
  31. package/dist/init.js +80 -0
  32. package/dist/init.js.map +1 -0
  33. package/dist/initContainerEntry.cjs +43 -0
  34. package/dist/initContainerEntry.cjs.map +1 -0
  35. package/dist/initContainerEntry.d.ts +7 -0
  36. package/dist/initContainerEntry.js +42 -0
  37. package/dist/initContainerEntry.js.map +1 -0
  38. package/dist/initializeSharing.cjs +61 -0
  39. package/dist/initializeSharing.cjs.map +1 -0
  40. package/dist/initializeSharing.d.ts +13 -0
  41. package/dist/initializeSharing.js +61 -0
  42. package/dist/initializeSharing.js.map +1 -0
  43. package/dist/installInitialConsumes.cjs +63 -0
  44. package/dist/installInitialConsumes.cjs.map +1 -0
  45. package/dist/installInitialConsumes.js +63 -0
  46. package/dist/installInitialConsumes.js.map +1 -0
  47. package/dist/remotes.cjs +74 -0
  48. package/dist/remotes.cjs.map +1 -0
  49. package/dist/remotes.js +73 -0
  50. package/dist/remotes.js.map +1 -0
  51. package/dist/runtime-core/dist/core.d.ts +132 -0
  52. package/dist/runtime-core/dist/global.d.ts +25 -0
  53. package/dist/runtime-core/dist/helpers.d.ts +4 -0
  54. package/dist/runtime-core/dist/index.d.ts +15 -0
  55. package/dist/runtime-core/dist/module/index.d.ts +33 -0
  56. package/dist/runtime-core/dist/plugins/snapshot/SnapshotHandler.d.ts +68 -0
  57. package/dist/runtime-core/dist/remote/index.d.ts +121 -0
  58. package/dist/runtime-core/dist/shared/index.d.ts +85 -0
  59. package/dist/runtime-core/dist/type/config.d.ts +117 -0
  60. package/dist/runtime-core/dist/type/index.d.ts +3 -0
  61. package/dist/runtime-core/dist/type/plugin.d.ts +28 -0
  62. package/dist/runtime-core/dist/type/preload.d.ts +32 -0
  63. package/dist/runtime-core/dist/types.d.ts +4 -0
  64. package/dist/runtime-core/dist/utils/env.d.ts +1 -0
  65. package/dist/runtime-core/dist/utils/hooks/asyncHook.d.ts +11 -0
  66. package/dist/runtime-core/dist/utils/hooks/asyncWaterfallHooks.d.ts +13 -0
  67. package/dist/runtime-core/dist/utils/hooks/pluginSystem.d.ts +20 -0
  68. package/dist/runtime-core/dist/utils/hooks/syncHook.d.ts +17 -0
  69. package/dist/runtime-core/dist/utils/hooks/syncWaterfallHook.d.ts +12 -0
  70. package/dist/runtime-core/dist/utils/index.d.ts +2 -0
  71. package/dist/runtime-core/dist/utils/load.d.ts +16 -0
  72. package/dist/runtime-core/dist/utils/logger.d.ts +1 -0
  73. package/dist/runtime-core/dist/utils/manifest.d.ts +2 -0
  74. package/dist/runtime-core/dist/utils/plugin.d.ts +2 -0
  75. package/dist/runtime-core/dist/utils/preload.d.ts +4 -0
  76. package/dist/runtime-core/dist/utils/share.d.ts +3 -0
  77. package/dist/runtime-core/dist/utils/tool.d.ts +1 -0
  78. package/dist/sdk/dist/types/plugins/ModuleFederationPlugin.d.ts +13 -0
  79. package/dist/types.d.ts +201 -0
  80. package/dist/updateOptions.cjs +112 -0
  81. package/dist/updateOptions.cjs.map +1 -0
  82. package/dist/updateOptions.js +110 -0
  83. package/dist/updateOptions.js.map +1 -0
  84. package/package.json +9 -9
  85. package/dist/constant.cjs.cjs +0 -6
  86. package/dist/constant.cjs.cjs.map +0 -1
  87. package/dist/constant.cjs.d.ts +0 -1
  88. package/dist/constant.esm.js +0 -4
  89. package/dist/constant.esm.js.map +0 -1
  90. package/dist/index.cjs.cjs +0 -793
  91. package/dist/index.cjs.cjs.map +0 -1
  92. package/dist/index.cjs.d.ts +0 -2
  93. package/dist/index.esm.js +0 -772
  94. package/dist/index.esm.js.map +0 -1
  95. package/dist/src/attachShareScopeMap.d.ts +0 -2
  96. package/dist/src/constant.d.ts +0 -1
  97. package/dist/src/consumes.d.ts +0 -2
  98. package/dist/src/getSharedFallbackGetter.d.ts +0 -2
  99. package/dist/src/getUsedExports.d.ts +0 -2
  100. package/dist/src/index.d.ts +0 -39
  101. package/dist/src/init.d.ts +0 -4
  102. package/dist/src/initContainerEntry.d.ts +0 -2
  103. package/dist/src/initializeSharing.d.ts +0 -2
  104. package/dist/src/installInitialConsumes.d.ts +0 -2
  105. package/dist/src/remotes.d.ts +0 -2
  106. package/dist/src/types.d.ts +0 -194
  107. package/dist/src/updateOptions.d.ts +0 -3
package/dist/index.esm.js DELETED
@@ -1,772 +0,0 @@
1
- import * as runtime from '@module-federation/runtime';
2
- import { getRemoteEntry } from '@module-federation/runtime';
3
- export { runtime };
4
- import { FEDERATION_SUPPORTED_TYPES } from './constant.esm.js';
5
- import { decodeName, ENCODE_NAME_PREFIX } from '@module-federation/sdk';
6
- import helpers from '@module-federation/runtime/helpers';
7
-
8
- function attachShareScopeMap(webpackRequire) {
9
- if (!webpackRequire.S ||
10
- webpackRequire.federation.hasAttachShareScopeMap ||
11
- !webpackRequire.federation.instance ||
12
- !webpackRequire.federation.instance.shareScopeMap) {
13
- return;
14
- }
15
- webpackRequire.S = webpackRequire.federation.instance.shareScopeMap;
16
- webpackRequire.federation.hasAttachShareScopeMap = true;
17
- }
18
-
19
- function updateConsumeOptions(options) {
20
- const { webpackRequire, moduleToHandlerMapping } = options;
21
- const { consumesLoadingData, initializeSharingData } = webpackRequire;
22
- const { sharedFallback, bundlerRuntime, libraryType } = webpackRequire.federation;
23
- if (consumesLoadingData && !consumesLoadingData._updated) {
24
- const { moduleIdToConsumeDataMapping: updatedModuleIdToConsumeDataMapping = {}, initialConsumes: updatedInitialConsumes = [], chunkMapping: updatedChunkMapping = {}, } = consumesLoadingData;
25
- Object.entries(updatedModuleIdToConsumeDataMapping).forEach(([id, data]) => {
26
- if (!moduleToHandlerMapping[id]) {
27
- moduleToHandlerMapping[id] = {
28
- // @ts-ignore
29
- getter: sharedFallback
30
- ? bundlerRuntime?.getSharedFallbackGetter({
31
- shareKey: data.shareKey,
32
- factory: data.fallback,
33
- webpackRequire,
34
- libraryType,
35
- })
36
- : data.fallback,
37
- treeShakingGetter: sharedFallback ? data.fallback : undefined,
38
- shareInfo: {
39
- shareConfig: {
40
- requiredVersion: data.requiredVersion,
41
- strictVersion: data.strictVersion,
42
- singleton: data.singleton,
43
- eager: data.eager,
44
- layer: data.layer,
45
- },
46
- scope: Array.isArray(data.shareScope)
47
- ? data.shareScope
48
- : [data.shareScope || 'default'],
49
- treeShaking: sharedFallback
50
- ? {
51
- get: data.fallback,
52
- mode: data.treeShakingMode,
53
- }
54
- : undefined,
55
- },
56
- shareKey: data.shareKey,
57
- };
58
- }
59
- });
60
- if ('initialConsumes' in options) {
61
- const { initialConsumes = [] } = options;
62
- updatedInitialConsumes.forEach((id) => {
63
- if (!initialConsumes.includes(id)) {
64
- initialConsumes.push(id);
65
- }
66
- });
67
- }
68
- if ('chunkMapping' in options) {
69
- const { chunkMapping = {} } = options;
70
- Object.entries(updatedChunkMapping).forEach(([id, chunkModules]) => {
71
- if (!chunkMapping[id]) {
72
- chunkMapping[id] = [];
73
- }
74
- chunkModules.forEach((moduleId) => {
75
- if (!chunkMapping[id].includes(moduleId)) {
76
- chunkMapping[id].push(moduleId);
77
- }
78
- });
79
- });
80
- }
81
- consumesLoadingData._updated = 1;
82
- }
83
- if (initializeSharingData && !initializeSharingData._updated) {
84
- const { federation } = webpackRequire;
85
- if (!federation.instance ||
86
- !initializeSharingData.scopeToSharingDataMapping) {
87
- return;
88
- }
89
- const shared = {};
90
- for (let [scope, stages] of Object.entries(initializeSharingData.scopeToSharingDataMapping)) {
91
- for (let stage of stages) {
92
- if (typeof stage === 'object' && stage !== null) {
93
- const { name, version, factory, eager, singleton, requiredVersion, strictVersion, } = stage;
94
- const shareConfig = {
95
- requiredVersion: `^${version}`,
96
- };
97
- const isValidValue = function (val) {
98
- return typeof val !== 'undefined';
99
- };
100
- if (isValidValue(singleton)) {
101
- shareConfig.singleton = singleton;
102
- }
103
- if (isValidValue(requiredVersion)) {
104
- shareConfig.requiredVersion = requiredVersion;
105
- }
106
- if (isValidValue(eager)) {
107
- shareConfig.eager = eager;
108
- }
109
- if (isValidValue(strictVersion)) {
110
- shareConfig.strictVersion = strictVersion;
111
- }
112
- const options = {
113
- version,
114
- scope: [scope],
115
- shareConfig,
116
- get: factory,
117
- };
118
- if (shared[name]) {
119
- shared[name].push(options);
120
- }
121
- else {
122
- shared[name] = [options];
123
- }
124
- }
125
- }
126
- }
127
- federation.instance.registerShared(shared);
128
- initializeSharingData._updated = 1;
129
- }
130
- }
131
- function updateRemoteOptions(options) {
132
- const { webpackRequire, idToExternalAndNameMapping = {}, idToRemoteMap = {}, chunkMapping = {}, } = options;
133
- const { remotesLoadingData } = webpackRequire;
134
- const remoteInfos = webpackRequire.federation?.bundlerRuntimeOptions?.remotes?.remoteInfos;
135
- if (!remotesLoadingData || remotesLoadingData._updated || !remoteInfos) {
136
- return;
137
- }
138
- const { chunkMapping: updatedChunkMapping, moduleIdToRemoteDataMapping } = remotesLoadingData;
139
- if (!updatedChunkMapping || !moduleIdToRemoteDataMapping) {
140
- return;
141
- }
142
- for (let [moduleId, data] of Object.entries(moduleIdToRemoteDataMapping)) {
143
- if (!idToExternalAndNameMapping[moduleId]) {
144
- idToExternalAndNameMapping[moduleId] = [
145
- data.shareScope,
146
- data.name,
147
- data.externalModuleId,
148
- ];
149
- }
150
- if (!idToRemoteMap[moduleId] && remoteInfos[data.remoteName]) {
151
- const items = remoteInfos[data.remoteName];
152
- idToRemoteMap[moduleId] ||= [];
153
- items.forEach((item) => {
154
- if (!idToRemoteMap[moduleId].includes(item)) {
155
- idToRemoteMap[moduleId].push(item);
156
- }
157
- });
158
- }
159
- }
160
- if (chunkMapping) {
161
- Object.entries(updatedChunkMapping).forEach(([id, chunkModules]) => {
162
- if (!chunkMapping[id]) {
163
- chunkMapping[id] = [];
164
- }
165
- chunkModules.forEach((moduleId) => {
166
- if (!chunkMapping[id].includes(moduleId)) {
167
- chunkMapping[id].push(moduleId);
168
- }
169
- });
170
- });
171
- }
172
- remotesLoadingData._updated = 1;
173
- }
174
-
175
- function remotes(options) {
176
- updateRemoteOptions(options);
177
- const { chunkId, promises, webpackRequire, chunkMapping, idToExternalAndNameMapping, idToRemoteMap, } = options;
178
- attachShareScopeMap(webpackRequire);
179
- if (webpackRequire.o(chunkMapping, chunkId)) {
180
- chunkMapping[chunkId].forEach((id) => {
181
- let getScope = webpackRequire.R;
182
- if (!getScope) {
183
- getScope = [];
184
- }
185
- const data = idToExternalAndNameMapping[id];
186
- const remoteInfos = idToRemoteMap[id] || [];
187
- // @ts-ignore seems not work
188
- if (getScope.indexOf(data) >= 0) {
189
- return;
190
- }
191
- // @ts-ignore seems not work
192
- getScope.push(data);
193
- if (data.p) {
194
- return promises.push(data.p);
195
- }
196
- const onError = (error) => {
197
- if (!error) {
198
- error = new Error('Container missing');
199
- }
200
- if (typeof error.message === 'string') {
201
- error.message += `\nwhile loading "${data[1]}" from ${data[2]}`;
202
- }
203
- webpackRequire.m[id] = () => {
204
- throw error;
205
- };
206
- data.p = 0;
207
- };
208
- const handleFunction = (fn, arg1, arg2, d, next, first) => {
209
- try {
210
- const promise = fn(arg1, arg2);
211
- if (promise && promise.then) {
212
- const p = promise.then((result) => next(result, d), onError);
213
- if (first) {
214
- promises.push((data.p = p));
215
- }
216
- else {
217
- return p;
218
- }
219
- }
220
- else {
221
- return next(promise, d, first);
222
- }
223
- }
224
- catch (error) {
225
- onError(error);
226
- }
227
- };
228
- const onExternal = (external, _, first) => external
229
- ? handleFunction(webpackRequire.I, data[0], 0, external, onInitialized, first)
230
- : onError();
231
- // eslint-disable-next-line no-var
232
- var onInitialized = (_, external, first) => handleFunction(external.get, data[1], getScope, 0, onFactory, first);
233
- // eslint-disable-next-line no-var
234
- var onFactory = (factory) => {
235
- data.p = 1;
236
- webpackRequire.m[id] = (module) => {
237
- module.exports = factory();
238
- };
239
- };
240
- const onRemoteLoaded = () => {
241
- try {
242
- const remoteName = decodeName(remoteInfos[0].name, ENCODE_NAME_PREFIX);
243
- const remoteModuleName = remoteName + data[1].slice(1);
244
- const instance = webpackRequire.federation.instance;
245
- const loadRemote = () => webpackRequire.federation.instance.loadRemote(remoteModuleName, {
246
- loadFactory: false,
247
- from: 'build',
248
- });
249
- if (instance.options.shareStrategy === 'version-first') {
250
- return Promise.all(instance.sharedHandler.initializeSharing(data[0])).then(() => {
251
- return loadRemote();
252
- });
253
- }
254
- return loadRemote();
255
- }
256
- catch (error) {
257
- onError(error);
258
- }
259
- };
260
- const useRuntimeLoad = remoteInfos.length === 1 &&
261
- FEDERATION_SUPPORTED_TYPES.includes(remoteInfos[0].externalType) &&
262
- remoteInfos[0].name;
263
- if (useRuntimeLoad) {
264
- handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
265
- }
266
- else {
267
- handleFunction(webpackRequire, data[2], 0, 0, onExternal, 1);
268
- }
269
- });
270
- }
271
- }
272
-
273
- function getUsedExports(webpackRequire, sharedName) {
274
- const usedExports = webpackRequire.federation.usedExports;
275
- if (!usedExports) {
276
- return;
277
- }
278
- return usedExports[sharedName];
279
- }
280
-
281
- function consumes(options) {
282
- updateConsumeOptions(options);
283
- const { chunkId, promises, installedModules, webpackRequire, chunkMapping, moduleToHandlerMapping, } = options;
284
- attachShareScopeMap(webpackRequire);
285
- if (webpackRequire.o(chunkMapping, chunkId)) {
286
- chunkMapping[chunkId].forEach((id) => {
287
- if (webpackRequire.o(installedModules, id)) {
288
- return promises.push(installedModules[id]);
289
- }
290
- const onFactory = (factory) => {
291
- installedModules[id] = 0;
292
- webpackRequire.m[id] = (module) => {
293
- delete webpackRequire.c[id];
294
- const result = factory();
295
- // Add layer property from shareConfig if available
296
- const { shareInfo } = moduleToHandlerMapping[id];
297
- if (shareInfo?.shareConfig?.layer &&
298
- result &&
299
- typeof result === 'object') {
300
- try {
301
- // Only set layer if it's not already defined or if it's undefined
302
- if (!result.hasOwnProperty('layer') ||
303
- result.layer === undefined) {
304
- result.layer = shareInfo.shareConfig.layer;
305
- }
306
- }
307
- catch (e) {
308
- // Ignore if layer property is read-only
309
- }
310
- }
311
- module.exports = result;
312
- };
313
- };
314
- const onError = (error) => {
315
- delete installedModules[id];
316
- webpackRequire.m[id] = (module) => {
317
- delete webpackRequire.c[id];
318
- throw error;
319
- };
320
- };
321
- try {
322
- const federationInstance = webpackRequire.federation.instance;
323
- if (!federationInstance) {
324
- throw new Error('Federation instance not found!');
325
- }
326
- const { shareKey, getter, shareInfo, treeShakingGetter } = moduleToHandlerMapping[id];
327
- const usedExports = getUsedExports(webpackRequire, shareKey);
328
- const customShareInfo = { ...shareInfo };
329
- if (usedExports) {
330
- customShareInfo.treeShaking = {
331
- usedExports,
332
- useIn: [federationInstance.options.name],
333
- };
334
- }
335
- const promise = federationInstance
336
- .loadShare(shareKey, {
337
- customShareInfo,
338
- })
339
- .then((factory) => {
340
- if (factory === false) {
341
- return treeShakingGetter?.() || getter();
342
- }
343
- return factory;
344
- });
345
- if (promise.then) {
346
- promises.push((installedModules[id] = promise.then(onFactory).catch(onError)));
347
- }
348
- else {
349
- // @ts-ignore maintain previous logic
350
- onFactory(promise);
351
- }
352
- }
353
- catch (e) {
354
- onError(e);
355
- }
356
- });
357
- }
358
- }
359
-
360
- function initializeSharing({ shareScopeName, webpackRequire, initPromises, initTokens, initScope, }) {
361
- const shareScopeKeys = Array.isArray(shareScopeName)
362
- ? shareScopeName
363
- : [shareScopeName];
364
- var initializeSharingPromises = [];
365
- var _initializeSharing = function (shareScopeKey) {
366
- if (!initScope)
367
- initScope = [];
368
- const mfInstance = webpackRequire.federation.instance;
369
- // handling circular init calls
370
- var initToken = initTokens[shareScopeKey];
371
- if (!initToken)
372
- initToken = initTokens[shareScopeKey] = { from: mfInstance.name };
373
- if (initScope.indexOf(initToken) >= 0)
374
- return;
375
- initScope.push(initToken);
376
- const promise = initPromises[shareScopeKey];
377
- if (promise)
378
- return promise;
379
- var warn = (msg) => typeof console !== 'undefined' && console.warn && console.warn(msg);
380
- var initExternal = (id) => {
381
- var handleError = (err) => warn('Initialization of sharing external failed: ' + err);
382
- try {
383
- var module = webpackRequire(id);
384
- if (!module)
385
- return;
386
- var initFn = (module) => module &&
387
- module.init &&
388
- // @ts-ignore compat legacy mf shared behavior
389
- module.init(webpackRequire.S[shareScopeKey], initScope, {
390
- shareScopeMap: webpackRequire.S || {},
391
- shareScopeKeys: shareScopeName,
392
- });
393
- if (module.then)
394
- return promises.push(module.then(initFn, handleError));
395
- var initResult = initFn(module);
396
- // @ts-ignore
397
- if (initResult && typeof initResult !== 'boolean' && initResult.then)
398
- // @ts-ignore
399
- return promises.push(initResult['catch'](handleError));
400
- }
401
- catch (err) {
402
- handleError(err);
403
- }
404
- };
405
- const promises = mfInstance.initializeSharing(shareScopeKey, {
406
- strategy: mfInstance.options.shareStrategy,
407
- initScope,
408
- from: 'build',
409
- });
410
- attachShareScopeMap(webpackRequire);
411
- const bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes;
412
- if (bundlerRuntimeRemotesOptions) {
413
- Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach((moduleId) => {
414
- const info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId];
415
- const externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2];
416
- if (info.length > 1) {
417
- initExternal(externalModuleId);
418
- }
419
- else if (info.length === 1) {
420
- const remoteInfo = info[0];
421
- if (!FEDERATION_SUPPORTED_TYPES.includes(remoteInfo.externalType)) {
422
- initExternal(externalModuleId);
423
- }
424
- }
425
- });
426
- }
427
- if (!promises.length) {
428
- return (initPromises[shareScopeKey] = true);
429
- }
430
- return (initPromises[shareScopeKey] = Promise.all(promises).then(() => (initPromises[shareScopeKey] = true)));
431
- };
432
- shareScopeKeys.forEach((key) => {
433
- initializeSharingPromises.push(_initializeSharing(key));
434
- });
435
- return Promise.all(initializeSharingPromises).then(() => true);
436
- }
437
-
438
- function handleInitialConsumes(options) {
439
- const { moduleId, moduleToHandlerMapping, webpackRequire, asyncLoad } = options;
440
- const federationInstance = webpackRequire.federation.instance;
441
- if (!federationInstance) {
442
- throw new Error('Federation instance not found!');
443
- }
444
- const { shareKey, shareInfo } = moduleToHandlerMapping[moduleId];
445
- try {
446
- const usedExports = getUsedExports(webpackRequire, shareKey);
447
- const customShareInfo = { ...shareInfo };
448
- if (usedExports) {
449
- customShareInfo.treeShaking = {
450
- usedExports,
451
- useIn: [federationInstance.options.name],
452
- };
453
- }
454
- if (asyncLoad) {
455
- return federationInstance.loadShare(shareKey, {
456
- customShareInfo,
457
- });
458
- }
459
- return federationInstance.loadShareSync(shareKey, {
460
- customShareInfo,
461
- });
462
- }
463
- catch (err) {
464
- console.error('loadShareSync failed! The function should not be called unless you set "eager:true". If you do not set it, and encounter this issue, you can check whether an async boundary is implemented.');
465
- console.error('The original error message is as follows: ');
466
- throw err;
467
- }
468
- }
469
- function installInitialConsumes(options) {
470
- updateConsumeOptions(options);
471
- const { moduleToHandlerMapping, webpackRequire, installedModules, initialConsumes, asyncLoad, } = options;
472
- const factoryIdSets = [];
473
- initialConsumes.forEach((id) => {
474
- const factoryGetter = () => handleInitialConsumes({
475
- moduleId: id,
476
- moduleToHandlerMapping,
477
- webpackRequire,
478
- asyncLoad,
479
- });
480
- factoryIdSets.push([id, factoryGetter]);
481
- });
482
- const setModule = (id, factoryGetter) => {
483
- webpackRequire.m[id] = (module) => {
484
- // Handle scenario when module is used synchronously
485
- installedModules[id] = 0;
486
- delete webpackRequire.c[id];
487
- const factory = factoryGetter();
488
- if (typeof factory !== 'function') {
489
- throw new Error(`Shared module is not available for eager consumption: ${id}`);
490
- }
491
- const result = factory();
492
- // Add layer property from shareConfig if available
493
- const { shareInfo } = moduleToHandlerMapping[id];
494
- if (shareInfo?.shareConfig?.layer &&
495
- result &&
496
- typeof result === 'object') {
497
- try {
498
- // Only set layer if it's not already defined or if it's undefined
499
- if (!result.hasOwnProperty('layer') ||
500
- result.layer === undefined) {
501
- result.layer = shareInfo.shareConfig.layer;
502
- }
503
- }
504
- catch (e) {
505
- // Ignore if layer property is read-only
506
- }
507
- }
508
- module.exports = result;
509
- };
510
- };
511
- if (asyncLoad) {
512
- return Promise.all(factoryIdSets.map(async ([id, factoryGetter]) => {
513
- const result = await factoryGetter();
514
- setModule(id, () => result);
515
- }));
516
- }
517
- factoryIdSets.forEach(([id, factoryGetter]) => {
518
- setModule(id, factoryGetter);
519
- });
520
- }
521
-
522
- function initContainerEntry(options) {
523
- const { webpackRequire, shareScope, initScope, shareScopeKey, remoteEntryInitOptions, } = options;
524
- if (!webpackRequire.S)
525
- return;
526
- if (!webpackRequire.federation ||
527
- !webpackRequire.federation.instance ||
528
- !webpackRequire.federation.initOptions)
529
- return;
530
- const federationInstance = webpackRequire.federation.instance;
531
- federationInstance.initOptions({
532
- name: webpackRequire.federation.initOptions.name,
533
- remotes: [],
534
- ...remoteEntryInitOptions,
535
- });
536
- const hostShareScopeKeys = remoteEntryInitOptions?.shareScopeKeys;
537
- const hostShareScopeMap = remoteEntryInitOptions?.shareScopeMap;
538
- // host: 'default' remote: 'default' remote['default'] = hostShareScopeMap['default']
539
- // host: ['default', 'scope1'] remote: 'default' remote['default'] = hostShareScopeMap['default']; remote['scope1'] = hostShareScopeMap['scop1']
540
- // host: 'default' remote: ['default','scope1'] remote['default'] = hostShareScopeMap['default']; remote['scope1'] = hostShareScopeMap['scope1'] = {}
541
- // host: ['scope1','default'] remote: ['scope1','scope2'] => remote['scope1'] = hostShareScopeMap['scope1']; remote['scope2'] = hostShareScopeMap['scope2'] = {};
542
- if (!shareScopeKey || typeof shareScopeKey === 'string') {
543
- const key = shareScopeKey || 'default';
544
- if (Array.isArray(hostShareScopeKeys)) {
545
- // const sc = hostShareScopeMap![key];
546
- // if (!sc) {
547
- // throw new Error('shareScopeKey is not exist in hostShareScopeMap');
548
- // }
549
- // federationInstance.initShareScopeMap(key, sc, {
550
- // hostShareScopeMap: remoteEntryInitOptions?.shareScopeMap || {},
551
- // });
552
- hostShareScopeKeys.forEach((hostKey) => {
553
- if (!hostShareScopeMap[hostKey]) {
554
- hostShareScopeMap[hostKey] = {};
555
- }
556
- const sc = hostShareScopeMap[hostKey];
557
- federationInstance.initShareScopeMap(hostKey, sc, {
558
- hostShareScopeMap: remoteEntryInitOptions?.shareScopeMap || {},
559
- });
560
- });
561
- }
562
- else {
563
- federationInstance.initShareScopeMap(key, shareScope, {
564
- hostShareScopeMap: remoteEntryInitOptions?.shareScopeMap || {},
565
- });
566
- }
567
- }
568
- else {
569
- shareScopeKey.forEach((key) => {
570
- if (!hostShareScopeKeys || !hostShareScopeMap) {
571
- federationInstance.initShareScopeMap(key, shareScope, {
572
- hostShareScopeMap: remoteEntryInitOptions?.shareScopeMap || {},
573
- });
574
- return;
575
- }
576
- if (!hostShareScopeMap[key]) {
577
- hostShareScopeMap[key] = {};
578
- }
579
- const sc = hostShareScopeMap[key];
580
- federationInstance.initShareScopeMap(key, sc, {
581
- hostShareScopeMap: remoteEntryInitOptions?.shareScopeMap || {},
582
- });
583
- });
584
- }
585
- if (webpackRequire.federation.attachShareScopeMap) {
586
- webpackRequire.federation.attachShareScopeMap(webpackRequire);
587
- }
588
- if (typeof webpackRequire.federation.prefetch === 'function') {
589
- webpackRequire.federation.prefetch();
590
- }
591
- if (!Array.isArray(shareScopeKey)) {
592
- // @ts-ignore
593
- return webpackRequire.I(shareScopeKey || 'default', initScope);
594
- }
595
- var proxyInitializeSharing = Boolean(webpackRequire.federation.initOptions.shared);
596
- if (proxyInitializeSharing) {
597
- // @ts-ignore
598
- return webpackRequire.I(shareScopeKey, initScope);
599
- }
600
- // @ts-ignore
601
- return Promise.all(shareScopeKey.map((key) => {
602
- // @ts-ignore
603
- return webpackRequire.I(key, initScope);
604
- })).then(() => true);
605
- }
606
-
607
- function init({ webpackRequire }) {
608
- const { initOptions, runtime, sharedFallback, bundlerRuntime, libraryType } = webpackRequire.federation;
609
- if (!initOptions) {
610
- throw new Error('initOptions is required!');
611
- }
612
- const treeShakingSharePlugin = function () {
613
- return {
614
- name: 'tree-shake-plugin',
615
- beforeInit(args) {
616
- const { userOptions, origin, options: registeredOptions } = args;
617
- const version = userOptions.version || registeredOptions.version;
618
- if (!sharedFallback) {
619
- return args;
620
- }
621
- const currentShared = userOptions.shared || {};
622
- const shared = [];
623
- Object.keys(currentShared).forEach((sharedName) => {
624
- const sharedArgs = Array.isArray(currentShared[sharedName])
625
- ? currentShared[sharedName]
626
- : [currentShared[sharedName]];
627
- sharedArgs.forEach((sharedArg) => {
628
- shared.push([sharedName, sharedArg]);
629
- if ('get' in sharedArg) {
630
- sharedArg.treeShaking ||= {};
631
- sharedArg.treeShaking.get = sharedArg.get;
632
- sharedArg.get = bundlerRuntime.getSharedFallbackGetter({
633
- shareKey: sharedName,
634
- factory: sharedArg.get,
635
- webpackRequire,
636
- libraryType,
637
- version: sharedArg.version,
638
- });
639
- }
640
- });
641
- });
642
- // read snapshot to override re-shake getter
643
- const hostGlobalSnapshot = helpers.global.getGlobalSnapshotInfoByModuleInfo({
644
- name: origin.name,
645
- version: version,
646
- });
647
- if (!hostGlobalSnapshot || !('shared' in hostGlobalSnapshot)) {
648
- return args;
649
- }
650
- Object.keys(registeredOptions.shared || {}).forEach((pkgName) => {
651
- const sharedInfo = registeredOptions.shared[pkgName];
652
- sharedInfo.forEach((sharedArg) => {
653
- shared.push([pkgName, sharedArg]);
654
- });
655
- });
656
- const patchShared = (pkgName, shared) => {
657
- const shareSnapshot = hostGlobalSnapshot.shared.find((item) => item.sharedName === pkgName);
658
- if (!shareSnapshot) {
659
- return;
660
- }
661
- const { treeShaking } = shared;
662
- if (!treeShaking) {
663
- return;
664
- }
665
- const { secondarySharedTreeShakingName, secondarySharedTreeShakingEntry, treeShakingStatus, } = shareSnapshot;
666
- if (treeShaking.status === treeShakingStatus) {
667
- return;
668
- }
669
- treeShaking.status = treeShakingStatus;
670
- if (secondarySharedTreeShakingEntry &&
671
- libraryType &&
672
- secondarySharedTreeShakingName) {
673
- treeShaking.get = async () => {
674
- const shareEntry = await getRemoteEntry({
675
- origin,
676
- remoteInfo: {
677
- name: secondarySharedTreeShakingName,
678
- entry: secondarySharedTreeShakingEntry,
679
- type: libraryType,
680
- entryGlobalName: secondarySharedTreeShakingName,
681
- // current not used
682
- shareScope: 'default',
683
- },
684
- });
685
- // TODO: add errorLoad hook ?
686
- // @ts-ignore
687
- await shareEntry.init(origin,
688
- // @ts-ignore
689
- __webpack_require__.federation.bundlerRuntime);
690
- // @ts-ignore
691
- const getter = shareEntry.get();
692
- return getter;
693
- };
694
- }
695
- };
696
- shared.forEach(([pkgName, sharedArg]) => {
697
- patchShared(pkgName, sharedArg);
698
- });
699
- return args;
700
- },
701
- };
702
- };
703
- initOptions.plugins ||= [];
704
- initOptions.plugins.push(treeShakingSharePlugin());
705
- return runtime.init(initOptions);
706
- }
707
-
708
- const getSharedFallbackGetter = ({ shareKey, factory, version, webpackRequire, libraryType = 'global', }) => {
709
- const { runtime, instance, bundlerRuntime, sharedFallback } = webpackRequire.federation;
710
- if (!sharedFallback) {
711
- return factory;
712
- }
713
- // { react: [ [ react/19.0.0/index.js , 19.0.0, react_global_name, var ] ] }
714
- const fallbackItems = sharedFallback[shareKey];
715
- if (!fallbackItems) {
716
- return factory;
717
- }
718
- const fallbackItem = version
719
- ? fallbackItems.find((item) => item[1] === version)
720
- : fallbackItems[0];
721
- if (!fallbackItem) {
722
- throw new Error(`No fallback item found for shareKey: ${shareKey} and version: ${version}`);
723
- }
724
- return () => runtime
725
- .getRemoteEntry({
726
- origin: webpackRequire.federation.instance,
727
- remoteInfo: {
728
- name: fallbackItem[2],
729
- entry: `${webpackRequire.p}${fallbackItem[0]}`,
730
- type: libraryType,
731
- entryGlobalName: fallbackItem[2],
732
- // current not used
733
- shareScope: 'default',
734
- },
735
- })
736
- // @ts-ignore
737
- .then((shareEntry) => {
738
- if (!shareEntry) {
739
- throw new Error(`Failed to load fallback entry for shareKey: ${shareKey} and version: ${version}`);
740
- }
741
- return (shareEntry
742
- // @ts-ignore
743
- .init(webpackRequire.federation.instance, bundlerRuntime)
744
- // @ts-ignore
745
- .then(() => shareEntry.get()));
746
- });
747
- };
748
-
749
- const federation = {
750
- runtime,
751
- instance: undefined,
752
- initOptions: undefined,
753
- bundlerRuntime: {
754
- remotes,
755
- consumes,
756
- I: initializeSharing,
757
- S: {},
758
- installInitialConsumes,
759
- initContainerEntry,
760
- init,
761
- getSharedFallbackGetter,
762
- },
763
- attachShareScopeMap,
764
- bundlerRuntimeOptions: {},
765
- };
766
- const instance = federation.instance;
767
- const initOptions = federation.initOptions;
768
- const bundlerRuntime = federation.bundlerRuntime;
769
- const bundlerRuntimeOptions = federation.bundlerRuntimeOptions;
770
-
771
- export { attachShareScopeMap, bundlerRuntime, bundlerRuntimeOptions, federation as default, initOptions, instance };
772
- //# sourceMappingURL=index.esm.js.map