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