@module-federation/runtime-core 0.0.0-feat-shared-treeshake-poc-20260112063915 → 0.0.0-feat-modern-3-0-20260113090730

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.
@@ -201,7 +201,7 @@ function getGlobalFederationConstructor() {
201
201
  function setGlobalFederationConstructor(FederationConstructor, isDebug = sdk.isDebugMode()) {
202
202
  if (isDebug) {
203
203
  CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
204
- CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.0-feat-shared-treeshake-poc-20260112063915";
204
+ CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.0-feat-modern-3-0-20260113090730";
205
205
  }
206
206
  }
207
207
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -808,9 +808,6 @@ function formatShare(shareArgs, from, name, shareStrategy) {
808
808
  throw new Error(`Can not get shared '${name}'!`);
809
809
  });
810
810
  }
811
- if (shareArgs.shareConfig?.eager && shareArgs.treeShaking) {
812
- throw new Error('Can not set "eager:true" and "treeShaking" at the same time!');
813
- }
814
811
  return {
815
812
  deps: [],
816
813
  useIn: [],
@@ -831,67 +828,37 @@ function formatShare(shareArgs, from, name, shareStrategy) {
831
828
  ? shareArgs.scope
832
829
  : [shareArgs.scope ?? 'default'],
833
830
  strategy: (shareArgs.strategy ?? shareStrategy) || 'version-first',
834
- treeShaking: shareArgs.treeShaking
835
- ? {
836
- ...shareArgs.treeShaking,
837
- mode: shareArgs.treeShaking.mode ?? 'server-calc',
838
- status: shareArgs.treeShaking.status ?? 1 /* TreeShakingStatus.UNKNOWN */,
839
- useIn: [],
840
- }
841
- : undefined,
842
831
  };
843
832
  }
