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

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,14 @@ 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
+ hostShareScopeMap
1406
1490
  });
1407
1491
  }
1408
1492
  setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
@@ -1504,7 +1588,7 @@ class RemoteHandler {
1504
1588
  id
1505
1589
  });
1506
1590
  const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
1507
- const moduleOrFactory = await module.get(expose, options);
1591
+ const moduleOrFactory = await module.get(idRes, expose, options);
1508
1592
  const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
1509
1593
  id: idRes,
1510
1594
  pkgNameOrAlias,
@@ -1516,10 +1600,6 @@ class RemoteHandler {
1516
1600
  moduleInstance: module,
1517
1601
  origin: host
1518
1602
  });
1519
- this.idToRemoteMap[id] = {
1520
- name: remote.name,
1521
- expose
1522
- };
1523
1603
  if (typeof moduleWrapper === 'function') {
1524
1604
  return moduleWrapper;
1525
1605
  }
@@ -1678,61 +1758,49 @@ class RemoteHandler {
1678
1758
  }
1679
1759
  }
1680
1760
  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);
1761
+ const { host } = this;
1762
+ const { name } = remote;
1763
+ const remoteIndex = host.options.remotes.findIndex((item)=>item.name === name);
1764
+ if (remoteIndex !== -1) {
1765
+ host.options.remotes.splice(remoteIndex, 1);
1766
+ }
1767
+ const loadedModule = host.moduleCache.get(remote.name);
1768
+ if (loadedModule) {
1769
+ var _Object_getOwnPropertyDescriptor;
1770
+ const remoteInfo = loadedModule.remoteInfo;
1771
+ const key = remoteInfo.entryGlobalName;
1772
+ if (globalThis[key] && ((_Object_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(globalThis, key)) == null ? void 0 : _Object_getOwnPropertyDescriptor.configurable)) {
1773
+ delete globalThis[key];
1687
1774
  }
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];
1775
+ const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
1776
+ if (share.globalLoading[remoteEntryUniqueKey]) {
1777
+ delete share.globalLoading[remoteEntryUniqueKey];
1778
+ }
1779
+ // delete un loaded shared and instance
1780
+ let remoteInsId = remoteInfo.buildVersion ? sdk.composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
1781
+ const remoteInsIndex = globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins)=>{
1782
+ if (remoteInfo.buildVersion) {
1783
+ return ins.options.id === remoteInsId;
1784
+ } else {
1785
+ return ins.name === remoteInsId;
1698
1786
  }
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
- }
1787
+ });
1788
+ if (remoteInsIndex !== -1) {
1789
+ const remoteIns = globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
1790
+ remoteInsId = remoteIns.options.id || remoteInsId;
1791
+ const globalShareScopeMap = share.getGlobalShareScope();
1792
+ let isAllSharedNotUsed = true;
1793
+ const needDeleteKeys = [];
1794
+ Object.keys(globalShareScopeMap).forEach((instId)=>{
1795
+ Object.keys(globalShareScopeMap[instId]).forEach((shareScope)=>{
1796
+ Object.keys(globalShareScopeMap[instId][shareScope]).forEach((shareName)=>{
1797
+ Object.keys(globalShareScopeMap[instId][shareScope][shareName]).forEach((shareVersion)=>{
1798
+ const shared = globalShareScopeMap[instId][shareScope][shareName][shareVersion];
1799
+ if (shared.from === remoteInfo.name) {
1800
+ if (shared.loaded || shared.loading) {
1801
+ shared.useIn = shared.useIn.filter((usedHostName)=>usedHostName !== remoteInfo.name);
1802
+ if (shared.useIn.length) {
1803
+ isAllSharedNotUsed = false;
1736
1804
  } else {
1737
1805
  needDeleteKeys.push([
1738
1806
  instId,
@@ -1741,25 +1809,30 @@ class RemoteHandler {
1741
1809
  shareVersion
1742
1810
  ]);
1743
1811
  }
1812
+ } else {
1813
+ needDeleteKeys.push([
1814
+ instId,
1815
+ shareScope,
1816
+ shareName,
1817
+ shareVersion
1818
+ ]);
1744
1819
  }
1745
- });
1820
+ }
1746
1821
  });
1747
1822
  });
1748
1823
  });
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);
1824
+ });
1825
+ if (isAllSharedNotUsed) {
1826
+ remoteIns.shareScopeMap = {};
1827
+ delete globalShareScopeMap[remoteInsId];
1758
1828
  }
