@module-federation/runtime-core 0.0.0-feat-shared-treeshake-poc-20260112033647 → 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.
- package/dist/index.cjs.cjs +150 -351
- package/dist/index.cjs.cjs.map +1 -1
- package/dist/index.esm.js +150 -351
- package/dist/index.esm.js.map +1 -1
- package/dist/src/core.d.ts +2 -6
- package/dist/src/module/index.d.ts +2 -2
- package/dist/src/shared/index.d.ts +3 -12
- package/dist/src/type/config.d.ts +1 -14
- package/dist/src/utils/share.d.ts +9 -27
- package/package.json +3 -3
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-
|
|
203
|
+
CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.0-feat-modern-3-0-20260113090730";
|
|
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.treeShaking) {
|
|
811
|
-
throw new Error('Can not set "eager:true" and "treeShaking" 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
|
-
treeShaking: shareArgs.treeShaking
|
|
834
|
-
? {
|
|
835
|
-
...shareArgs.treeShaking,
|
|
836
|
-
strategy: shareArgs.treeShaking.strategy ?? 'server',
|
|
837
|
-
status: shareArgs.treeShaking.status ?? 1 /* TreeShakingStatus.UNKNOWN */,
|
|
838
|
-
useIn: [],
|
|
839
|
-
}
|
|
840
|
-
: undefined,
|
|
841
830
|
};
|
|
842
831
|
}
|
|
843
|
-
function formatShareConfigs(
|
|
844
|
-
const shareArgs =
|
|
845
|
-
const from =
|
|
846
|
-
const
|
|
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,
|
|
839
|
+
res[pkgName].push(formatShare(shareConfig, from, pkgName, userOptions.shareStrategy));
|
|
851
840
|
});
|
|
852
841
|
return res;
|
|
853
842
|
}, {});
|
|
854
|
-
const
|
|
855
|
-
...
|
|
843
|
+
const shared = {
|
|
844
|
+
...globalOptions.shared,
|
|
856
845
|
};
|
|
857
|
-
Object.keys(
|
|
858
|
-
if (!
|
|
859
|
-
|
|
846
|
+
Object.keys(shareInfos).forEach((shareKey) => {
|
|
847
|
+
if (!shared[shareKey]) {
|
|
848
|
+
shared[shareKey] = shareInfos[shareKey];
|
|
860
849
|
}
|
|
861
850
|
else {
|
|
862
|
-
|
|
863
|
-
const isSameVersion =
|
|
851
|
+
shareInfos[shareKey].forEach((newUserSharedOptions) => {
|
|
852
|
+
const isSameVersion = shared[shareKey].find((sharedVal) => sharedVal.version === newUserSharedOptions.version);
|
|
864
853
|
if (!isSameVersion) {
|
|
865
|
-
|
|
854
|
+
shared[shareKey].push(newUserSharedOptions);
|
|
866
855
|
}
|
|
867
856
|
});
|
|
868
857
|
}
|
|
869
858
|
});
|
|
870
|
-
return {
|
|
871
|
-
}
|
|
872
|
-
function shouldUseTreeShaking(treeShaking, usedExports) {
|
|
873
|
-
if (!treeShaking) {
|
|
874
|
-
return false;
|
|
875
|
-
}
|
|
876
|
-
const { status, strategy } = treeShaking;
|
|
877
|
-
if (status === 0 /* TreeShakingStatus.NO_USE */) {
|
|
878
|
-
return false;
|
|
879
|
-
}
|
|
880
|
-
if (status === 2 /* TreeShakingStatus.CALCULATED */) {
|
|
881
|
-
return true;
|
|
882
|
-
}
|
|
883
|
-
if (strategy === 'infer') {
|
|
884
|
-
if (!usedExports) {
|
|
885
|
-
return true;
|
|
886
|
-
}
|
|
887
|
-
return isMatchUsedExports(treeShaking, usedExports);
|
|
888
|
-
}
|
|
889
|
-
return false;
|
|
859
|
+
return { shared, shareInfos };
|
|
890
860
|
}
|
|
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
|
-
|
|
940
|
-
if (!treeShaking || !usedExports) {
|
|
941
|
-
return false;
|
|
942
|
-
}
|
|
943
|
-
const { usedExports: treeShakingUsedExports } = treeShaking;
|
|
944
|
-
if (!treeShakingUsedExports) {
|
|
945
|
-
return false;
|
|
946
|
-
}
|
|
947
|
-
if (usedExports.every((e) => treeShakingUsedExports.includes(e))) {
|
|
948
|
-
return true;
|
|
949
|
-
}
|
|
950
|
-
return false;
|
|
951
|
-
};
|
|
952
|
-
function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName, treeShaking) {
|
|
906
|
+
function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
|
|
953
907
|
const versions = shareScopeMap[scope][pkgName];
|
|
954
|
-
let version = '';
|
|
955
|
-
let useTreesShaking = shouldUseTreeShaking(treeShaking);
|
|
956
|
-
// return false means use prev version
|
|
957
908
|
const callback = function (prev, cur) {
|
|
958
|
-
if (useTreesShaking) {
|
|
959
|
-
if (!versions[prev].treeShaking) {
|
|
960
|
-
return true;
|
|
961
|
-
}
|
|
962
|
-
if (!versions[cur].treeShaking) {
|
|
963
|
-
return false;
|
|
964
|
-
}
|
|
965
|
-
return !isLoaded(versions[prev].treeShaking) && versionLt(prev, cur);
|
|
966
|
-
}
|
|
967
909
|
return !isLoaded(versions[prev]) && versionLt(prev, cur);
|
|
968
910
|
};
|
|
969
|
-
|
|
970
|
-
version = findVersion(shareScopeMap[scope][pkgName], callback);
|
|
971
|
-
if (version) {
|
|
972
|
-
return {
|
|
973
|
-
version,
|
|
974
|
-
useTreesShaking,
|
|
975
|
-
};
|
|
976
|
-
}
|
|
977
|
-
useTreesShaking = false;
|
|
978
|
-
}
|
|
979
|
-
return {
|
|
980
|
-
version: findVersion(shareScopeMap[scope][pkgName], callback),
|
|
981
|
-
useTreesShaking,
|
|
982
|
-
};
|
|
911
|
+
return findVersion(shareScopeMap[scope][pkgName], callback);
|
|
983
912
|
}
|
|
984
|
-
|
|
985
|
-
return isLoaded(shared) || isLoading(shared);
|
|
986
|
-
};
|
|
987
|
-
function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName, treeShaking) {
|
|
913
|
+
function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
|
|
988
914
|
const versions = shareScopeMap[scope][pkgName];
|
|
989
|
-
let version = '';
|
|
990
|
-
let useTreesShaking = shouldUseTreeShaking(treeShaking);
|
|
991
|
-
// return false means use prev version
|
|
992
915
|
const callback = function (prev, cur) {
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
}
|
|
997
|
-
if (!versions[cur].treeShaking) {
|
|
998
|
-
return false;
|
|
999
|
-
}
|
|
1000
|
-
if (isLoadingOrLoaded(versions[cur].treeShaking)) {
|
|
1001
|
-
if (isLoadingOrLoaded(versions[prev].treeShaking)) {
|
|
1002
|
-
return Boolean(versionLt(prev, cur));
|
|
1003
|
-
}
|
|
1004
|
-
else {
|
|
1005
|
-
return true;
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
if (isLoadingOrLoaded(versions[prev].treeShaking)) {
|
|
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
|
-
|
|
1026
|
-
version = findVersion(shareScopeMap[scope][pkgName], callback);
|
|
1027
|
-
if (version) {
|
|
1028
|
-
return {
|
|
1029
|
-
version,
|
|
1030
|
-
useTreesShaking,
|
|
1031
|
-
};
|
|
1032
|
-
}
|
|
1033
|
-
useTreesShaking = false;
|
|
1034
|
-
}
|
|
1035
|
-
return {
|
|
1036
|
-
version: findVersion(shareScopeMap[scope][pkgName], callback),
|
|
1037
|
-
useTreesShaking,
|
|
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
|
|
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
|
|
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 &&
|
|
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
|
-
useTreesShaking,
|
|
1075
|
-
};
|
|
967
|
+
return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
|
|
1076
968
|
}
|
|
1077
969
|
else {
|
|
1078
970
|
if (requiredVersion === false || requiredVersion === '*') {
|
|
1079
|
-
return
|
|
1080
|
-
shared,
|
|
1081
|
-
useTreesShaking,
|
|
1082
|
-
};
|
|
971
|
+
return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
|
|
1083
972
|
}
|
|
1084
973
|
if (satisfy(maxOrSingletonVersion, requiredVersion)) {
|
|
1085
|
-
return
|
|
1086
|
-
shared,
|
|
1087
|
-
useTreesShaking,
|
|
1088
|
-
};
|
|
1089
|
-
}
|
|
1090
|
-
const _usedTreeShaking = shouldUseTreeShaking(treeShaking);
|
|
1091
|
-
if (_usedTreeShaking) {
|
|
1092
|
-
for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])) {
|
|
1093
|
-
if (!shouldUseTreeShaking(versionValue.treeShaking, treeShaking?.usedExports)) {
|
|
1094
|
-
continue;
|
|
1095
|
-
}
|
|
1096
|
-
if (satisfy(versionKey, requiredVersion)) {
|
|
1097
|
-
return {
|
|
1098
|
-
shared: versionValue,
|
|
1099
|
-
useTreesShaking: _usedTreeShaking,
|
|
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
|
-
useTreesShaking: 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 treeShaking 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
|
-
|
|
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, useTreesShaking) {
|
|
1180
|
-
if (useTreesShaking && shared.treeShaking) {
|
|
1181
|
-
return shared.treeShaking;
|
|
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;
|
|
@@ -1747,12 +1546,37 @@ class Module {
|
|
|
1747
1546
|
return this.remoteEntryExports;
|
|
1748
1547
|
}
|
|
1749
1548
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
1750
|
-
async
|
|
1751
|
-
const { loadFactory = true } = options || { loadFactory: true };
|
|
1549
|
+
async init(id, remoteSnapshot) {
|
|
1752
1550
|
// Get remoteEntry.js
|
|
1753
1551
|
const remoteEntryExports = await this.getEntry();
|
|
1754
1552
|
if (!this.inited) {
|
|
1755
|
-
const
|
|
1553
|
+
const localShareScopeMap = this.host.shareScopeMap;
|
|
1554
|
+
const shareScopeKeys = Array.isArray(this.remoteInfo.shareScope)
|
|
1555
|
+
? this.remoteInfo.shareScope
|
|
1556
|
+
: [this.remoteInfo.shareScope];
|
|
1557
|
+
if (!shareScopeKeys.length) {
|
|
1558
|
+
shareScopeKeys.push('default');
|
|
1559
|
+
}
|
|
1560
|
+
shareScopeKeys.forEach((shareScopeKey) => {
|
|
1561
|
+
if (!localShareScopeMap[shareScopeKey]) {
|
|
1562
|
+
localShareScopeMap[shareScopeKey] = {};
|
|
1563
|
+
}
|
|
1564
|
+
});
|
|
1565
|
+
// TODO: compate legacy init params, should use shareScopeMap if exist
|
|
1566
|
+
const shareScope = localShareScopeMap[shareScopeKeys[0]];
|
|
1567
|
+
const initScope = [];
|
|
1568
|
+
const remoteEntryInitOptions = {
|
|
1569
|
+
version: this.remoteInfo.version || '',
|
|
1570
|
+
shareScopeKeys: Array.isArray(this.remoteInfo.shareScope)
|
|
1571
|
+
? shareScopeKeys
|
|
1572
|
+
: this.remoteInfo.shareScope || 'default',
|
|
1573
|
+
};
|
|
1574
|
+
// Help to find host instance
|
|
1575
|
+
Object.defineProperty(remoteEntryInitOptions, 'shareScopeMap', {
|
|
1576
|
+
value: localShareScopeMap,
|
|
1577
|
+
// remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
|
|
1578
|
+
enumerable: false,
|
|
1579
|
+
});
|
|
1756
1580
|
const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
|
|
1757
1581
|
shareScope,
|
|
1758
1582
|
// @ts-ignore shareScopeMap will be set by Object.defineProperty
|
|
@@ -1777,6 +1601,12 @@ class Module {
|
|
|
1777
1601
|
remoteEntryExports,
|
|
1778
1602
|
});
|
|
1779
1603
|
}
|
|
1604
|
+
return remoteEntryExports;
|
|
1605
|
+
}
|
|
1606
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
1607
|
+
async get(id, expose, options, remoteSnapshot) {
|
|
1608
|
+
const { loadFactory = true } = options || { loadFactory: true };
|
|
1609
|
+
const remoteEntryExports = await this.init(id, remoteSnapshot);
|
|
1780
1610
|
this.lib = remoteEntryExports;
|
|
1781
1611
|
this.inited = true;
|
|
1782
1612
|
let moduleFactory;
|
|
@@ -2232,7 +2062,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
|
|
|
2232
2062
|
}, true, memo, remoteSnapshot);
|
|
2233
2063
|
if (remoteSnapshot.shared && remoteSnapshot.shared.length > 0) {
|
|
2234
2064
|
const collectSharedAssets = (shareInfo, snapshotShared) => {
|
|
2235
|
-
const
|
|
2065
|
+
const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare);
|
|
2236
2066
|
// 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
2067
|
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
2238
2068
|
snapshotShared.assets.js.sync.forEach((asset) => {
|
|
@@ -2523,7 +2353,6 @@ class SnapshotHandler {
|
|
|
2523
2353
|
class SharedHandler {
|
|
2524
2354
|
constructor(host) {
|
|
2525
2355
|
this.hooks = new PluginSystem({
|
|
2526
|
-
beforeRegisterShare: new SyncWaterfallHook('beforeRegisterShare'),
|
|
2527
2356
|
afterResolve: new AsyncWaterfallHook('afterResolve'),
|
|
2528
2357
|
beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
|
|
2529
2358
|
// not used yet
|
|
@@ -2539,17 +2368,12 @@ class SharedHandler {
|
|
|
2539
2368
|
}
|
|
2540
2369
|
// register shared in shareScopeMap
|
|
2541
2370
|
registerShared(globalOptions, userOptions) {
|
|
2542
|
-
const {
|
|
2543
|
-
const sharedKeys = Object.keys(
|
|
2371
|
+
const { shareInfos, shared } = formatShareConfigs(globalOptions, userOptions);
|
|
2372
|
+
const sharedKeys = Object.keys(shareInfos);
|
|
2544
2373
|
sharedKeys.forEach((sharedKey) => {
|
|
2545
|
-
const sharedVals =
|
|
2374
|
+
const sharedVals = shareInfos[sharedKey];
|
|
2546
2375
|
sharedVals.forEach((sharedVal) => {
|
|
2547
2376
|
sharedVal.scope.forEach((sc) => {
|
|
2548
|
-
this.hooks.lifecycle.beforeRegisterShare.emit({
|
|
2549
|
-
origin: this.host,
|
|
2550
|
-
pkgName: sharedKey,
|
|
2551
|
-
shared: sharedVal,
|
|
2552
|
-
});
|
|
2553
2377
|
const registeredShared = this.shareScopeMap[sc]?.[sharedKey];
|
|
2554
2378
|
if (!registeredShared) {
|
|
2555
2379
|
this.setShared({
|
|
@@ -2565,8 +2389,8 @@ class SharedHandler {
|
|
|
2565
2389
|
});
|
|
2566
2390
|
});
|
|
2567
2391
|
return {
|
|
2568
|
-
|
|
2569
|
-
|
|
2392
|
+
shareInfos,
|
|
2393
|
+
shared,
|
|
2570
2394
|
};
|
|
2571
2395
|
}
|
|
2572
2396
|
async loadShare(pkgName, extraOptions) {
|
|
@@ -2597,61 +2421,61 @@ class SharedHandler {
|
|
|
2597
2421
|
const { shareInfo: shareOptionsRes } = loadShareRes;
|
|
2598
2422
|
// Assert that shareInfoRes exists, if not, throw an error
|
|
2599
2423
|
assert(shareOptionsRes, `Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
if (
|
|
2604
|
-
|
|
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
|
-
treeShaking: useTreesShaking
|
|
2633
|
-
? targetShared
|
|
2634
|
-
: undefined,
|
|
2635
|
-
});
|
|
2636
|
-
return loading;
|
|
2424
|
+
// Retrieve from cache
|
|
2425
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
|
|
2426
|
+
const addUseIn = (shared) => {
|
|
2427
|
+
if (!shared.useIn) {
|
|
2428
|
+
shared.useIn = [];
|
|
2637
2429
|
}
|
|
2430
|
+
addUniqueItem(shared.useIn, host.options.name);
|
|
2431
|
+
};
|
|
2432
|
+
if (registeredShared && registeredShared.lib) {
|
|
2433
|
+
addUseIn(registeredShared);
|
|
2434
|
+
return registeredShared.lib;
|
|
2435
|
+
}
|
|
2436
|
+
else if (registeredShared &&
|
|
2437
|
+
registeredShared.loading &&
|
|
2438
|
+
!registeredShared.loaded) {
|
|
2439
|
+
const factory = await registeredShared.loading;
|
|
2440
|
+
registeredShared.loaded = true;
|
|
2441
|
+
if (!registeredShared.lib) {
|
|
2442
|
+
registeredShared.lib = factory;
|
|
2443
|
+
}
|
|
2444
|
+
addUseIn(registeredShared);
|
|
2445
|
+
return factory;
|
|
2446
|
+
}
|
|
2447
|
+
else if (registeredShared) {
|
|
2448
|
+
const asyncLoadProcess = async () => {
|
|
2449
|
+
const factory = await registeredShared.get();
|
|
2450
|
+
addUseIn(registeredShared);
|
|
2451
|
+
registeredShared.loaded = true;
|
|
2452
|
+
registeredShared.lib = factory;
|
|
2453
|
+
return factory;
|
|
2454
|
+
};
|
|
2455
|
+
const loading = asyncLoadProcess();
|
|
2456
|
+
this.setShared({
|
|
2457
|
+
pkgName,
|
|
2458
|
+
loaded: false,
|
|
2459
|
+
shared: registeredShared,
|
|
2460
|
+
from: host.options.name,
|
|
2461
|
+
lib: null,
|
|
2462
|
+
loading,
|
|
2463
|
+
});
|
|
2464
|
+
return loading;
|
|
2638
2465
|
}
|
|
2639
2466
|
else {
|
|
2640
2467
|
if (extraOptions?.customShareInfo) {
|
|
2641
2468
|
return false;
|
|
2642
2469
|
}
|
|
2643
|
-
const _useTreeShaking = shouldUseTreeShaking(shareOptionsRes.treeShaking);
|
|
2644
|
-
const targetShared = directShare(shareOptionsRes, _useTreeShaking);
|
|
2645
2470
|
const asyncLoadProcess = async () => {
|
|
2646
|
-
const factory = await
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
addUseIn(
|
|
2650
|
-
const
|
|
2471
|
+
const factory = await shareOptionsRes.get();
|
|
2472
|
+
shareOptionsRes.lib = factory;
|
|
2473
|
+
shareOptionsRes.loaded = true;
|
|
2474
|
+
addUseIn(shareOptionsRes);
|
|
2475
|
+
const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptionsRes, this.hooks.lifecycle.resolveShare);
|
|
2651
2476
|
if (gShared) {
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
targetGShared.loaded = true;
|
|
2477
|
+
gShared.lib = factory;
|
|
2478
|
+
gShared.loaded = true;
|
|
2655
2479
|
gShared.from = shareOptionsRes.from;
|
|
2656
2480
|
}
|
|
2657
2481
|
return factory;
|
|
@@ -2664,9 +2488,6 @@ class SharedHandler {
|
|
|
2664
2488
|
from: host.options.name,
|
|
2665
2489
|
lib: null,
|
|
2666
2490
|
loading,
|
|
2667
|
-
treeShaking: _useTreeShaking
|
|
2668
|
-
? targetShared
|
|
2669
|
-
: undefined,
|
|
2670
2491
|
});
|
|
2671
2492
|
return loading;
|
|
2672
2493
|
}
|
|
@@ -2706,17 +2527,15 @@ class SharedHandler {
|
|
|
2706
2527
|
const { version, eager } = shared;
|
|
2707
2528
|
scope[name] = scope[name] || {};
|
|
2708
2529
|
const versions = scope[name];
|
|
2709
|
-
const activeVersion = versions[version]
|
|
2530
|
+
const activeVersion = versions[version];
|
|
2710
2531
|
const activeVersionEager = Boolean(activeVersion &&
|
|
2711
|
-
(
|
|
2712
|
-
('shareConfig' in activeVersion &&
|
|
2713
|
-
activeVersion.shareConfig?.eager)));
|
|
2532
|
+
(activeVersion.eager || activeVersion.shareConfig?.eager));
|
|
2714
2533
|
if (!activeVersion ||
|
|
2715
2534
|
(activeVersion.strategy !== 'loaded-first' &&
|
|
2716
2535
|
!activeVersion.loaded &&
|
|
2717
2536
|
(Boolean(!eager) !== !activeVersionEager
|
|
2718
2537
|
? eager
|
|
2719
|
-
: hostName >
|
|
2538
|
+
: hostName > activeVersion.from))) {
|
|
2720
2539
|
versions[version] = shared;
|
|
2721
2540
|
}
|
|
2722
2541
|
};
|
|
@@ -2724,32 +2543,7 @@ class SharedHandler {
|
|
|
2724
2543
|
const { module } = await host.remoteHandler.getRemoteModuleAndOptions({
|
|
2725
2544
|
id: key,
|
|
2726
2545
|
});
|
|
2727
|
-
|
|
2728
|
-
let remoteEntryExports;
|
|
2729
|
-
try {
|
|
2730
|
-
remoteEntryExports = await module.getEntry();
|
|
2731
|
-
}
|
|
2732
|
-
catch (error) {
|
|
2733
|
-
remoteEntryExports =
|
|
2734
|
-
(await host.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
|
|
2735
|
-
id: key,
|
|
2736
|
-
error,
|
|
2737
|
-
from: 'runtime',
|
|
2738
|
-
lifecycle: 'beforeLoadShare',
|
|
2739
|
-
origin: host,
|
|
2740
|
-
}));
|
|
2741
|
-
}
|
|
2742
|
-
if (!module.inited) {
|
|
2743
|
-
const initFn = (mod) => {
|
|
2744
|
-
const { remoteEntryInitOptions } = createRemoteEntryInitOptions(module.remoteInfo, host.shareScopeMap);
|
|
2745
|
-
return (mod &&
|
|
2746
|
-
mod.init &&
|
|
2747
|
-
mod.init(shareScope[shareScopeName], initScope, remoteEntryInitOptions));
|
|
2748
|
-
};
|
|
2749
|
-
await initFn(remoteEntryExports);
|
|
2750
|
-
module.inited = true;
|
|
2751
|
-
}
|
|
2752
|
-
}
|
|
2546
|
+
await module.init();
|
|
2753
2547
|
};
|
|
2754
2548
|
Object.keys(host.options.shared).forEach((shareName) => {
|
|
2755
2549
|
const sharedArr = host.options.shared[shareName];
|
|
@@ -2786,10 +2580,16 @@ class SharedHandler {
|
|
|
2786
2580
|
this.initializeSharing(shareScope, { strategy: shareOptions.strategy });
|
|
2787
2581
|
});
|
|
2788
2582
|
}
|
|
2789
|
-
const
|
|
2583
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareOptions, this.hooks.lifecycle.resolveShare);
|
|
2584
|
+
const addUseIn = (shared) => {
|
|
2585
|
+
if (!shared.useIn) {
|
|
2586
|
+
shared.useIn = [];
|
|
2587
|
+
}
|
|
2588
|
+
addUniqueItem(shared.useIn, host.options.name);
|
|
2589
|
+
};
|
|
2790
2590
|
if (registeredShared) {
|
|
2791
2591
|
if (typeof registeredShared.lib === 'function') {
|
|
2792
|
-
addUseIn(registeredShared
|
|
2592
|
+
addUseIn(registeredShared);
|
|
2793
2593
|
if (!registeredShared.loaded) {
|
|
2794
2594
|
registeredShared.loaded = true;
|
|
2795
2595
|
if (registeredShared.from === host.options.name) {
|
|
@@ -2801,7 +2601,7 @@ class SharedHandler {
|
|
|
2801
2601
|
if (typeof registeredShared.get === 'function') {
|
|
2802
2602
|
const module = registeredShared.get();
|
|
2803
2603
|
if (!(module instanceof Promise)) {
|
|
2804
|
-
addUseIn(registeredShared
|
|
2604
|
+
addUseIn(registeredShared);
|
|
2805
2605
|
this.setShared({
|
|
2806
2606
|
pkgName,
|
|
2807
2607
|
loaded: true,
|
|
@@ -2854,20 +2654,9 @@ class SharedHandler {
|
|
|
2854
2654
|
hostShareScopeMap: extraOptions.hostShareScopeMap,
|
|
2855
2655
|
});
|
|
2856
2656
|
}
|
|
2857
|
-
setShared({ pkgName, shared, from, lib, loading, loaded, get,
|
|
2657
|
+
setShared({ pkgName, shared, from, lib, loading, loaded, get, }) {
|
|
2858
2658
|
const { version, scope = 'default', ...shareInfo } = shared;
|
|
2859
2659
|
const scopes = Array.isArray(scope) ? scope : [scope];
|
|
2860
|
-
const mergeAttrs = (shared) => {
|
|
2861
|
-
const merge = (s, key, val) => {
|
|
2862
|
-
if (val && !s[key]) {
|
|
2863
|
-
s[key] = val;
|
|
2864
|
-
}
|
|
2865
|
-
};
|
|
2866
|
-
const targetShared = (treeShaking ? shared.treeShaking : shared);
|
|
2867
|
-
merge(targetShared, 'loaded', loaded);
|
|
2868
|
-
merge(targetShared, 'loading', loading);
|
|
2869
|
-
merge(targetShared, 'get', get);
|
|
2870
|
-
};
|
|
2871
2660
|
scopes.forEach((sc) => {
|
|
2872
2661
|
if (!this.shareScopeMap[sc]) {
|
|
2873
2662
|
this.shareScopeMap[sc] = {};
|
|
@@ -2881,10 +2670,21 @@ class SharedHandler {
|
|
|
2881
2670
|
scope: [sc],
|
|
2882
2671
|
...shareInfo,
|
|
2883
2672
|
lib,
|
|
2673
|
+
loaded,
|
|
2674
|
+
loading,
|
|
2884
2675
|
};
|
|
2676
|
+
if (get) {
|
|
2677
|
+
this.shareScopeMap[sc][pkgName][version].get = get;
|
|
2678
|
+
}
|
|
2679
|
+
return;
|
|
2885
2680
|
}
|
|
2886
2681
|
const registeredShared = this.shareScopeMap[sc][pkgName][version];
|
|
2887
|
-
|
|
2682
|
+
if (loading && !registeredShared.loading) {
|
|
2683
|
+
registeredShared.loading = loading;
|
|
2684
|
+
}
|
|
2685
|
+
if (loaded && !registeredShared.loaded) {
|
|
2686
|
+
registeredShared.loaded = loaded;
|
|
2687
|
+
}
|
|
2888
2688
|
if (from && registeredShared.from !== from) {
|
|
2889
2689
|
registeredShared.from = from;
|
|
2890
2690
|
}
|
|
@@ -2912,7 +2712,6 @@ class RemoteHandler {
|
|
|
2912
2712
|
generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
|
|
2913
2713
|
// not used yet
|
|
2914
2714
|
afterPreloadRemote: new AsyncHook(),
|
|
2915
|
-
// TODO: Move to loaderHook
|
|
2916
2715
|
loadEntry: new AsyncHook(),
|
|
2917
2716
|
});
|
|
2918
2717
|
this.host = host;
|
|
@@ -3261,7 +3060,7 @@ class ModuleFederation {
|
|
|
3261
3060
|
// maybe will change, temporarily for internal use only
|
|
3262
3061
|
initContainer: new AsyncWaterfallHook('initContainer'),
|
|
3263
3062
|
});
|
|
3264
|
-
this.version = "0.0.0-feat-
|
|
3063
|
+
this.version = "0.0.0-feat-modern-3-0-20260113090730";
|
|
3265
3064
|
this.moduleCache = new Map();
|
|
3266
3065
|
this.loaderHook = new PluginSystem({
|
|
3267
3066
|
// FIXME: may not be suitable , not open to the public yet
|
|
@@ -3342,7 +3141,7 @@ class ModuleFederation {
|
|
|
3342
3141
|
this.sharedHandler.initShareScopeMap(scopeName, shareScope, extraOptions);
|
|
3343
3142
|
}
|
|
3344
3143
|
formatOptions(globalOptions, userOptions) {
|
|
3345
|
-
const {
|
|
3144
|
+
const { shared } = formatShareConfigs(globalOptions, userOptions);
|
|
3346
3145
|
const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
|
|
3347
3146
|
origin: this,
|
|
3348
3147
|
userOptions,
|
|
@@ -3350,7 +3149,7 @@ class ModuleFederation {
|
|
|
3350
3149
|
shareInfo: shared,
|
|
3351
3150
|
});
|
|
3352
3151
|
const remotes = this.remoteHandler.formatAndRegisterRemote(globalOptionsRes, userOptionsRes);
|
|
3353
|
-
const {
|
|
3152
|
+
const { shared: handledShared } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
|
|
3354
3153
|
const plugins = [...globalOptionsRes.plugins];
|
|
3355
3154
|
if (userOptionsRes.plugins) {
|
|
3356
3155
|
userOptionsRes.plugins.forEach((plugin) => {
|
|
@@ -3364,7 +3163,7 @@ class ModuleFederation {
|
|
|
3364
3163
|
...userOptions,
|
|
3365
3164
|
plugins,
|
|
3366
3165
|
remotes,
|
|
3367
|
-
shared:
|
|
3166
|
+
shared: handledShared,
|
|
3368
3167
|
};
|
|
3369
3168
|
this.hooks.lifecycle.init.emit({
|
|
3370
3169
|
origin: this,
|