@module-federation/runtime 0.0.0-next-20231225095220 → 0.0.0-next-20231229025030

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.
@@ -3,7 +3,7 @@
3
3
  var share = require('./share.cjs.js');
4
4
 
5
5
  const ShareUtils = {
6
- getRegisteredShare: share.getRegisteredShare,
6
+ getGlobalShare: share.getGlobalShare,
7
7
  getGlobalShareScope: share.getGlobalShareScope
8
8
  };
9
9
  const GlobalUtils = {
@@ -1,7 +1,7 @@
1
- import { l as getRegisteredShare, t as getGlobalShareScope, F as nativeGlobal, H as resetFederationGlobalInfo, A as getGlobalFederationInstance, C as setGlobalFederationInstance, B as getGlobalFederationConstructor, z as setGlobalFederationConstructor, m as getInfoWithoutType, r as getGlobalSnapshot, I as getTargetSnapshotInfoByModuleInfo, p as getGlobalSnapshotInfoByModuleInfo, q as setGlobalSnapshotInfoByModuleInfo, J as addGlobalSnapshot, c as getRemoteEntryExports, E as registerGlobalPlugins, g as getGlobalHostPlugins, n as getPreloaded, o as setPreloaded } from './share.esm.js';
1
+ import { l as getGlobalShare, x as getGlobalShareScope, F as nativeGlobal, H as resetFederationGlobalInfo, A as getGlobalFederationInstance, C as setGlobalFederationInstance, B as getGlobalFederationConstructor, z as setGlobalFederationConstructor, m as getInfoWithoutType, r as getGlobalSnapshot, I as getTargetSnapshotInfoByModuleInfo, p as getGlobalSnapshotInfoByModuleInfo, q as setGlobalSnapshotInfoByModuleInfo, J as addGlobalSnapshot, c as getRemoteEntryExports, E as registerGlobalPlugins, g as getGlobalHostPlugins, n as getPreloaded, o as setPreloaded } from './share.esm.js';
2
2
 
3
3
  const ShareUtils = {
4
- getRegisteredShare,
4
+ getGlobalShare,
5
5
  getGlobalShareScope
6
6
  };
7
7
  const GlobalUtils = {
package/dist/index.cjs.js CHANGED
@@ -121,24 +121,6 @@ async function loadEntryScript({ name, globalName, entry, createScriptHook }) {
121
121
  if (remoteEntryExports) {
122
122
  return remoteEntryExports;
123
123
  }
124
- if (typeof document === 'undefined') {
125
- return sdk.loadScriptNode(entry, {
126
- attrs: {
127
- name,
128
- globalName
129
- },
130
- createScriptHook
131
- }).then(()=>{
132
- const { remoteEntryKey, entryExports } = share.getRemoteEntryExports(name, globalName);
133
- share.assert(entryExports, `
134
- Unable to use the ${name}'s '${entry}' URL with ${remoteEntryKey}'s globalName to get remoteEntry exports.
135
- Possible reasons could be:\n
136
- 1. '${entry}' is not the correct URL, or the remoteEntry resource or name is incorrect.\n
137
- 2. ${remoteEntryKey} cannot be used to get remoteEntry exports in the window object.
138
- `);
139
- return entryExports;
140
- });
141
- }
142
124
  return sdk.loadScript(entry, {
143
125
  attrs: {},
144
126
  createScriptHook
@@ -212,10 +194,6 @@ let Module = class Module {
212
194
  const res = this.loaderHook.lifecycle.createScript.emit({
213
195
  url
214
196
  });
215
- if (typeof document === 'undefined') {
216
- //todo: needs real fix
217
- return res;
218
- }
219
197
  if (res instanceof HTMLScriptElement) {
220
198
  return res;
221
199
  }
@@ -231,16 +209,15 @@ let Module = class Module {
231
209
  const { loadFactory = true } = options || {
232
210
  loadFactory: true
233
211
  };
234
- this.hostInfo.name;
235
212
  // Get remoteEntry.js
236
213
  const remoteEntryExports = await this.getEntry();
237
214
  if (!this.inited) {
238
- const localShareScopeMap = this.shareScopeMap;
215
+ const globalShareScope = share.Global.__FEDERATION__.__SHARE__;
239
216
  const remoteShareScope = this.remoteInfo.shareScope || 'default';
240
- if (!localShareScopeMap[remoteShareScope]) {
241
- localShareScopeMap[remoteShareScope] = {};
217
+ if (!globalShareScope[remoteShareScope]) {
218
+ globalShareScope[remoteShareScope] = {};
242
219
  }
243
- const shareScope = localShareScopeMap[remoteShareScope];
220
+ const shareScope = globalShareScope[remoteShareScope];
244
221
  // TODO: compat logic , it could be moved after providing startup hooks
245
222
  const remoteEntryInitOptions = {
246
223
  version: this.remoteInfo.version || '',
@@ -250,18 +227,10 @@ let Module = class Module {
250
227
  remoteEntryExports.init(shareScope, [], remoteEntryInitOptions);
251
228
  const federationInstance = share.Global.__FEDERATION__.__INSTANCES__.find((i)=>i.options.id === sdk.composeKeyWithSeparator(this.remoteInfo.name, this.remoteInfo.buildVersion));
252
229
  if (federationInstance) {
253
- // means the instance is prev vmok instance
254
- if (!federationInstance.releaseNumber || Number(federationInstance.releaseNumber) <= 100) {
255
- // 兼容旧的生产者传参
256
- federationInstance.initOptions(_extends$4({}, remoteEntryInitOptions, {
257
- remotes: [],
258
- name: this.remoteInfo.name
259
- }));
260
- if (!__FEDERATION__.__SHARE__['default'] && this.shareScopeMap && this.shareScopeMap['default']) {
261
- // @ts-ignore compat prev logic , and it will be optimized by supporting startup hook
262
- __FEDERATION__.__SHARE__['default'] = this.shareScopeMap['default'];
263
- }
264
- }
230
+ federationInstance.initOptions(_extends$4({}, remoteEntryInitOptions, {
231
+ remotes: [],
232
+ name: this.remoteInfo.name
233
+ }));
265
234
  }
266
235
  }
267
236
  this.lib = remoteEntryExports;
@@ -276,7 +245,7 @@ let Module = class Module {
276
245
  return exposeContent;
277
246
  }
278
247
  // loading: Record<string, undefined | Promise<RemoteEntryExports | void>> = {};
279
- constructor({ hostInfo, remoteInfo, shared, loaderHook, shareScopeMap }){
248
+ constructor({ hostInfo, remoteInfo, shared, loaderHook }){
280
249
  this.inited = false;
281
250
  this.shared = {};
282
251
  this.lib = undefined;
@@ -284,7 +253,6 @@ let Module = class Module {
284
253
  this.remoteInfo = remoteInfo;
285
254
  this.shared = shared;
286
255
  this.loaderHook = loaderHook;
287
- this.shareScopeMap = shareScopeMap;
288
256
  }
289
257
  };
290
258
 
@@ -811,9 +779,9 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
811
779
  if (!shareInfo) {
812
780
  return;
813
781
  }
814
- const registeredShared = share.getRegisteredShare(origin.shareScopeMap, shared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
782
+ const globalShare = share.getGlobalShare(shared.sharedName, shareInfo);
815
783
  // If the global share does not exist, or the lib function does not exist, it means that the shared has not been loaded yet and can be preloaded.
816
- if (registeredShared && typeof registeredShared.lib === 'function') {
784
+ if (globalShare && typeof globalShare.lib === 'function') {
817
785
  shared.assets.js.sync.forEach((asset)=>{
818
786
  loadedSharedJsAssets.add(asset);
819
787
  });
@@ -1140,13 +1108,6 @@ function _object_without_properties_loose(source, excluded) {
1140
1108
  return target;
1141
1109
  }
1142
1110
  class FederationHost {
1143
- _setGlobalShareScopeMap() {
1144
- const globalShareScopeMap = share.getGlobalShareScope();
1145
- const identifier = this.options.id || this.options.name;
1146
- if (identifier && !globalShareScopeMap[identifier]) {
1147
- globalShareScopeMap[identifier] = this.shareScopeMap;
1148
- }
1149
- }
1150
1111
  initOptions(userOptions) {
1151
1112
  this.registerPlugins(userOptions.plugins);
1152
1113
  const options = this.formatOptions(this.options, userOptions);
@@ -1161,11 +1122,6 @@ class FederationHost {
1161
1122
  // 2. Searches globally for a matching share, if found, it uses it directly
1162
1123
  // 3. If not found, it retrieves it from the current share and stores the obtained share globally.
1163
1124
  const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
1164
- if (shareInfo == null ? void 0 : shareInfo.scope) {
1165
- shareInfo.scope.forEach((shareScope)=>{
1166
- this.initializeSharing(shareScope, shareInfo.strategy);
1167
- });
1168
- }
1169
1125
  const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
1170
1126
  pkgName,
1171
1127
  shareInfo,
@@ -1176,42 +1132,30 @@ class FederationHost {
1176
1132
  // Assert that shareInfoRes exists, if not, throw an error
1177
1133
  share.assert(shareInfoRes, `Cannot find ${pkgName} Share in the ${this.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
1178
1134
  // Retrieve from cache
1179
- const registeredShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
1180
- const addUseIn = (shared)=>{
1181
- if (!shared.useIn) {
1182
- shared.useIn = [];
1183
- }
1184
- share.addUniqueItem(shared.useIn, this.options.name);
1185
- };
1186
- if (registeredShared && registeredShared.lib) {
1187
- addUseIn(registeredShared);
1188
- return registeredShared.lib;
1189
- } else if (registeredShared && registeredShared.loading && !registeredShared.loaded) {
1190
- const factory = await registeredShared.loading;
1191
- registeredShared.loaded = true;
1192
- if (!registeredShared.lib) {
1193
- registeredShared.lib = factory;
1194
- }
1195
- addUseIn(registeredShared);
1135
+ const globalShare = share.getGlobalShare(pkgName, shareInfoRes);
1136
+ if (globalShare && globalShare.lib) {
1137
+ share.addUniqueItem(globalShare.useIn, this.options.name);
1138
+ return globalShare.lib;
1139
+ } else if (globalShare && globalShare.loading) {
1140
+ const factory = await globalShare.loading;
1141
+ share.addUniqueItem(globalShare.useIn, this.options.name);
1196
1142
  return factory;
1197
- } else if (registeredShared) {
1143
+ } else if (globalShare) {
1198
1144
  const asyncLoadProcess = async ()=>{
1199
- const factory = await registeredShared.get();
1145
+ const factory = await globalShare.get();
1200
1146
  shareInfoRes.lib = factory;
1201
- shareInfoRes.loaded = true;
1202
- addUseIn(shareInfoRes);
1203
- const gShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
1147
+ share.addUniqueItem(shareInfoRes.useIn, this.options.name);
1148
+ const gShared = share.getGlobalShare(pkgName, shareInfoRes);
1204
1149
  if (gShared) {
1205
1150
  gShared.lib = factory;
1206
- gShared.loaded = true;
1207
1151
  }
1208
1152
  return factory;
1209
1153
  };
1210
1154
  const loading = asyncLoadProcess();
1211
1155
  this.setShared({
1212
1156
  pkgName,
1213
- loaded: false,
1214
- shared: registeredShared,
1157
+ loaded: true,
1158
+ shared: shareInfoRes,
1215
1159
  from: this.options.name,
1216
1160
  lib: null,
1217
1161
  loading
@@ -1224,19 +1168,17 @@ class FederationHost {
1224
1168
  const asyncLoadProcess = async ()=>{
1225
1169
  const factory = await shareInfoRes.get();
1226
1170
  shareInfoRes.lib = factory;
1227
- shareInfoRes.loaded = true;
1228
- addUseIn(shareInfoRes);
1229
- const gShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
1171
+ share.addUniqueItem(shareInfoRes.useIn, this.options.name);
1172
+ const gShared = share.getGlobalShare(pkgName, shareInfoRes);
1230
1173
  if (gShared) {
1231
1174
  gShared.lib = factory;
1232
- gShared.loaded = true;
1233
1175
  }
1234
1176
  return factory;
1235
1177
  };
1236
1178
  const loading = asyncLoadProcess();
1237
1179
  this.setShared({
1238
1180
  pkgName,
1239
- loaded: false,
1181
+ loaded: true,
1240
1182
  shared: shareInfoRes,
1241
1183
  from: this.options.name,
1242
1184
  lib: null,
@@ -1252,16 +1194,16 @@ class FederationHost {
1252
1194
  loadShareSync(pkgName) {
1253
1195
  var _this_options_shared;
1254
1196
  const shareInfo = (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName];
1255
- const registeredShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfo, this.hooks.lifecycle.resolveShare);
1256
- if (registeredShared && typeof registeredShared.lib === 'function') {
1257
- share.addUniqueItem(registeredShared.useIn, this.options.name);
1258
- if (!registeredShared.loaded) {
1259
- registeredShared.loaded = true;
1260
- if (registeredShared.from === this.options.name) {
1197
+ const globalShare = share.getGlobalShare(pkgName, shareInfo);
1198
+ if (globalShare && typeof globalShare.lib === 'function') {
1199
+ share.addUniqueItem(globalShare.useIn, this.options.name);
1200
+ if (!globalShare.loaded) {
1201
+ globalShare.loaded = true;
1202
+ if (globalShare.from === this.options.name) {
1261
1203
  shareInfo.loaded = true;
1262
1204
  }
1263
1205
  }
1264
- return registeredShared.lib;
1206
+ return globalShare.lib;
1265
1207
  }
1266
1208
  if (shareInfo.lib) {
1267
1209
  if (!shareInfo.loaded) {
@@ -1332,8 +1274,7 @@ class FederationHost {
1332
1274
  remoteInfo,
1333
1275
  shared: this.options.shared || {},
1334
1276
  plugins: this.options.plugins,
1335
- loaderHook: this.loaderHook,
1336
- shareScopeMap: this.shareScopeMap
1277
+ loaderHook: this.loaderHook
1337
1278
  };
1338
1279
  if (!module) {
1339
1280
  module = new Module(moduleOptions);
@@ -1413,9 +1354,14 @@ class FederationHost {
1413
1354
  * It accepts one argument, the name of the share scope.
1414
1355
  * If the share scope does not exist, it creates one.
1415
1356
  */ // eslint-disable-next-line @typescript-eslint/member-ordering
1416
- initializeSharing(shareScopeName = share.DEFAULT_SCOPE, strategy) {
1417
- const shareScope = this.shareScopeMap;
1357
+ initializeSharing(shareScopeName = share.DEFAULT_SCOPE) {
1358
+ const shareScopeLoading = share.Global.__FEDERATION__.__SHARE_SCOPE_LOADING__;
1359
+ const shareScope = share.Global.__FEDERATION__.__SHARE__;
1418
1360
  const hostName = this.options.name;
1361
+ // Executes only once
1362
+ if (shareScopeLoading[shareScopeName]) {
1363
+ return shareScopeLoading[shareScopeName];
1364
+ }
1419
1365
  // Creates a new share scope if necessary
1420
1366
  if (!shareScope[shareScopeName]) {
1421
1367
  shareScope[shareScopeName] = {};
@@ -1423,27 +1369,21 @@ class FederationHost {
1423
1369
  // Executes all initialization snippets from all accessible modules
1424
1370
  const scope = shareScope[shareScopeName];
1425
1371
  const register = (name, shared)=>{
1426
- var _activeVersion_shareConfig;
1427
1372
  const { version, eager } = shared;
1428
1373
  scope[name] = scope[name] || {};
1429
1374
  const versions = scope[name];
1430
1375
  const activeVersion = versions[version];
1431
- const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || ((_activeVersion_shareConfig = activeVersion.shareConfig) == null ? void 0 : _activeVersion_shareConfig.eager)));
1376
+ const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || activeVersion.shareConfig.eager));
1432
1377
  if (!activeVersion || !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
1433
1378
  versions[version] = shared;
1434
1379
  }
1435
1380
  };
1436
1381
  const promises = [];
1437
- const initFn = (mod)=>mod && mod.init && mod.init(shareScope[shareScopeName]);
1438
1382
  const initRemoteModule = async (key)=>{
1439
1383
  const { module } = await this._getRemoteModuleAndOptions(key);
1440
- if (module.getEntry) {
1441
- const entry = await module.getEntry();
1442
- if (!module.inited) {
1443
- initFn(entry);
1444
- module.inited = true;
1445
- }
1446
- }
1384
+ const initFn = (mod)=>mod && mod.init && mod.init(shareScope[shareScopeName]);
1385
+ const entry = await module.getEntry();
1386
+ initFn(entry);
1447
1387
  };
1448
1388
  Object.keys(this.options.shared).forEach((shareName)=>{
1449
1389
  const shared = this.options.shared[shareName];
@@ -1451,17 +1391,15 @@ class FederationHost {
1451
1391
  register(shareName, shared);
1452
1392
  }
1453
1393
  });
1454
- if (strategy === 'version-first') {
1455
- this.options.remotes.forEach((remote)=>{
1456
- if (remote.shareScope === shareScopeName) {
1457
- promises.push(initRemoteModule(remote.name));
1458
- }
1459
- });
1394
+ this.options.remotes.forEach((remote)=>{
1395
+ if (remote.shareScope === shareScopeName) {
1396
+ promises.push(initRemoteModule(remote.name));
1397
+ }
1398
+ });
1399
+ if (!promises.length) {
1400
+ return shareScopeLoading[shareScopeName] = true;
1460
1401
  }
1461
- return promises;
1462
- }
1463
- initShareScopeMap(scopeName, shareScope) {
1464
- this.shareScopeMap[scopeName] = shareScope;
1402
+ return shareScopeLoading[shareScopeName] = Promise.all(promises).then(()=>shareScopeLoading[shareScopeName] = true);
1465
1403
  }
1466
1404
  formatOptions(globalOptions, userOptions) {
1467
1405
  const formatShareOptions = share.formatShareConfigs(userOptions.shared || {}, userOptions.name);
@@ -1501,17 +1439,16 @@ class FederationHost {
1501
1439
  }
1502
1440
  return res;
1503
1441
  }, globalOptionsRes.remotes);
1504
- // register shared in shareScopeMap
1442
+ // register shared include lib
1505
1443
  const sharedKeys = Object.keys(formatShareOptions);
1506
1444
  sharedKeys.forEach((sharedKey)=>{
1507
1445
  const sharedVal = formatShareOptions[sharedKey];
1508
- const registeredShared = share.getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
1509
- if (!registeredShared && sharedVal && sharedVal.lib) {
1446
+ const globalShare = share.getGlobalShare(sharedKey, sharedVal);
1447
+ if (!globalShare && sharedVal && sharedVal.lib) {
1510
1448
  this.setShared({
1511
1449
  pkgName: sharedKey,
1512
1450
  lib: sharedVal.lib,
1513
1451
  get: sharedVal.get,
1514
- loaded: true,
1515
1452
  shared: sharedVal,
1516
1453
  from: userOptions.name
1517
1454
  });
@@ -1546,6 +1483,7 @@ class FederationHost {
1546
1483
  ]);
1547
1484
  }
1548
1485
  setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
1486
+ const target = share.getGlobalShareScope();
1549
1487
  const { version, scope = 'default' } = shared, shareInfo = _object_without_properties_loose(shared, [
1550
1488
  "version",
1551
1489
  "scope"
@@ -1554,23 +1492,23 @@ class FederationHost {
1554
1492
  scope
1555
1493
  ];
1556
1494
  scopes.forEach((sc)=>{
1557
- if (!this.shareScopeMap[sc]) {
1558
- this.shareScopeMap[sc] = {};
1495
+ if (!target[sc]) {
1496
+ target[sc] = {};
1559
1497
  }
1560
- if (!this.shareScopeMap[sc][pkgName]) {
1561
- this.shareScopeMap[sc][pkgName] = {};
1498
+ if (!target[sc][pkgName]) {
1499
+ target[sc][pkgName] = {};
1562
1500
  }
1563
- if (this.shareScopeMap[sc][pkgName][version]) {
1501
+ if (target[sc][pkgName][version]) {
1564
1502
  share.warn(// eslint-disable-next-line max-len
1565
1503
  `The share \n ${share.safeToString({
1566
1504
  scope: sc,
1567
1505
  pkgName,
1568
1506
  version,
1569
- from: this.shareScopeMap[sc][pkgName][version].from
1507
+ from: target[sc][pkgName][version].from
1570
1508
  })} has been registered`);
1571
1509
  return;
1572
1510
  }
1573
- this.shareScopeMap[sc][pkgName][version] = _extends({
1511
+ target[sc][pkgName][version] = _extends({
1574
1512
  version,
1575
1513
  scope: [
1576
1514
  'default'
@@ -1581,7 +1519,7 @@ class FederationHost {
1581
1519
  loading
1582
1520
  });
1583
1521
  if (get) {
1584
- this.shareScopeMap[sc][pkgName][version].get = get;
1522
+ target[sc][pkgName][version].get = get;
1585
1523
  }
1586
1524
  });
1587
1525
  }
@@ -1596,13 +1534,11 @@ class FederationHost {
1596
1534
  errorLoadRemote: new AsyncHook('errorLoadRemote'),
1597
1535
  beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
1598
1536
  loadShare: new AsyncHook(),
1599
- resolveShare: new SyncWaterfallHook('resolveShare'),
1600
1537
  beforePreloadRemote: new AsyncHook(),
1601
1538
  generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
1602
1539
  afterPreloadRemote: new AsyncHook()
1603
1540
  });
1604
- this.releaseNumber = `4`;
1605
- this.version = `0.0.4`;
1541
+ this.version = '0.0.4';
1606
1542
  this.moduleCache = new Map();
1607
1543
  this.loaderHook = new PluginSystem({
1608
1544
  // FIXME: may not be suitable
@@ -1610,6 +1546,7 @@ class FederationHost {
1610
1546
  createScript: new SyncHook(),
1611
1547
  fetch: new AsyncHook('fetch')
1612
1548
  });
1549
+ this.loadingShare = {};
1613
1550
  // TODO: Validate the details of the options
1614
1551
  // Initialize options with default values
1615
1552
  const defaultOptions = {
@@ -1625,8 +1562,6 @@ class FederationHost {
1625
1562
  };
1626
1563
  this.name = userOptions.name;
1627
1564
  this.options = defaultOptions;
1628
- this.shareScopeMap = {};
1629
- this._setGlobalShareScopeMap();
1630
1565
  this.snapshotHandler = new SnapshotHandler(this);
1631
1566
  this.registerPlugins([
1632
1567
  ...defaultOptions.plugins,
@@ -1676,10 +1611,6 @@ function preloadRemote(...args) {
1676
1611
  share.setGlobalFederationConstructor(FederationHost);
1677
1612
 
1678
1613
  exports.registerGlobalPlugins = share.registerGlobalPlugins;
1679
- Object.defineProperty(exports, 'loadScript', {
1680
- enumerable: true,
1681
- get: function () { return sdk.loadScript; }
1682
- });
1683
1614
  exports.FederationHost = FederationHost;
1684
1615
  exports.init = init;
1685
1616
  exports.loadRemote = loadRemote;