844
- function formatShareConfigs(prevOptions, newOptions) {
845
- const shareArgs = newOptions.shared || {};
846
- const from = newOptions.name;
847
- const newShareInfos = Object.keys(shareArgs).reduce((res, pkgName) => {
833
+ function formatShareConfigs(globalOptions, userOptions) {
834
+ const shareArgs = userOptions.shared || {};
835
+ const from = userOptions.name;
836
+ const shareInfos = Object.keys(shareArgs).reduce((res, pkgName) => {
848
837
  const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
849
838
  res[pkgName] = res[pkgName] || [];
850
839
  arrayShareArgs.forEach((shareConfig) => {
851
- res[pkgName].push(formatShare(shareConfig, from, pkgName, newOptions.shareStrategy));
840
+ res[pkgName].push(formatShare(shareConfig, from, pkgName, userOptions.shareStrategy));
852
841
  });
853
842
  return res;
854
843
  }, {});
855
- const allShareInfos = {
856
- ...prevOptions.shared,
844
+ const shared = {
845
+ ...globalOptions.shared,
857
846
  };
858
- Object.keys(newShareInfos).forEach((shareKey) => {
859
- if (!allShareInfos[shareKey]) {
860
- allShareInfos[shareKey] = newShareInfos[shareKey];
847
+ Object.keys(shareInfos).forEach((shareKey) => {
848
+ if (!shared[shareKey]) {
849
+ shared[shareKey] = shareInfos[shareKey];
861
850
  }
862
851
  else {
863
- newShareInfos[shareKey].forEach((newUserSharedOptions) => {
864
- const isSameVersion = allShareInfos[shareKey].find((sharedVal) => sharedVal.version === newUserSharedOptions.version);
852
+ shareInfos[shareKey].forEach((newUserSharedOptions) => {
853
+ const isSameVersion = shared[shareKey].find((sharedVal) => sharedVal.version === newUserSharedOptions.version);
865
854
  if (!isSameVersion) {
866
- allShareInfos[shareKey].push(newUserSharedOptions);
855
+ shared[shareKey].push(newUserSharedOptions);
867
856
  }
868
857
  });
869
858
  }
870
859
  });
871
- return { allShareInfos, newShareInfos };
872
- }
873
- function shouldUseTreeShaking(treeShaking, usedExports) {
874
- if (!treeShaking) {
875
- return false;
876
- }
877
- const { status, mode } = treeShaking;
878
- if (status === 0 /* TreeShakingStatus.NO_USE */) {
879
- return false;
880
- }
881
- if (status === 2 /* TreeShakingStatus.CALCULATED */) {
882
- return true;
883
- }
884
- if (mode === 'runtime-infer') {
885
- if (!usedExports) {
886
- return true;
887
- }
888
- return isMatchUsedExports(treeShaking, usedExports);
889
- }
890
- return false;
860
+ return { shared, shareInfos };
891
861
  }
892
- /**
893
- * compare version a and b, return true if a is less than b
894
- */
895
862
  function versionLt(a, b) {
896
863
  const transformInvalidVersion = (version) => {
897
864
  const isNumberVersion = !Number.isNaN(Number(version));
@@ -937,79 +904,19 @@ const isLoaded = (shared) => {
937
904
  const isLoading = (shared) => {
938
905
  return Boolean(shared.loading);
939
906
  };
940
- const isMatchUsedExports = (treeShaking, usedExports) => {
941
- if (!treeShaking || !usedExports) {
942
- return false;
943
- }
944
- const { usedExports: treeShakingUsedExports } = treeShaking;
945
- if (!treeShakingUsedExports) {
946
- return false;
947
- }
948
- if (usedExports.every((e) => treeShakingUsedExports.includes(e))) {
949
- return true;
950
- }
951
- return false;
952
- };
953
- function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName, treeShaking) {
907
+ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
954
908
  const versions = shareScopeMap[scope][pkgName];
955
- let version = '';
956
- let useTreesShaking = shouldUseTreeShaking(treeShaking);
957
- // return false means use prev version
958
909
  const callback = function (prev, cur) {
959
- if (useTreesShaking) {
960
- if (!versions[prev].treeShaking) {
961
- return true;
962
- }
963
- if (!versions[cur].treeShaking) {
964
- return false;
965
- }
966
- return !isLoaded(versions[prev].treeShaking) && versionLt(prev, cur);
967
- }
968
910
  return !isLoaded(versions[prev]) && versionLt(prev, cur);
969
911
  };
970
- if (useTreesShaking) {
971
- version = findVersion(shareScopeMap[scope][pkgName], callback);
972
- if (version) {
973
- return {
974
- version,
975
- useTreesShaking,
976
- };
977
- }
978
- useTreesShaking = false;
979
- }
980
- return {
981
- version: findVersion(shareScopeMap[scope][pkgName], callback),
982
- useTreesShaking,
983
- };
912
+ return findVersion(shareScopeMap[scope][pkgName], callback);
984
913
  }
985
- const isLoadingOrLoaded = (shared) => {
986
- return isLoaded(shared) || isLoading(shared);
987
- };
988
- function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName, treeShaking) {
914
+ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
989
915
  const versions = shareScopeMap[scope][pkgName];
990
- let version = '';
991
- let useTreesShaking = shouldUseTreeShaking(treeShaking);
992
- // return false means use prev version
993
916
  const callback = function (prev, cur) {
994
- if (useTreesShaking) {
995
- if (!versions[prev].treeShaking) {
996
- return true;
997
- }
998
- if (!versions[cur].treeShaking) {
999
- return false;
1000
- }
1001
- if (isLoadingOrLoaded(versions[cur].treeShaking)) {
1002
- if (isLoadingOrLoaded(versions[prev].treeShaking)) {
1003
- return Boolean(versionLt(prev, cur));
1004
- }
1005
- else {
1006
- return true;
1007
- }
1008
- }
1009
- if (isLoadingOrLoaded(versions[prev].treeShaking)) {
1010
- return false;
1011
- }
1012
- }
917
+ const isLoadingOrLoaded = (shared) => {
918
+ return isLoaded(shared) || isLoading(shared);
919
+ };
1013
920
  if (isLoadingOrLoaded(versions[cur])) {
1014
921
  if (isLoadingOrLoaded(versions[prev])) {
1015
922
  return Boolean(versionLt(prev, cur));
@@ -1023,20 +930,7 @@ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName, treeSh
1023
930
  }
1024
931
  return versionLt(prev, cur);
1025
932
  };
1026
- if (useTreesShaking) {
1027
- version = findVersion(shareScopeMap[scope][pkgName], callback);
1028
- if (version) {
1029
- return {
1030
- version,
1031
- useTreesShaking,
1032
- };
1033
- }
1034
- useTreesShaking = false;
1035
- }
1036
- return {
1037
- version: findVersion(shareScopeMap[scope][pkgName], callback),
1038
- useTreesShaking,
1039
- };
933
+ return findVersion(shareScopeMap[scope][pkgName], callback);
1040
934
  }
1041
935
  function getFindShareFunction(strategy) {
1042
936
  if (strategy === 'loaded-first') {
@@ -1048,7 +942,7 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
1048
942
  if (!localShareScopeMap) {
1049
943
  return;
1050
944
  }
1051
- const { shareConfig, scope = DEFAULT_SCOPE, strategy, treeShaking, } = shareInfo;
945
+ const { shareConfig, scope = DEFAULT_SCOPE, strategy } = shareInfo;
1052
946
  const scopes = Array.isArray(scope) ? scope : [scope];
1053
947
  for (const sc of scopes) {
1054
948
  if (shareConfig &&
@@ -1056,13 +950,14 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
1056
950
  localShareScopeMap[sc][pkgName]) {
1057
951
  const { requiredVersion } = shareConfig;
1058
952
  const findShareFunction = getFindShareFunction(strategy);
1059
- const { version: maxOrSingletonVersion, useTreesShaking } = findShareFunction(localShareScopeMap, sc, pkgName, treeShaking);
953
+ const maxOrSingletonVersion = findShareFunction(localShareScopeMap, sc, pkgName);
954
+ //@ts-ignore
1060
955
  const defaultResolver = () => {
1061
- const shared = localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1062
956
  if (shareConfig.singleton) {
1063
957
  if (typeof requiredVersion === 'string' &&
1064
958
  !satisfy(maxOrSingletonVersion, requiredVersion)) {
1065
- const msg = `Version ${maxOrSingletonVersion} from ${maxOrSingletonVersion && shared.from} of shared singleton module ${pkgName} does not satisfy the requirement of ${shareInfo.from} which needs ${requiredVersion})`;
959
+ const msg = `Version ${maxOrSingletonVersion} from ${maxOrSingletonVersion &&
960
+ localShareScopeMap[sc][pkgName][maxOrSingletonVersion].from} of shared singleton module ${pkgName} does not satisfy the requirement of ${shareInfo.from} which needs ${requiredVersion})`;
1066
961
  if (shareConfig.strictVersion) {
1067
962
  error(msg);
1068
963
  }
@@ -1070,48 +965,21 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
1070
965
  warn(msg);
1071
966
  }
1072
967
  }
1073
- return {
1074
- shared,
1075
- useTreesShaking,
1076
- };
968
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1077
969
  }
1078
970
  else {
1079
971
  if (requiredVersion === false || requiredVersion === '*') {
1080
- return {
1081
- shared,
1082
- useTreesShaking,
1083
- };
972
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1084
973
  }
1085
974
  if (satisfy(maxOrSingletonVersion, requiredVersion)) {
1086
- return {
1087
- shared,
1088
- useTreesShaking,
1089
- };
1090
- }
1091
- const _usedTreeShaking = shouldUseTreeShaking(treeShaking);
1092
- if (_usedTreeShaking) {
1093
- for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
1094
- if (!shouldUseTreeShaking(versionValue.treeShaking, treeShaking?.usedExports)) {
1095
- continue;
1096
- }
1097
- if (satisfy(versionKey, requiredVersion)) {
1098
- return {
1099
- shared: versionValue,
1100
- useTreesShaking: _usedTreeShaking,
1101
- };
1102
- }
1103
- }
975
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1104
976
  }
1105
977
  for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
1106
978
  if (satisfy(versionKey, requiredVersion)) {
1107
- return {
1108
- shared: versionValue,
1109
- useTreesShaking: false,
1110
- };
979
+ return versionValue;
1111
980
  }
1112
981
  }
1113
982
  }
1114
- return;
1115
983
  };
1116
984
  const params = {
1117
985
  shareScopeMap: localShareScopeMap,
@@ -1119,7 +987,6 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
1119
987
  pkgName,
1120
988
  version: maxOrSingletonVersion,
1121
989
  GlobalFederation: Global.__FEDERATION__,
1122
- shareInfo,
1123
990
  resolver: defaultResolver,
1124
991
  };
1125
992
  const resolveShared = resolveShare.emit(params) || params;
@@ -1141,47 +1008,13 @@ function getTargetSharedOptions(options) {
1141
1008
  shareVersionMap[shared.version] = shared;
1142
1009
  });
1143
1010
  const callback = function (prev, cur) {
1144
- return (
1145
- // TODO: consider multiple treeShaking shared scenes
1146
- !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur));
1011
+ return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
1147
1012
  };
1148
1013
  const maxVersion = findVersion(shareVersionMap, callback);
1149
1014
  return shareVersionMap[maxVersion];
1150
1015
  };
1151
1016
  const resolver = extraOptions?.resolver ?? defaultResolver;
1152
- const isPlainObject = (val) => {
1153
- return val !== null && typeof val === 'object' && !Array.isArray(val);
1154
- };
1155
- const merge = (...sources) => {
1156
- const out = {};
1157
- for (const src of sources) {
1158
- if (!src)
1159
- continue;
1160
- for (const [key, value] of Object.entries(src)) {
1161
- const prev = out[key];
1162
- if (isPlainObject(prev) && isPlainObject(value)) {
1163
- out[key] = merge(prev, value);
1164
- }
1165
- else if (value !== undefined) {
1166
- out[key] = value;
1167
- }
1168
- }
1169
- }
1170
- return out;
1171
- };
1172
- return merge(resolver(shareInfos[pkgName]), extraOptions?.customShareInfo);
1173
- }
1174
- const addUseIn = (shared, from) => {
1175
- if (!shared.useIn) {
1176
- shared.useIn = [];
1177
- }
1178
- addUniqueItem(shared.useIn, from);
1179
- };
1180
- function directShare(shared, useTreesShaking) {
1181
- if (useTreesShaking && shared.treeShaking) {
1182
- return shared.treeShaking;
1183
- }
1184
- return shared;
1017
+ return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions?.customShareInfo);
1185
1018
  }
1186
1019
 
1187
1020
  function getBuilderId() {
@@ -1693,40 +1526,6 @@ var helpers = {
1693
1526
  },
1694
1527
  };
1695
1528
 
1696
- function createRemoteEntryInitOptions(remoteInfo, hostShareScopeMap) {
1697
- const localShareScopeMap = hostShareScopeMap;
1698
- const shareScopeKeys = Array.isArray(remoteInfo.shareScope)
1699
- ? remoteInfo.shareScope
1700
- : [remoteInfo.shareScope];
1701
- if (!shareScopeKeys.length) {
1702
- shareScopeKeys.push('default');
1703
- }
1704
- shareScopeKeys.forEach((shareScopeKey) => {
1705
- if (!localShareScopeMap[shareScopeKey]) {
1706
- localShareScopeMap[shareScopeKey] = {};
1707
- }
1708
- });
1709
- const remoteEntryInitOptions = {
1710
- version: remoteInfo.version || '',
1711
- shareScopeKeys: Array.isArray(remoteInfo.shareScope)
1712
- ? shareScopeKeys
1713
- : remoteInfo.shareScope || 'default',
1714
- };
1715
- // Help to find host instance
1716
- Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
1717
- value: localShareScopeMap,
1718
- // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
1719
- enumerable: false,
1720
- });
1721
- // TODO: compate legacy init params, should use shareScopeMap if exist
1722
- const shareScope = localShareScopeMap[shareScopeKeys[0]];
1723
- const initScope = [];
1724
- return {
1725
- remoteEntryInitOptions,
1726
- shareScope,
1727
- initScope,
1728
- };
1729
- }
1730
1529
  class Module {
1731
1530
  constructor({ remoteInfo, host, }) {
1732
1531
  this.inited = false;
@@ -1748,12 +1547,37 @@ class Module {
1748
1547
  return this.remoteEntryExports;
1749
1548
  }
1750
1549
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1751
- async get(id, expose, options, remoteSnapshot) {
1752
- const { loadFactory = true } = options || { loadFactory: true };
1550
+ async init(id, remoteSnapshot) {
1753
1551
  // Get remoteEntry.js
1754
1552
  const remoteEntryExports = await this.getEntry();
1755
1553
  if (!this.inited) {
1756
- const { remoteEntryInitOptions, shareScope, initScope } = createRemoteEntryInitOptions(this.remoteInfo, this.host.shareScopeMap);
1554
+ const localShareScopeMap = this.host.shareScopeMap;
1555
+ const shareScopeKeys = Array.isArray(this.remoteInfo.shareScope)
1556
+ ? this.remoteInfo.shareScope
1557
+ : [this.remoteInfo.shareScope];
1558
+ if (!shareScopeKeys.length) {
1559
+ shareScopeKeys.push('default');
1560
+ }
1561
+ shareScopeKeys.forEach((shareScopeKey) => {
1562
+ if (!localShareScopeMap[shareScopeKey]) {
1563
+ localShareScopeMap[shareScopeKey] = {};
1564
+ }
1565
+ });
1566
+ // TODO: compate legacy init params, should use shareScopeMap if exist
1567
+ const shareScope = localShareScopeMap[shareScopeKeys[0]];
1568
+ const initScope = [];
1569
+ const remoteEntryInitOptions = {
1570
+ version: this.remoteInfo.version || '',
1571
+ shareScopeKeys: Array.isArray(this.remoteInfo.shareScope)
1572
+ ? shareScopeKeys
1573
+ : this.remoteInfo.shareScope || 'default',
1574
+ };
1575
+ // Help to find host instance
1576
+ Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
1577
+ value: localShareScopeMap,
1578
+ // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
1579
+ enumerable: false,
1580
+ });
1757
1581
  const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
1758
1582
  shareScope,
1759
1583
  // @ts-ignore shareScopeMap will be set by Object.defineProperty
@@ -1778,6 +1602,12 @@ class Module {
1778
1602
  remoteEntryExports,
1779
1603
  });
1780
1604
  }
1605
+ return remoteEntryExports;
1606
+ }
1607
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1608
+ async get(id, expose, options, remoteSnapshot) {
1609
+ const { loadFactory = true } = options || { loadFactory: true };
1610
+ const remoteEntryExports = await this.init(id, remoteSnapshot);
1781
1611
  this.lib = remoteEntryExports;
1782
1612
  this.inited = true;
1783
1613
  let moduleFactory;
@@ -2233,7 +2063,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
2233
2063
  }, true, memo, remoteSnapshot);
2234
2064
  if (remoteSnapshot.shared && remoteSnapshot.shared.length > 0) {
2235
2065
  const collectSharedAssets = (shareInfo, snapshotShared) => {
2236
- const { shared: registeredShared } = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare) || {};
2066
+ const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare);
2237
2067
  // If the global share does not exist, or the lib function does not exist, it means that the shared has not been loaded yet and can be preloaded.
2238
2068
  if (registeredShared && typeof registeredShared.lib === 'function') {
2239
2069
  snapshotShared.assets.js.sync.forEach((asset) => {
@@ -2524,7 +2354,6 @@ class SnapshotHandler {
2524
2354
  class SharedHandler {
2525
2355
  constructor(host) {
2526
2356
  this.hooks = new PluginSystem({
2527
- beforeRegisterShare: new SyncWaterfallHook('beforeRegisterShare'),
2528
2357
  afterResolve: new AsyncWaterfallHook('afterResolve'),
2529
2358
  beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
2530
2359
  // not used yet
@@ -2540,17 +2369,12 @@ class SharedHandler {
2540
2369
  }
2541
2370
  // register shared in shareScopeMap
2542
2371
  registerShared(globalOptions, userOptions) {
2543
- const { newShareInfos, allShareInfos } = formatShareConfigs(globalOptions, userOptions);
2544
- const sharedKeys = Object.keys(newShareInfos);
2372
+ const { shareInfos, shared } = formatShareConfigs(globalOptions, userOptions);
2373
+ const sharedKeys = Object.keys(shareInfos);
2545
2374
  sharedKeys.forEach((sharedKey) => {
2546
- const sharedVals = newShareInfos[sharedKey];
2375
+ const sharedVals = shareInfos[sharedKey];
2547
2376
  sharedVals.forEach((sharedVal) => {
2548
2377
  sharedVal.scope.forEach((sc) => {
2549
- this.hooks.lifecycle.beforeRegisterShare.emit({
2550
- origin: this.host,
2551
- pkgName: sharedKey,
2552
- shared: sharedVal,
2553
- });
2554
2378
  const registeredShared = this.shareScopeMap[sc]?.[sharedKey];
2555
2379
  if (!registeredShared) {
2556
2380
  this.setShared({
@@ -2566,8 +2390,8 @@ class SharedHandler {
2566
2390
  });
2567
2391
  });
2568
2392
  return {
2569
- newShareInfos,
2570
- allShareInfos,
2393
+ shareInfos,
2394
+ shared,
2571
2395
  };
2572
2396
  }
2573
2397
  async loadShare(pkgName, extraOptions) {
@@ -2598,61 +2422,61 @@ class SharedHandler {
2598
2422
  const { shareInfo: shareOptionsRes } = loadShareRes;
2599
2423
  // Assert that shareInfoRes exists, if not, throw an error
2600
2424
  assert(shareOptionsRes, `Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
2601
- const { shared: registeredShared, useTreesShaking } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
2602
- if (registeredShared) {
2603
- const targetShared = directShare(registeredShared, useTreesShaking);
2604
- if (targetShared.lib) {
2605
- addUseIn(targetShared, host.options.name);
2606
- return targetShared.lib;
2607
- }
2608
- else if (targetShared.loading && !targetShared.loaded) {
2609
- const factory = await targetShared.loading;
2610
- targetShared.loaded = true;
2611
- if (!targetShared.lib) {
2612
- targetShared.lib = factory;
2613
- }
2614
- addUseIn(targetShared, host.options.name);
2615
- return factory;
2616
- }
2617
- else {
2618
- const asyncLoadProcess = async () => {
2619
- const factory = await targetShared.get();
2620
- addUseIn(targetShared, host.options.name);
2621
- targetShared.loaded = true;
2622
- targetShared.lib = factory;
2623
- return factory;
2624
- };
2625
- const loading = asyncLoadProcess();
2626
- this.setShared({
2627
- pkgName,
2628
- loaded: false,
2629
- shared: registeredShared,
2630
- from: host.options.name,
2631
- lib: null,
2632
- loading,
2633
- treeShaking: useTreesShaking
2634
- ? targetShared
2635
- : undefined,
2636
- });
2637
- return loading;
2425
+ // Retrieve from cache
2426
+ const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
2427
+ const addUseIn = (shared) => {
2428
+ if (!shared.useIn) {
2429
+ shared.useIn = [];
2638
2430
  }
2431
+ addUniqueItem(shared.useIn, host.options.name);
2432
+ };
2433
+ if (registeredShared && registeredShared.lib) {
2434
+ addUseIn(registeredShared);
2435
+ return registeredShared.lib;
2436
+ }
2437
+ else if (registeredShared &&
2438
+ registeredShared.loading &&
2439
+ !registeredShared.loaded) {
2440
+ const factory = await registeredShared.loading;
2441
+ registeredShared.loaded = true;
2442
+ if (!registeredShared.lib) {
2443
+ registeredShared.lib = factory;
2444
+ }
2445
+ addUseIn(registeredShared);
2446
+ return factory;
2447
+ }
2448
+ else if (registeredShared) {
2449
+ const asyncLoadProcess = async () => {
2450
+ const factory = await registeredShared.get();
2451
+ addUseIn(registeredShared);
2452
+ registeredShared.loaded = true;
2453
+ registeredShared.lib = factory;
2454
+ return factory;
2455
+ };
2456
+ const loading = asyncLoadProcess();
2457
+ this.setShared({
2458
+ pkgName,
2459
+ loaded: false,
2460
+ shared: registeredShared,
2461
+ from: host.options.name,
2462
+ lib: null,
2463
+ loading,
2464
+ });
2465
+ return loading;
2639
2466
  }
2640
2467
  else {
2641
2468
  if (extraOptions?.customShareInfo) {
2642
2469
  return false;
2643
2470
  }
2644
- const _useTreeShaking = shouldUseTreeShaking(shareOptionsRes.treeShaking);
2645
- const targetShared = directShare(shareOptionsRes, _useTreeShaking);
2646
2471
  const asyncLoadProcess = async () => {
2647
- const factory = await targetShared.get();
2648
- targetShared.lib = factory;
2649
- targetShared.loaded = true;
2650
- addUseIn(targetShared, host.options.name);
2651
- const { shared: gShared, useTreesShaking: gUseTreeShaking } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
2472
+ const factory = await shareOptionsRes.get();
2473
+ shareOptionsRes.lib = factory;
2474
+ shareOptionsRes.loaded = true;
2475
+ addUseIn(shareOptionsRes);
2476
+ const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
2652
2477
  if (gShared) {
2653
- const targetGShared = directShare(gShared, gUseTreeShaking);
2654
- targetGShared.lib = factory;
2655
- targetGShared.loaded = true;
2478
+ gShared.lib = factory;
2479
+ gShared.loaded = true;
2656
2480
  gShared.from = shareOptionsRes.from;
2657
2481
  }
2658
2482
  return factory;
@@ -2665,9 +2489,6 @@ class SharedHandler {
2665
2489
  from: host.options.name,
2666
2490
  lib: null,
2667
2491
  loading,
2668
- treeShaking: _useTreeShaking
2669
- ? targetShared
2670
- : undefined,
2671
2492
  });
2672
2493
  return loading;
2673
2494
  }
@@ -2707,17 +2528,15 @@ class SharedHandler {
2707
2528
  const { version, eager } = shared;
2708
2529
  scope[name] = scope[name] || {};
2709
2530
  const versions = scope[name];
2710
- const activeVersion = versions[version] && directShare(versions[version]);
2531
+ const activeVersion = versions[version];
2711
2532
  const activeVersionEager = Boolean(activeVersion &&
2712
- (('eager' in activeVersion && activeVersion.eager) ||
2713
- ('shareConfig' in activeVersion &&
2714
- activeVersion.shareConfig?.eager)));
2533
+ (activeVersion.eager || activeVersion.shareConfig?.eager));
2715
2534
  if (!activeVersion ||
2716
2535
  (activeVersion.strategy !== 'loaded-first' &&
2717
2536
  !activeVersion.loaded &&
2718
2537
  (Boolean(!eager) !== !activeVersionEager
2719
2538
  ? eager
2720
- : hostName > versions[version].from))) {
2539
+ : hostName > activeVersion.from))) {
2721
2540
  versions[version] = shared;
2722
2541
  }
2723
2542
  };
@@ -2725,32 +2544,7 @@ class SharedHandler {
2725
2544
  const { module } = await host.remoteHandler.getRemoteModuleAndOptions({
2726
2545
  id: key,
2727
2546
  });
2728
- if (module.getEntry) {
2729
- let remoteEntryExports;
2730
- try {
2731
- remoteEntryExports = await module.getEntry();
2732
- }
2733
- catch (error) {
2734
- remoteEntryExports =
2735
- (await host.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
2736
- id: key,
2737
- error,
2738
- from: 'runtime',
2739
- lifecycle: 'beforeLoadShare',
2740
- origin: host,
2741
- }));
2742
- }
2743
- if (!module.inited) {
2744
- const initFn = (mod) => {
2745
- const { remoteEntryInitOptions } = createRemoteEntryInitOptions(module.remoteInfo, host.shareScopeMap);
2746
- return (mod &&
2747
- mod.init &&
2748
- mod.init(shareScope[shareScopeName], initScope, remoteEntryInitOptions));
2749
- };
2750
- await initFn(remoteEntryExports);
2751
- module.inited = true;
2752
- }
2753
- }
2547
+ await module.init();
2754
2548
  };
2755
2549
  Object.keys(host.options.shared).forEach((shareName) => {
2756
2550
  const sharedArr = host.options.shared[shareName];
@@ -2787,10 +2581,16 @@ class SharedHandler {
2787
2581
  this.initializeSharing(shareScope, { strategy: shareOptions.strategy });
2788
2582
  });
2789
2583
  }
2790
- const { shared: registeredShared, useTreesShaking } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare) || {};
2584
+ const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare);
2585
+ const addUseIn = (shared) => {
2586
+ if (!shared.useIn) {
2587
+ shared.useIn = [];
2588
+ }
2589
+ addUniqueItem(shared.useIn, host.options.name);
2590
+ };
2791
2591
  if (registeredShared) {
2792
2592
  if (typeof registeredShared.lib === 'function') {
2793
- addUseIn(registeredShared, host.options.name);
2593
+ addUseIn(registeredShared);
2794
2594
  if (!registeredShared.loaded) {
2795
2595
  registeredShared.loaded = true;
2796
2596
  if (registeredShared.from === host.options.name) {
@@ -2802,7 +2602,7 @@ class SharedHandler {
2802
2602
  if (typeof registeredShared.get === 'function') {
2803
2603
  const module = registeredShared.get();
2804
2604
  if (!(module instanceof Promise)) {
2805
- addUseIn(registeredShared, host.options.name);
2605
+ addUseIn(registeredShared);
2806
2606
  this.setShared({
2807
2607
  pkgName,
2808
2608
  loaded: true,
@@ -2855,20 +2655,9 @@ class SharedHandler {
2855
2655
  hostShareScopeMap: extraOptions.hostShareScopeMap,
2856
2656
  });
2857
2657
  }
2858
- setShared({ pkgName, shared, from, lib, loading, loaded, get, treeShaking, }) {
2658
+ setShared({ pkgName, shared, from, lib, loading, loaded, get, }) {
2859
2659
  const { version, scope = 'default', ...shareInfo } = shared;
2860
2660
  const scopes = Array.isArray(scope) ? scope : [scope];
2861
- const mergeAttrs = (shared) => {
2862
- const merge = (s, key, val) => {
2863
- if (val && !s[key]) {
2864
- s[key] = val;
2865
- }
2866
- };
2867
- const targetShared = (treeShaking ? shared.treeShaking : shared);
2868
- merge(targetShared, 'loaded', loaded);
2869
- merge(targetShared, 'loading', loading);
2870
- merge(targetShared, 'get', get);
2871
- };
2872
2661
  scopes.forEach((sc) => {
2873
2662
  if (!this.shareScopeMap[sc]) {
2874
2663
  this.shareScopeMap[sc] = {};
@@ -2882,10 +2671,21 @@ class SharedHandler {
2882
2671
  scope: [sc],
2883
2672
  ...shareInfo,
2884
2673
  lib,
2674
+ loaded,
2675
+ loading,
2885
2676
  };
2677
+ if (get) {
2678
+ this.shareScopeMap[sc][pkgName][version].get = get;
2679
+ }
2680
+ return;
2886
2681
  }
2887
2682
  const registeredShared = this.shareScopeMap[sc][pkgName][version];
2888
- mergeAttrs(registeredShared);
2683
+ if (loading && !registeredShared.loading) {
2684
+ registeredShared.loading = loading;
2685
+ }
2686
+ if (loaded && !registeredShared.loaded) {
2687
+ registeredShared.loaded = loaded;
2688
+ }
2889
2689
  if (from && registeredShared.from !== from) {
2890
2690
  registeredShared.from = from;
2891
2691
  }
@@ -2913,7 +2713,6 @@ class RemoteHandler {
2913
2713
  generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
2914
2714
  // not used yet
2915
2715
  afterPreloadRemote: new AsyncHook(),
2916
- // TODO: Move to loaderHook
2917
2716
  loadEntry: new AsyncHook(),
2918
2717
  });
2919
2718
  this.host = host;
@@ -3262,7 +3061,7 @@ class ModuleFederation {
3262
3061
  // maybe will change, temporarily for internal use only
3263
3062
  initContainer: new AsyncWaterfallHook('initContainer'),
3264
3063
  });
3265
- this.version = "0.0.0-feat-shared-treeshake-poc-20260112063915";
3064
+ this.version = "0.0.0-feat-modern-3-0-20260113090730";
3266
3065
  this.moduleCache = new Map();
3267
3066
  this.loaderHook = new PluginSystem({
3268
3067
  // FIXME: may not be suitable , not open to the public yet
@@ -3343,7 +3142,7 @@ class ModuleFederation {
3343
3142
  this.sharedHandler.initShareScopeMap(scopeName, shareScope, extraOptions);
3344
3143
  }
3345
3144
  formatOptions(globalOptions, userOptions) {
3346
- const { allShareInfos: shared} = formatShareConfigs(globalOptions, userOptions);
3145
+ const { shared } = formatShareConfigs(globalOptions, userOptions);
3347
3146
  const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
3348
3147
  origin: this,
3349
3148
  userOptions,
@@ -3351,7 +3150,7 @@ class ModuleFederation {
3351
3150
  shareInfo: shared,
3352
3151
  });
3353
3152
  const remotes = this.remoteHandler.formatAndRegisterRemote(globalOptionsRes, userOptionsRes);
3354
- const { allShareInfos } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
3153
+ const { shared: handledShared } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
3355
3154
  const plugins = [...globalOptionsRes.plugins];
3356
3155
  if (userOptionsRes.plugins) {
3357
3156
  userOptionsRes.plugins.forEach((plugin) => {
@@ -3365,7 +3164,7 @@ class ModuleFederation {
3365
3164
  ...userOptions,
3366
3165
  plugins,
3367
3166
  remotes,
3368
- shared: allShareInfos,
3167
+ shared: handledShared,
3369
3168
  };
3370
3169
  this.hooks.lifecycle.init.emit({
3371
3170
  origin: this,