1759
- host.moduleCache.delete(remote.name);
1829
+ needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion])=>{
1830
+ var _globalShareScopeMap_insId_shareScope_shareName, _globalShareScopeMap_insId_shareScope, _globalShareScopeMap_insId;
1831
+ (_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];
1832
+ });
1833
+ globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
1760
1834
  }
1761
- } catch (err) {
1762
- console.log('removeRemote fail: ', err);
1835
+ host.moduleCache.delete(remote.name);
1763
1836
  }
1764
1837
  }
1765
1838
  constructor(host){
@@ -1774,7 +1847,6 @@ class RemoteHandler {
1774
1847
  afterPreloadRemote: new AsyncHook()
1775
1848
  });
1776
1849
  this.host = host;
1777
- this.idToRemoteMap = {};
1778
1850
  }
1779
1851
  }
1780
1852
 
@@ -1834,8 +1906,8 @@ class FederationHost {
1834
1906
  async preloadRemote(preloadOptions) {
1835
1907
  return this.remoteHandler.preloadRemote(preloadOptions);
1836
1908
  }
1837
- initShareScopeMap(scopeName, shareScope) {
1838
- this.sharedHandler.initShareScopeMap(scopeName, shareScope);
1909
+ initShareScopeMap(scopeName, shareScope, hostShareScopeMap) {
1910
+ this.sharedHandler.initShareScopeMap(scopeName, shareScope, hostShareScopeMap);
1839
1911
  }
1840
1912
  formatOptions(globalOptions, userOptions) {
1841
1913
  const { shared } = share.formatShareConfigs(globalOptions, userOptions);
@@ -1897,7 +1969,7 @@ class FederationHost {
1897
1969
  // maybe will change, temporarily for internal use only
1898
1970
  initContainer: new AsyncWaterfallHook('initContainer')
1899
1971
  });
1900
- this.version = "0.1.15";
1972
+ this.version = "0.1.18";
1901
1973
  this.moduleCache = new Map();
1902
1974
  this.loaderHook = new PluginSystem({
1903
1975
  // FIXME: may not be suitable , not open to the public yet
@@ -1986,9 +2058,6 @@ function registerPlugins(...args) {
1986
2058
  // eslint-disable-next-line prefer-spread
1987
2059
  return FederationInstance.registerPlugins.apply(FederationInstance, args);
1988
2060
  }
1989
- function getInstance() {
1990
- return FederationInstance;
1991
- }
1992
2061
  // Inject for debug
1993
2062
  share.setGlobalFederationConstructor(FederationHost);
1994
2063
 
@@ -2002,7 +2071,6 @@ Object.defineProperty(exports, 'loadScriptNode', {
2002
2071
  get: function () { return sdk.loadScriptNode; }
2003
2072
  });
2004
2073
  exports.FederationHost = FederationHost;
2005
- exports.getInstance = getInstance;
2006
2074
  exports.getRemoteEntry = getRemoteEntry;
2007
2075
  exports.getRemoteInfo = getRemoteInfo;
2008
2076
  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,14 @@ 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
+ hostShareScopeMap
1404
1488
  });
1405
1489
  }
1406
1490
  setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
@@ -1502,7 +1586,7 @@ class RemoteHandler {
1502
1586
  id
1503
1587
  });
1504
1588
  const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
1505
- const moduleOrFactory = await module.get(expose, options);
1589
+ const moduleOrFactory = await module.get(idRes, expose, options);
1506
1590
  const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
