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