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