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