@module-federation/webpack-bundler-runtime 0.0.7 → 0.0.8

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/README.md CHANGED
@@ -1 +1,23 @@
1
1
  # `@module-federation/webpack-bundler-runtime`
2
+
3
+ - Extract the build runtime and combine it with the internal runtime
4
+ - Used with webpack/rspack
5
+
6
+ ## Usage
7
+
8
+ The package needs to be used with webpack/rspack bundler. It will export federation object which includes runtime, instance, bundlerRuntime, initOptions, attachShareScopeMap, bundlerRuntimeOptions.
9
+
10
+ After referencing, mount it to the corresponding bundler runtime, and then use the corresponding api/instance.
11
+
12
+ * example
13
+
14
+ ```ts
15
+ import federation from '@module-federation/webpack-bundler-runtime';
16
+
17
+ __webpack_require__.federation = federation;
18
+
19
+ __webpack_require__.f.remotes = __webpack_require__.federation.remotes(options)
20
+ __webpack_require__.f.consumes = __webpack_require__.federation.remotes(options)
21
+ ```
22
+
23
+
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 };
package/dist/index.cjs.js CHANGED
@@ -1,6 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var runtime = require('@module-federation/runtime');
4
+ var sdk = require('@module-federation/sdk');
5
+ var constant = require('./constant.cjs.js');
4
6
 
