@module-federation/runtime 0.0.0-next-20240605083609 → 0.0.0-next-20240605100049

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.
@@ -1,4 +1,4 @@
1
- import { q as getRegisteredShare, A as getGlobalShareScope, G as Global, K as nativeGlobal, L as resetFederationGlobalInfo, F as getGlobalFederationInstance, I as setGlobalFederationInstance, H as getGlobalFederationConstructor, E as setGlobalFederationConstructor, n as getInfoWithoutType, x as getGlobalSnapshot, M as getTargetSnapshotInfoByModuleInfo, t as getGlobalSnapshotInfoByModuleInfo, v as setGlobalSnapshotInfoByModuleInfo, u as addGlobalSnapshot, c as getRemoteEntryExports, J as registerGlobalPlugins, g as getGlobalHostPlugins, o as getPreloaded, p as setPreloaded } from './share.esm.js';
1
+ import { p as getRegisteredShare, z as getGlobalShareScope, G as Global, J as nativeGlobal, K as resetFederationGlobalInfo, E as getGlobalFederationInstance, H as setGlobalFederationInstance, F as getGlobalFederationConstructor, C as setGlobalFederationConstructor, m as getInfoWithoutType, v as getGlobalSnapshot, L as getTargetSnapshotInfoByModuleInfo, r as getGlobalSnapshotInfoByModuleInfo, u as setGlobalSnapshotInfoByModuleInfo, t as addGlobalSnapshot, c as getRemoteEntryExports, I as registerGlobalPlugins, g as getGlobalHostPlugins, n as getPreloaded, o as setPreloaded } from './share.esm.js';
2
2
 
