@module-federation/webpack-bundler-runtime 0.1.5 → 0.1.7

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present zhanghang(2heal1)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ export * from "./src/constant";
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var ENCODE_NAME_PREFIX = "ENCODE_NAME_PREFIX";
6
+ var FEDERATION_SUPPORTED_TYPES = [
7
+ "script"
8
+ ];
9
+
10
+ exports.ENCODE_NAME_PREFIX = ENCODE_NAME_PREFIX;
11
+ exports.FEDERATION_SUPPORTED_TYPES = FEDERATION_SUPPORTED_TYPES;
@@ -0,0 +1,6 @@
1
+ var ENCODE_NAME_PREFIX = "ENCODE_NAME_PREFIX";
2
+ var FEDERATION_SUPPORTED_TYPES = [
3
+ "script"
4
+ ];
5
+
6
+ export { ENCODE_NAME_PREFIX, FEDERATION_SUPPORTED_TYPES };
@@ -0,0 +1,2 @@
1
+ export * from "./src/index";
2
+ export { default } from "./src/index";
@@ -0,0 +1,324 @@
1
+ 'use strict';
2
+
3
+ var runtime = require('@module-federation/runtime');
4
+ var sdk = require('@module-federation/sdk');
5
+ var constant = require('./constant.cjs.js');
6
+
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule) return e;
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n["default"] = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var runtime__namespace = /*#__PURE__*/_interopNamespace(runtime);
26
+
27
+ function attachShareScopeMap(webpackRequire) {
28
+ if (!webpackRequire.S || webpackRequire.federation.hasAttachShareScopeMap || !webpackRequire.federation.instance || !webpackRequire.federation.instance.shareScopeMap) {
29
+ return;
30
+ }
31
+ webpackRequire.S = webpackRequire.federation.instance.shareScopeMap;
32
+ webpackRequire.federation.hasAttachShareScopeMap = true;
33
+ }
34
+
35
+ function remotes(options) {
36
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire, idToRemoteMap = options.idToRemoteMap;
37
+ attachShareScopeMap(webpackRequire);
38
+ if (webpackRequire.o(chunkMapping, chunkId)) {
39
+ chunkMapping[chunkId].forEach(function(id) {
40
+ var getScope = webpackRequire.R;
41
+ if (!getScope) {
42
+ getScope = [];
43
+ }
44
+ var data = idToExternalAndNameMapping[id];
45
+ var remoteInfos = idToRemoteMap[id];
46
+ // @ts-ignore seems not work
47
+ if (getScope.indexOf(data) >= 0) {
48
+ return;
49
+ }
50
+ // @ts-ignore seems not work
51
+ getScope.push(data);
52
+ if (data.p) {
53
+ return promises.push(data.p);
54
+ }
55
+ var onError = function(error) {
56
+ if (!error) {
57
+ error = new Error("Container missing");
58
+ }
59
+ if (typeof error.message === "string") {
60
+ error.message += '\nwhile loading "'.concat(data[1], '" from ').concat(data[2]);
61
+ }
62
+ webpackRequire.m[id] = function() {
63
+ throw error;
64
+ };
65
+ data.p = 0;
66
+ };
67
+ var handleFunction = function(fn, arg1, arg2, d, next, first) {
68
+ try {
69
+ var promise = fn(arg1, arg2);
70
+ if (promise && promise.then) {
71
+ var p = promise.then(function(result) {
72
+ return next(result, d);
73
+ }, onError);
74
+ if (first) {
75
+ promises.push(data.p = p);
76
+ } else {
77
+ return p;
78
+ }
79
+ } else {
80
+ return next(promise, d, first);
81
+ }
82
+ } catch (error) {
83
+ onError(error);
84
+ }
85
+ };
86
+ var onExternal = function(external, _, first) {
87
+ return external ? handleFunction(webpackRequire.I, data[0], 0, external, onInitialized, first) : onError();
88
+ };
89
+ // eslint-disable-next-line no-var
90
+ var onInitialized = function(_, external, first) {
91
+ return handleFunction(external.get, data[1], getScope, 0, onFactory, first);
92
+ };
93
+ // eslint-disable-next-line no-var
94
+ var onFactory = function(factory) {
95
+ data.p = 1;
96
+ webpackRequire.m[id] = function(module) {
97
+ module.exports = factory();
98
+ };
99
+ };
100
+ var onRemoteLoaded = function() {
101
+ try {
102
+ var remoteName = sdk.decodeName(remoteInfos[0].name, constant.ENCODE_NAME_PREFIX);
103
+ var remoteModuleName = remoteName + data[1].slice(1);
104
+ return webpackRequire.federation.instance.loadRemote(remoteModuleName, {
105
+ loadFactory: false,
106
+ from: "build"
107
+ });
108
+ } catch (error) {
109
+ onError(error);
110
+ }
111
+ };
112
+ var useRuntimeLoad = remoteInfos.length === 1 && [
113
+ "script"
114
+ ].includes(remoteInfos[0].externalType) && remoteInfos[0].name;
115
+ if (useRuntimeLoad) {
116
+ handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
117
+ } else {
118
+ handleFunction(webpackRequire, data[2], 0, 0, onExternal, 1);
119
+ }
120
+ });
121
+ }
122
+ }
123
+
124
+ function consumes(options) {
125
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, installedModules = options.installedModules, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
126
+ attachShareScopeMap(webpackRequire);
127
+ if (webpackRequire.o(chunkMapping, chunkId)) {
128
+ chunkMapping[chunkId].forEach(function(id) {
129
+ if (webpackRequire.o(installedModules, id)) {
130
+ return promises.push(installedModules[id]);
131
+ }
132
+ var onFactory = function(factory) {
133
+ installedModules[id] = 0;
134
+ webpackRequire.m[id] = function(module) {
135
+ delete webpackRequire.c[id];
136
+ module.exports = factory();
137
+ };
138
+ };
139
+ var onError = function(error) {
140
+ delete installedModules[id];
141
+ webpackRequire.m[id] = function(module) {
142
+ delete webpackRequire.c[id];
143
+ throw error;
144
+ };
145
+ };
146
+ try {
147
+ var federationInstance = webpackRequire.federation.instance;
148
+ if (!federationInstance) {
149
+ throw new Error("Federation instance not found!");
150
+ }
151
+ var _moduleToHandlerMapping_id = moduleToHandlerMapping[id], shareKey = _moduleToHandlerMapping_id.shareKey, getter = _moduleToHandlerMapping_id.getter, shareInfo = _moduleToHandlerMapping_id.shareInfo;
152
+ var promise = federationInstance.loadShare(shareKey, {
153
+ customShareInfo: shareInfo
154
+ }).then(function(factory) {
155
+ if (factory === false) {
156
+ return getter();
157
+ }
158
+ return factory;
159
+ });
160
+ if (promise.then) {
161
+ promises.push(installedModules[id] = promise.then(onFactory).catch(onError));
162
+ } else {
163
+ // @ts-ignore maintain previous logic
164
+ onFactory(promise);
165
+ }
166
+ } catch (e) {
167
+ onError(e);
168
+ }
169
+ });
170
+ }
171
+ }
172
+
173
+ function initializeSharing(param) {
174
+ var shareScopeName = param.shareScopeName, webpackRequire = param.webpackRequire, initPromises = param.initPromises, initTokens = param.initTokens, initScope = param.initScope;
175
+ if (!initScope) initScope = [];
176
+ // handling circular init calls
177
+ var initToken = initTokens[shareScopeName];
178
+ if (!initToken) initToken = initTokens[shareScopeName] = {};
179
+ if (initScope.indexOf(initToken) >= 0) return;
180
+ initScope.push(initToken);
181
+ var promise = initPromises[shareScopeName];
182
+ if (promise) return promise;
183
+ var warn = function(msg) {
184
+ return typeof console !== "undefined" && console.warn && console.warn(msg);
185
+ };
186
+ var initExternal = function(id) {
187
+ var handleError = function(err) {
188
+ return warn("Initialization of sharing external failed: " + err);
189
+ };
190
+ try {
191
+ var module = webpackRequire(id);
192
+ if (!module) return;
193
+ var initFn = function(module) {
194
+ return module && module.init && // @ts-ignore compat legacy mf shared behavior
195
+ module.init(webpackRequire.S[shareScopeName], initScope);
196
+ };
197
+ if (module.then) return promises.push(module.then(initFn, handleError));
198
+ var initResult = initFn(module);
199
+ // @ts-ignore
200
+ if (initResult && typeof initResult !== "boolean" && initResult.then) // @ts-ignore
201
+ return promises.push(initResult["catch"](handleError));
202
+ } catch (err) {
203
+ handleError(err);
204
+ }
205
+ };
206
+ var promises = webpackRequire.federation.instance.initializeSharing(shareScopeName);
207
+ attachShareScopeMap(webpackRequire);
208
+ var bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes;
209
+ if (bundlerRuntimeRemotesOptions) {
210
+ Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach(function(moduleId) {
211
+ var info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId];
212
+ var externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2];
213
+ if (info.length > 1) {
214
+ initExternal(externalModuleId);
215
+ } else if (info.length === 1) {
216
+ var 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[shareScopeName] = true;
225
+ }
226
+ return initPromises[shareScopeName] = Promise.all(promises).then(function() {
227
+ return initPromises[shareScopeName] = true;
228
+ });
229
+ }
230
+
231
+ function handleInitialConsumes(options) {
232
+ var moduleId = options.moduleId, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
233
+ var federationInstance = webpackRequire.federation.instance;
234
+ if (!federationInstance) {
235
+ throw new Error("Federation instance not found!");
236
+ }
237
+ var _moduleToHandlerMapping_moduleId = moduleToHandlerMapping[moduleId], shareKey = _moduleToHandlerMapping_moduleId.shareKey, shareInfo = _moduleToHandlerMapping_moduleId.shareInfo;
238
+ return federationInstance.loadShareSync(shareKey, {
239
+ customShareInfo: shareInfo
240
+ });
241
+ }
242
+ function installInitialConsumes(options) {
243
+ var moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire, installedModules = options.installedModules, initialConsumes = options.initialConsumes;
244
+ initialConsumes.forEach(function(id) {
245
+ webpackRequire.m[id] = function(module) {
246
+ // Handle scenario when module is used synchronously
247
+ installedModules[id] = 0;
248
+ delete webpackRequire.c[id];
249
+ var factory = handleInitialConsumes({
250
+ moduleId: id,
251
+ moduleToHandlerMapping: moduleToHandlerMapping,
252
+ webpackRequire: webpackRequire
253
+ });
254
+ if (typeof factory !== "function") {
255
+ throw new Error("Shared module is not available for eager consumption: ".concat(id));
256
+ }
257
+ module.exports = factory();
258
+ };
259
+ });
260
+ }
261
+
262
+ function _define_property(obj, key, value) {
263
+ if (key in obj) {
264
+ Object.defineProperty(obj, key, {
265
+ value: value,
266
+ enumerable: true,
267
+ configurable: true,
268
+ writable: true
269
+ });
270
+ } else {
271
+ obj[key] = value;
272
+ }
273
+ return obj;
274
+ }
275
+ function _object_spread(target) {
276
+ for(var i = 1; i < arguments.length; i++){
277
+ var source = arguments[i] != null ? arguments[i] : {};
278
+ var ownKeys = Object.keys(source);
279
+ if (typeof Object.getOwnPropertySymbols === "function") {
280
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
281
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
282
+ }));
283
+ }
284
+ ownKeys.forEach(function(key) {
285
+ _define_property(target, key, source[key]);
286
+ });
287
+ }
288
+ return target;
289
+ }
290
+ function initContainerEntry(options) {
291
+ var webpackRequire = options.webpackRequire, shareScope = options.shareScope, initScope = options.initScope, shareScopeKey = options.shareScopeKey, remoteEntryInitOptions = options.remoteEntryInitOptions;
292
+ if (!webpackRequire.S) return;
293
+ if (!webpackRequire.federation || !webpackRequire.federation.instance || !webpackRequire.federation.initOptions) return;
294
+ var federationInstance = webpackRequire.federation.instance;
295
+ var name = shareScopeKey || "default";
296
+ federationInstance.initOptions(_object_spread({
297
+ name: webpackRequire.federation.initOptions.name,
298
+ remotes: []
299
+ }, remoteEntryInitOptions));
300
+ federationInstance.initShareScopeMap(name, shareScope);
301
+ if (webpackRequire.federation.attachShareScopeMap) {
302
+ webpackRequire.federation.attachShareScopeMap(webpackRequire);
303
+ }
304
+ // @ts-ignore
305
+ return webpackRequire.I(name, initScope);
306
+ }
307
+
308
+ var federation = {
309
+ runtime: runtime__namespace,
310
+ instance: undefined,
311
+ initOptions: undefined,
312
+ bundlerRuntime: {
313
+ remotes: remotes,
314
+ consumes: consumes,
315
+ I: initializeSharing,
316
+ S: {},
317
+ installInitialConsumes: installInitialConsumes,
318
+ initContainerEntry: initContainerEntry
319
+ },
320
+ attachShareScopeMap: attachShareScopeMap,
321
+ bundlerRuntimeOptions: {}
322
+ };
323
+
324
+ module.exports = federation;
@@ -0,0 +1,302 @@
1
+ import * as runtime from '@module-federation/runtime';
2
+ import { decodeName } from '@module-federation/sdk';
3
+ import { ENCODE_NAME_PREFIX, FEDERATION_SUPPORTED_TYPES } from './constant.esm.js';
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
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire, idToRemoteMap = options.idToRemoteMap;
15
+ attachShareScopeMap(webpackRequire);
16
+ if (webpackRequire.o(chunkMapping, chunkId)) {
17
+ chunkMapping[chunkId].forEach(function(id) {
18
+ var getScope = webpackRequire.R;
19
+ if (!getScope) {
20
+ getScope = [];
21
+ }
22
+ var data = idToExternalAndNameMapping[id];
23
+ var 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
+ var onError = function(error) {
34
+ if (!error) {
35
+ error = new Error("Container missing");
36
+ }
37
+ if (typeof error.message === "string") {
38
+ error.message += '\nwhile loading "'.concat(data[1], '" from ').concat(data[2]);
39
+ }
40
+ webpackRequire.m[id] = function() {
41
+ throw error;
42
+ };
43
+ data.p = 0;
44
+ };
45
+ var handleFunction = function(fn, arg1, arg2, d, next, first) {
46
+ try {
47
+ var promise = fn(arg1, arg2);
48
+ if (promise && promise.then) {
49
+ var p = promise.then(function(result) {
50
+ return next(result, d);
51
+ }, onError);
52
+ if (first) {
53
+ promises.push(data.p = p);
54
+ } else {
55
+ return p;
56
+ }
57
+ } else {
58
+ return next(promise, d, first);
59
+ }
60
+ } catch (error) {
61
+ onError(error);
62
+ }
63
+ };
64
+ var onExternal = function(external, _, first) {
65
+ return external ? handleFunction(webpackRequire.I, data[0], 0, external, onInitialized, first) : onError();
66
+ };
67
+ // eslint-disable-next-line no-var
68
+ var onInitialized = function(_, external, first) {
69
+ return handleFunction(external.get, data[1], getScope, 0, onFactory, first);
70
+ };
71
+ // eslint-disable-next-line no-var
72
+ var onFactory = function(factory) {
73
+ data.p = 1;
74
+ webpackRequire.m[id] = function(module) {
75
+ module.exports = factory();
76
+ };
77
+ };
78
+ var onRemoteLoaded = function() {
79
+ try {
80
+ var remoteName = decodeName(remoteInfos[0].name, ENCODE_NAME_PREFIX);
81
+ var remoteModuleName = remoteName + data[1].slice(1);
82
+ return webpackRequire.federation.instance.loadRemote(remoteModuleName, {
83
+ loadFactory: false,
84
+ from: "build"
85
+ });
86
+ } catch (error) {
87
+ onError(error);
88
+ }
89
+ };
90
+ var useRuntimeLoad = remoteInfos.length === 1 && [
91
+ "script"
92
+ ].includes(remoteInfos[0].externalType) && remoteInfos[0].name;
93
+ if (useRuntimeLoad) {
94
+ handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
95
+ } else {
96
+ handleFunction(webpackRequire, data[2], 0, 0, onExternal, 1);
97
+ }
98
+ });
99
+ }
100
+ }
101
+
102
+ function consumes(options) {
103
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, installedModules = options.installedModules, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
104
+ attachShareScopeMap(webpackRequire);
105
+ if (webpackRequire.o(chunkMapping, chunkId)) {
106
+ chunkMapping[chunkId].forEach(function(id) {
107
+ if (webpackRequire.o(installedModules, id)) {
108
+ return promises.push(installedModules[id]);
109
+ }
110
+ var onFactory = function(factory) {
111
+ installedModules[id] = 0;
112
+ webpackRequire.m[id] = function(module) {
113
+ delete webpackRequire.c[id];
114
+ module.exports = factory();
115
+ };
116
+ };
117
+ var onError = function(error) {
118
+ delete installedModules[id];
119
+ webpackRequire.m[id] = function(module) {
120
+ delete webpackRequire.c[id];
121
+ throw error;
122
+ };
123
+ };
124
+ try {
125
+ var federationInstance = webpackRequire.federation.instance;
126
+ if (!federationInstance) {
127
+ throw new Error("Federation instance not found!");
128
+ }
129
+ var _moduleToHandlerMapping_id = moduleToHandlerMapping[id], shareKey = _moduleToHandlerMapping_id.shareKey, getter = _moduleToHandlerMapping_id.getter, shareInfo = _moduleToHandlerMapping_id.shareInfo;
130
+ var promise = federationInstance.loadShare(shareKey, {
131
+ customShareInfo: shareInfo
132
+ }).then(function(factory) {
133
+ if (factory === false) {
134
+ return getter();
135
+ }
136
+ return factory;
137
+ });
138
+ if (promise.then) {
139
+ promises.push(installedModules[id] = promise.then(onFactory).catch(onError));
140
+ } else {
141
+ // @ts-ignore maintain previous logic
142
+ onFactory(promise);
143
+ }
144
+ } catch (e) {
145
+ onError(e);
146
+ }
147
+ });
148
+ }
149
+ }
150
+
151
+ function initializeSharing(param) {
152
+ var shareScopeName = param.shareScopeName, webpackRequire = param.webpackRequire, initPromises = param.initPromises, initTokens = param.initTokens, initScope = param.initScope;
153
+ if (!initScope) initScope = [];
154
+ // handling circular init calls
155
+ var initToken = initTokens[shareScopeName];
156
+ if (!initToken) initToken = initTokens[shareScopeName] = {};
157
+ if (initScope.indexOf(initToken) >= 0) return;
158
+ initScope.push(initToken);
159
+ var promise = initPromises[shareScopeName];
160
+ if (promise) return promise;
161
+ var warn = function(msg) {
162
+ return typeof console !== "undefined" && console.warn && console.warn(msg);
163
+ };
164
+ var initExternal = function(id) {
165
+ var handleError = function(err) {
166
+ return warn("Initialization of sharing external failed: " + err);
167
+ };
168
+ try {
169
+ var module = webpackRequire(id);
170
+ if (!module) return;
171
+ var initFn = function(module) {
172
+ return module && module.init && // @ts-ignore compat legacy mf shared behavior
173
+ module.init(webpackRequire.S[shareScopeName], initScope);
174
+ };
175
+ if (module.then) return promises.push(module.then(initFn, handleError));
176
+ var initResult = initFn(module);
177
+ // @ts-ignore
178
+ if (initResult && typeof initResult !== "boolean" && initResult.then) // @ts-ignore
179
+ return promises.push(initResult["catch"](handleError));
180
+ } catch (err) {
181
+ handleError(err);
182
+ }
183
+ };
184
+ var promises = webpackRequire.federation.instance.initializeSharing(shareScopeName);
185
+ attachShareScopeMap(webpackRequire);
186
+ var bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes;
187
+ if (bundlerRuntimeRemotesOptions) {
188
+ Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach(function(moduleId) {
189
+ var info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId];
190
+ var externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2];
191
+ if (info.length > 1) {
192
+ initExternal(externalModuleId);
193
+ } else if (info.length === 1) {
194
+ var remoteInfo = info[0];
195
+ if (!FEDERATION_SUPPORTED_TYPES.includes(remoteInfo.externalType)) {
196
+ initExternal(externalModuleId);
197
+ }
198
+ }
199
+ });
200
+ }
201
+ if (!promises.length) {
202
+ return initPromises[shareScopeName] = true;
203
+ }
204
+ return initPromises[shareScopeName] = Promise.all(promises).then(function() {
205
+ return initPromises[shareScopeName] = true;
206
+ });
207
+ }
208
+
209
+ function handleInitialConsumes(options) {
210
+ var moduleId = options.moduleId, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
211
+ var federationInstance = webpackRequire.federation.instance;
212
+ if (!federationInstance) {
213
+ throw new Error("Federation instance not found!");
214
+ }
215
+ var _moduleToHandlerMapping_moduleId = moduleToHandlerMapping[moduleId], shareKey = _moduleToHandlerMapping_moduleId.shareKey, shareInfo = _moduleToHandlerMapping_moduleId.shareInfo;
216
+ return federationInstance.loadShareSync(shareKey, {
217
+ customShareInfo: shareInfo
218
+ });
219
+ }
220
+ function installInitialConsumes(options) {
221
+ var moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire, installedModules = options.installedModules, initialConsumes = options.initialConsumes;
222
+ initialConsumes.forEach(function(id) {
223
+ webpackRequire.m[id] = function(module) {
224
+ // Handle scenario when module is used synchronously
225
+ installedModules[id] = 0;
226
+ delete webpackRequire.c[id];
227
+ var factory = handleInitialConsumes({
228
+ moduleId: id,
229
+ moduleToHandlerMapping: moduleToHandlerMapping,
230
+ webpackRequire: webpackRequire
231
+ });
232
+ if (typeof factory !== "function") {
233
+ throw new Error("Shared module is not available for eager consumption: ".concat(id));
234
+ }
235
+ module.exports = factory();
236
+ };
237
+ });
238
+ }
239
+
240
+ function _define_property(obj, key, value) {
241
+ if (key in obj) {
242
+ Object.defineProperty(obj, key, {
243
+ value: value,
244
+ enumerable: true,
245
+ configurable: true,
246
+ writable: true
247
+ });
248
+ } else {
249
+ obj[key] = value;
250
+ }
251
+ return obj;
252
+ }
253
+ function _object_spread(target) {
254
+ for(var i = 1; i < arguments.length; i++){
255
+ var source = arguments[i] != null ? arguments[i] : {};
256
+ var ownKeys = Object.keys(source);
257
+ if (typeof Object.getOwnPropertySymbols === "function") {
258
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
259
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
260
+ }));
261
+ }
262
+ ownKeys.forEach(function(key) {
263
+ _define_property(target, key, source[key]);
264
+ });
265
+ }
266
+ return target;
267
+ }
268
+ function initContainerEntry(options) {
269
+ var webpackRequire = options.webpackRequire, shareScope = options.shareScope, initScope = options.initScope, shareScopeKey = options.shareScopeKey, remoteEntryInitOptions = options.remoteEntryInitOptions;
270
+ if (!webpackRequire.S) return;
271
+ if (!webpackRequire.federation || !webpackRequire.federation.instance || !webpackRequire.federation.initOptions) return;
272
+ var federationInstance = webpackRequire.federation.instance;
273
+ var name = shareScopeKey || "default";
274
+ federationInstance.initOptions(_object_spread({
275
+ name: webpackRequire.federation.initOptions.name,
276
+ remotes: []
277
+ }, remoteEntryInitOptions));
278
+ federationInstance.initShareScopeMap(name, shareScope);
279
+ if (webpackRequire.federation.attachShareScopeMap) {
280
+ webpackRequire.federation.attachShareScopeMap(webpackRequire);
281
+ }
282
+ // @ts-ignore
283
+ return webpackRequire.I(name, initScope);
284
+ }
285
+
286
+ var federation = {
287
+ runtime: runtime,
288
+ instance: undefined,
289
+ initOptions: undefined,
290
+ bundlerRuntime: {
291
+ remotes: remotes,
292
+ consumes: consumes,
293
+ I: initializeSharing,
294
+ S: {},
295
+ installInitialConsumes: installInitialConsumes,
296
+ initContainerEntry: initContainerEntry
297
+ },
298
+ attachShareScopeMap: attachShareScopeMap,
299
+ bundlerRuntimeOptions: {}
300
+ };
301
+
302
+ export { federation as default };
@@ -0,0 +1,47 @@
1
+ {
2
+ "public": true,
3
+ "name": "@module-federation/webpack-bundler-runtime",
4
+ "version": "0.1.7",
5
+ "license": "MIT",
6
+ "description": "Module Federation Runtime for webpack",
7
+ "keywords": [
8
+ "Module Federation",
9
+ "bundler runtime"
10
+ ],
11
+ "files": [
12
+ "dist/",
13
+ "README.md"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "author": "zhanghang <hanric.zhang@gmail.com>",
19
+ "main": "./index.cjs.js",
20
+ "module": "./index.esm.js",
21
+ "types": "./dist/index.cjs.d.ts",
22
+ "dependencies": {
23
+ "@module-federation/runtime": "workspace:*",
24
+ "@module-federation/sdk": "workspace:*"
25
+ },
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/index.esm.js",
29
+ "require": "./dist/index.cjs.js"
30
+ },
31
+ "./constant": {
32
+ "import": "./dist/constant.esm.js",
33
+ "require": "./dist/constant.cjs.js"
34
+ },
35
+ "./*": "./*"
36
+ },
37
+ "typesVersions": {
38
+ "*": {
39
+ ".": [
40
+ "./dist/index.cjs.d.ts"
41
+ ],
42
+ "constant": [
43
+ "./dist/constant.cjs.d.ts"
44
+ ]
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,2 @@
1
+ import { WebpackRequire } from './types';
2
+ export declare function attachShareScopeMap(webpackRequire: WebpackRequire): void;
@@ -0,0 +1,2 @@
1
+ export declare const ENCODE_NAME_PREFIX = "ENCODE_NAME_PREFIX";
2
+ export declare const FEDERATION_SUPPORTED_TYPES: string[];
@@ -0,0 +1,2 @@
1
+ import { ConsumesOptions } from './types';
2
+ export declare function consumes(options: ConsumesOptions): void;
@@ -0,0 +1,4 @@
1
+ import { Federation } from './types';
2
+ export * from './types';
3
+ declare const federation: Federation;
4
+ export default federation;
@@ -0,0 +1,2 @@
1
+ import { InitContainerEntryOptions, WebpackRequire } from './types';
2
+ export declare function initContainerEntry(options: InitContainerEntryOptions): WebpackRequire['I'] | void;
@@ -0,0 +1,2 @@
1
+ import { InitializeSharingOptions } from './types';
2
+ export declare function initializeSharing({ shareScopeName, webpackRequire, initPromises, initTokens, initScope, }: InitializeSharingOptions): Promise<boolean> | boolean | void;
@@ -0,0 +1,2 @@
1
+ import { InstallInitialConsumesOptions } from './types';
2
+ export declare function installInitialConsumes(options: InstallInitialConsumesOptions): void;
@@ -0,0 +1,2 @@
1
+ import { RemotesOptions } from './types';
2
+ export declare function remotes(options: RemotesOptions): void;
@@ -0,0 +1,111 @@
1
+ import * as runtime from '@module-federation/runtime';
2
+ import type { RemoteEntryInitOptions, SharedConfig } from '@module-federation/runtime/types';
3
+ import { initializeSharing } from './initializeSharing';
4
+ import { attachShareScopeMap } from './attachShareScopeMap';
5
+ import { initContainerEntry } from './initContainerEntry';
6
+ type ExcludeUndefined<T> = T extends undefined ? never : T;
7
+ type Shared = InitOptions['shared'];
8
+ type NonUndefined<T = Shared> = ExcludeUndefined<T>;
9
+ type InitOptions = Parameters<typeof runtime.init>[0];
10
+ type ModuleCache = runtime.FederationHost['moduleCache'];
11
+ type InferModule<T> = T extends Map<string, infer U> ? U : never;
12
+ type InferredModule = InferModule<ModuleCache>;
13
+ export type ShareScopeMap = runtime.FederationHost['shareScopeMap'];
14
+ type InitToken = Record<string, Record<string, any>>;
15
+ export interface InitializeSharingOptions {
16
+ shareScopeName: string;
17
+ webpackRequire: WebpackRequire;
18
+ initPromises: Record<string, Promise<boolean> | boolean>;
19
+ initTokens: InitToken;
20
+ initScope: InitToken[];
21
+ }
22
+ export type RemoteEntryExports = NonUndefined<InferredModule['remoteEntryExports']>;
23
+ type ExtractInitParameters<T> = T extends {
24
+ init: (shareScope: infer U, ...args: any[]) => void;
25
+ } ? U : never;
26
+ type InferredShareScope = ExtractInitParameters<RemoteEntryExports>;
27
+ type InferredGlobalShareScope = {
28
+ [scope: string]: InferredShareScope;
29
+ };
30
+ type IdToExternalAndNameMappingItem = [string, string, string | number];
31
+ interface IdToExternalAndNameMappingItemWithPromise extends IdToExternalAndNameMappingItem {
32
+ p?: Promise<any> | number;
33
+ }
34
+ export interface WebpackRequire {
35
+ (moduleId: string | number): any;
36
+ o: (obj: Record<string, any>, key: string | number) => boolean;
37
+ R: Array<string | number>;
38
+ m: Record<string, (mod: any) => any>;
39
+ c: Record<string, any>;
40
+ I: (scopeName: string, initScope?: InitializeSharingOptions['initScope']) => ReturnType<typeof initializeSharing>;
41
+ S?: InferredGlobalShareScope;
42
+ federation: Federation;
43
+ }
44
+ interface ShareInfo {
45
+ shareConfig: SharedConfig;
46
+ scope: Array<string>;
47
+ }
48
+ interface ModuleToHandlerMappingItem {
49
+ getter: () => Promise<any>;
50
+ shareInfo: ShareInfo;
51
+ shareKey: string;
52
+ }
53
+ interface IdToRemoteMapItem {
54
+ externalType: string;
55
+ name: string;
56
+ externalModuleId?: string | number;
57
+ }
58
+ export interface RemotesOptions {
59
+ chunkId: string | number;
60
+ promises: Promise<any>[];
61
+ chunkMapping: Record<string, Array<string | number>>;
62
+ idToExternalAndNameMapping: Record<string, IdToExternalAndNameMappingItemWithPromise>;
63
+ idToRemoteMap: Record<string, IdToRemoteMapItem[]>;
64
+ webpackRequire: WebpackRequire;
65
+ }
66
+ export interface HandleInitialConsumesOptions {
67
+ moduleId: string | number;
68
+ moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
69
+ webpackRequire: WebpackRequire;
70
+ }
71
+ export interface InstallInitialConsumesOptions {
72
+ moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
73
+ webpackRequire: WebpackRequire;
74
+ installedModules: Record<string, Promise<any> | 0>;
75
+ initialConsumes: Array<string | number>;
76
+ }
77
+ export interface ConsumesOptions {
78
+ chunkId: string | number;
79
+ promises: Promise<any>[];
80
+ chunkMapping: Record<string, Array<string | number>>;
81
+ installedModules: Record<string, Promise<any> | 0>;
82
+ moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
83
+ webpackRequire: WebpackRequire;
84
+ }
85
+ export interface InitContainerEntryOptions {
86
+ shareScope: ShareScopeMap[string];
87
+ shareScopeKey: string;
88
+ webpackRequire: WebpackRequire;
89
+ remoteEntryInitOptions?: RemoteEntryInitOptions;
90
+ initScope?: InitializeSharingOptions['initScope'];
91
+ }
92
+ export interface Federation {
93
+ runtime?: typeof runtime;
94
+ instance?: runtime.FederationHost;
95
+ initOptions?: InitOptions;
96
+ installInitialConsumes?: (options: InstallInitialConsumesOptions) => any;
97
+ bundlerRuntime?: {
98
+ remotes: (options: RemotesOptions) => void;
99
+ consumes: (options: ConsumesOptions) => void;
100
+ I: typeof initializeSharing;
101
+ S: InferredGlobalShareScope;
102
+ installInitialConsumes: (options: InstallInitialConsumesOptions) => any;
103
+ initContainerEntry: typeof initContainerEntry;
104
+ };
105
+ bundlerRuntimeOptions: {
106
+ remotes?: Exclude<RemotesOptions, 'chunkId' | 'promises'>;
107
+ };
108
+ attachShareScopeMap?: typeof attachShareScopeMap;
109
+ hasAttachShareScopeMap?: boolean;
110
+ }
111
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "@module-federation/webpack-bundler-runtime",
4
- "version": "0.1.5",
4
+ "version": "0.1.7",
5
5
  "license": "MIT",
6
6
  "description": "Module Federation Runtime for webpack",
7
7
  "keywords": [
@@ -20,8 +20,8 @@
20
20
  "module": "./dist/index.esm.js",
21
21
  "types": "./dist/index.cjs.d.ts",
22
22
  "dependencies": {
23
- "@module-federation/runtime": "0.1.5",
24
- "@module-federation/sdk": "0.1.5"
23
+ "@module-federation/runtime": "0.1.7",
24
+ "@module-federation/sdk": "0.1.7"
25
25
  },
26
26
  "exports": {
27
27
  ".": {