@module-federation/runtime-core 0.0.0-feat-shared-treeshake-poc-20251211025043 → 0.0.0-feat-rspack-async-startup-20251217123938

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -200,7 +200,7 @@ function getGlobalFederationConstructor() {
200
200
  function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
201
201
  if (isDebug) {
202
202
  CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
203
- CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.0-feat-shared-treeshake-poc-20251211025043";
203
+ CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.0-feat-rspack-async-startup-20251217123938";
204
204
  }
205
205
  }
206
206
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -807,9 +807,6 @@ function formatShare(shareArgs, from, name, shareStrategy) {
807
807
  throw new Error(`Can not get shared '${name}'!`);
808
808
  });
809
809
  }
810
- if (shareArgs.shareConfig?.eager && shareArgs.treeshake) {
811
- throw new Error('Can not set "eager:true" and "treeshake" at the same time!');
812
- }
813
810
  return {
814
811
  deps: [],
815
812
  useIn: [],
@@ -830,67 +827,37 @@ function formatShare(shareArgs, from, name, shareStrategy) {
830
827
  ? shareArgs.scope
831
828
  : [shareArgs.scope ?? 'default'],
832
829
  strategy: (shareArgs.strategy ?? shareStrategy) || 'version-first',
833
- treeshake: shareArgs.treeshake
834
- ? {
835
- ...shareArgs.treeshake,
836
- strategy: shareArgs.treeshake.strategy ?? 'server',
837
- status: shareArgs.treeshake.status ?? 1 /* TreeshakeStatus.UNKNOWN */,
838
- useIn: [],
839
- }
840
- : undefined,
841
830
  };
842
831
  }