3
3
  const ShareUtils = {
4
4
  getRegisteredShare,
package/dist/index.cjs.js CHANGED
@@ -172,7 +172,10 @@ async function getRemoteEntry({ remoteEntryExports, remoteInfo, createScriptHook
172
172
  return remoteEntryExports;
173
173
  }
174
174
  if (!share.globalLoading[uniqueKey]) {
175
- if (type === 'esm') {
175
+ if ([
176
+ 'esm',
177
+ 'module'
178
+ ].includes(type)) {
176
179
  share.globalLoading[uniqueKey] = loadEsmEntry({
177
180
  entry,
178
181
  remoteEntryExports
@@ -243,7 +246,7 @@ let Module = class Module {
243
246
  return this.remoteEntryExports;
244
247
  }
245
248
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
246
- async get(expose, options) {
249
+ async get(id, expose, options) {
247
250
  const { loadFactory = true } = options || {
248
251
  loadFactory: true
249
252
  };
@@ -261,14 +264,14 @@ let Module = class Module {
261
264
  version: this.remoteInfo.version || ''
262
265
  };
263
266
  // Help to find host instance
264
- Object.defineProperty(remoteEntryInitOptions, 'hostId', {
265
- value: this.host.options.id || this.host.name,
267
+ Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
268
+ value: localShareScopeMap,
266
269
  // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
267
270
  enumerable: false
268
271
  });
269
272
  const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
270
273
  shareScope,
271
- // @ts-ignore hostId will be set by Object.defineProperty
274
+ // @ts-ignore shareScopeMap will be set by Object.defineProperty
272
275
  remoteEntryInitOptions,
273
276
  initScope,
274
277
  remoteInfo: this.remoteInfo,
@@ -284,12 +287,38 @@ let Module = class Module {
284
287
  // get exposeGetter
285
288
  const moduleFactory = await remoteEntryExports.get(expose);
286
289
  share.assert(moduleFactory, `${share.getFMId(this.remoteInfo)} remote don't export ${expose}.`);
290
+ const wrapModuleFactory = this.wraperFactory(moduleFactory, id);
287
291
  if (!loadFactory) {
288
- return moduleFactory;
292
+ return wrapModuleFactory;
289
293
  }
290
- const exposeContent = await moduleFactory();
294
+ const exposeContent = await wrapModuleFactory();
291
295
  return exposeContent;
292
296
  }
297
+ wraperFactory(moduleFactory, id) {
298
+ function defineModuleId(res, id) {
299
+ if (res && typeof res === 'object' && !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))) {
300
+ Object.defineProperty(res, Symbol.for('mf_module_id'), {
301
+ value: id,
302
+ enumerable: false
303
+ });
304
+ }
305
+ }
306
+ if (moduleFactory instanceof Promise) {
307
+ return async ()=>{
308
+ const res = await moduleFactory();
309
+ // This parameter is used for bridge debugging
310
+ defineModuleId(res, id);
311
+ return res;
312
+ };
313
+ } else {
314
+ return ()=>{
315
+ const res = moduleFactory();
316
+ // This parameter is used for bridge debugging
317
+ defineModuleId(res, id);
318
+ return res;
319
+ };
320
+ }
321
+ }
293
322
  constructor({ remoteInfo, host }){
294
323
  this.inited = false;
295
324
  this.lib = undefined;
@@ -534,7 +563,8 @@ function normalizePreloadExposes(exposes) {
534
563
  return expose;
535
564
  });
536
565
  }
537
- function preloadAssets(remoteInfo, host, assets) {
566
+ function preloadAssets(remoteInfo, host, assets, // It is used to distinguish preload from load remote parallel loading
567
+ useLinkPreload = true) {
538
568
  const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
539
569
  if (host.options.inBrowser) {
540
570
  entryAssets.forEach((asset)=>{
@@ -586,40 +616,94 @@ function preloadAssets(remoteInfo, host, assets) {
586
616
  });
587
617
  }
588
618
  });
589
- const fragment = document.createDocumentFragment();
590
- cssAssets.forEach((cssUrl)=>{
591
- const { link: cssEl, needAttach } = sdk.createLink(cssUrl, ()=>{}, {
592
- rel: 'preload',
593
- as: 'style'
594
- }, (url)=>{
595
- const res = host.loaderHook.lifecycle.createLink.emit({
596
- url
619
+ if (useLinkPreload) {
620
+ cssAssets.forEach((cssUrl)=>{
621
+ const { link: cssEl, needAttach } = sdk.createLink({
622
+ url: cssUrl,
623
+ cb: ()=>{},
624
+ attrs: {
625
+ rel: 'preload',
626
+ as: 'style',
627
+ crossorigin: 'anonymous'
628
+ },
629
+ createLinkHook: (url)=>{
630
+ const res = host.loaderHook.lifecycle.createLink.emit({
631
+ url
632
+ });
633
+ if (res instanceof HTMLLinkElement) {
634
+ return res;
635
+ }
636
+ return;
637
+ }
597
638
  });
598
- if (res instanceof HTMLLinkElement) {
599
- return res;
600
- }
601
- return;
639
+ needAttach && document.head.appendChild(cssEl);
602
640
  });
603
- needAttach && fragment.appendChild(cssEl);
604
- });
605
- jsAssetsWithoutEntry.forEach((jsUrl)=>{
606
- const { link: linkEl, needAttach } = sdk.createLink(jsUrl, ()=>{
607
- // noop
608
- }, {
609
- rel: 'preload',
610
- as: 'script'
611
- }, (url)=>{
612
- const res = host.loaderHook.lifecycle.createLink.emit({
613
- url
641
+ } else {
642
+ cssAssets.forEach((cssUrl)=>{
643
+ const { link: cssEl, needAttach } = sdk.createLink({
644
+ url: cssUrl,
645
+ cb: ()=>{},
646
+ attrs: {
647
+ rel: 'stylesheet',
648
+ type: 'text/css'
649
+ },
650
+ createLinkHook: (url)=>{
651
+ const res = host.loaderHook.lifecycle.createLink.emit({
652
+ url
653
+ });
654
+ if (res instanceof HTMLLinkElement) {
655
+ return res;
656
+ }
657
+ return;
658
+ }
614
659
  });
615
- if (res instanceof HTMLLinkElement) {
616
- return res;
617
- }
618
- return;
660
+ needAttach && document.head.appendChild(cssEl);
619
661
  });
620
- needAttach && document.head.appendChild(linkEl);
621
- });
622
- document.head.appendChild(fragment);
662
+ }
663
+ if (useLinkPreload) {
664
+ jsAssetsWithoutEntry.forEach((jsUrl)=>{
665
+ const { link: linkEl, needAttach } = sdk.createLink({
666
+ url: jsUrl,
667
+ cb: ()=>{},
668
+ attrs: {
669
+ rel: 'preload',
670
+ as: 'script',
671
+ crossorigin: 'anonymous'
672
+ },
673
+ createLinkHook: (url)=>{
674
+ const res = host.loaderHook.lifecycle.createLink.emit({
675
+ url
676
+ });
677
+ if (res instanceof HTMLLinkElement) {
678
+ return res;
679
+ }
680
+ return;
681
+ }
682
+ });
683
+ needAttach && document.head.appendChild(linkEl);
684
+ });
685
+ } else {
686
+ jsAssetsWithoutEntry.forEach((jsUrl)=>{
687
+ const { script: scriptEl, needAttach } = sdk.createScript({
688
+ url: jsUrl,
689
+ cb: ()=>{},
690
+ attrs: {
691
+ crossorigin: 'anonymous',
692
+ fetchpriority: 'high'
693
+ },
694
+ createScriptHook: (url)=>{
695
+ const res = host.loaderHook.lifecycle.createScript.emit({
696
+ url
697
+ });
698
+ if (res instanceof HTMLScriptElement) {
699
+ return res;
700
+ }
701
+ return;
702
+ }
703
+ });
704
+ needAttach && document.head.appendChild(scriptEl);
705
+ });
706
+ }
623
707
  }
624
708
  }
625
709
 
@@ -638,16 +722,16 @@ function _extends$4() {
638
722
  return _extends$4.apply(this, arguments);
639
723
  }
640
724
  function assignRemoteInfo(remoteInfo, remoteSnapshot) {
641
- const remoteEntryInfo = share.getRemoteEntryInfoFromSnapshot(remoteSnapshot);
642
- if (!remoteEntryInfo.url) {
643
- share.error(`The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`);
725
+ if (!('remoteEntry' in remoteSnapshot) || !remoteSnapshot.remoteEntry) {
726
+ share.error(`The attribute remoteEntry of ${name} must not be undefined.`);
644
727
  }
645
- let entryUrl = sdk.getResourceUrl(remoteSnapshot, remoteEntryInfo.url);
728
+ const { remoteEntry } = remoteSnapshot;
729
+ let entryUrl = sdk.getResourceUrl(remoteSnapshot, remoteEntry);
646
730
  if (!share.isBrowserEnv() && !entryUrl.startsWith('http')) {
647
731
  entryUrl = `https:${entryUrl}`;
648
732
  }
649
- remoteInfo.type = remoteEntryInfo.type;
650
- remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
733
+ remoteInfo.type = remoteSnapshot.remoteEntryType;
734
+ remoteInfo.entryGlobalName = remoteSnapshot.globalName;
651
735
  remoteInfo.entry = entryUrl;
652
736
  remoteInfo.version = remoteSnapshot.version;
653
737
  remoteInfo.buildVersion = remoteSnapshot.buildVersion;
@@ -682,7 +766,7 @@ function snapshotPlugin() {
682
766
  globalSnapshot
683
767
  });
684
768
  if (assets) {
685
- preloadAssets(remoteInfo, origin, assets);
769
+ preloadAssets(remoteInfo, origin, assets, false);
686
770
  }
687
771
  return _extends$4({}, args, {
688
772
  remoteSnapshot
@@ -772,7 +856,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
772
856
  return;
773
857
  }
774
858
  }
775
- const remoteEntryUrl = sdk.getResourceUrl(moduleInfoSnapshot, share.getRemoteEntryInfoFromSnapshot(moduleInfoSnapshot).url);
859
+ const remoteEntryUrl = sdk.getResourceUrl(moduleInfoSnapshot, 'remoteEntry' in moduleInfoSnapshot ? moduleInfoSnapshot.remoteEntry : '');
776
860
  if (remoteEntryUrl) {
777
861
  entryAssets.push({
778
862
  name: remoteInfo.name,
@@ -978,13 +1062,12 @@ class SnapshotHandler {
978
1062
  // global snapshot includes manifest or module info includes manifest
979
1063
  if (globalRemoteSnapshot) {
980
1064
  if (sdk.isManifestProvider(globalRemoteSnapshot)) {
981
- const remoteEntry = share.isBrowserEnv() ? globalRemoteSnapshot.remoteEntry : globalRemoteSnapshot.ssrRemoteEntry || globalRemoteSnapshot.remoteEntry || '';
982
- const moduleSnapshot = await this.getManifestJson(remoteEntry, moduleInfo, {});
1065
+ const moduleSnapshot = await this.getManifestJson(globalRemoteSnapshot.remoteEntry, moduleInfo, {});
983
1066
  // eslint-disable-next-line @typescript-eslint/no-shadow
984
1067
  const globalSnapshotRes = share.setGlobalSnapshotInfoByModuleInfo(_extends$3({}, moduleInfo, {
985
1068
  // The global remote may be overridden
986
1069
  // Therefore, set the snapshot key to the global address of the actual request
987
- entry: remoteEntry
1070
+ entry: globalRemoteSnapshot.remoteEntry
988
1071
  }), moduleSnapshot);
989
1072
  return {
990
1073
  remoteSnapshot: moduleSnapshot,
@@ -1396,13 +1479,15 @@ class SharedHandler {
1396
1479
  2. The ${pkgName} share was not registered with the 'lib' attribute.\n
1397
1480
  `);
1398
1481
  }
1399
- initShareScopeMap(scopeName, shareScope) {
1482
+ initShareScopeMap(scopeName, shareScope, hostShareScopeMap) {
1400
1483
  const { host } = this;
1401
1484
  this.shareScopeMap[scopeName] = shareScope;
1402
1485
  this.hooks.lifecycle.initContainerShareScopeMap.emit({
1403
1486
  shareScope,
1404
1487
  options: host.options,
1405
- origin: host
1488
+ origin: host,
1489
+ scopeName,
1490
+ hostShareScopeMap
1406
1491
  });
1407
1492
  }
1408
1493
  setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
@@ -1453,7 +1538,7 @@ class SharedHandler {
1453
1538
  loadShare: new AsyncHook(),
1454
1539
  resolveShare: new SyncWaterfallHook('resolveShare'),
1455
1540
  // maybe will change, temporarily for internal use only
1456
- initContainerShareScopeMap: new AsyncWaterfallHook('initContainer')
1541
+ initContainerShareScopeMap: new SyncWaterfallHook('initContainerShareScopeMap')
1457
1542
  });
1458
1543
  this.host = host;
1459
1544
  this.shareScopeMap = {};
@@ -1504,7 +1589,7 @@ class RemoteHandler {
1504
1589
  id
1505
1590
  });
1506
1591
  const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
1507
- const moduleOrFactory = await module.get(expose, options);
1592
+ const moduleOrFactory = await module.get(idRes, expose, options);
1508
1593
  const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
1509
1594
  id: idRes,
1510
1595
  pkgNameOrAlias,
@@ -1516,10 +1601,6 @@ class RemoteHandler {
1516
1601
  moduleInstance: module,
1517
1602
  origin: host
1518
1603
  });
1519
- this.idToRemoteMap[id] = {
1520
- name: remote.name,
1521
- expose
1522
- };
1523
1604
  if (typeof moduleWrapper === 'function') {
1524
1605
  return moduleWrapper;
1525
1606
  }
@@ -1678,61 +1759,49 @@ class RemoteHandler {
1678
1759
  }
1679
1760
  }
1680
1761
  removeRemote(remote) {
1681
- try {
1682
- const { host } = this;
1683
- const { name } = remote;
1684
- const remoteIndex = host.options.remotes.findIndex((item)=>item.name === name);
1685
- if (remoteIndex !== -1) {
1686
- host.options.remotes.splice(remoteIndex, 1);
1762
+ const { host } = this;
1763
+ const { name } = remote;
1764
+ const remoteIndex = host.options.remotes.findIndex((item)=>item.name === name);
1765
+ if (remoteIndex !== -1) {
1766
+ host.options.remotes.splice(remoteIndex, 1);
1767
+ }
1768
+ const loadedModule = host.moduleCache.get(remote.name);
1769
+ if (loadedModule) {
1770
+ var _Object_getOwnPropertyDescriptor;
1771
+ const remoteInfo = loadedModule.remoteInfo;
1772
+ const key = remoteInfo.entryGlobalName;
1773
+ if (globalThis[key] && ((_Object_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(globalThis, key)) == null ? void 0 : _Object_getOwnPropertyDescriptor.configurable)) {
1774
+ delete globalThis[key];
1687
1775
  }
1688
- const loadedModule = host.moduleCache.get(remote.name);
1689
- if (loadedModule) {
1690
- const remoteInfo = loadedModule.remoteInfo;
1691
- const key = remoteInfo.entryGlobalName;
1692
- if (globalThis[key]) {
1693
- delete globalThis[key];
1694
- }
1695
- const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
1696
- if (share.globalLoading[remoteEntryUniqueKey]) {
1697
- delete share.globalLoading[remoteEntryUniqueKey];
1776
+ const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
1777
+ if (share.globalLoading[remoteEntryUniqueKey]) {
1778
+ delete share.globalLoading[remoteEntryUniqueKey];
1779
+ }
1780
+ // delete un loaded shared and instance
1781
+ let remoteInsId = remoteInfo.buildVersion ? sdk.composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
1782
+ const remoteInsIndex = globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins)=>{
1783
+ if (remoteInfo.buildVersion) {
1784
+ return ins.options.id === remoteInsId;
1785
+ } else {
1786
+ return ins.name === remoteInsId;
1698
1787
  }
1699
- host.snapshotHandler.manifestCache.delete(remoteInfo.entry);
1700
- // delete un loaded shared and instance
1701
- let remoteInsId = remoteInfo.buildVersion ? sdk.composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
1702
- const remoteInsIndex = globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins)=>{
1703
- if (remoteInfo.buildVersion) {
1704
- return ins.options.id === remoteInsId;
1705
- } else {
1706
- return ins.name === remoteInsId;
1707
- }
1708
- });
1709
- if (remoteInsIndex !== -1) {
1710
- const remoteIns = globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
1711
- remoteInsId = remoteIns.options.id || remoteInsId;
1712
- const globalShareScopeMap = share.getGlobalShareScope();
1713
- let isAllSharedNotUsed = true;
1714
- const needDeleteKeys = [];
1715
- Object.keys(globalShareScopeMap).forEach((instId)=>{
1716
- const shareScopeMap = globalShareScopeMap[instId];
1717
- shareScopeMap && Object.keys(shareScopeMap).forEach((shareScope)=>{
1718
- const shareScopeVal = shareScopeMap[shareScope];
1719
- shareScopeVal && Object.keys(shareScopeVal).forEach((shareName)=>{
1720
- const sharedPkgs = shareScopeVal[shareName];
1721
- sharedPkgs && Object.keys(sharedPkgs).forEach((shareVersion)=>{
1722
- const shared = sharedPkgs[shareVersion];
1723
- if (shared && typeof shared === 'object' && shared.from === remoteInfo.name) {
1724
- if (shared.loaded || shared.loading) {
1725
- shared.useIn = shared.useIn.filter((usedHostName)=>usedHostName !== remoteInfo.name);
1726
- if (shared.useIn.length) {
1727
- isAllSharedNotUsed = false;
1728
- } else {
1729
- needDeleteKeys.push([
1730
- instId,
1731
- shareScope,
1732
- shareName,
1733
- shareVersion
1734
- ]);
1735
- }
1788
+ });
1789
+ if (remoteInsIndex !== -1) {
1790
+ const remoteIns = globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
1791
+ remoteInsId = remoteIns.options.id || remoteInsId;
1792
+ const globalShareScopeMap = share.getGlobalShareScope();
1793
+ let isAllSharedNotUsed = true;
1794
+ const needDeleteKeys = [];
1795
+ Object.keys(globalShareScopeMap).forEach((instId)=>{
1796
+ Object.keys(globalShareScopeMap[instId]).forEach((shareScope)=>{
1797
+ Object.keys(globalShareScopeMap[instId][shareScope]).forEach((shareName)=>{
1798
+ Object.keys(globalShareScopeMap[instId][shareScope][shareName]).forEach((shareVersion)=>{
1799
+ const shared = globalShareScopeMap[instId][shareScope][shareName][shareVersion];
1800
+ if (shared.from === remoteInfo.name) {
1801
+ if (shared.loaded || shared.loading) {
1802
+ shared.useIn = shared.useIn.filter((usedHostName)=>usedHostName !== remoteInfo.name);
1803
+ if (shared.useIn.length) {
1804
+ isAllSharedNotUsed = false;
1736
1805
  } else {
1737
1806
  needDeleteKeys.push([
1738
1807
  instId,
@@ -1741,25 +1810,30 @@ class RemoteHandler {
1741
1810
  shareVersion
1742
1811
  ]);
1743
1812
  }
1813
+ } else {
1814
+ needDeleteKeys.push([
1815
+ instId,
1816
+ shareScope,
1817
+ shareName,
1818
+ shareVersion
1819
+ ]);
1744
1820
  }
1745
- });
1821
+ }
1746
1822
  });
1747
1823
  });
1748
1824
  });
1749
- if (isAllSharedNotUsed) {
1750
- remoteIns.shareScopeMap = {};
1751
- delete globalShareScopeMap[remoteInsId];
1752
- }
1753
- needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion])=>{
1754
- var _globalShareScopeMap_insId_shareScope_shareName, _globalShareScopeMap_insId_shareScope, _globalShareScopeMap_insId;
1755
- (_globalShareScopeMap_insId = globalShareScopeMap[insId]) == null ? true : (_globalShareScopeMap_insId_shareScope = _globalShareScopeMap_insId[shareScope]) == null ? true : (_globalShareScopeMap_insId_shareScope_shareName = _globalShareScopeMap_insId_shareScope[shareName]) == null ? true : delete _globalShareScopeMap_insId_shareScope_shareName[shareVersion];
1756
- });
1757
- globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
1825
+ });
1826
+ if (isAllSharedNotUsed) {
1827
+ remoteIns.shareScopeMap = {};
1828
+ delete globalShareScopeMap[remoteInsId];
1758
1829
  }
1759
- host.moduleCache.delete(remote.name);
1830
+ needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion])=>{
1831
+ var _globalShareScopeMap_insId_shareScope_shareName, _globalShareScopeMap_insId_shareScope, _globalShareScopeMap_insId;
1832
+ (_globalShareScopeMap_insId = globalShareScopeMap[insId]) == null ? true : (_globalShareScopeMap_insId_shareScope = _globalShareScopeMap_insId[shareScope]) == null ? true : (_globalShareScopeMap_insId_shareScope_shareName = _globalShareScopeMap_insId_shareScope[shareName]) == null ? true : delete _globalShareScopeMap_insId_shareScope_shareName[shareVersion];
1833
+ });
1834
+ globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
1760
1835
  }
1761
- } catch (err) {
1762
- console.log('removeRemote fail: ', err);
1836
+ host.moduleCache.delete(remote.name);
1763
1837
  }
1764
1838
  }
1765
1839
  constructor(host){
@@ -1774,7 +1848,6 @@ class RemoteHandler {
1774
1848
  afterPreloadRemote: new AsyncHook()
1775
1849
  });
1776
1850
  this.host = host;
1777
- this.idToRemoteMap = {};
1778
1851
  }
1779
1852
  }
1780
1853
 
@@ -1834,8 +1907,8 @@ class FederationHost {
1834
1907
  async preloadRemote(preloadOptions) {
1835
1908
  return this.remoteHandler.preloadRemote(preloadOptions);
1836
1909
  }
1837
- initShareScopeMap(scopeName, shareScope) {
1838
- this.sharedHandler.initShareScopeMap(scopeName, shareScope);
1910
+ initShareScopeMap(scopeName, shareScope, hostShareScopeMap) {
1911
+ this.sharedHandler.initShareScopeMap(scopeName, shareScope, hostShareScopeMap);
1839
1912
  }
1840
1913
  formatOptions(globalOptions, userOptions) {
1841
1914
  const { shared } = share.formatShareConfigs(globalOptions, userOptions);
@@ -1897,7 +1970,7 @@ class FederationHost {
1897
1970
  // maybe will change, temporarily for internal use only
1898
1971
  initContainer: new AsyncWaterfallHook('initContainer')
1899
1972
  });
1900
- this.version = "0.1.15";
1973
+ this.version = "0.1.18";
1901
1974
  this.moduleCache = new Map();
1902
1975
  this.loaderHook = new PluginSystem({
1903
1976
  // FIXME: may not be suitable , not open to the public yet
@@ -1986,9 +2059,6 @@ function registerPlugins(...args) {
1986
2059
  // eslint-disable-next-line prefer-spread
1987
2060
  return FederationInstance.registerPlugins.apply(FederationInstance, args);
1988
2061
  }
1989
- function getInstance() {
1990
- return FederationInstance;
1991
- }
1992
2062
  // Inject for debug
1993
2063
  share.setGlobalFederationConstructor(FederationHost);
1994
2064
 
@@ -2002,7 +2072,6 @@ Object.defineProperty(exports, 'loadScriptNode', {
2002
2072
  get: function () { return sdk.loadScriptNode; }
2003
2073
  });
2004
2074
  exports.FederationHost = FederationHost;
2005
- exports.getInstance = getInstance;
2006
2075
  exports.getRemoteEntry = getRemoteEntry;
2007
2076
  exports.getRemoteInfo = getRemoteInfo;
2008
2077
  exports.init = init;
package/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
- import { g as getGlobalHostPlugins, a as globalLoading, D as DEFAULT_REMOTE_TYPE, b as DEFAULT_SCOPE, c as getRemoteEntryExports, d as assert, s as safeToString, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as getRemoteEntryInfoFromSnapshot, m as isBrowserEnv, n as getInfoWithoutType, o as getPreloaded, p as setPreloaded, q as getRegisteredShare, r as arrayOptions, t as getGlobalSnapshotInfoByModuleInfo, u as addGlobalSnapshot, v as setGlobalSnapshotInfoByModuleInfo, x as getGlobalSnapshot, G as Global, y as formatShareConfigs, z as getTargetSharedOptions, A as getGlobalShareScope, B as addUniqueItem, C as getBuilderId, E as setGlobalFederationConstructor, F as getGlobalFederationInstance, H as getGlobalFederationConstructor, I as setGlobalFederationInstance } from './share.esm.js';
2
- export { J as registerGlobalPlugins } from './share.esm.js';
3
- import { loadScriptNode, loadScript, composeKeyWithSeparator, createLink, getResourceUrl, isManifestProvider, generateSnapshotFromManifest, warn as warn$1, isBrowserEnv as isBrowserEnv$1 } from '@module-federation/sdk';
1
+ import { g as getGlobalHostPlugins, a as globalLoading, D as DEFAULT_REMOTE_TYPE, b as DEFAULT_SCOPE, c as getRemoteEntryExports, d as assert, s as safeToString, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as isBrowserEnv, m as getInfoWithoutType, n as getPreloaded, o as setPreloaded, p as getRegisteredShare, q as arrayOptions, r as getGlobalSnapshotInfoByModuleInfo, t as addGlobalSnapshot, u as setGlobalSnapshotInfoByModuleInfo, v as getGlobalSnapshot, G as Global, x as formatShareConfigs, y as getTargetSharedOptions, z as getGlobalShareScope, A as addUniqueItem, B as getBuilderId, C as setGlobalFederationConstructor, E as getGlobalFederationInstance, F as getGlobalFederationConstructor, H as setGlobalFederationInstance } from './share.esm.js';
2
+ export { I as registerGlobalPlugins } from './share.esm.js';
3
+ import { loadScriptNode, loadScript, composeKeyWithSeparator, createLink, createScript, getResourceUrl, isManifestProvider, generateSnapshotFromManifest, warn as warn$1, isBrowserEnv as isBrowserEnv$1 } from '@module-federation/sdk';
4
4
  export { loadScript, loadScriptNode } from '@module-federation/sdk';
5
5
 
6
6
  // Function to match a remote with its name and expose
@@ -170,7 +170,10 @@ async function getRemoteEntry({ remoteEntryExports, remoteInfo, createScriptHook
170
170
  return remoteEntryExports;
171
171
  }
172
172
  if (!globalLoading[uniqueKey]) {
173
- if (type === 'esm') {
173
+ if ([
174
+ 'esm',
175
+ 'module'
176
+ ].includes(type)) {
174
177
  globalLoading[uniqueKey] = loadEsmEntry({
175
178
  entry,
176
179
  remoteEntryExports
@@ -241,7 +244,7 @@ let Module = class Module {
241
244
  return this.remoteEntryExports;
242
245
  }
243
246
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
244
- async get(expose, options) {
247
+ async get(id, expose, options) {
245
248
  const { loadFactory = true } = options || {
246
249
  loadFactory: true
247
250
  };
@@ -259,14 +262,14 @@ let Module = class Module {
259
262
  version: this.remoteInfo.version || ''
260
263
  };
261
264
  // Help to find host instance
262
- Object.defineProperty(remoteEntryInitOptions, 'hostId', {
263
- value: this.host.options.id || this.host.name,
265
+ Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
266
+ value: localShareScopeMap,
264
267
  // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
265
268
  enumerable: false
266
269
  });
267
270
  const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
268
271
  shareScope,
269
- // @ts-ignore hostId will be set by Object.defineProperty
272
+ // @ts-ignore shareScopeMap will be set by Object.defineProperty
270
273
  remoteEntryInitOptions,
271
274
  initScope,
272
275
  remoteInfo: this.remoteInfo,
@@ -282,12 +285,38 @@ let Module = class Module {
282
285
  // get exposeGetter
283
286
  const moduleFactory = await remoteEntryExports.get(expose);
284
287
  assert(moduleFactory, `${getFMId(this.remoteInfo)} remote don't export ${expose}.`);
288
+ const wrapModuleFactory = this.wraperFactory(moduleFactory, id);
285
289
  if (!loadFactory) {
286
- return moduleFactory;
290
+ return wrapModuleFactory;
287
291
  }
288
- const exposeContent = await moduleFactory();
292
+ const exposeContent = await wrapModuleFactory();
289
293
  return exposeContent;
290
294
  }
295
+ wraperFactory(moduleFactory, id) {
296
+ function defineModuleId(res, id) {
297
+ if (res && typeof res === 'object' && !Object.getOwnPropertyDescriptor(res, Symbol.for('mf_module_id'))) {
298
+ Object.defineProperty(res, Symbol.for('mf_module_id'), {
299
+ value: id,
300
+ enumerable: false
301
+ });
302
+ }
303
+ }
304
+ if (moduleFactory instanceof Promise) {
305
+ return async ()=>{
306
+ const res = await moduleFactory();
307
+ // This parameter is used for bridge debugging
308
+ defineModuleId(res, id);
309
+ return res;
310
+ };
311
+ } else {
312
+ return ()=>{
313
+ const res = moduleFactory();
314
+ // This parameter is used for bridge debugging
315
+ defineModuleId(res, id);
316
+ return res;
317
+ };
318
+ }
319
+ }
291
320
  constructor({ remoteInfo, host }){
292
321
  this.inited = false;
293
322
  this.lib = undefined;
@@ -532,7 +561,8 @@ function normalizePreloadExposes(exposes) {
532
561
  return expose;
533
562
  });
534
563
  }
535
- function preloadAssets(remoteInfo, host, assets) {
564
+ function preloadAssets(remoteInfo, host, assets, // It is used to distinguish preload from load remote parallel loading
565
+ useLinkPreload = true) {
536
566
  const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
537
567
  if (host.options.inBrowser) {
538
568
  entryAssets.forEach((asset)=>{
@@ -584,40 +614,94 @@ function preloadAssets(remoteInfo, host, assets) {
584
614
  });
585
615
  }
586
616
  });
587
- const fragment = document.createDocumentFragment();
588
- cssAssets.forEach((cssUrl)=>{
589
- const { link: cssEl, needAttach } = createLink(cssUrl, ()=>{}, {
590
- rel: 'preload',
591
- as: 'style'
592
- }, (url)=>{
593
- const res = host.loaderHook.lifecycle.createLink.emit({
594
- url
617
+ if (useLinkPreload) {
618
+ cssAssets.forEach((cssUrl)=>{
619
+ const { link: cssEl, needAttach } = createLink({
620
+ url: cssUrl,
621
+ cb: ()=>{},
622
+ attrs: {
623
+ rel: 'preload',
624
+ as: 'style',
625
+ crossorigin: 'anonymous'
626
+ },
627
+ createLinkHook: (url)=>{
628
+ const res = host.loaderHook.lifecycle.createLink.emit({
629
+ url
630
+ });
631
+ if (res instanceof HTMLLinkElement) {
632
+ return res;
633
+ }
634
+ return;
635
+ }
595
636
  });
596
- if (res instanceof HTMLLinkElement) {
597
- return res;
598
- }
599
- return;
637
+ needAttach && document.head.appendChild(cssEl);
600
638
  });
601
- needAttach && fragment.appendChild(cssEl);
602
- });
603
- jsAssetsWithoutEntry.forEach((jsUrl)=>{
604
- const { link: linkEl, needAttach } = createLink(jsUrl, ()=>{
605
- // noop
606
- }, {
607
- rel: 'preload',
608
- as: 'script'
609
- }, (url)=>{
610
- const res = host.loaderHook.lifecycle.createLink.emit({
611
- url
639
+ } else {
640
+ cssAssets.forEach((cssUrl)=>{
641
+ const { link: cssEl, needAttach } = createLink({
642
+ url: cssUrl,
643
+ cb: ()=>{},
644
+ attrs: {
645
+ rel: 'stylesheet',
646
+ type: 'text/css'
647
+ },
648
+ createLinkHook: (url)=>{
649
+ const res = host.loaderHook.lifecycle.createLink.emit({
650
+ url
651
+ });
652
+ if (res instanceof HTMLLinkElement) {
653
+ return res;
654
+ }
655
+ return;
656
+ }
612
657
  });
613
- if (res instanceof HTMLLinkElement) {
614
- return res;
615
- }
616
- return;
658
+ needAttach && document.head.appendChild(cssEl);
617
659
  });
618
- needAttach && document.head.appendChild(linkEl);
619
- });
620
- document.head.appendChild(fragment);
660
+ }
661
+ if (useLinkPreload) {
662
+ jsAssetsWithoutEntry.forEach((jsUrl)=>{
663
+ const { link: linkEl, needAttach } = createLink({
664
+ url: jsUrl,
665
+ cb: ()=>{},
666
+ attrs: {
667
+ rel: 'preload',
668
+ as: 'script',
669
+ crossorigin: 'anonymous'
670
+ },
671
+ createLinkHook: (url)=>{
672
+ const res = host.loaderHook.lifecycle.createLink.emit({
673
+ url
674
+ });
675
+ if (res instanceof HTMLLinkElement) {
676
+ return res;
677
+ }
678
+ return;
679
+ }
680
+ });
681
+ needAttach && document.head.appendChild(linkEl);
682
+ });
683
+ } else {
684
+ jsAssetsWithoutEntry.forEach((jsUrl)=>{
685
+ const { script: scriptEl, needAttach } = createScript({
686
+ url: jsUrl,
687
+ cb: ()=>{},
688
+ attrs: {
689
+ crossorigin: 'anonymous',
690
+ fetchpriority: 'high'
691
+ },
692
+ createScriptHook: (url)=>{
693
+ const res = host.loaderHook.lifecycle.createScript.emit({
694
+ url
695
+ });
696
+ if (res instanceof HTMLScriptElement) {
697
+ return res;
698
+ }
699
+ return;
700
+ }
701
+ });
702
+ needAttach && document.head.appendChild(scriptEl);
703
+ });
704
+ }
621
705
  }
622
706
  }
623
707
 
@@ -636,16 +720,16 @@ function _extends$4() {
636
720
  return _extends$4.apply(this, arguments);
637
721
  }
638
722
  function assignRemoteInfo(remoteInfo, remoteSnapshot) {
639
- const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);
640
- if (!remoteEntryInfo.url) {
641
- error(`The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`);
723
+ if (!('remoteEntry' in remoteSnapshot) || !remoteSnapshot.remoteEntry) {
724
+ error(`The attribute remoteEntry of ${name} must not be undefined.`);
642
725
  }
643
- let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);
726
+ const { remoteEntry } = remoteSnapshot;
727
+ let entryUrl = getResourceUrl(remoteSnapshot, remoteEntry);
644
728
  if (!isBrowserEnv() && !entryUrl.startsWith('http')) {
645
729
  entryUrl = `https:${entryUrl}`;
646
730
  }
647
- remoteInfo.type = remoteEntryInfo.type;
648
- remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
731
+ remoteInfo.type = remoteSnapshot.remoteEntryType;
732
+ remoteInfo.entryGlobalName = remoteSnapshot.globalName;
649
733
  remoteInfo.entry = entryUrl;
650
734
  remoteInfo.version = remoteSnapshot.version;
651
735
  remoteInfo.buildVersion = remoteSnapshot.buildVersion;
@@ -680,7 +764,7 @@ function snapshotPlugin() {
680
764
  globalSnapshot
681
765
  });
682
766
  if (assets) {
683
- preloadAssets(remoteInfo, origin, assets);
767
+ preloadAssets(remoteInfo, origin, assets, false);
684
768
  }
685
769
  return _extends$4({}, args, {
686
770
  remoteSnapshot
@@ -770,7 +854,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
770
854
  return;
771
855
  }
772
856
  }
773
- const remoteEntryUrl = getResourceUrl(moduleInfoSnapshot, getRemoteEntryInfoFromSnapshot(moduleInfoSnapshot).url);
857
+ const remoteEntryUrl = getResourceUrl(moduleInfoSnapshot, 'remoteEntry' in moduleInfoSnapshot ? moduleInfoSnapshot.remoteEntry : '');
774
858
  if (remoteEntryUrl) {
775
859
  entryAssets.push({
776
860
  name: remoteInfo.name,
@@ -976,13 +1060,12 @@ class SnapshotHandler {
976
1060
  // global snapshot includes manifest or module info includes manifest
977
1061
  if (globalRemoteSnapshot) {
978
1062
  if (isManifestProvider(globalRemoteSnapshot)) {
979
- const remoteEntry = isBrowserEnv() ? globalRemoteSnapshot.remoteEntry : globalRemoteSnapshot.ssrRemoteEntry || globalRemoteSnapshot.remoteEntry || '';
980
- const moduleSnapshot = await this.getManifestJson(remoteEntry, moduleInfo, {});
1063
+ const moduleSnapshot = await this.getManifestJson(globalRemoteSnapshot.remoteEntry, moduleInfo, {});
981
1064
  // eslint-disable-next-line @typescript-eslint/no-shadow
982
1065
  const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(_extends$3({}, moduleInfo, {
983
1066
  // The global remote may be overridden
984
1067
  // Therefore, set the snapshot key to the global address of the actual request
985
- entry: remoteEntry
1068
+ entry: globalRemoteSnapshot.remoteEntry
986
1069
  }), moduleSnapshot);
987
1070
  return {
988
1071
  remoteSnapshot: moduleSnapshot,
@@ -1394,13 +1477,15 @@ class SharedHandler {
1394
1477
  2. The ${pkgName} share was not registered with the 'lib' attribute.\n
1395
1478
  `);
1396
1479
  }
1397
- initShareScopeMap(scopeName, shareScope) {
1480
+ initShareScopeMap(scopeName, shareScope, hostShareScopeMap) {
1398
1481
  const { host } = this;
1399
1482
  this.shareScopeMap[scopeName] = shareScope;
1400
1483
  this.hooks.lifecycle.initContainerShareScopeMap.emit({
1401
1484
  shareScope,
1402
1485
  options: host.options,
1403
- origin: host
1486
+ origin: host,
1487
+ scopeName,
1488
+ hostShareScopeMap
1404
1489
  });
1405
1490
  }
1406
1491
  setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
@@ -1451,7 +1536,7 @@ class SharedHandler {
1451
1536
  loadShare: new AsyncHook(),
1452
1537
  resolveShare: new SyncWaterfallHook('resolveShare'),
1453
1538
  // maybe will change, temporarily for internal use only
1454
- initContainerShareScopeMap: new AsyncWaterfallHook('initContainer')
1539
+ initContainerShareScopeMap: new SyncWaterfallHook('initContainerShareScopeMap')
1455
1540
  });
1456
1541
  this.host = host;
1457
1542
  this.shareScopeMap = {};
@@ -1502,7 +1587,7 @@ class RemoteHandler {
1502
1587
  id
1503
1588
  });
1504
1589
  const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
1505
- const moduleOrFactory = await module.get(expose, options);
1590
+ const moduleOrFactory = await module.get(idRes, expose, options);
1506
1591
  const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
1507
1592
  id: idRes,
1508
1593
  pkgNameOrAlias,
@@ -1514,10 +1599,6 @@ class RemoteHandler {
1514
1599
  moduleInstance: module,
1515
1600
  origin: host
1516
1601
  });
1517
- this.idToRemoteMap[id] = {
1518
- name: remote.name,
1519
- expose
1520
- };
1521
1602
  if (typeof moduleWrapper === 'function') {
1522
1603
  return moduleWrapper;
1523
1604
  }
@@ -1676,61 +1757,49 @@ class RemoteHandler {
1676
1757
  }
1677
1758
  }
1678
1759
  removeRemote(remote) {
1679
- try {
1680
- const { host } = this;
1681
- const { name } = remote;
1682
- const remoteIndex = host.options.remotes.findIndex((item)=>item.name === name);
1683
- if (remoteIndex !== -1) {
1684
- host.options.remotes.splice(remoteIndex, 1);
1760
+ const { host } = this;
1761
+ const { name } = remote;
1762
+ const remoteIndex = host.options.remotes.findIndex((item)=>item.name === name);
1763
+ if (remoteIndex !== -1) {
1764
+ host.options.remotes.splice(remoteIndex, 1);
1765
+ }
1766
+ const loadedModule = host.moduleCache.get(remote.name);
1767
+ if (loadedModule) {
1768
+ var _Object_getOwnPropertyDescriptor;
1769
+ const remoteInfo = loadedModule.remoteInfo;
1770
+ const key = remoteInfo.entryGlobalName;
1771
+ if (globalThis[key] && ((_Object_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(globalThis, key)) == null ? void 0 : _Object_getOwnPropertyDescriptor.configurable)) {
1772
+ delete globalThis[key];
1685
1773
  }
1686
- const loadedModule = host.moduleCache.get(remote.name);
1687
- if (loadedModule) {
1688
- const remoteInfo = loadedModule.remoteInfo;
1689
- const key = remoteInfo.entryGlobalName;
1690
- if (globalThis[key]) {
1691
- delete globalThis[key];
1692
- }
1693
- const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
1694
- if (globalLoading[remoteEntryUniqueKey]) {
1695
- delete globalLoading[remoteEntryUniqueKey];
1774
+ const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
1775
+ if (globalLoading[remoteEntryUniqueKey]) {
1776
+ delete globalLoading[remoteEntryUniqueKey];
1777
+ }
1778
+ // delete un loaded shared and instance
1779
+ let remoteInsId = remoteInfo.buildVersion ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
1780
+ const remoteInsIndex = globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins)=>{
1781
+ if (remoteInfo.buildVersion) {
1782
+ return ins.options.id === remoteInsId;
1783
+ } else {
1784
+ return ins.name === remoteInsId;
1696
1785
  }
1697
- host.snapshotHandler.manifestCache.delete(remoteInfo.entry);
1698
- // delete un loaded shared and instance
1699
- let remoteInsId = remoteInfo.buildVersion ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
1700
- const remoteInsIndex = globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins)=>{
1701
- if (remoteInfo.buildVersion) {
1702
- return ins.options.id === remoteInsId;
1703
- } else {
1704
- return ins.name === remoteInsId;
1705
- }
1706
- });
1707
- if (remoteInsIndex !== -1) {
1708
- const remoteIns = globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
1709
- remoteInsId = remoteIns.options.id || remoteInsId;
1710
- const globalShareScopeMap = getGlobalShareScope();
1711
- let isAllSharedNotUsed = true;
1712
- const needDeleteKeys = [];
1713
- Object.keys(globalShareScopeMap).forEach((instId)=>{
1714
- const shareScopeMap = globalShareScopeMap[instId];
1715
- shareScopeMap && Object.keys(shareScopeMap).forEach((shareScope)=>{
1716
- const shareScopeVal = shareScopeMap[shareScope];
1717
- shareScopeVal && Object.keys(shareScopeVal).forEach((shareName)=>{
1718
- const sharedPkgs = shareScopeVal[shareName];
1719
- sharedPkgs && Object.keys(sharedPkgs).forEach((shareVersion)=>{
1720
- const shared = sharedPkgs[shareVersion];
1721
- if (shared && typeof shared === 'object' && shared.from === remoteInfo.name) {
1722
- if (shared.loaded || shared.loading) {
1723
- shared.useIn = shared.useIn.filter((usedHostName)=>usedHostName !== remoteInfo.name);
1724
- if (shared.useIn.length) {
1725
- isAllSharedNotUsed = false;
1726
- } else {
1727
- needDeleteKeys.push([
1728
- instId,
1729
- shareScope,
1730
- shareName,
1731
- shareVersion
1732
- ]);
1733
- }
1786
+ });
1787
+ if (remoteInsIndex !== -1) {
1788
+ const remoteIns = globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
1789
+ remoteInsId = remoteIns.options.id || remoteInsId;
1790
+ const globalShareScopeMap = getGlobalShareScope();
1791
+ let isAllSharedNotUsed = true;
1792
+ const needDeleteKeys = [];
1793
+ Object.keys(globalShareScopeMap).forEach((instId)=>{
1794
+ Object.keys(globalShareScopeMap[instId]).forEach((shareScope)=>{
1795
+ Object.keys(globalShareScopeMap[instId][shareScope]).forEach((shareName)=>{
1796
+ Object.keys(globalShareScopeMap[instId][shareScope][shareName]).forEach((shareVersion)=>{
1797
+ const shared = globalShareScopeMap[instId][shareScope][shareName][shareVersion];
1798
+ if (shared.from === remoteInfo.name) {
1799
+ if (shared.loaded || shared.loading) {
1800
+ shared.useIn = shared.useIn.filter((usedHostName)=>usedHostName !== remoteInfo.name);
1801
+ if (shared.useIn.length) {
1802
+ isAllSharedNotUsed = false;
1734
1803
  } else {
1735
1804
  needDeleteKeys.push([
1736
1805
  instId,
@@ -1739,25 +1808,30 @@ class RemoteHandler {
1739
1808
  shareVersion
1740
1809
  ]);
1741
1810
  }
1811
+ } else {
1812
+ needDeleteKeys.push([
1813
+ instId,
1814
+ shareScope,
1815
+ shareName,
1816
+ shareVersion
1817
+ ]);
1742
1818
  }
1743
- });
1819
+ }
1744
1820
  });
1745
1821
  });
1746
1822
  });
1747
- if (isAllSharedNotUsed) {
1748
- remoteIns.shareScopeMap = {};
1749
- delete globalShareScopeMap[remoteInsId];
1750
- }
1751
- needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion])=>{
1752
- var _globalShareScopeMap_insId_shareScope_shareName, _globalShareScopeMap_insId_shareScope, _globalShareScopeMap_insId;
1753
- (_globalShareScopeMap_insId = globalShareScopeMap[insId]) == null ? true : (_globalShareScopeMap_insId_shareScope = _globalShareScopeMap_insId[shareScope]) == null ? true : (_globalShareScopeMap_insId_shareScope_shareName = _globalShareScopeMap_insId_shareScope[shareName]) == null ? true : delete _globalShareScopeMap_insId_shareScope_shareName[shareVersion];
1754
- });
1755
- globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
1823
+ });
1824
+ if (isAllSharedNotUsed) {
1825
+ remoteIns.shareScopeMap = {};
1826
+ delete globalShareScopeMap[remoteInsId];
1756
1827
  }
1757
- host.moduleCache.delete(remote.name);
1828
+ needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion])=>{
1829
+ var _globalShareScopeMap_insId_shareScope_shareName, _globalShareScopeMap_insId_shareScope, _globalShareScopeMap_insId;
1830
+ (_globalShareScopeMap_insId = globalShareScopeMap[insId]) == null ? true : (_globalShareScopeMap_insId_shareScope = _globalShareScopeMap_insId[shareScope]) == null ? true : (_globalShareScopeMap_insId_shareScope_shareName = _globalShareScopeMap_insId_shareScope[shareName]) == null ? true : delete _globalShareScopeMap_insId_shareScope_shareName[shareVersion];
1831
+ });
1832
+ globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
1758
1833
  }
1759
- } catch (err) {
1760
- console.log('removeRemote fail: ', err);
1834
+ host.moduleCache.delete(remote.name);
1761
1835
  }
1762
1836
  }
1763
1837
  constructor(host){
@@ -1772,7 +1846,6 @@ class RemoteHandler {
1772
1846
  afterPreloadRemote: new AsyncHook()
1773
1847
  });
1774
1848
  this.host = host;
1775
- this.idToRemoteMap = {};
1776
1849
  }
1777
1850
  }
1778
1851
 
@@ -1832,8 +1905,8 @@ class FederationHost {
1832
1905
  async preloadRemote(preloadOptions) {
1833
1906
  return this.remoteHandler.preloadRemote(preloadOptions);
1834
1907
  }
1835
- initShareScopeMap(scopeName, shareScope) {
1836
- this.sharedHandler.initShareScopeMap(scopeName, shareScope);
1908
+ initShareScopeMap(scopeName, shareScope, hostShareScopeMap) {
1909
+ this.sharedHandler.initShareScopeMap(scopeName, shareScope, hostShareScopeMap);
1837
1910
  }
1838
1911
  formatOptions(globalOptions, userOptions) {
1839
1912
  const { shared } = formatShareConfigs(globalOptions, userOptions);
@@ -1895,7 +1968,7 @@ class FederationHost {
1895
1968
  // maybe will change, temporarily for internal use only
1896
1969
  initContainer: new AsyncWaterfallHook('initContainer')
1897
1970
  });
1898
- this.version = "0.1.15";
1971
+ this.version = "0.1.18";
1899
1972
  this.moduleCache = new Map();
1900
1973
  this.loaderHook = new PluginSystem({
1901
1974
  // FIXME: may not be suitable , not open to the public yet
@@ -1984,10 +2057,7 @@ function registerPlugins(...args) {
1984
2057
  // eslint-disable-next-line prefer-spread
1985
2058
  return FederationInstance.registerPlugins.apply(FederationInstance, args);
1986
2059
  }
1987
- function getInstance() {
1988
- return FederationInstance;
1989
- }
1990
2060
  // Inject for debug
1991
2061
  setGlobalFederationConstructor(FederationHost);
1992
2062
 
1993
- export { FederationHost, getInstance, getRemoteEntry, getRemoteInfo, init, loadRemote, loadShare, loadShareSync, preloadRemote, registerPlugins, registerRemotes };
2063
+ export { FederationHost, getRemoteEntry, getRemoteInfo, init, loadRemote, loadShare, loadShareSync, preloadRemote, registerPlugins, registerRemotes };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/runtime",
3
- "version": "0.1.15",
3
+ "version": "0.1.18",
4
4
  "author": "zhouxiao <codingzx@gmail.com>",
5
5
  "main": "./index.cjs.js",
6
6
  "module": "./index.esm.js",
package/dist/share.cjs.js CHANGED
@@ -76,28 +76,6 @@ function arrayOptions(options) {
76
76
  options
77
77
  ];
78
78
  }
79
- function getRemoteEntryInfoFromSnapshot(snapshot) {
80
- const defaultRemoteEntryInfo = {
81
- url: '',
82
- type: 'global',
83
- globalName: ''
84
- };
85
- if (isBrowserEnv()) {
86
- return 'remoteEntry' in snapshot ? {
87
- url: snapshot.remoteEntry,
88
- type: snapshot.remoteEntryType,
89
- globalName: snapshot.globalName
90
- } : defaultRemoteEntryInfo;
91
- }
92
- if ('ssrRemoteEntry' in snapshot) {
93
- return {
94
- url: snapshot.ssrRemoteEntry || defaultRemoteEntryInfo.url,
95
- type: snapshot.ssrRemoteEntryType || defaultRemoteEntryInfo.type,
96
- globalName: snapshot.globalName
97
- };
98
- }
99
- return defaultRemoteEntryInfo;
100
- }
101
79
 
102
80
  function _extends$1() {
103
81
  _extends$1 = Object.assign || function(target) {
@@ -212,7 +190,7 @@ function getGlobalFederationConstructor() {
212
190
  function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
213
191
  if (isDebug) {
214
192
  globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
215
- globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.1.15";
193
+ globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.1.18";
216
194
  }
217
195
  }
218
196
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -915,7 +893,6 @@ exports.getInfoWithoutType = getInfoWithoutType;
915
893
  exports.getPreloaded = getPreloaded;
916
894
  exports.getRegisteredShare = getRegisteredShare;
917
895
  exports.getRemoteEntryExports = getRemoteEntryExports;
918
- exports.getRemoteEntryInfoFromSnapshot = getRemoteEntryInfoFromSnapshot;
919
896
  exports.getTargetSharedOptions = getTargetSharedOptions;
920
897
  exports.getTargetSnapshotInfoByModuleInfo = getTargetSnapshotInfoByModuleInfo;
921
898
  exports.globalLoading = globalLoading;
package/dist/share.esm.js CHANGED
@@ -74,28 +74,6 @@ function arrayOptions(options) {
74
74
  options
75
75
  ];
76
76
  }
77
- function getRemoteEntryInfoFromSnapshot(snapshot) {
78
- const defaultRemoteEntryInfo = {
79
- url: '',
80
- type: 'global',
81
- globalName: ''
82
- };
83
- if (isBrowserEnv()) {
84
- return 'remoteEntry' in snapshot ? {
85
- url: snapshot.remoteEntry,
86
- type: snapshot.remoteEntryType,
87
- globalName: snapshot.globalName
88
- } : defaultRemoteEntryInfo;
89
- }
90
- if ('ssrRemoteEntry' in snapshot) {
91
- return {
92
- url: snapshot.ssrRemoteEntry || defaultRemoteEntryInfo.url,
93
- type: snapshot.ssrRemoteEntryType || defaultRemoteEntryInfo.type,
94
- globalName: snapshot.globalName
95
- };
96
- }
97
- return defaultRemoteEntryInfo;
98
- }
99
77
 
100
78
  function _extends$1() {
101
79
  _extends$1 = Object.assign || function(target) {
@@ -210,7 +188,7 @@ function getGlobalFederationConstructor() {
210
188
  function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
211
189
  if (isDebug) {
212
190
  globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
213
- globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.1.15";
191
+ globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.1.18";
214
192
  }
215
193
  }
216
194
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -892,4 +870,4 @@ function getTargetSharedOptions(options) {
892
870
  return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
893
871
  }
894
872
 
895
- export { getGlobalShareScope as A, addUniqueItem as B, getBuilderId as C, DEFAULT_REMOTE_TYPE as D, setGlobalFederationConstructor as E, getGlobalFederationInstance as F, Global as G, getGlobalFederationConstructor as H, setGlobalFederationInstance as I, registerGlobalPlugins as J, nativeGlobal as K, resetFederationGlobalInfo as L, getTargetSnapshotInfoByModuleInfo as M, globalLoading as a, DEFAULT_SCOPE as b, getRemoteEntryExports as c, assert as d, getFMId as e, error as f, getGlobalHostPlugins as g, isPlainObject as h, isObject as i, isRemoteInfoWithEntry as j, isPureRemoteEntry as k, getRemoteEntryInfoFromSnapshot as l, isBrowserEnv as m, getInfoWithoutType as n, getPreloaded as o, setPreloaded as p, getRegisteredShare as q, arrayOptions as r, safeToString as s, getGlobalSnapshotInfoByModuleInfo as t, addGlobalSnapshot as u, setGlobalSnapshotInfoByModuleInfo as v, warn as w, getGlobalSnapshot as x, formatShareConfigs as y, getTargetSharedOptions as z };
873
+ export { addUniqueItem as A, getBuilderId as B, setGlobalFederationConstructor as C, DEFAULT_REMOTE_TYPE as D, getGlobalFederationInstance as E, getGlobalFederationConstructor as F, Global as G, setGlobalFederationInstance as H, registerGlobalPlugins as I, nativeGlobal as J, resetFederationGlobalInfo as K, getTargetSnapshotInfoByModuleInfo as L, globalLoading as a, DEFAULT_SCOPE as b, getRemoteEntryExports as c, assert as d, getFMId as e, error as f, getGlobalHostPlugins as g, isPlainObject as h, isObject as i, isRemoteInfoWithEntry as j, isPureRemoteEntry as k, isBrowserEnv as l, getInfoWithoutType as m, getPreloaded as n, setPreloaded as o, getRegisteredShare as p, arrayOptions as q, getGlobalSnapshotInfoByModuleInfo as r, safeToString as s, addGlobalSnapshot as t, setGlobalSnapshotInfoByModuleInfo as u, getGlobalSnapshot as v, warn as w, formatShareConfigs as x, getTargetSharedOptions as y, getGlobalShareScope as z };
@@ -74,7 +74,7 @@ export declare class FederationHost {
74
74
  from: 'build' | 'runtime';
75
75
  }): Promise<T | null>;
76
76
  preloadRemote(preloadOptions: Array<PreloadRemoteArgs>): Promise<void>;
77
- initShareScopeMap(scopeName: string, shareScope: ShareScopeMap[string]): void;
77
+ initShareScopeMap(scopeName: string, shareScope: ShareScopeMap[string], hostShareScopeMap?: ShareScopeMap): void;
78
78
  private formatOptions;
79
79
  registerPlugins(plugins: UserOptions['plugins']): void;
80
80
  registerRemotes(remotes: Remote[], options?: {
@@ -13,4 +13,3 @@ export declare function loadShareSync<T>(...args: Parameters<FederationHost['loa
13
13
  export declare function preloadRemote(...args: Parameters<FederationHost['preloadRemote']>): ReturnType<FederationHost['preloadRemote']>;
14
14
  export declare function registerRemotes(...args: Parameters<FederationHost['registerRemotes']>): ReturnType<FederationHost['registerRemotes']>;
15
15
  export declare function registerPlugins(...args: Parameters<FederationHost['registerPlugins']>): ReturnType<FederationHost['registerRemotes']>;
16
- export declare function getInstance(): FederationHost | null;
@@ -12,8 +12,9 @@ declare class Module {
12
12
  host: FederationHost;
13
13
  });
14
14
  getEntry(): Promise<RemoteEntryExports>;
15
- get(expose: string, options?: {
15
+ get(id: string, expose: string, options?: {
16
16
  loadFactory?: boolean;
17
17
  }): Promise<any>;
18
+ private wraperFactory;
18
19
  }
19
20
  export { Module };
@@ -39,10 +39,6 @@ export declare class SnapshotHandler {
39
39
  remoteSnapshot: ModuleInfo;
40
40
  globalSnapshot: GlobalModuleInfo;
41
41
  }> | never;
42
- getGlobalRemoteInfo(moduleInfo: Remote): {
43
- hostGlobalSnapshot: ModuleInfo | undefined;
44
- globalSnapshot: ReturnType<typeof getGlobalSnapshot>;
45
- remoteSnapshot: GlobalModuleInfo[string] | undefined;
46
- };
42
+ private getGlobalRemoteInfo;
47
43
  private getManifestJson;
48
44
  }
@@ -15,10 +15,6 @@ export interface LoadRemoteMatch {
15
15
  }
16
16
  export declare class RemoteHandler {
17
17
  host: FederationHost;
18
- idToRemoteMap: Record<string, {
19
- name: string;
20
- expose: string;
21
- }>;
22
18
  hooks: PluginSystem<{
23
19
  beforeRequest: AsyncWaterfallHook<{
24
20
  id: string;
@@ -23,10 +23,12 @@ export declare class SharedHandler {
23
23
  GlobalFederation: Federation;
24
24
  resolver: () => Shared | undefined;
25
25
  }>;
26
- initContainerShareScopeMap: AsyncWaterfallHook<{
26
+ initContainerShareScopeMap: SyncWaterfallHook<{
27
27
  shareScope: ShareScopeMap[string];
28
28
  options: Options;
29
29
  origin: FederationHost;
30
+ scopeName: string;
31
+ hostShareScopeMap?: ShareScopeMap | undefined;
30
32
  }>;
31
33
  }>;
32
34
  constructor(host: FederationHost);
@@ -50,7 +52,7 @@ export declare class SharedHandler {
50
52
  customShareInfo?: Partial<Shared>;
51
53
  resolver?: (sharedOptions: ShareInfos[string]) => Shared;
52
54
  }): () => T | never;
53
- initShareScopeMap(scopeName: string, shareScope: ShareScopeMap[string]): void;
55
+ initShareScopeMap(scopeName: string, shareScope: ShareScopeMap[string], hostShareScopeMap?: ShareScopeMap): void;
54
56
  private setShared;
55
57
  private _setGlobalShareScopeMap;
56
58
  }
@@ -94,7 +94,7 @@ export type LoadModuleOptions = {
94
94
  };
95
95
  export type RemoteEntryInitOptions = {
96
96
  version: string;
97
- hostId: string;
97
+ shareScopeMap: ShareScopeMap;
98
98
  };
99
99
  export type InitScope = Array<Record<string, never>>;
100
100
  export type RemoteEntryExports = {
@@ -3,4 +3,4 @@ import { FederationHost } from '../core';
3
3
  export declare function defaultPreloadArgs(preloadConfig: PreloadRemoteArgs | depsPreloadArg): PreloadConfig;
4
4
  export declare function formatPreloadArgs(remotes: Array<Remote>, preloadArgs: Array<PreloadRemoteArgs>): PreloadOptions;
5
5
  export declare function normalizePreloadExposes(exposes?: string[]): string[];
6
- export declare function preloadAssets(remoteInfo: RemoteInfo, host: FederationHost, assets: PreloadAssets): void;
6
+ export declare function preloadAssets(remoteInfo: RemoteInfo, host: FederationHost, assets: PreloadAssets, useLinkPreload?: boolean): void;
@@ -1,4 +1,4 @@
1
- import type { RemoteWithEntry, ModuleInfo, RemoteEntryType } from '@module-federation/sdk';
1
+ import type { RemoteWithEntry } from '@module-federation/sdk';
2
2
  import { Remote, RemoteInfoOptionalVersion } from '../type';
3
3
  export declare function addUniqueItem(arr: Array<string>, item: string): Array<string>;
4
4
  export declare function getFMId(remoteInfo: RemoteInfoOptionalVersion | RemoteWithEntry): string;
@@ -11,8 +11,3 @@ export declare const objectToString: () => string;
11
11
  export declare function isPlainObject(val: any): val is object;
12
12
  export declare function isStaticResourcesEqual(url1: string, url2: string): boolean;
13
13
  export declare function arrayOptions<T>(options: T | Array<T>): Array<T>;
14
- export declare function getRemoteEntryInfoFromSnapshot(snapshot: ModuleInfo): {
15
- url: string;
16
- type: RemoteEntryType;
17
- globalName: string;
18
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/runtime",
3
- "version": "0.0.0-next-20240605083609",
3
+ "version": "0.0.0-next-20240605100049",
4
4
  "author": "zhouxiao <codingzx@gmail.com>",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.esm.js",
@@ -45,6 +45,6 @@
45
45
  }
46
46
  },
47
47
  "dependencies": {
48
- "@module-federation/sdk": "0.0.0-next-20240605083609"
48
+ "@module-federation/sdk": "0.0.0-next-20240605100049"
49
49
  }
50
50
  }