1507
1591
  id: idRes,
1508
1592
  pkgNameOrAlias,
@@ -1514,10 +1598,6 @@ class RemoteHandler {
1514
1598
  moduleInstance: module,
1515
1599
  origin: host
1516
1600
  });
1517
- this.idToRemoteMap[id] = {
1518
- name: remote.name,
1519
- expose
1520
- };
1521
1601
  if (typeof moduleWrapper === 'function') {
1522
1602
  return moduleWrapper;
1523
1603
  }
@@ -1676,61 +1756,49 @@ class RemoteHandler {
1676
1756
  }
1677
1757
  }
1678
1758
  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);
1759
+ const { host } = this;
1760
+ const { name } = remote;
1761
+ const remoteIndex = host.options.remotes.findIndex((item)=>item.name === name);
1762
+ if (remoteIndex !== -1) {
1763
+ host.options.remotes.splice(remoteIndex, 1);
1764
+ }
1765
+ const loadedModule = host.moduleCache.get(remote.name);
1766
+ if (loadedModule) {
1767
+ var _Object_getOwnPropertyDescriptor;
1768
+ const remoteInfo = loadedModule.remoteInfo;
1769
+ const key = remoteInfo.entryGlobalName;
1770
+ if (globalThis[key] && ((_Object_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(globalThis, key)) == null ? void 0 : _Object_getOwnPropertyDescriptor.configurable)) {
1771
+ delete globalThis[key];
1685
1772
  }
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];
1773
+ const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
1774
+ if (globalLoading[remoteEntryUniqueKey]) {
1775
+ delete globalLoading[remoteEntryUniqueKey];
1776
+ }
1777
+ // delete un loaded shared and instance
1778
+ let remoteInsId = remoteInfo.buildVersion ? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
1779
+ const remoteInsIndex = globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins)=>{
1780
+ if (remoteInfo.buildVersion) {
1781
+ return ins.options.id === remoteInsId;
1782
+ } else {
1783
+ return ins.name === remoteInsId;
1696
1784
  }
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
- }
1785
+ });
1786
+ if (remoteInsIndex !== -1) {
1787
+ const remoteIns = globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
1788
+ remoteInsId = remoteIns.options.id || remoteInsId;
1789
+ const globalShareScopeMap = getGlobalShareScope();
1790
+ let isAllSharedNotUsed = true;
1791
+ const needDeleteKeys = [];
1792
+ Object.keys(globalShareScopeMap).forEach((instId)=>{
1793
+ Object.keys(globalShareScopeMap[instId]).forEach((shareScope)=>{
1794
+ Object.keys(globalShareScopeMap[instId][shareScope]).forEach((shareName)=>{
1795
+ Object.keys(globalShareScopeMap[instId][shareScope][shareName]).forEach((shareVersion)=>{
1796
+ const shared = globalShareScopeMap[instId][shareScope][shareName][shareVersion];
1797
+ if (shared.from === remoteInfo.name) {
1798
+ if (shared.loaded || shared.loading) {
1799
+ shared.useIn = shared.useIn.filter((usedHostName)=>usedHostName !== remoteInfo.name);
1800
+ if (shared.useIn.length) {
1801
+ isAllSharedNotUsed = false;
1734
1802
  } else {
1735
1803
  needDeleteKeys.push([
1736
1804
  instId,
@@ -1739,25 +1807,30 @@ class RemoteHandler {
1739
1807
  shareVersion
1740
1808
  ]);
1741
1809
  }
1810
+ } else {
1811
+ needDeleteKeys.push([
1812
+ instId,
1813
+ shareScope,
1814
+ shareName,
1815
+ shareVersion
1816
+ ]);
1742
1817
  }
1743
- });
1818
+ }
1744
1819
  });
1745
1820
  });
1746
1821
  });
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);
1822
+ });
1823
+ if (isAllSharedNotUsed) {
1824
+ remoteIns.shareScopeMap = {};
1825
+ delete globalShareScopeMap[remoteInsId];
1756
1826
  }