843
- function formatShareConfigs(prevOptions, newOptions) {
844
- const shareArgs = newOptions.shared || {};
845
- const from = newOptions.name;
846
- const newShareInfos = Object.keys(shareArgs).reduce((res, pkgName) => {
832
+ function formatShareConfigs(globalOptions, userOptions) {
833
+ const shareArgs = userOptions.shared || {};
834
+ const from = userOptions.name;
835
+ const shareInfos = Object.keys(shareArgs).reduce((res, pkgName) => {
847
836
  const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
848
837
  res[pkgName] = res[pkgName] || [];
849
838
  arrayShareArgs.forEach((shareConfig) => {
850
- res[pkgName].push(formatShare(shareConfig, from, pkgName, newOptions.shareStrategy));
839
+ res[pkgName].push(formatShare(shareConfig, from, pkgName, userOptions.shareStrategy));
851
840
  });
852
841
  return res;
853
842
  }, {});
854
- const allShareInfos = {
855
- ...prevOptions.shared,
843
+ const shared = {
844
+ ...globalOptions.shared,
856
845
  };
857
- Object.keys(newShareInfos).forEach((shareKey) => {
858
- if (!allShareInfos[shareKey]) {
859
- allShareInfos[shareKey] = newShareInfos[shareKey];
846
+ Object.keys(shareInfos).forEach((shareKey) => {
847
+ if (!shared[shareKey]) {
848
+ shared[shareKey] = shareInfos[shareKey];
860
849
  }
861
850
  else {
862
- newShareInfos[shareKey].forEach((newUserSharedOptions) => {
863
- const isSameVersion = allShareInfos[shareKey].find((sharedVal) => sharedVal.version === newUserSharedOptions.version);
851
+ shareInfos[shareKey].forEach((newUserSharedOptions) => {
852
+ const isSameVersion = shared[shareKey].find((sharedVal) => sharedVal.version === newUserSharedOptions.version);
864
853
  if (!isSameVersion) {
865
- allShareInfos[shareKey].push(newUserSharedOptions);
854
+ shared[shareKey].push(newUserSharedOptions);
866
855
  }
867
856
  });
868
857
  }
869
858
  });
870
- return { allShareInfos, newShareInfos };
859
+ return { shared, shareInfos };
871
860
  }
872
- function shouldUseTreeshake(treeshake, usedExports) {
873
- if (!treeshake) {
874
- return false;
875
- }
876
- const { status, strategy } = treeshake;
877
- if (status === 0 /* TreeshakeStatus.NO_USE */) {
878
- return false;
879
- }
880
- if (status === 2 /* TreeshakeStatus.CALCULATED */) {
881
- return true;
882
- }
883
- if (strategy === 'infer') {
884
- if (!usedExports) {
885
- return true;
886
- }
887
- return isMatchUsedExports(treeshake, usedExports);
888
- }
889
- return false;
890
- }
891
- /**
892
- * compare version a and b, return true if a is less than b
893
- */
894
861
  function versionLt(a, b) {
895
862
  const transformInvalidVersion = (version) => {
896
863
  const isNumberVersion = !Number.isNaN(Number(version));
@@ -936,79 +903,19 @@ const isLoaded = (shared) => {
936
903
  const isLoading = (shared) => {
937
904
  return Boolean(shared.loading);
938
905
  };
939
- const isMatchUsedExports = (treeshake, usedExports) => {
940
- if (!treeshake || !usedExports) {
941
- return false;
942
- }
943
- const { usedExports: treeshakeUsedExports } = treeshake;
944
- if (!treeshakeUsedExports) {
945
- return false;
946
- }
947
- if (usedExports.every((e) => treeshakeUsedExports.includes(e))) {
948
- return true;
949
- }
950
- return false;
951
- };
952
- function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName, treeshake) {
906
+ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
953
907
  const versions = shareScopeMap[scope][pkgName];
954
- let version = '';
955
- let useTreeshake = shouldUseTreeshake(treeshake);
956
- // return false means use prev version
957
908
  const callback = function (prev, cur) {
958
- if (useTreeshake) {
959
- if (!versions[prev].treeshake) {
960
- return true;
961
- }
962
- if (!versions[cur].treeshake) {
963
- return false;
964
- }
965
- return !isLoaded(versions[prev].treeshake) && versionLt(prev, cur);
966
- }
967
909
  return !isLoaded(versions[prev]) && versionLt(prev, cur);
968
910
  };
969
- if (useTreeshake) {
970
- version = findVersion(shareScopeMap[scope][pkgName], callback);
971
- if (version) {
972
- return {
973
- version,
974
- useTreeshake,
975
- };
976
- }
977
- useTreeshake = false;
978
- }
979
- return {
980
- version: findVersion(shareScopeMap[scope][pkgName], callback),
981
- useTreeshake,
982
- };
911
+ return findVersion(shareScopeMap[scope][pkgName], callback);
983
912
  }
984
- const isLoadingOrLoaded = (shared) => {
985
- return isLoaded(shared) || isLoading(shared);
986
- };
987
- function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName, treeshake) {
913
+ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
988
914
  const versions = shareScopeMap[scope][pkgName];
989
- let version = '';
990
- let useTreeshake = shouldUseTreeshake(treeshake);
991
- // return false means use prev version
992
915
  const callback = function (prev, cur) {
993
- if (useTreeshake) {
994
- if (!versions[prev].treeshake) {
995
- return true;
996
- }
997
- if (!versions[cur].treeshake) {
998
- return false;
999
- }
1000
- if (isLoadingOrLoaded(versions[cur].treeshake)) {
1001
- if (isLoadingOrLoaded(versions[prev].treeshake)) {
1002
- return Boolean(versionLt(prev, cur));
1003
- }
1004
- else {
1005
- return true;
1006
- }
1007
- }
1008
- if (isLoadingOrLoaded(versions[prev].treeshake)) {
1009
- return false;
1010
- }
1011
- }
916
+ const isLoadingOrLoaded = (shared) => {
917
+ return isLoaded(shared) || isLoading(shared);
918
+ };
1012
919
  if (isLoadingOrLoaded(versions[cur])) {
1013
920
  if (isLoadingOrLoaded(versions[prev])) {
1014
921
  return Boolean(versionLt(prev, cur));
@@ -1022,20 +929,7 @@ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName, treesh
1022
929
  }
1023
930
  return versionLt(prev, cur);
1024
931
  };
1025
- if (useTreeshake) {
1026
- version = findVersion(shareScopeMap[scope][pkgName], callback);
1027
- if (version) {
1028
- return {
1029
- version,
1030
- useTreeshake,
1031
- };
1032
- }
1033
- useTreeshake = false;
1034
- }
1035
- return {
1036
- version: findVersion(shareScopeMap[scope][pkgName], callback),
1037
- useTreeshake,
1038
- };
932
+ return findVersion(shareScopeMap[scope][pkgName], callback);
1039
933
  }
1040
934
  function getFindShareFunction(strategy) {
1041
935
  if (strategy === 'loaded-first') {
@@ -1047,7 +941,7 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
1047
941
  if (!localShareScopeMap) {
1048
942
  return;
1049
943
  }
1050
- const { shareConfig, scope = DEFAULT_SCOPE, strategy, treeshake } = shareInfo;
944
+ const { shareConfig, scope = DEFAULT_SCOPE, strategy } = shareInfo;
1051
945
  const scopes = Array.isArray(scope) ? scope : [scope];
1052
946
  for (const sc of scopes) {
1053
947
  if (shareConfig &&
@@ -1055,13 +949,14 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
1055
949
  localShareScopeMap[sc][pkgName]) {
1056
950
  const { requiredVersion } = shareConfig;
1057
951
  const findShareFunction = getFindShareFunction(strategy);
1058
- const { version: maxOrSingletonVersion, useTreeshake } = findShareFunction(localShareScopeMap, sc, pkgName, treeshake);
952
+ const maxOrSingletonVersion = findShareFunction(localShareScopeMap, sc, pkgName);
953
+ //@ts-ignore
1059
954
  const defaultResolver = () => {
1060
- const shared = localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1061
955
  if (shareConfig.singleton) {
1062
956
  if (typeof requiredVersion === 'string' &&
1063
957
  !satisfy(maxOrSingletonVersion, requiredVersion)) {
1064
- const msg = `Version ${maxOrSingletonVersion} from ${maxOrSingletonVersion && shared.from} of shared singleton module ${pkgName} does not satisfy the requirement of ${shareInfo.from} which needs ${requiredVersion})`;
958
+ const msg = `Version ${maxOrSingletonVersion} from ${maxOrSingletonVersion &&
959
+ localShareScopeMap[sc][pkgName][maxOrSingletonVersion].from} of shared singleton module ${pkgName} does not satisfy the requirement of ${shareInfo.from} which needs ${requiredVersion})`;
1065
960
  if (shareConfig.strictVersion) {
1066
961
  error(msg);
1067
962
  }
@@ -1069,48 +964,21 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
1069
964
  warn(msg);
1070
965
  }
1071
966
  }
1072
- return {
1073
- shared,
1074
- useTreeshake,
1075
- };
967
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1076
968
  }
1077
969
  else {
1078
970
  if (requiredVersion === false || requiredVersion === '*') {
1079
- return {
1080
- shared,
1081
- useTreeshake,
1082
- };
971
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1083
972
  }
1084
973
  if (satisfy(maxOrSingletonVersion, requiredVersion)) {
1085
- return {
1086
- shared,
1087
- useTreeshake,
1088
- };
1089
- }
1090
- const _usedTreeshake = shouldUseTreeshake(treeshake);
1091
- if (_usedTreeshake) {
1092
- for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
1093
- if (!shouldUseTreeshake(versionValue.treeshake, treeshake?.usedExports)) {
1094
- continue;
1095
- }
1096
- if (satisfy(versionKey, requiredVersion)) {
1097
- return {
1098
- shared: versionValue,
1099
- useTreeshake: _usedTreeshake,
1100
- };
1101
- }
1102
- }
974
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1103
975
  }
1104
976
  for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
1105
977
  if (satisfy(versionKey, requiredVersion)) {
1106
- return {
1107
- shared: versionValue,
1108
- useTreeshake: false,
1109
- };
978
+ return versionValue;
1110
979
  }
1111
980
  }
1112
981
  }
1113
- return;
1114
982
  };
1115
983
  const params = {
1116
984
  shareScopeMap: localShareScopeMap,
@@ -1118,7 +986,6 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
1118
986
  pkgName,
1119
987
  version: maxOrSingletonVersion,
1120
988
  GlobalFederation: Global.__FEDERATION__,
1121
- shareInfo,
1122
989
  resolver: defaultResolver,
1123
990
  };
1124
991
  const resolveShared = resolveShare.emit(params) || params;
@@ -1140,47 +1007,13 @@ function getTargetSharedOptions(options) {
1140
1007
  shareVersionMap[shared.version] = shared;
1141
1008
  });
1142
1009
  const callback = function (prev, cur) {
1143
- return (
1144
- // TODO: consider multiple treeshake shared scenes
1145
- !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur));
1010
+ return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
1146
1011
  };
1147
1012
  const maxVersion = findVersion(shareVersionMap, callback);
1148
1013
  return shareVersionMap[maxVersion];
1149
1014
  };
1150
1015
  const resolver = extraOptions?.resolver ?? defaultResolver;
1151
- const isPlainObject = (val) => {
1152
- return val !== null && typeof val === 'object' && !Array.isArray(val);
1153
- };
1154
- const merge = (...sources) => {
1155
- const out = {};
1156
- for (const src of sources) {
1157
- if (!src)
1158
- continue;
1159
- for (const [key, value] of Object.entries(src)) {
1160
- const prev = out[key];
1161
- if (isPlainObject(prev) && isPlainObject(value)) {
1162
- out[key] = merge(prev, value);
1163
- }
1164
- else if (value !== undefined) {
1165
- out[key] = value;
1166
- }
1167
- }
1168
- }
1169
- return out;
1170
- };
1171
- return merge(resolver(shareInfos[pkgName]), extraOptions?.customShareInfo);
1172
- }
1173
- const addUseIn = (shared, from) => {
1174
- if (!shared.useIn) {
1175
- shared.useIn = [];
1176
- }
1177
- addUniqueItem(shared.useIn, from);
1178
- };
1179
- function directShare(shared, useTreeshake) {
1180
- if (useTreeshake && shared.treeshake) {
1181
- return shared.treeshake;
1182
- }
1183
- return shared;
1016
+ return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions?.customShareInfo);
1184
1017
  }
1185
1018
 
1186
1019
  function getBuilderId() {
@@ -1692,40 +1525,6 @@ var helpers = {
1692
1525
  },
1693
1526
  };
1694
1527
 
1695
- function createRemoteEntryInitOptions(remoteInfo, hostShareScopeMap) {
1696
- const localShareScopeMap = hostShareScopeMap;
1697
- const shareScopeKeys = Array.isArray(remoteInfo.shareScope)
1698
- ? remoteInfo.shareScope
1699
- : [remoteInfo.shareScope];
1700
- if (!shareScopeKeys.length) {
1701
- shareScopeKeys.push('default');
1702
- }
1703
- shareScopeKeys.forEach((shareScopeKey) => {
1704
- if (!localShareScopeMap[shareScopeKey]) {
1705
- localShareScopeMap[shareScopeKey] = {};
1706
- }
1707
- });
1708
- const remoteEntryInitOptions = {
1709
- version: remoteInfo.version || '',
1710
- shareScopeKeys: Array.isArray(remoteInfo.shareScope)
1711
- ? shareScopeKeys
1712
- : remoteInfo.shareScope || 'default',
1713
- };
1714
- // Help to find host instance
1715
- Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
1716
- value: localShareScopeMap,
1717
- // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
1718
- enumerable: false,
1719
- });
1720
- // TODO: compate legacy init params, should use shareScopeMap if exist
1721
- const shareScope = localShareScopeMap[shareScopeKeys[0]];
1722
- const initScope = [];
1723
- return {
1724
- remoteEntryInitOptions,
1725
- shareScope,
1726
- initScope,
1727
- };
1728
- }
1729
1528
  class Module {
1730
1529
  constructor({ remoteInfo, host, }) {
1731
1530
  this.inited = false;
@@ -1737,7 +1536,8 @@ class Module {
1737
1536
  if (this.remoteEntryExports) {
1738
1537
  return this.remoteEntryExports;
1739
1538
  }
1740
- const remoteEntryExports = await getRemoteEntry({
1539
+ let remoteEntryExports;
1540
+ remoteEntryExports = await getRemoteEntry({
1741
1541
  origin: this.host,
1742
1542
  remoteInfo: this.remoteInfo,
1743
1543
  remoteEntryExports: this.remoteEntryExports,
@@ -1752,7 +1552,33 @@ class Module {
1752
1552
  // Get remoteEntry.js
1753
1553
  const remoteEntryExports = await this.getEntry();
1754
1554
  if (!this.inited) {
1755
- const { remoteEntryInitOptions, shareScope, initScope } = createRemoteEntryInitOptions(this.remoteInfo, this.host.shareScopeMap);
1555
+ const localShareScopeMap = this.host.shareScopeMap;
1556
+ const shareScopeKeys = Array.isArray(this.remoteInfo.shareScope)
1557
+ ? this.remoteInfo.shareScope
1558
+ : [this.remoteInfo.shareScope];
1559
+ if (!shareScopeKeys.length) {
1560
+ shareScopeKeys.push('default');
1561
+ }
1562
+ shareScopeKeys.forEach((shareScopeKey) => {
1563
+ if (!localShareScopeMap[shareScopeKey]) {
1564
+ localShareScopeMap[shareScopeKey] = {};
1565
+ }
1566
+ });
1567
+ // TODO: compate legacy init params, should use shareScopeMap if exist
1568
+ const shareScope = localShareScopeMap[shareScopeKeys[0]];
1569
+ const initScope = [];
1570
+ const remoteEntryInitOptions = {
1571
+ version: this.remoteInfo.version || '',
1572
+ shareScopeKeys: Array.isArray(this.remoteInfo.shareScope)
1573
+ ? shareScopeKeys
1574
+ : this.remoteInfo.shareScope || 'default',
1575
+ };
1576
+ // Help to find host instance
1577
+ Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
1578
+ value: localShareScopeMap,
1579
+ // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
1580
+ enumerable: false,
1581
+ });
1756
1582
  const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
1757
1583
  shareScope,
1758
1584
  // @ts-ignore shareScopeMap will be set by Object.defineProperty
@@ -2232,7 +2058,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
2232
2058
  }, true, memo, remoteSnapshot);
2233
2059
  if (remoteSnapshot.shared && remoteSnapshot.shared.length > 0) {
2234
2060
  const collectSharedAssets = (shareInfo, snapshotShared) => {
2235
- const { shared: registeredShared } = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare) || {};
2061
+ const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare);
2236
2062
  // 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.
2237
2063
  if (registeredShared && typeof registeredShared.lib === 'function') {
2238
2064
  snapshotShared.assets.js.sync.forEach((asset) => {
@@ -2523,7 +2349,6 @@ class SnapshotHandler {
2523
2349
  class SharedHandler {
2524
2350
  constructor(host) {
2525
2351
  this.hooks = new PluginSystem({
2526
- beforeRegisterShare: new SyncWaterfallHook('beforeRegisterShare'),
2527
2352
  afterResolve: new AsyncWaterfallHook('afterResolve'),
2528
2353
  beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
2529
2354
  // not used yet
@@ -2539,17 +2364,12 @@ class SharedHandler {
2539
2364
  }
2540
2365
  // register shared in shareScopeMap
2541
2366
  registerShared(globalOptions, userOptions) {
2542
- const { newShareInfos, allShareInfos } = formatShareConfigs(globalOptions, userOptions);
2543
- const sharedKeys = Object.keys(newShareInfos);
2367
+ const { shareInfos, shared } = formatShareConfigs(globalOptions, userOptions);
2368
+ const sharedKeys = Object.keys(shareInfos);
2544
2369
  sharedKeys.forEach((sharedKey) => {
2545
- const sharedVals = newShareInfos[sharedKey];
2370
+ const sharedVals = shareInfos[sharedKey];
2546
2371
  sharedVals.forEach((sharedVal) => {
2547
2372
  sharedVal.scope.forEach((sc) => {
2548
- this.hooks.lifecycle.beforeRegisterShare.emit({
2549
- origin: this.host,
2550
- pkgName: sharedKey,
2551
- shared: sharedVal,
2552
- });
2553
2373
  const registeredShared = this.shareScopeMap[sc]?.[sharedKey];
2554
2374
  if (!registeredShared) {
2555
2375
  this.setShared({
@@ -2565,8 +2385,8 @@ class SharedHandler {
2565
2385
  });
2566
2386
  });
2567
2387
  return {
2568
- newShareInfos,
2569
- allShareInfos,
2388
+ shareInfos,
2389
+ shared,
2570
2390
  };
2571
2391
  }
2572
2392
  async loadShare(pkgName, extraOptions) {
@@ -2597,59 +2417,61 @@ class SharedHandler {
2597
2417
  const { shareInfo: shareOptionsRes } = loadShareRes;
2598
2418
  // Assert that shareInfoRes exists, if not, throw an error
2599
2419
  assert(shareOptionsRes, `Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
2600
- const { shared: registeredShared, useTreeshake } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
2601
- if (registeredShared) {
2602
- const targetShared = directShare(registeredShared, useTreeshake);
2603
- if (targetShared.lib) {
2604
- addUseIn(targetShared, host.options.name);
2605
- return targetShared.lib;
2606
- }
2607
- else if (targetShared.loading && !targetShared.loaded) {
2608
- const factory = await targetShared.loading;
2609
- targetShared.loaded = true;
2610
- if (!targetShared.lib) {
2611
- targetShared.lib = factory;
2612
- }
2613
- addUseIn(targetShared, host.options.name);
2614
- return factory;
2615
- }
2616
- else {
2617
- const asyncLoadProcess = async () => {
2618
- const factory = await targetShared.get();
2619
- addUseIn(targetShared, host.options.name);
2620
- targetShared.loaded = true;
2621
- targetShared.lib = factory;
2622
- return factory;
2623
- };
2624
- const loading = asyncLoadProcess();
2625
- this.setShared({
2626
- pkgName,
2627
- loaded: false,
2628
- shared: registeredShared,
2629
- from: host.options.name,
2630
- lib: null,
2631
- loading,
2632
- treeshake: useTreeshake ? targetShared : undefined,
2633
- });
2634
- return loading;
2420
+ // Retrieve from cache
2421
+ const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
2422
+ const addUseIn = (shared) => {
2423
+ if (!shared.useIn) {
2424
+ shared.useIn = [];
2635
2425
  }
2426
+ addUniqueItem(shared.useIn, host.options.name);
2427
+ };
2428
+ if (registeredShared && registeredShared.lib) {
2429
+ addUseIn(registeredShared);
2430
+ return registeredShared.lib;
2431
+ }
2432
+ else if (registeredShared &&
2433
+ registeredShared.loading &&
2434
+ !registeredShared.loaded) {
2435
+ const factory = await registeredShared.loading;
2436
+ registeredShared.loaded = true;
2437
+ if (!registeredShared.lib) {
2438
+ registeredShared.lib = factory;
2439
+ }
2440
+ addUseIn(registeredShared);
2441
+ return factory;
2442
+ }
2443
+ else if (registeredShared) {
2444
+ const asyncLoadProcess = async () => {
2445
+ const factory = await registeredShared.get();
2446
+ addUseIn(registeredShared);
2447
+ registeredShared.loaded = true;
2448
+ registeredShared.lib = factory;
2449
+ return factory;
2450
+ };
2451
+ const loading = asyncLoadProcess();
2452
+ this.setShared({
2453
+ pkgName,
2454
+ loaded: false,
2455
+ shared: registeredShared,
2456
+ from: host.options.name,
2457
+ lib: null,
2458
+ loading,
2459
+ });
2460
+ return loading;
2636
2461
  }
2637
2462
  else {
2638
2463
  if (extraOptions?.customShareInfo) {
2639
2464
  return false;
2640
2465
  }
2641
- const _useTreeshake = shouldUseTreeshake(shareOptionsRes.treeshake);
2642
- const targetShared = directShare(shareOptionsRes, _useTreeshake);
2643
2466
  const asyncLoadProcess = async () => {
2644
- const factory = await targetShared.get();
2645
- targetShared.lib = factory;
2646
- targetShared.loaded = true;
2647
- addUseIn(targetShared, host.options.name);
2648
- const { shared: gShared, useTreeshake: gUseTreeshake } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
2467
+ const factory = await shareOptionsRes.get();
2468
+ shareOptionsRes.lib = factory;
2469
+ shareOptionsRes.loaded = true;
2470
+ addUseIn(shareOptionsRes);
2471
+ const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
2649
2472
  if (gShared) {
2650
- const targetGShared = directShare(gShared, gUseTreeshake);
2651
- targetGShared.lib = factory;
2652
- targetGShared.loaded = true;
2473
+ gShared.lib = factory;
2474
+ gShared.loaded = true;
2653
2475
  gShared.from = shareOptionsRes.from;
2654
2476
  }
2655
2477
  return factory;
@@ -2662,7 +2484,6 @@ class SharedHandler {
2662
2484
  from: host.options.name,
2663
2485
  lib: null,
2664
2486
  loading,
2665
- treeshake: _useTreeshake ? targetShared : undefined,
2666
2487
  });
2667
2488
  return loading;
2668
2489
  }
@@ -2702,20 +2523,19 @@ class SharedHandler {
2702
2523
  const { version, eager } = shared;
2703
2524
  scope[name] = scope[name] || {};
2704
2525
  const versions = scope[name];
2705
- const activeVersion = versions[version] && directShare(versions[version]);
2526
+ const activeVersion = versions[version];
2706
2527
  const activeVersionEager = Boolean(activeVersion &&
2707
- (('eager' in activeVersion && activeVersion.eager) ||
2708
- ('shareConfig' in activeVersion &&
2709
- activeVersion.shareConfig?.eager)));
2528
+ (activeVersion.eager || activeVersion.shareConfig?.eager));
2710
2529
  if (!activeVersion ||
2711
2530
  (activeVersion.strategy !== 'loaded-first' &&
2712
2531
  !activeVersion.loaded &&
2713
2532
  (Boolean(!eager) !== !activeVersionEager
2714
2533
  ? eager
2715
- : hostName > versions[version].from))) {
2534
+ : hostName > activeVersion.from))) {
2716
2535
  versions[version] = shared;
2717
2536
  }
2718
2537
  };
2538
+ const initFn = (mod) => mod && mod.init && mod.init(shareScope[shareScopeName], initScope);
2719
2539
  const initRemoteModule = async (key) => {
2720
2540
  const { module } = await host.remoteHandler.getRemoteModuleAndOptions({
2721
2541
  id: key,
@@ -2736,12 +2556,6 @@ class SharedHandler {
2736
2556
  }));
2737
2557
  }
2738
2558
  if (!module.inited) {
2739
- const initFn = (mod) => {
2740
- const { remoteEntryInitOptions } = createRemoteEntryInitOptions(module.remoteInfo, host.shareScopeMap);
2741
- return (mod &&
2742
- mod.init &&
2743
- mod.init(shareScope[shareScopeName], initScope, remoteEntryInitOptions));
2744
- };
2745
2559
  await initFn(remoteEntryExports);
2746
2560
  module.inited = true;
2747
2561
  }
@@ -2782,10 +2596,16 @@ class SharedHandler {
2782
2596
  this.initializeSharing(shareScope, { strategy: shareOptions.strategy });
2783
2597
  });
2784
2598
  }
2785
- const { shared: registeredShared, useTreeshake } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare) || {};
2599
+ const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare);
2600
+ const addUseIn = (shared) => {
2601
+ if (!shared.useIn) {
2602
+ shared.useIn = [];
2603
+ }
2604
+ addUniqueItem(shared.useIn, host.options.name);
2605
+ };
2786
2606
  if (registeredShared) {
2787
2607
  if (typeof registeredShared.lib === 'function') {
2788
- addUseIn(registeredShared, host.options.name);
2608
+ addUseIn(registeredShared);
2789
2609
  if (!registeredShared.loaded) {
2790
2610
  registeredShared.loaded = true;
2791
2611
  if (registeredShared.from === host.options.name) {
@@ -2797,7 +2617,7 @@ class SharedHandler {
2797
2617
  if (typeof registeredShared.get === 'function') {
2798
2618
  const module = registeredShared.get();
2799
2619
  if (!(module instanceof Promise)) {
2800
- addUseIn(registeredShared, host.options.name);
2620
+ addUseIn(registeredShared);
2801
2621
  this.setShared({
2802
2622
  pkgName,
2803
2623
  loaded: true,
@@ -2841,39 +2661,6 @@ class SharedHandler {
2841
2661
  }
2842
2662
  initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
2843
2663
  const { host } = this;
2844
- // const existedShareScope = this.shareScopeMap[scopeName];
2845
- // if (existedShareScope) {
2846
- // Object.entries(shareScope).forEach(([pkgName, newVersions]) => {
2847
- // const existedShareMap = existedShareScope[pkgName];
2848
- // if (!existedShareMap) {
2849
- // return;
2850
- // }
2851
- // Object.entries(existedShareMap).forEach(([version, existedShared]) => {
2852
- // if (!shareScope[pkgName][version]) {
2853
- // shareScope[pkgName][version] = existedShared;
2854
- // }
2855
- // // const newShared = newVersions[version];
2856
- // // if (
2857
- // // newShared &&
2858
- // // newShared.treeshakeStatus === TreeshakeStatus.UNKNOWN &&
2859
- // // newShared.usedExports &&
2860
- // // existedShared.usedExports &&
2861
- // // existedShared.usedExports.some(
2862
- // // (exportName) => !newShared.usedExports?.includes(exportName),
2863
- // // )
2864
- // // ) {
2865
- // // newShared.treeshakeStatus = TreeshakeStatus.NO_USE;
2866
- // // newShared._noMatchedUsedExports =
2867
- // // existedShared._noMatchedUsedExports || [];
2868
- // // const item: NoMatchedUsedExportsItem = [existedShared.from];
2869
- // // if (isDebugMode() && existedShared.usedExports) {
2870
- // // item.push(existedShared.usedExports);
2871
- // // }
2872
- // // newShared._noMatchedUsedExports.push(item);
2873
- // // }
2874
- // });
2875
- // });
2876
- // }
2877
2664
  this.shareScopeMap[scopeName] = shareScope;
2878
2665
  this.hooks.lifecycle.initContainerShareScopeMap.emit({
2879
2666
  shareScope,
@@ -2883,20 +2670,9 @@ class SharedHandler {
2883
2670
  hostShareScopeMap: extraOptions.hostShareScopeMap,
2884
2671
  });
2885
2672
  }
2886
- setShared({ pkgName, shared, from, lib, loading, loaded, get, treeshake, }) {
2673
+ setShared({ pkgName, shared, from, lib, loading, loaded, get, }) {
2887
2674
  const { version, scope = 'default', ...shareInfo } = shared;
2888
2675
  const scopes = Array.isArray(scope) ? scope : [scope];
2889
- const mergeAttrs = (shared) => {
2890
- const merge = (s, key, val) => {
2891
- if (val && !s[key]) {
2892
- s[key] = val;
2893
- }
2894
- };
2895
- const targetShared = (treeshake ? shared.treeshake : shared);
2896
- merge(targetShared, 'loaded', loaded);
2897
- merge(targetShared, 'loading', loading);
2898
- merge(targetShared, 'get', get);
2899
- };
2900
2676
  scopes.forEach((sc) => {
2901
2677
  if (!this.shareScopeMap[sc]) {
2902
2678
  this.shareScopeMap[sc] = {};
@@ -2910,10 +2686,21 @@ class SharedHandler {
2910
2686
  scope: [sc],
2911
2687
  ...shareInfo,
2912
2688
  lib,
2689
+ loaded,
2690
+ loading,
2913
2691
  };
2692
+ if (get) {
2693
+ this.shareScopeMap[sc][pkgName][version].get = get;
2694
+ }
2695
+ return;
2914
2696
  }
2915
2697
  const registeredShared = this.shareScopeMap[sc][pkgName][version];
2916
- mergeAttrs(registeredShared);
2698
+ if (loading && !registeredShared.loading) {
2699
+ registeredShared.loading = loading;
2700
+ }
2701
+ if (loaded && !registeredShared.loaded) {
2702
+ registeredShared.loaded = loaded;
2703
+ }
2917
2704
  if (from && registeredShared.from !== from) {
2918
2705
  registeredShared.from = from;
2919
2706
  }
@@ -2941,7 +2728,6 @@ class RemoteHandler {
2941
2728
  generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
2942
2729
  // not used yet
2943
2730
  afterPreloadRemote: new AsyncHook(),
2944
- // TODO: Move to loaderHook
2945
2731
  loadEntry: new AsyncHook(),
2946
2732
  });
2947
2733
  this.host = host;
@@ -3290,7 +3076,7 @@ class ModuleFederation {
3290
3076
  // maybe will change, temporarily for internal use only
3291
3077
  initContainer: new AsyncWaterfallHook('initContainer'),
3292
3078
  });
3293
- this.version = "0.0.0-feat-shared-treeshake-poc-20251211025043";
3079
+ this.version = "0.0.0-feat-rspack-async-startup-20251217123938";
3294
3080
  this.moduleCache = new Map();
3295
3081
  this.loaderHook = new PluginSystem({
3296
3082
  // FIXME: may not be suitable , not open to the public yet
@@ -3371,7 +3157,7 @@ class ModuleFederation {
3371
3157
  this.sharedHandler.initShareScopeMap(scopeName, shareScope, extraOptions);
3372
3158
  }
3373
3159
  formatOptions(globalOptions, userOptions) {
3374
- const { allShareInfos: shared} = formatShareConfigs(globalOptions, userOptions);
3160
+ const { shared } = formatShareConfigs(globalOptions, userOptions);
3375
3161
  const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
3376
3162
  origin: this,
3377
3163
  userOptions,
@@ -3379,7 +3165,7 @@ class ModuleFederation {
3379
3165
  shareInfo: shared,
3380
3166
  });
3381
3167
  const remotes = this.remoteHandler.formatAndRegisterRemote(globalOptionsRes, userOptionsRes);
3382
- const { allShareInfos } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
3168
+ const { shared: handledShared } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
3383
3169
  const plugins = [...globalOptionsRes.plugins];
3384
3170
  if (userOptionsRes.plugins) {
3385
3171
  userOptionsRes.plugins.forEach((plugin) => {
@@ -3393,7 +3179,7 @@ class ModuleFederation {
3393
3179
  ...userOptions,
3394
3180
  plugins,
3395
3181
  remotes,
3396
- shared: allShareInfos,
3182
+ shared: handledShared,
3397
3183
  };
3398
3184
  this.hooks.lifecycle.init.emit({
3399
3185
  origin: this,