@module-federation/webpack-bundler-runtime 0.11.4 → 0.13.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.
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const FEDERATION_SUPPORTED_TYPES = ['script'];
3
+ const FEDERATION_SUPPORTED_TYPES = [
4
+ 'script'
5
+ ];
4
6
 
5
7
  exports.FEDERATION_SUPPORTED_TYPES = FEDERATION_SUPPORTED_TYPES;
@@ -0,0 +1,5 @@
1
+ const FEDERATION_SUPPORTED_TYPES = [
2
+ 'script'
3
+ ];
4
+
5
+ export { FEDERATION_SUPPORTED_TYPES };
@@ -0,0 +1,376 @@
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
+
7
+ function _interopNamespaceDefault(e) {
8
+ var n = Object.create(null);
9
+ if (e) {
10
+ for (var k in e) {
11
+ n[k] = e[k];
12
+ }
13
+ }
14
+ n.default = e;
15
+ return Object.freeze(n);
16
+ }
17
+
18
+ var runtime__namespace = /*#__PURE__*/_interopNamespaceDefault(runtime);
19
+
20
+ function attachShareScopeMap(webpackRequire) {
21
+ if (!webpackRequire.S || webpackRequire.federation.hasAttachShareScopeMap || !webpackRequire.federation.instance || !webpackRequire.federation.instance.shareScopeMap) {
22
+ return;
23
+ }
24
+ webpackRequire.S = webpackRequire.federation.instance.shareScopeMap;
25
+ webpackRequire.federation.hasAttachShareScopeMap = true;
26
+ }
27
+
28
+ function remotes(options) {
29
+ const { chunkId, promises, chunkMapping, idToExternalAndNameMapping, webpackRequire, idToRemoteMap } = options;
30
+ attachShareScopeMap(webpackRequire);
31
+ if (webpackRequire.o(chunkMapping, chunkId)) {
32
+ chunkMapping[chunkId].forEach((id)=>{
33
+ let getScope = webpackRequire.R;
34
+ if (!getScope) {
35
+ getScope = [];
36
+ }
37
+ const data = idToExternalAndNameMapping[id];
38
+ const remoteInfos = idToRemoteMap[id];
39
+ // @ts-ignore seems not work
40
+ if (getScope.indexOf(data) >= 0) {
41
+ return;
42
+ }
43
+ // @ts-ignore seems not work
44
+ getScope.push(data);
45
+ if (data.p) {
46
+ return promises.push(data.p);
47
+ }
48
+ const onError = (error)=>{
49
+ if (!error) {
50
+ error = new Error('Container missing');
51
+ }
52
+ if (typeof error.message === 'string') {
53
+ error.message += `\nwhile loading "${data[1]}" from ${data[2]}`;
54
+ }
55
+ webpackRequire.m[id] = ()=>{
56
+ throw error;
57
+ };
58
+ data.p = 0;
59
+ };
60
+ const handleFunction = (fn, arg1, arg2, d, next, first)=>{
61
+ try {
62
+ const promise = fn(arg1, arg2);
63
+ if (promise && promise.then) {
64
+ const p = promise.then((result)=>next(result, d), onError);
65
+ if (first) {
66
+ promises.push(data.p = p);
67
+ } else {
68
+ return p;
69
+ }
70
+ } else {
71
+ return next(promise, d, first);
72
+ }
73
+ } catch (error) {
74
+ onError(error);
75
+ }
76
+ };
77
+ const onExternal = (external, _, first)=>external ? handleFunction(webpackRequire.I, data[0], 0, external, onInitialized, first) : onError();
78
+ // eslint-disable-next-line no-var
79
+ var onInitialized = (_, external, first)=>handleFunction(external.get, data[1], getScope, 0, onFactory, first);
80
+ // eslint-disable-next-line no-var
81
+ var onFactory = (factory)=>{
82
+ data.p = 1;
83
+ webpackRequire.m[id] = (module)=>{
84
+ module.exports = factory();
85
+ };
86
+ };
87
+ const onRemoteLoaded = ()=>{
88
+ try {
89
+ const remoteName = sdk.decodeName(remoteInfos[0].name, sdk.ENCODE_NAME_PREFIX);
90
+ const remoteModuleName = remoteName + data[1].slice(1);
91
+ const instance = webpackRequire.federation.instance;
92
+ const loadRemote = ()=>webpackRequire.federation.instance.loadRemote(remoteModuleName, {
93
+ loadFactory: false,
94
+ from: 'build'
95
+ });
96
+ if (instance.options.shareStrategy === 'version-first') {
97
+ return Promise.all(instance.sharedHandler.initializeSharing(data[0])).then(()=>{
98
+ return loadRemote();
99
+ });
100
+ }
101
+ return loadRemote();
102
+ } catch (error) {
103
+ onError(error);
104
+ }
105
+ };
106
+ const useRuntimeLoad = remoteInfos.length === 1 && constant.FEDERATION_SUPPORTED_TYPES.includes(remoteInfos[0].externalType) && remoteInfos[0].name;
107
+ if (useRuntimeLoad) {
108
+ handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
109
+ } else {
110
+ handleFunction(webpackRequire, data[2], 0, 0, onExternal, 1);
111
+ }
112
+ });
113
+ }
114
+ }
115
+
116
+ function consumes(options) {
117
+ const { chunkId, promises, chunkMapping, installedModules, moduleToHandlerMapping, webpackRequire } = options;
118
+ attachShareScopeMap(webpackRequire);
119
+ if (webpackRequire.o(chunkMapping, chunkId)) {
120
+ chunkMapping[chunkId].forEach((id)=>{
121
+ if (webpackRequire.o(installedModules, id)) {
122
+ return promises.push(installedModules[id]);
123
+ }
124
+ const onFactory = (factory)=>{
125
+ installedModules[id] = 0;
126
+ webpackRequire.m[id] = (module)=>{
127
+ delete webpackRequire.c[id];
128
+ module.exports = factory();
129
+ };
130
+ };
131
+ const onError = (error)=>{
132
+ delete installedModules[id];
133
+ webpackRequire.m[id] = (module)=>{
134
+ delete webpackRequire.c[id];
135
+ throw error;
136
+ };
137
+ };
138
+ try {
139
+ const federationInstance = webpackRequire.federation.instance;
140
+ if (!federationInstance) {
141
+ throw new Error('Federation instance not found!');
142
+ }
143
+ const { shareKey, getter, shareInfo } = moduleToHandlerMapping[id];
144
+ const promise = federationInstance.loadShare(shareKey, {
145
+ customShareInfo: shareInfo
146
+ }).then((factory)=>{
147
+ if (factory === false) {
148
+ return getter();
149
+ }
150
+ return factory;
151
+ });
152
+ if (promise.then) {
153
+ promises.push(installedModules[id] = promise.then(onFactory).catch(onError));
154
+ } else {
155
+ // @ts-ignore maintain previous logic
156
+ onFactory(promise);
157
+ }
158
+ } catch (e) {
159
+ onError(e);
160
+ }
161
+ });
162
+ }
163
+ }
164
+
165
+ function initializeSharing({ shareScopeName, webpackRequire, initPromises, initTokens, initScope }) {
166
+ const shareScopeKeys = Array.isArray(shareScopeName) ? shareScopeName : [
167
+ shareScopeName
168
+ ];
169
+ var initializeSharingPromises = [];
170
+ var _initializeSharing = function(shareScopeKey) {
171
+ if (!initScope) initScope = [];
172
+ const mfInstance = webpackRequire.federation.instance;
173
+ // handling circular init calls
174
+ var initToken = initTokens[shareScopeKey];
175
+ if (!initToken) initToken = initTokens[shareScopeKey] = {
176
+ from: mfInstance.name
177
+ };
178
+ if (initScope.indexOf(initToken) >= 0) return;
179
+ initScope.push(initToken);
180
+ const promise = initPromises[shareScopeKey];
181
+ if (promise) return promise;
182
+ var warn = (msg)=>typeof console !== 'undefined' && console.warn && console.warn(msg);
183
+ var initExternal = (id)=>{
184
+ var handleError = (err)=>warn('Initialization of sharing external failed: ' + err);
185
+ try {
186
+ var module = webpackRequire(id);
187
+ if (!module) return;
188
+ var initFn = (module)=>module && module.init && // @ts-ignore compat legacy mf shared behavior
189
+ module.init(webpackRequire.S[shareScopeKey], initScope, {
190
+ shareScopeMap: webpackRequire.S || {},
191
+ shareScopeKeys: shareScopeName
192
+ });
193
+ if (module.then) return promises.push(module.then(initFn, handleError));
194
+ var initResult = initFn(module);
195
+ // @ts-ignore
196
+ if (initResult && typeof initResult !== 'boolean' && initResult.then) // @ts-ignore
197
+ return promises.push(initResult['catch'](handleError));
198
+ } catch (err) {
199
+ handleError(err);
200
+ }
201
+ };
202
+ const promises = mfInstance.initializeSharing(shareScopeKey, {
203
+ strategy: mfInstance.options.shareStrategy,
204
+ initScope,
205
+ from: 'build'
206
+ });
207
+ attachShareScopeMap(webpackRequire);
208
+ const bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes;
209
+ if (bundlerRuntimeRemotesOptions) {
210
+ Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach((moduleId)=>{
211
+ const info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId];
212
+ const externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2];
213
+ if (info.length > 1) {
214
+ initExternal(externalModuleId);
215
+ } else if (info.length === 1) {
216
+ const remoteInfo = info[0];
217
+ if (!constant.FEDERATION_SUPPORTED_TYPES.includes(remoteInfo.externalType)) {
218
+ initExternal(externalModuleId);
219
+ }
220
+ }
221
+ });
222
+ }
223
+ if (!promises.length) {
224
+ return initPromises[shareScopeKey] = true;
225
+ }
226
+ return initPromises[shareScopeKey] = Promise.all(promises).then(()=>initPromises[shareScopeKey] = true);
227
+ };
228
+ shareScopeKeys.forEach((key)=>{
229
+ initializeSharingPromises.push(_initializeSharing(key));
230
+ });
231
+ return Promise.all(initializeSharingPromises).then(()=>true);
232
+ }
233
+
234
+ function handleInitialConsumes(options) {
235
+ const { moduleId, moduleToHandlerMapping, webpackRequire } = options;
236
+ const federationInstance = webpackRequire.federation.instance;
237
+ if (!federationInstance) {
238
+ throw new Error('Federation instance not found!');
239
+ }
240
+ const { shareKey, shareInfo } = moduleToHandlerMapping[moduleId];
241
+ try {
242
+ return federationInstance.loadShareSync(shareKey, {
243
+ customShareInfo: shareInfo
244
+ });
245
+ } catch (err) {
246
+ 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.');
247
+ console.error('The original error message is as follows: ');
248
+ throw err;
249
+ }
250
+ }
251
+ function installInitialConsumes(options) {
252
+ const { moduleToHandlerMapping, webpackRequire, installedModules, initialConsumes } = options;
253
+ initialConsumes.forEach((id)=>{
254
+ webpackRequire.m[id] = (module)=>{
255
+ // Handle scenario when module is used synchronously
256
+ installedModules[id] = 0;
257
+ delete webpackRequire.c[id];
258
+ const factory = handleInitialConsumes({
259
+ moduleId: id,
260
+ moduleToHandlerMapping,
261
+ webpackRequire
262
+ });
263
+ if (typeof factory !== 'function') {
264
+ throw new Error(`Shared module is not available for eager consumption: ${id}`);
265
+ }
266
+ module.exports = factory();
267
+ };
268
+ });
269
+ }
270
+
271
+ function _extends() {
272
+ _extends = Object.assign || function assign(target) {
273
+ for(var i = 1; i < arguments.length; i++){
274
+ var source = arguments[i];
275
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
276
+ }
277
+ return target;
278
+ };
279
+ return _extends.apply(this, arguments);
280
+ }
281
+
282
+ function initContainerEntry(options) {
283
+ const { webpackRequire, shareScope, initScope, shareScopeKey, remoteEntryInitOptions } = options;
284
+ if (!webpackRequire.S) return;
285
+ if (!webpackRequire.federation || !webpackRequire.federation.instance || !webpackRequire.federation.initOptions) return;
286
+ const federationInstance = webpackRequire.federation.instance;
287
+ federationInstance.initOptions(_extends({
288
+ name: webpackRequire.federation.initOptions.name,
289
+ remotes: []
290
+ }, remoteEntryInitOptions));
291
+ const hostShareScopeKeys = remoteEntryInitOptions == null ? void 0 : remoteEntryInitOptions.shareScopeKeys;
292
+ const hostShareScopeMap = remoteEntryInitOptions == null ? void 0 : remoteEntryInitOptions.shareScopeMap;
293
+ // host: 'default' remote: 'default' remote['default'] = hostShareScopeMap['default']
294
+ // host: ['default', 'scope1'] remote: 'default' remote['default'] = hostShareScopeMap['default']; remote['scope1'] = hostShareScopeMap['scop1']
295
+ // host: 'default' remote: ['default','scope1'] remote['default'] = hostShareScopeMap['default']; remote['scope1'] = hostShareScopeMap['scope1'] = {}
296
+ // host: ['scope1','default'] remote: ['scope1','scope2'] => remote['scope1'] = hostShareScopeMap['scope1']; remote['scope2'] = hostShareScopeMap['scope2'] = {};
297
+ if (!shareScopeKey || typeof shareScopeKey === 'string') {
298
+ const key = shareScopeKey || 'default';
299
+ if (Array.isArray(hostShareScopeKeys)) {
300
+ // const sc = hostShareScopeMap![key];
301
+ // if (!sc) {
302
+ // throw new Error('shareScopeKey is not exist in hostShareScopeMap');
303
+ // }
304
+ // federationInstance.initShareScopeMap(key, sc, {
305
+ // hostShareScopeMap: remoteEntryInitOptions?.shareScopeMap || {},
306
+ // });
307
+ hostShareScopeKeys.forEach((hostKey)=>{
308
+ if (!hostShareScopeMap[hostKey]) {
309
+ hostShareScopeMap[hostKey] = {};
310
+ }
311
+ const sc = hostShareScopeMap[hostKey];
312
+ federationInstance.initShareScopeMap(hostKey, sc, {
313
+ hostShareScopeMap: (remoteEntryInitOptions == null ? void 0 : remoteEntryInitOptions.shareScopeMap) || {}
314
+ });
315
+ });
316
+ } else {
317
+ federationInstance.initShareScopeMap(key, shareScope, {
318
+ hostShareScopeMap: (remoteEntryInitOptions == null ? void 0 : remoteEntryInitOptions.shareScopeMap) || {}
319
+ });
320
+ }
321
+ } else {
322
+ shareScopeKey.forEach((key)=>{
323
+ if (!hostShareScopeKeys || !hostShareScopeMap) {
324
+ federationInstance.initShareScopeMap(key, shareScope, {
325
+ hostShareScopeMap: (remoteEntryInitOptions == null ? void 0 : remoteEntryInitOptions.shareScopeMap) || {}
326
+ });
327
+ return;
328
+ }
329
+ if (!hostShareScopeMap[key]) {
330
+ hostShareScopeMap[key] = {};
331
+ }
332
+ const sc = hostShareScopeMap[key];
333
+ federationInstance.initShareScopeMap(key, sc, {
334
+ hostShareScopeMap: (remoteEntryInitOptions == null ? void 0 : remoteEntryInitOptions.shareScopeMap) || {}
335
+ });
336
+ });
337
+ }
338
+ if (webpackRequire.federation.attachShareScopeMap) {
339
+ webpackRequire.federation.attachShareScopeMap(webpackRequire);
340
+ }
341
+ if (typeof webpackRequire.federation.prefetch === 'function') {
342
+ webpackRequire.federation.prefetch();
343
+ }
344
+ if (!Array.isArray(shareScopeKey)) {
345
+ // @ts-ignore
346
+ return webpackRequire.I(shareScopeKey || 'default', initScope);
347
+ }
348
+ var proxyInitializeSharing = Boolean(webpackRequire.federation.initOptions.shared);
349
+ if (proxyInitializeSharing) {
350
+ // @ts-ignore
351
+ return webpackRequire.I(shareScopeKey, initScope);
352
+ }
353
+ // @ts-ignore
354
+ return Promise.all(shareScopeKey.map((key)=>{
355
+ // @ts-ignore
356
+ return webpackRequire.I(key, initScope);
357
+ })).then(()=>true);
358
+ }
359
+
360
+ const federation = {
361
+ runtime: runtime__namespace,
362
+ instance: undefined,
363
+ initOptions: undefined,
364
+ bundlerRuntime: {
365
+ remotes,
366
+ consumes,
367
+ I: initializeSharing,
368
+ S: {},
369
+ installInitialConsumes,
370
+ initContainerEntry
371
+ },
372
+ attachShareScopeMap,
373
+ bundlerRuntimeOptions: {}
374
+ };
375
+
376
+ module.exports = federation;