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

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-20251211144042";
204
+ CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.0-feat-rspack-async-startup-20251223091151";
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.treeshake) {
812
- throw new Error('Can not set "eager:true" and "treeshake" 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
- treeshake: shareArgs.treeshake
835
- ? {
836
- ...shareArgs.treeshake,
837
- strategy: shareArgs.treeshake.strategy ?? 'server',
838
- status: shareArgs.treeshake.status ?? 1 /* TreeshakeStatus.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 };
860
+ return { shared, shareInfos };
872
861
  }
873
- function shouldUseTreeshake(treeshake, usedExports) {
874
- if (!treeshake) {
875
- return false;
876
- }
877
- const { status, strategy } = treeshake;
878
- if (status === 0 /* TreeshakeStatus.NO_USE */) {
879
- return false;
880
- }
881
- if (status === 2 /* TreeshakeStatus.CALCULATED */) {
882
- return true;
883
- }
884
- if (strategy === 'infer') {
885
- if (!usedExports) {
886
- return true;
887
- }
888
- return isMatchUsedExports(treeshake, usedExports);
889
- }
890
- return false;
891
- }
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 = (treeshake, usedExports) => {
941
- if (!treeshake || !usedExports) {
942
- return false;
943
- }
944
- const { usedExports: treeshakeUsedExports } = treeshake;
945
- if (!treeshakeUsedExports) {
946
- return false;
947
- }
948
- if (usedExports.every((e) => treeshakeUsedExports.includes(e))) {
949
- return true;
950
- }
951
- return false;
952
- };
953
- function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName, treeshake) {
907
+ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
954
908
  const versions = shareScopeMap[scope][pkgName];
955
- let version = '';
956
- let useTreeshake = shouldUseTreeshake(treeshake);
957
- // return false means use prev version
958
909
  const callback = function (prev, cur) {
959
- if (useTreeshake) {
960
- if (!versions[prev].treeshake) {
961
- return true;
962
- }
963
- if (!versions[cur].treeshake) {
964
- return false;
965
- }
966
- return !isLoaded(versions[prev].treeshake) && versionLt(prev, cur);
967
- }
968
910
  return !isLoaded(versions[prev]) && versionLt(prev, cur);
969
911
  };
970
- if (useTreeshake) {
971
- version = findVersion(shareScopeMap[scope][pkgName], callback);
972
- if (version) {
973
- return {
974
- version,
975
- useTreeshake,
976
- };
977
- }
978
- useTreeshake = false;
979
- }
980
- return {
981
- version: findVersion(shareScopeMap[scope][pkgName], callback),
982
- useTreeshake,
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, treeshake) {
914
+ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
989
915
  const versions = shareScopeMap[scope][pkgName];
990
- let version = '';
991
- let useTreeshake = shouldUseTreeshake(treeshake);
992
- // return false means use prev version
993
916
  const callback = function (prev, cur) {
994
- if (useTreeshake) {
995
- if (!versions[prev].treeshake) {
996
- return true;
997
- }
998
- if (!versions[cur].treeshake) {
999
- return false;
1000
- }
1001
- if (isLoadingOrLoaded(versions[cur].treeshake)) {
1002
- if (isLoadingOrLoaded(versions[prev].treeshake)) {
1003
- return Boolean(versionLt(prev, cur));
1004
- }
1005
- else {
1006
- return true;
1007
- }
1008
- }
1009
- if (isLoadingOrLoaded(versions[prev].treeshake)) {
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 (useTreeshake) {
1027
- version = findVersion(shareScopeMap[scope][pkgName], callback);
1028
- if (version) {
1029
- return {
1030
- version,
1031
- useTreeshake,
1032
- };
1033
- }
1034
- useTreeshake = false;
1035
- }
1036
- return {
1037
- version: findVersion(shareScopeMap[scope][pkgName], callback),
1038
- useTreeshake,
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, treeshake } = 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, useTreeshake } = findShareFunction(localShareScopeMap, sc, pkgName, treeshake);
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
- useTreeshake,
1076
- };
968
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1077
969
  }
1078
970
  else {
1079
971
  if (requiredVersion === false || requiredVersion === '*') {
1080
- return {
1081
- shared,
1082
- useTreeshake,
1083
- };
972
+ return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
1084
973
  }
1085
974
  if (satisfy(maxOrSingletonVersion, requiredVersion)) {
1086
- return {
1087
- shared,
1088
- useTreeshake,
1089
- };
1090
- }
1091
- const _usedTreeshake = shouldUseTreeshake(treeshake);
1092
- if (_usedTreeshake) {
1093
- for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
1094
- if (!shouldUseTreeshake(versionValue.treeshake, treeshake?.usedExports)) {
1095
- continue;
1096
- }
1097
- if (satisfy(versionKey, requiredVersion)) {
1098
- return {
1099
- shared: versionValue,
1100
- useTreeshake: _usedTreeshake,
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
- useTreeshake: 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 treeshake 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, useTreeshake) {
1181
- if (useTreeshake && shared.treeshake) {
1182
- return shared.treeshake;
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;
@@ -1738,7 +1537,8 @@ class Module {
1738
1537
  if (this.remoteEntryExports) {
1739
1538
  return this.remoteEntryExports;
1740
1539
  }
1741
- const remoteEntryExports = await getRemoteEntry({
1540
+ let remoteEntryExports;
1541
+ remoteEntryExports = await getRemoteEntry({
1742
1542
  origin: this.host,
1743
1543
  remoteInfo: this.remoteInfo,
1744
1544
  remoteEntryExports: this.remoteEntryExports,
@@ -1753,7 +1553,33 @@ class Module {
1753
1553
  // Get remoteEntry.js
1754
1554
  const remoteEntryExports = await this.getEntry();
1755
1555
  if (!this.inited) {
1756
- const { remoteEntryInitOptions, shareScope, initScope } = createRemoteEntryInitOptions(this.remoteInfo, this.host.shareScopeMap);
1556
+ const localShareScopeMap = this.host.shareScopeMap;
1557
+ const shareScopeKeys = Array.isArray(this.remoteInfo.shareScope)
1558
+ ? this.remoteInfo.shareScope
1559
+ : [this.remoteInfo.shareScope];
1560
+ if (!shareScopeKeys.length) {
1561
+ shareScopeKeys.push('default');
1562
+ }
1563
+ shareScopeKeys.forEach((shareScopeKey) => {
1564
+ if (!localShareScopeMap[shareScopeKey]) {
1565
+ localShareScopeMap[shareScopeKey] = {};
1566
+ }
1567
+ });
1568
+ // TODO: compate legacy init params, should use shareScopeMap if exist
1569
+ const shareScope = localShareScopeMap[shareScopeKeys[0]];
1570
+ const initScope = [];
1571
+ const remoteEntryInitOptions = {
1572
+ version: this.remoteInfo.version || '',
1573
+ shareScopeKeys: Array.isArray(this.remoteInfo.shareScope)
1574
+ ? shareScopeKeys
1575
+ : this.remoteInfo.shareScope || 'default',
1576
+ };
1577
+ // Help to find host instance
1578
+ Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
1579
+ value: localShareScopeMap,
1580
+ // remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
1581
+ enumerable: false,
1582
+ });
1757
1583
  const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
1758
1584
  shareScope,
1759
1585
  // @ts-ignore shareScopeMap will be set by Object.defineProperty
@@ -2233,7 +2059,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
2233
2059
  }, true, memo, remoteSnapshot);
2234
2060
  if (remoteSnapshot.shared && remoteSnapshot.shared.length > 0) {
2235
2061
  const collectSharedAssets = (shareInfo, snapshotShared) => {
2236
- const { shared: registeredShared } = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare) || {};
2062
+ const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare);
2237
2063
  // 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
2064
  if (registeredShared && typeof registeredShared.lib === 'function') {
2239
2065
  snapshotShared.assets.js.sync.forEach((asset) => {
@@ -2524,7 +2350,6 @@ class SnapshotHandler {
2524
2350
  class SharedHandler {
2525
2351
  constructor(host) {
2526
2352
  this.hooks = new PluginSystem({
2527
- beforeRegisterShare: new SyncWaterfallHook('beforeRegisterShare'),
2528
2353
  afterResolve: new AsyncWaterfallHook('afterResolve'),
2529
2354
  beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
2530
2355
  // not used yet
@@ -2540,17 +2365,12 @@ class SharedHandler {
2540
2365
  }
2541
2366
  // register shared in shareScopeMap
2542
2367
  registerShared(globalOptions, userOptions) {
2543
- const { newShareInfos, allShareInfos } = formatShareConfigs(globalOptions, userOptions);
2544
- const sharedKeys = Object.keys(newShareInfos);
2368
+ const { shareInfos, shared } = formatShareConfigs(globalOptions, userOptions);
2369
+ const sharedKeys = Object.keys(shareInfos);
2545
2370
  sharedKeys.forEach((sharedKey) => {
2546
- const sharedVals = newShareInfos[sharedKey];
2371
+ const sharedVals = shareInfos[sharedKey];
2547
2372
  sharedVals.forEach((sharedVal) => {
2548
2373
  sharedVal.scope.forEach((sc) => {
2549
- this.hooks.lifecycle.beforeRegisterShare.emit({
2550
- origin: this.host,
2551
- pkgName: sharedKey,
2552
- shared: sharedVal,
2553
- });
2554
2374
  const registeredShared = this.shareScopeMap[sc]?.[sharedKey];
2555
2375
  if (!registeredShared) {
2556
2376
  this.setShared({
@@ -2566,8 +2386,8 @@ class SharedHandler {
2566
2386
  });
2567
2387
  });
2568
2388
  return {
2569
- newShareInfos,
2570
- allShareInfos,
2389
+ shareInfos,
2390
+ shared,
2571
2391
  };
2572
2392
  }
2573
2393
  async loadShare(pkgName, extraOptions) {
@@ -2598,59 +2418,61 @@ class SharedHandler {
2598
2418
  const { shareInfo: shareOptionsRes } = loadShareRes;
2599
2419
  // Assert that shareInfoRes exists, if not, throw an error
2600
2420
  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, useTreeshake } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
2602
- if (registeredShared) {
2603
- const targetShared = directShare(registeredShared, useTreeshake);
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
- treeshake: useTreeshake ? targetShared : undefined,
2634
- });
2635
- return loading;
2421
+ // Retrieve from cache
2422
+ const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
2423
+ const addUseIn = (shared) => {
2424
+ if (!shared.useIn) {
2425
+ shared.useIn = [];
2636
2426
  }
2427
+ addUniqueItem(shared.useIn, host.options.name);
2428
+ };
2429
+ if (registeredShared && registeredShared.lib) {
2430
+ addUseIn(registeredShared);
2431
+ return registeredShared.lib;
2432
+ }
2433
+ else if (registeredShared &&
2434
+ registeredShared.loading &&
2435
+ !registeredShared.loaded) {
2436
+ const factory = await registeredShared.loading;
2437
+ registeredShared.loaded = true;
2438
+ if (!registeredShared.lib) {
2439
+ registeredShared.lib = factory;
2440
+ }
2441
+ addUseIn(registeredShared);
2442
+ return factory;
2443
+ }
2444
+ else if (registeredShared) {
2445
+ const asyncLoadProcess = async () => {
2446
+ const factory = await registeredShared.get();
2447
+ addUseIn(registeredShared);
2448
+ registeredShared.loaded = true;
2449
+ registeredShared.lib = factory;
2450
+ return factory;
2451
+ };
2452
+ const loading = asyncLoadProcess();
2453
+ this.setShared({
2454
+ pkgName,
2455
+ loaded: false,
2456
+ shared: registeredShared,
2457
+ from: host.options.name,
2458
+ lib: null,
2459
+ loading,
2460
+ });
2461
+ return loading;
2637
2462
  }
2638
2463
  else {
2639
2464
  if (extraOptions?.customShareInfo) {
2640
2465
  return false;
2641
2466
  }
2642
- const _useTreeshake = shouldUseTreeshake(shareOptionsRes.treeshake);
2643
- const targetShared = directShare(shareOptionsRes, _useTreeshake);
2644
2467
  const asyncLoadProcess = async () => {
2645
- const factory = await targetShared.get();
2646
- targetShared.lib = factory;
2647
- targetShared.loaded = true;
2648
- addUseIn(targetShared, host.options.name);
2649
- const { shared: gShared, useTreeshake: gUseTreeshake } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare) || {};
2468
+ const factory = await shareOptionsRes.get();
2469
+ shareOptionsRes.lib = factory;
2470
+ shareOptionsRes.loaded = true;
2471
+ addUseIn(shareOptionsRes);
2472
+ const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
2650
2473
  if (gShared) {
2651
- const targetGShared = directShare(gShared, gUseTreeshake);
2652
- targetGShared.lib = factory;
2653
- targetGShared.loaded = true;
2474
+ gShared.lib = factory;
2475
+ gShared.loaded = true;
2654
2476
  gShared.from = shareOptionsRes.from;
2655
2477
  }
2656
2478
  return factory;
@@ -2663,7 +2485,6 @@ class SharedHandler {
2663
2485
  from: host.options.name,
2664
2486
  lib: null,
2665
2487
  loading,
2666
- treeshake: _useTreeshake ? targetShared : undefined,
2667
2488
  });
2668
2489
  return loading;
2669
2490
  }
@@ -2703,20 +2524,19 @@ class SharedHandler {
2703
2524
  const { version, eager } = shared;
2704
2525
  scope[name] = scope[name] || {};
2705
2526
  const versions = scope[name];
2706
- const activeVersion = versions[version] && directShare(versions[version]);
2527
+ const activeVersion = versions[version];
2707
2528
  const activeVersionEager = Boolean(activeVersion &&
2708
- (('eager' in activeVersion && activeVersion.eager) ||
2709
- ('shareConfig' in activeVersion &&
2710
- activeVersion.shareConfig?.eager)));
2529
+ (activeVersion.eager || activeVersion.shareConfig?.eager));
2711
2530
  if (!activeVersion ||
2712
2531
  (activeVersion.strategy !== 'loaded-first' &&
2713
2532
  !activeVersion.loaded &&
2714
2533
  (Boolean(!eager) !== !activeVersionEager
2715
2534
  ? eager
2716
- : hostName > versions[version].from))) {
2535
+ : hostName > activeVersion.from))) {
2717
2536
  versions[version] = shared;
2718
2537
  }
2719
2538
  };
2539
+ const initFn = (mod) => mod && mod.init && mod.init(shareScope[shareScopeName], initScope);
2720
2540
  const initRemoteModule = async (key) => {
2721
2541
  const { module } = await host.remoteHandler.getRemoteModuleAndOptions({
2722
2542
  id: key,
@@ -2737,12 +2557,6 @@ class SharedHandler {
2737
2557
  }));
2738
2558
  }
2739
2559
  if (!module.inited) {
2740
- const initFn = (mod) => {
2741
- const { remoteEntryInitOptions } = createRemoteEntryInitOptions(module.remoteInfo, host.shareScopeMap);
2742
- return (mod &&
2743
- mod.init &&
2744
- mod.init(shareScope[shareScopeName], initScope, remoteEntryInitOptions));
2745
- };
2746
2560
  await initFn(remoteEntryExports);
2747
2561
  module.inited = true;
2748
2562
  }
@@ -2783,10 +2597,16 @@ class SharedHandler {
2783
2597
  this.initializeSharing(shareScope, { strategy: shareOptions.strategy });
2784
2598
  });
2785
2599
  }
2786
- const { shared: registeredShared, useTreeshake } = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare) || {};
2600
+ const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare);
2601
+ const addUseIn = (shared) => {
2602
+ if (!shared.useIn) {
2603
+ shared.useIn = [];
2604
+ }
2605
+ addUniqueItem(shared.useIn, host.options.name);
2606
+ };
2787
2607
  if (registeredShared) {
2788
2608
  if (typeof registeredShared.lib === 'function') {
2789
- addUseIn(registeredShared, host.options.name);
2609
+ addUseIn(registeredShared);
2790
2610
  if (!registeredShared.loaded) {
2791
2611
  registeredShared.loaded = true;
2792
2612
  if (registeredShared.from === host.options.name) {
@@ -2798,7 +2618,7 @@ class SharedHandler {
2798
2618
  if (typeof registeredShared.get === 'function') {
2799
2619
  const module = registeredShared.get();
2800
2620
  if (!(module instanceof Promise)) {
2801
- addUseIn(registeredShared, host.options.name);
2621
+ addUseIn(registeredShared);
2802
2622
  this.setShared({
2803
2623
  pkgName,
2804
2624
  loaded: true,
@@ -2842,39 +2662,6 @@ class SharedHandler {
2842
2662
  }
2843
2663
  initShareScopeMap(scopeName, shareScope, extraOptions = {}) {
2844
2664
  const { host } = this;
2845
- // const existedShareScope = this.shareScopeMap[scopeName];
2846
- // if (existedShareScope) {
2847
- // Object.entries(shareScope).forEach(([pkgName, newVersions]) => {
2848
- // const existedShareMap = existedShareScope[pkgName];
2849
- // if (!existedShareMap) {
2850
- // return;
2851
- // }
2852
- // Object.entries(existedShareMap).forEach(([version, existedShared]) => {
2853
- // if (!shareScope[pkgName][version]) {
2854
- // shareScope[pkgName][version] = existedShared;
2855
- // }
2856
- // // const newShared = newVersions[version];
2857
- // // if (
2858
- // // newShared &&
2859
- // // newShared.treeshakeStatus === TreeshakeStatus.UNKNOWN &&
2860
- // // newShared.usedExports &&
2861
- // // existedShared.usedExports &&
2862
- // // existedShared.usedExports.some(
2863
- // // (exportName) => !newShared.usedExports?.includes(exportName),
2864
- // // )
2865
- // // ) {
2866
- // // newShared.treeshakeStatus = TreeshakeStatus.NO_USE;
2867
- // // newShared._noMatchedUsedExports =
2868
- // // existedShared._noMatchedUsedExports || [];
2869
- // // const item: NoMatchedUsedExportsItem = [existedShared.from];
2870
- // // if (isDebugMode() && existedShared.usedExports) {
2871
- // // item.push(existedShared.usedExports);
2872
- // // }
2873
- // // newShared._noMatchedUsedExports.push(item);
2874
- // // }
2875
- // });
2876
- // });
2877
- // }
2878
2665
  this.shareScopeMap[scopeName] = shareScope;
2879
2666
  this.hooks.lifecycle.initContainerShareScopeMap.emit({
2880
2667
  shareScope,
@@ -2884,20 +2671,9 @@ class SharedHandler {
2884
2671
  hostShareScopeMap: extraOptions.hostShareScopeMap,
2885
2672
  });
2886
2673
  }
2887
- setShared({ pkgName, shared, from, lib, loading, loaded, get, treeshake, }) {
2674
+ setShared({ pkgName, shared, from, lib, loading, loaded, get, }) {
2888
2675
  const { version, scope = 'default', ...shareInfo } = shared;
2889
2676
  const scopes = Array.isArray(scope) ? scope : [scope];
2890
- const mergeAttrs = (shared) => {
2891
- const merge = (s, key, val) => {
2892
- if (val && !s[key]) {
2893
- s[key] = val;
2894
- }
2895
- };
2896
- const targetShared = (treeshake ? shared.treeshake : shared);
2897
- merge(targetShared, 'loaded', loaded);
2898
- merge(targetShared, 'loading', loading);
2899
- merge(targetShared, 'get', get);
2900
- };
2901
2677
  scopes.forEach((sc) => {
2902
2678
  if (!this.shareScopeMap[sc]) {
2903
2679
  this.shareScopeMap[sc] = {};
@@ -2911,10 +2687,21 @@ class SharedHandler {
2911
2687
  scope: [sc],
2912
2688
  ...shareInfo,
2913
2689
  lib,
2690
+ loaded,
2691
+ loading,
2914
2692
  };
2693
+ if (get) {
2694
+ this.shareScopeMap[sc][pkgName][version].get = get;
2695
+ }
2696
+ return;
2915
2697
  }
2916
2698
  const registeredShared = this.shareScopeMap[sc][pkgName][version];
2917
- mergeAttrs(registeredShared);
2699
+ if (loading && !registeredShared.loading) {
2700
+ registeredShared.loading = loading;
2701
+ }
2702
+ if (loaded && !registeredShared.loaded) {
2703
+ registeredShared.loaded = loaded;
2704
+ }
2918
2705
  if (from && registeredShared.from !== from) {
2919
2706
  registeredShared.from = from;
2920
2707
  }
@@ -2942,7 +2729,6 @@ class RemoteHandler {
2942
2729
  generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
2943
2730
  // not used yet
2944
2731
  afterPreloadRemote: new AsyncHook(),
2945
- // TODO: Move to loaderHook
2946
2732
  loadEntry: new AsyncHook(),
2947
2733
  });
2948
2734
  this.host = host;
@@ -3291,7 +3077,7 @@ class ModuleFederation {
3291
3077
  // maybe will change, temporarily for internal use only
3292
3078
  initContainer: new AsyncWaterfallHook('initContainer'),
3293
3079
  });
3294
- this.version = "0.0.0-feat-shared-treeshake-poc-20251211144042";
3080
+ this.version = "0.0.0-feat-rspack-async-startup-20251223091151";
3295
3081
  this.moduleCache = new Map();
3296
3082
  this.loaderHook = new PluginSystem({
3297
3083
  // FIXME: may not be suitable , not open to the public yet
@@ -3372,7 +3158,7 @@ class ModuleFederation {
3372
3158
  this.sharedHandler.initShareScopeMap(scopeName, shareScope, extraOptions);
3373
3159
  }
3374
3160
  formatOptions(globalOptions, userOptions) {
3375
- const { allShareInfos: shared} = formatShareConfigs(globalOptions, userOptions);
3161
+ const { shared } = formatShareConfigs(globalOptions, userOptions);
3376
3162
  const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
3377
3163
  origin: this,
3378
3164
  userOptions,
@@ -3380,7 +3166,7 @@ class ModuleFederation {
3380
3166
  shareInfo: shared,
3381
3167
  });
3382
3168
  const remotes = this.remoteHandler.formatAndRegisterRemote(globalOptionsRes, userOptionsRes);
3383
- const { allShareInfos } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
3169
+ const { shared: handledShared } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
3384
3170
  const plugins = [...globalOptionsRes.plugins];
3385
3171
  if (userOptionsRes.plugins) {
3386
3172
  userOptionsRes.plugins.forEach((plugin) => {
@@ -3394,7 +3180,7 @@ class ModuleFederation {
3394
3180
  ...userOptions,
3395
3181
  plugins,
3396
3182
  remotes,
3397
- shared: allShareInfos,
3183
+ shared: handledShared,
3398
3184
  };
3399
3185
  this.hooks.lifecycle.init.emit({
3400
3186
  origin: this,