1757
- host.moduleCache.delete(remote.name);
1827
+ needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion])=>{
1828
+ var _globalShareScopeMap_insId_shareScope_shareName, _globalShareScopeMap_insId_shareScope, _globalShareScopeMap_insId;
1829
+ (_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];
1830
+ });
1831
+ globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
1758
1832
  }
1759
- } catch (err) {
1760
- console.log('removeRemote fail: ', err);
1833
+ host.moduleCache.delete(remote.name);
1761
1834
  }
1762
1835
  }
1763
1836
  constructor(host){
@@ -1772,7 +1845,6 @@ class RemoteHandler {
1772
1845
  afterPreloadRemote: new AsyncHook()
1773
1846
  });
1774
1847
  this.host = host;
1775
- this.idToRemoteMap = {};
1776
1848
  }
1777
1849
  }
1778
1850
 
@@ -1832,8 +1904,8 @@ class FederationHost {
1832
1904
  async preloadRemote(preloadOptions) {
1833
1905
  return this.remoteHandler.preloadRemote(preloadOptions);
1834
1906
  }
1835
- initShareScopeMap(scopeName, shareScope) {
1836
- this.sharedHandler.initShareScopeMap(scopeName, shareScope);
1907
+ initShareScopeMap(scopeName, shareScope, hostShareScopeMap) {
1908
+ this.sharedHandler.initShareScopeMap(scopeName, shareScope, hostShareScopeMap);
1837
1909
  }
1838
1910
  formatOptions(globalOptions, userOptions) {
1839
1911
  const { shared } = formatShareConfigs(globalOptions, userOptions);
@@ -1895,7 +1967,7 @@ class FederationHost {
1895
1967
  // maybe will change, temporarily for internal use only
1896
1968
  initContainer: new AsyncWaterfallHook('initContainer')
1897
1969
  });
1898
- this.version = "0.1.15";
1970
+ this.version = "0.1.18";
1899
1971
  this.moduleCache = new Map();
1900
1972
  this.loaderHook = new PluginSystem({
1901
1973
  // FIXME: may not be suitable , not open to the public yet
@@ -1984,10 +2056,7 @@ function registerPlugins(...args) {
1984
2056
  // eslint-disable-next-line prefer-spread
1985
2057
  return FederationInstance.registerPlugins.apply(FederationInstance, args);
1986
2058
  }
1987
- function getInstance() {
1988
- return FederationInstance;
1989
- }
1990
2059
  // Inject for debug
1991
2060
  setGlobalFederationConstructor(FederationHost);
1992
2061
 
1993
- export { FederationHost, getInstance, getRemoteEntry, getRemoteInfo, init, loadRemote, loadShare, loadShareSync, preloadRemote, registerPlugins, registerRemotes };
2062
+ 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;
@@ -27,6 +27,7 @@ export declare class SharedHandler {
27
27
  shareScope: ShareScopeMap[string];
28
28
  options: Options;
29
29
  origin: FederationHost;
30
+ hostShareScopeMap?: ShareScopeMap | undefined;
30
31
  }>;
31
32
  }>;
32
33
  constructor(host: FederationHost);
@@ -50,7 +51,7 @@ export declare class SharedHandler {
50
51
  customShareInfo?: Partial<Shared>;
51
52
  resolver?: (sharedOptions: ShareInfos[string]) => Shared;
52
53
  }): () => T | never;
53
- initShareScopeMap(scopeName: string, shareScope: ShareScopeMap[string]): void;
54
+ initShareScopeMap(scopeName: string, shareScope: ShareScopeMap[string], hostShareScopeMap?: ShareScopeMap): void;
54
55
  private setShared;
55
56
  private _setGlobalShareScopeMap;
56
57
  }
@@ -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-20240605092134",
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-20240605092134"
49
49
  }
50
50
  }