5
7
  function _interopNamespace(e) {
6
8
  if (e && e.__esModule) return e;
@@ -22,8 +24,17 @@ function _interopNamespace(e) {
22
24
 
23
25
  var runtime__namespace = /*#__PURE__*/_interopNamespace(runtime);
24
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
+
25
35
  function remotes(options) {
26
- var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire;
36
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire, idToRemoteMap = options.idToRemoteMap;
37
+ attachShareScopeMap(webpackRequire);
27
38
  if (webpackRequire.o(chunkMapping, chunkId)) {
28
39
  chunkMapping[chunkId].forEach(function(id) {
29
40
  var getScope = webpackRequire.R;
@@ -31,6 +42,7 @@ function remotes(options) {
31
42
  getScope = [];
32
43
  }
33
44
  var data = idToExternalAndNameMapping[id];
45
+ var remoteInfos = idToRemoteMap[id];
34
46
  // @ts-ignore seems not work
35
47
  if (getScope.indexOf(data) >= 0) {
36
48
  return;
@@ -78,9 +90,6 @@ function remotes(options) {
78
90
  var onInitialized = function(_, external, first) {
79
91
  return handleFunction(external.get, data[1], getScope, 0, onFactory, first);
80
92
  };
81
- var useRuntimeLoad = [
82
- "script"
83
- ].includes(data[3]) && data[4];
84
93
  // eslint-disable-next-line no-var
85
94
  var onFactory = function(factory) {
86
95
  data.p = 1;
@@ -90,14 +99,19 @@ function remotes(options) {
90
99
  };
91
100
  var onRemoteLoaded = function() {
92
101
  try {
93
- var remoteModuleName = data[4] + data[1].slice(1);
102
+ var remoteName = sdk.decodeName(remoteInfos[0].name, constant.ENCODE_NAME_PREFIX);
103
+ var remoteModuleName = remoteName + data[1].slice(1);
94
104
  return webpackRequire.federation.instance.loadRemote(remoteModuleName, {
95
- loadFactory: false
105
+ loadFactory: false,
106
+ from: "build"
96
107
  });
97
108
  } catch (error) {
98
109
  onError(error);
99
110
  }
100
111
  };
112
+ var useRuntimeLoad = remoteInfos.length === 1 && [
113
+ "script"
114
+ ].includes(remoteInfos[0].externalType) && remoteInfos[0].name;
101
115
  if (useRuntimeLoad) {
102
116
  handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
103
117
  } else {
@@ -107,25 +121,9 @@ function remotes(options) {
107
121
  }
108
122
  }
109
123
 
110
- function proxyShareScopeMap(__webpack_require__) {
111
- if (!__webpack_require__.S) {
112
- return;
113
- }
114
- // @ts-ignore FIXME: ideal situation is import type from @module-federation/runtime/type ,but the compile will throw error
115
- __webpack_require__.S = new Proxy(globalThis.__VMOK__.__SHARE__, {
116
- get: function get(target, prop, receiver) {
117
- return globalThis.__VMOK__.__SHARE__[prop];
118
- },
119
- set: function set(target, prop, value) {
120
- globalThis.__VMOK__.__SHARE__[prop] = value;
121
- return true;
122
- }
123
- });
124
- }
125
-
126
124
  function consumes(options) {
127
125
  var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, installedModules = options.installedModules, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
128
- proxyShareScopeMap(webpackRequire);
126
+ attachShareScopeMap(webpackRequire);
129
127
  if (webpackRequire.o(chunkMapping, chunkId)) {
130
128
  chunkMapping[chunkId].forEach(function(id) {
131
129
  if (webpackRequire.o(installedModules, id)) {
@@ -170,8 +168,62 @@ function consumes(options) {
170
168
  }
171
169
  }
172
170
 
173
- function initializeSharing(shareScopeName, webpackRequire) {
174
- return webpackRequire.federation.instance.initializeSharing(shareScopeName);
171
+ function initializeSharing(param) {
172
+ var shareScopeName = param.shareScopeName, webpackRequire = param.webpackRequire, initPromises = param.initPromises, initTokens = param.initTokens, initScope = param.initScope;
173
+ if (!initScope) initScope = [];
174
+ // handling circular init calls
175
+ var initToken = initTokens[shareScopeName];
176
+ if (!initToken) initToken = initTokens[shareScopeName] = {};
177
+ if (initScope.indexOf(initToken) >= 0) return;
178
+ initScope.push(initToken);
179
+ var promise = initPromises[shareScopeName];
180
+ if (promise) return promise;
181
+ var warn = function(msg) {
182
+ return typeof console !== "undefined" && console.warn && console.warn(msg);
183
+ };
184
+ var initExternal = function(id) {
185
+ var handleError = function(err) {
186
+ return warn("Initialization of sharing external failed: " + err);
187
+ };
188
+ try {
189
+ var module = webpackRequire(id);
190
+ if (!module) return;
191
+ var initFn = function(module) {
192
+ return module && module.init && // @ts-ignore compat legacy mf shared behavior
193
+ module.init(webpackRequire.S[shareScopeName], initScope);
194
+ };
195
+ if (module.then) return promises.push(module.then(initFn, handleError));
196
+ var initResult = initFn(module);
197
+ // @ts-ignore
198
+ if (initResult && typeof initResult !== "boolean" && initResult.then) // @ts-ignore
199
+ return promises.push(initResult["catch"](handleError));
200
+ } catch (err) {
201
+ handleError(err);
202
+ }
203
+ };
204
+ var promises = webpackRequire.federation.instance.initializeSharing(shareScopeName);
205
+ attachShareScopeMap(webpackRequire);
206
+ var bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes;
207
+ if (bundlerRuntimeRemotesOptions) {
208
+ Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach(function(moduleId) {
209
+ var info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId];
210
+ var externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2];
211
+ if (info.length > 1) {
212
+ initExternal(externalModuleId);
213
+ } else if (info.length === 1) {
214
+ var remoteInfo = info[0];
215
+ if (!constant.FEDERATION_SUPPORTED_TYPES.includes(remoteInfo.externalType)) {
216
+ initExternal(externalModuleId);
217
+ }
218
+ }
219
+ });
220
+ }
221
+ if (!promises.length) {
222
+ return initPromises[shareScopeName] = true;
223
+ }
224
+ return initPromises[shareScopeName] = Promise.all(promises).then(function() {
225
+ return initPromises[shareScopeName] = true;
226
+ });
175
227
  }
176
228
 
177
229
  function handleInitialConsumes(options) {
@@ -181,10 +233,7 @@ function handleInitialConsumes(options) {
181
233
  throw new Error("Federation instance not found!");
182
234
  }
183
235
  var _moduleToHandlerMapping_moduleId = moduleToHandlerMapping[moduleId], shareKey = _moduleToHandlerMapping_moduleId.shareKey, shareInfo = _moduleToHandlerMapping_moduleId.shareInfo;
184
- if (!shareInfo.shareConfig.eager) {
185
- throw new Error('Shared: "'.concat(shareKey, '" cannot be loaded synchronously unless "eager:true" is set or async entry is enabled.'));
186
- }
187
- return federationInstance.loadShareSync(shareKey);
236
+ return federationInstance.loadShareSync(shareKey, shareInfo);
188
237
  }
189
238
  function installInitialConsumes(options) {
190
239
  var moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire, installedModules = options.installedModules, initialConsumes = options.initialConsumes;
@@ -206,6 +255,53 @@ function installInitialConsumes(options) {
206
255
  });
207
256
  }
208
257
 
258
+ function _define_property(obj, key, value) {
259
+ if (key in obj) {
260
+ Object.defineProperty(obj, key, {
261
+ value: value,
262
+ enumerable: true,
263
+ configurable: true,
264
+ writable: true
265
+ });
266
+ } else {
267
+ obj[key] = value;
268
+ }
269
+ return obj;
270
+ }
271
+ function _object_spread(target) {
272
+ for(var i = 1; i < arguments.length; i++){
273
+ var source = arguments[i] != null ? arguments[i] : {};
274
+ var ownKeys = Object.keys(source);
275
+ if (typeof Object.getOwnPropertySymbols === "function") {
276
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
277
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
278
+ }));
279
+ }
280
+ ownKeys.forEach(function(key) {
281
+ _define_property(target, key, source[key]);
282
+ });
283
+ }
284
+ return target;
285
+ }
286
+ function initContainerEntry(options) {
287
+ var webpackRequire = options.webpackRequire, shareScope = options.shareScope, initScope = options.initScope, shareScopeKey = options.shareScopeKey, remoteEntryInitOptions = options.remoteEntryInitOptions;
288
+ if (!webpackRequire.S) return;
289
+ if (!webpackRequire.federation || !webpackRequire.federation.instance || !webpackRequire.federation.initOptions) return;
290
+ var federationInstance = webpackRequire.federation.instance;
291
+ var name = shareScopeKey || "default";
292
+ federationInstance.initOptions(_object_spread({
293
+ name: webpackRequire.federation.initOptions.name,
294
+ remotes: []
295
+ }, remoteEntryInitOptions));
296
+ federationInstance.initShareScopeMap(name, shareScope);
297
+ webpackRequire.S[name] = shareScope;
298
+ if (webpackRequire.federation.attachShareScopeMap) {
299
+ webpackRequire.federation.attachShareScopeMap(webpackRequire);
300
+ }
301
+ // @ts-ignore
302
+ return webpackRequire.I(name, initScope);
303
+ }
304
+
209
305
  var federation = {
210
306
  runtime: runtime__namespace,
211
307
  instance: undefined,
@@ -215,8 +311,11 @@ var federation = {
215
311
  consumes: consumes,
216
312
  I: initializeSharing,
217
313
  S: {},
218
- installInitialConsumes: installInitialConsumes
219
- }
314
+ installInitialConsumes: installInitialConsumes,
315
+ initContainerEntry: initContainerEntry
316
+ },
317
+ attachShareScopeMap: attachShareScopeMap,
318
+ bundlerRuntimeOptions: {}
220
319
  };
221
320
 
222
321
  module.exports = federation;
package/dist/index.esm.js CHANGED
@@ -1,7 +1,18 @@
1
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
+ }
2
12
 
3
13
  function remotes(options) {
4
- var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire;
14
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire, idToRemoteMap = options.idToRemoteMap;
15
+ attachShareScopeMap(webpackRequire);
5
16
  if (webpackRequire.o(chunkMapping, chunkId)) {
6
17
  chunkMapping[chunkId].forEach(function(id) {
7
18
  var getScope = webpackRequire.R;
@@ -9,6 +20,7 @@ function remotes(options) {
9
20
  getScope = [];
10
21
  }
11
22
  var data = idToExternalAndNameMapping[id];
23
+ var remoteInfos = idToRemoteMap[id];
12
24
  // @ts-ignore seems not work
13
25
  if (getScope.indexOf(data) >= 0) {
14
26
  return;
@@ -56,9 +68,6 @@ function remotes(options) {
56
68
  var onInitialized = function(_, external, first) {
57
69
  return handleFunction(external.get, data[1], getScope, 0, onFactory, first);
58
70
  };
59
- var useRuntimeLoad = [
60
- "script"
61
- ].includes(data[3]) && data[4];
62
71
  // eslint-disable-next-line no-var
63
72
  var onFactory = function(factory) {
64
73
  data.p = 1;
@@ -68,14 +77,19 @@ function remotes(options) {
68
77
  };
69
78
  var onRemoteLoaded = function() {
70
79
  try {
71
- var remoteModuleName = data[4] + data[1].slice(1);
80
+ var remoteName = decodeName(remoteInfos[0].name, ENCODE_NAME_PREFIX);
81
+ var remoteModuleName = remoteName + data[1].slice(1);
72
82
  return webpackRequire.federation.instance.loadRemote(remoteModuleName, {
73
- loadFactory: false
83
+ loadFactory: false,
84
+ from: "build"
74
85
  });
75
86
  } catch (error) {
76
87
  onError(error);
77
88
  }
78
89
  };
90
+ var useRuntimeLoad = remoteInfos.length === 1 && [
91
+ "script"
92
+ ].includes(remoteInfos[0].externalType) && remoteInfos[0].name;
79
93
  if (useRuntimeLoad) {
80
94
  handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
81
95
  } else {
@@ -85,25 +99,9 @@ function remotes(options) {
85
99
  }
86
100
  }
87
101
 
88
- function proxyShareScopeMap(__webpack_require__) {
89
- if (!__webpack_require__.S) {
90
- return;
91
- }
92
- // @ts-ignore FIXME: ideal situation is import type from @module-federation/runtime/type ,but the compile will throw error
93
- __webpack_require__.S = new Proxy(globalThis.__VMOK__.__SHARE__, {
94
- get: function get(target, prop, receiver) {
95
- return globalThis.__VMOK__.__SHARE__[prop];
96
- },
97
- set: function set(target, prop, value) {
98
- globalThis.__VMOK__.__SHARE__[prop] = value;
99
- return true;
100
- }
101
- });
102
- }
103
-
104
102
  function consumes(options) {
105
103
  var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, installedModules = options.installedModules, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
106
- proxyShareScopeMap(webpackRequire);
104
+ attachShareScopeMap(webpackRequire);
107
105
  if (webpackRequire.o(chunkMapping, chunkId)) {
108
106
  chunkMapping[chunkId].forEach(function(id) {
109
107
  if (webpackRequire.o(installedModules, id)) {
@@ -148,8 +146,62 @@ function consumes(options) {
148
146
  }
149
147
  }
150
148
 
151
- function initializeSharing(shareScopeName, webpackRequire) {
152
- return webpackRequire.federation.instance.initializeSharing(shareScopeName);
149
+ function initializeSharing(param) {
150
+ var shareScopeName = param.shareScopeName, webpackRequire = param.webpackRequire, initPromises = param.initPromises, initTokens = param.initTokens, initScope = param.initScope;
151
+ if (!initScope) initScope = [];
152
+ // handling circular init calls
153
+ var initToken = initTokens[shareScopeName];
154
+ if (!initToken) initToken = initTokens[shareScopeName] = {};
155
+ if (initScope.indexOf(initToken) >= 0) return;
156
+ initScope.push(initToken);
157
+ var promise = initPromises[shareScopeName];
158
+ if (promise) return promise;
159
+ var warn = function(msg) {
160
+ return typeof console !== "undefined" && console.warn && console.warn(msg);
161
+ };
162
+ var initExternal = function(id) {
163
+ var handleError = function(err) {
164
+ return warn("Initialization of sharing external failed: " + err);
165
+ };
166
+ try {
167
+ var module = webpackRequire(id);
168
+ if (!module) return;
169
+ var initFn = function(module) {
170
+ return module && module.init && // @ts-ignore compat legacy mf shared behavior
171
+ module.init(webpackRequire.S[shareScopeName], initScope);
172
+ };
173
+ if (module.then) return promises.push(module.then(initFn, handleError));
174
+ var initResult = initFn(module);
175
+ // @ts-ignore
176
+ if (initResult && typeof initResult !== "boolean" && initResult.then) // @ts-ignore
177
+ return promises.push(initResult["catch"](handleError));
178
+ } catch (err) {
179
+ handleError(err);
180
+ }
181
+ };
182
+ var promises = webpackRequire.federation.instance.initializeSharing(shareScopeName);
183
+ attachShareScopeMap(webpackRequire);
184
+ var bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes;
185
+ if (bundlerRuntimeRemotesOptions) {
186
+ Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach(function(moduleId) {
187
+ var info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId];
188
+ var externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2];
189
+ if (info.length > 1) {
190
+ initExternal(externalModuleId);
191
+ } else if (info.length === 1) {
192
+ var remoteInfo = info[0];
193
+ if (!FEDERATION_SUPPORTED_TYPES.includes(remoteInfo.externalType)) {
194
+ initExternal(externalModuleId);
195
+ }
196
+ }
197
+ });
198
+ }
199
+ if (!promises.length) {
200
+ return initPromises[shareScopeName] = true;
201
+ }
202
+ return initPromises[shareScopeName] = Promise.all(promises).then(function() {
203
+ return initPromises[shareScopeName] = true;
204
+ });
153
205
  }
154
206
 
155
207
  function handleInitialConsumes(options) {
@@ -159,10 +211,7 @@ function handleInitialConsumes(options) {
159
211
  throw new Error("Federation instance not found!");
160
212
  }
161
213
  var _moduleToHandlerMapping_moduleId = moduleToHandlerMapping[moduleId], shareKey = _moduleToHandlerMapping_moduleId.shareKey, shareInfo = _moduleToHandlerMapping_moduleId.shareInfo;
162
- if (!shareInfo.shareConfig.eager) {
163
- throw new Error('Shared: "'.concat(shareKey, '" cannot be loaded synchronously unless "eager:true" is set or async entry is enabled.'));
164
- }
165
- return federationInstance.loadShareSync(shareKey);
214
+ return federationInstance.loadShareSync(shareKey, shareInfo);
166
215
  }
167
216
  function installInitialConsumes(options) {
168
217
  var moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire, installedModules = options.installedModules, initialConsumes = options.initialConsumes;
@@ -184,6 +233,53 @@ function installInitialConsumes(options) {
184
233
  });
185
234
  }
186
235
 
236
+ function _define_property(obj, key, value) {
237
+ if (key in obj) {
238
+ Object.defineProperty(obj, key, {
239
+ value: value,
240
+ enumerable: true,
241
+ configurable: true,
242
+ writable: true
243
+ });
244
+ } else {
245
+ obj[key] = value;
246
+ }
247
+ return obj;
248
+ }
249
+ function _object_spread(target) {
250
+ for(var i = 1; i < arguments.length; i++){
251
+ var source = arguments[i] != null ? arguments[i] : {};
252
+ var ownKeys = Object.keys(source);
253
+ if (typeof Object.getOwnPropertySymbols === "function") {
254
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
255
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
256
+ }));
257
+ }
258
+ ownKeys.forEach(function(key) {
259
+ _define_property(target, key, source[key]);
260
+ });
261
+ }
262
+ return target;
263
+ }
264
+ function initContainerEntry(options) {
265
+ var webpackRequire = options.webpackRequire, shareScope = options.shareScope, initScope = options.initScope, shareScopeKey = options.shareScopeKey, remoteEntryInitOptions = options.remoteEntryInitOptions;
266
+ if (!webpackRequire.S) return;
267
+ if (!webpackRequire.federation || !webpackRequire.federation.instance || !webpackRequire.federation.initOptions) return;
268
+ var federationInstance = webpackRequire.federation.instance;
269
+ var name = shareScopeKey || "default";
270
+ federationInstance.initOptions(_object_spread({
271
+ name: webpackRequire.federation.initOptions.name,
272
+ remotes: []
273
+ }, remoteEntryInitOptions));
274
+ federationInstance.initShareScopeMap(name, shareScope);
275
+ webpackRequire.S[name] = shareScope;
276
+ if (webpackRequire.federation.attachShareScopeMap) {
277
+ webpackRequire.federation.attachShareScopeMap(webpackRequire);
278
+ }
279
+ // @ts-ignore
280
+ return webpackRequire.I(name, initScope);
281
+ }
282
+
187
283
  var federation = {
188
284
  runtime: runtime,
189
285
  instance: undefined,
@@ -193,8 +289,11 @@ var federation = {
193
289
  consumes: consumes,
194
290
  I: initializeSharing,
195
291
  S: {},
196
- installInitialConsumes: installInitialConsumes
197
- }
292
+ installInitialConsumes: installInitialConsumes,
293
+ initContainerEntry: initContainerEntry
294
+ },
295
+ attachShareScopeMap: attachShareScopeMap,
296
+ bundlerRuntimeOptions: {}
198
297
  };
199
298
 
200
299
  export { federation as default };
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "@module-federation/webpack-bundler-runtime",
4
- "version": "0.0.7",
4
+ "version": "0.0.8",
5
5
  "license": "MIT",
6
6
  "description": "Module Federation Runtime for webpack",
7
7
  "keywords": [
@@ -18,8 +18,30 @@
18
18
  "author": "zhanghang <hanric.zhang@gmail.com>",
19
19
  "main": "./index.cjs.js",
20
20
  "module": "./index.esm.js",
21
- "types": "./index.cjs.d.ts",
21
+ "types": "./dist/index.cjs.d.ts",
22
22
  "dependencies": {
23
- "@module-federation/runtime": "workspace:*"
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
+ }
24
46
  }
25
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[];
@@ -1,3 +1,4 @@
1
1
  import { Federation } from './types';
2
+ export * from './types';
2
3
  declare const federation: Federation;
3
4
  export default federation;
@@ -0,0 +1,2 @@
1
+ import { InitContainerEntryOptions, WebpackRequire } from './types';
2
+ export declare function initContainerEntry(options: InitContainerEntryOptions): WebpackRequire['I'] | void;
@@ -1,2 +1,2 @@
1
- import { WebpackRequire } from './types';
2
- export declare function initializeSharing(shareScopeName: string, webpackRequire: WebpackRequire): Promise<boolean> | boolean;
1
+ import { InitializeSharingOptions } from './types';
2
+ export declare function initializeSharing({ shareScopeName, webpackRequire, initPromises, initTokens, initScope, }: InitializeSharingOptions): Promise<boolean> | boolean | void;
@@ -1,5 +1,8 @@
1
1
  import * as runtime from '@module-federation/runtime';
2
+ import type { RemoteEntryInitOptions } from '@module-federation/runtime/types';
2
3
  import { initializeSharing } from './initializeSharing';
4
+ import { attachShareScopeMap } from './attachShareScopeMap';
5
+ import { initContainerEntry } from './initContainerEntry';
3
6
  type ExcludeUndefined<T> = T extends undefined ? never : T;
4
7
  type NonUndefined<T = Shared> = ExcludeUndefined<T>;
5
8
  type InitOptions = Parameters<typeof runtime.init>[0];
@@ -8,6 +11,15 @@ type SharedConfig = NonUndefined<NonUndefined[string]['shareConfig']>;
8
11
  type ModuleCache = runtime.FederationHost['moduleCache'];
9
12
  type InferModule<T> = T extends Map<string, infer U> ? U : never;
10
13
  type InferredModule = InferModule<ModuleCache>;
14
+ export type ShareScopeMap = runtime.FederationHost['shareScopeMap'];
15
+ type InitToken = Record<string, Record<string, any>>;
16
+ export interface InitializeSharingOptions {
17
+ shareScopeName: string;
18
+ webpackRequire: WebpackRequire;
19
+ initPromises: Record<string, Promise<boolean> | boolean>;
20
+ initTokens: InitToken;
21
+ initScope: InitToken[];
22
+ }
11
23
  export type RemoteEntryExports = NonUndefined<InferredModule['remoteEntryExports']>;
12
24
  type ExtractInitParameters<T> = T extends {
13
25
  init: (shareScope: infer U, ...args: any[]) => void;
@@ -16,13 +28,7 @@ type InferredShareScope = ExtractInitParameters<RemoteEntryExports>;
16
28
  type InferredGlobalShareScope = {
17
29
  [scope: string]: InferredShareScope;
18
30
  };
19
- type IdToExternalAndNameMappingItem = [
20
- string,
21
- string,
22
- string | number,
23
- string,
24
- string
25
- ];
31
+ type IdToExternalAndNameMappingItem = [string, string, string | number];
26
32
  interface IdToExternalAndNameMappingItemWithPromise extends IdToExternalAndNameMappingItem {
27
33
  p?: Promise<any> | number;
28
34
  }
@@ -32,7 +38,7 @@ export interface WebpackRequire {
32
38
  R: Array<string | number>;
33
39
  m: Record<string, (mod: any) => any>;
34
40
  c: Record<string, any>;
35
- I: typeof initializeSharing;
41
+ I: (scopeName: string, initScope?: InitializeSharingOptions['initScope']) => ReturnType<typeof initializeSharing>;
36
42
  S?: InferredGlobalShareScope;
37
43
  federation: Federation;
38
44
  }
@@ -45,11 +51,17 @@ interface ModuleToHandlerMappingItem {
45
51
  shareInfo: ShareInfo;
46
52
  shareKey: string;
47
53
  }
54
+ interface IdToRemoteMapItem {
55
+ externalType: string;
56
+ name: string;
57
+ externalModuleId?: string | number;
58
+ }
48
59
  export interface RemotesOptions {
49
60
  chunkId: string | number;
50
61
  promises: Promise<any>[];
51
62
  chunkMapping: Record<string, Array<string | number>>;
52
63
  idToExternalAndNameMapping: Record<string, IdToExternalAndNameMappingItemWithPromise>;
64
+ idToRemoteMap: Record<string, IdToRemoteMapItem[]>;
53
65
  webpackRequire: WebpackRequire;
54
66
  }
55
67
  export interface HandleInitialConsumesOptions {
@@ -71,6 +83,13 @@ export interface ConsumesOptions {
71
83
  moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
72
84
  webpackRequire: WebpackRequire;
73
85
  }
86
+ export interface InitContainerEntryOptions {
87
+ shareScope: ShareScopeMap[string];
88
+ shareScopeKey: string;
89
+ webpackRequire: WebpackRequire;
90
+ remoteEntryInitOptions?: RemoteEntryInitOptions;
91
+ initScope?: InitializeSharingOptions['initScope'];
92
+ }
74
93
  export interface Federation {
75
94
  runtime?: typeof runtime;
76
95
  instance?: runtime.FederationHost;
@@ -79,9 +98,15 @@ export interface Federation {
79
98
  bundlerRuntime?: {
80
99
  remotes: (options: RemotesOptions) => void;
81
100
  consumes: (options: ConsumesOptions) => void;
82
- I: (name: string, webpackRequire: WebpackRequire) => Promise<boolean> | boolean;
101
+ I: typeof initializeSharing;
83
102
  S: InferredGlobalShareScope;
84
103
  installInitialConsumes: (options: InstallInitialConsumesOptions) => any;
104
+ initContainerEntry: typeof initContainerEntry;
105
+ };
106
+ bundlerRuntimeOptions: {
107
+ remotes?: Exclude<RemotesOptions, 'chunkId' | 'promises'>;
85
108
  };
109
+ attachShareScopeMap?: typeof attachShareScopeMap;
110
+ hasAttachShareScopeMap?: boolean;
86
111
  }
87
112
  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.0.7",
4
+ "version": "0.0.8",
5
5
  "license": "MIT",
6
6
  "description": "Module Federation Runtime for webpack",
7
7
  "keywords": [
@@ -16,10 +16,32 @@
16
16
  "access": "public"
17
17
  },
18
18
  "author": "zhanghang <hanric.zhang@gmail.com>",
19
- "main": "./index.cjs.js",
20
- "module": "./index.esm.js",
21
- "types": "./index.cjs.d.ts",
19
+ "main": "./dist/index.cjs.js",
20
+ "module": "./dist/index.esm.js",
21
+ "types": "./dist/index.cjs.d.ts",
22
22
  "dependencies": {
23
- "@module-federation/runtime": "0.0.7"
23
+ "@module-federation/runtime": "0.0.8",
24
+ "@module-federation/sdk": "0.0.8"
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
+ }
24
46
  }
25
47
  }
@@ -1,2 +0,0 @@
1
- import { WebpackRequire } from './types';
2
- export declare function proxyShareScopeMap(__webpack_require__: WebpackRequire): void;