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