@module-federation/vite 1.18.1 → 1.18.2
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/README.md +1 -0
- package/lib/index.js +509 -326
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -490,6 +490,7 @@ function normalizeManifest(manifest) {
|
|
|
490
490
|
}
|
|
491
491
|
let config;
|
|
492
492
|
let explicitSharedKeys = /* @__PURE__ */ new Set();
|
|
493
|
+
const explicitSharedKeysByOptions = /* @__PURE__ */ new WeakMap();
|
|
493
494
|
function resolveRuntimeImplementation() {
|
|
494
495
|
const fallback = resolveImportPath("@module-federation/runtime");
|
|
495
496
|
try {
|
|
@@ -505,17 +506,16 @@ function resolveRuntimeImplementation() {
|
|
|
505
506
|
function getNormalizeModuleFederationOptions() {
|
|
506
507
|
return config;
|
|
507
508
|
}
|
|
508
|
-
function isExplicitSharedKey(key) {
|
|
509
|
-
return explicitSharedKeys
|
|
509
|
+
function isExplicitSharedKey(key, options) {
|
|
510
|
+
return (options ? explicitSharedKeysByOptions.get(options) : explicitSharedKeys)?.has(key) ?? false;
|
|
510
511
|
}
|
|
511
|
-
function getNormalizeShareItem(key) {
|
|
512
|
-
const options = getNormalizeModuleFederationOptions();
|
|
512
|
+
function getNormalizeShareItem(key, options = getNormalizeModuleFederationOptions()) {
|
|
513
513
|
return options.shared[key] || options.shared[getPackageName(key)] || options.shared[getPackageName(key) + "/"];
|
|
514
514
|
}
|
|
515
515
|
function normalizeModuleFederationOptions(options) {
|
|
516
516
|
warnOnReservedInternalNamePrefix(options.name, "containerName");
|
|
517
517
|
if (options.virtualModuleDir && options.virtualModuleDir.includes("/")) throw createModuleFederationError(`Invalid virtualModuleDir: "${options.virtualModuleDir}". The virtualModuleDir option cannot contain slashes (/). Please use a single directory name like '__mf__virtual__your_app_name'.`);
|
|
518
|
-
|
|
518
|
+
const normalized = {
|
|
519
519
|
exposes: normalizeExposes(options.exposes),
|
|
520
520
|
filename: options.filename || "remoteEntry-[hash]",
|
|
521
521
|
internalName: toInternalModuleFederationName(options.name),
|
|
@@ -546,6 +546,8 @@ function normalizeModuleFederationOptions(options) {
|
|
|
546
546
|
varFilename: options.varFilename,
|
|
547
547
|
target: options.target
|
|
548
548
|
};
|
|
549
|
+
explicitSharedKeysByOptions.set(normalized, new Set(explicitSharedKeys));
|
|
550
|
+
return config = normalized;
|
|
549
551
|
}
|
|
550
552
|
//#endregion
|
|
551
553
|
//#region src/utils/VirtualModule.ts
|
|
@@ -575,7 +577,7 @@ function decodeViteId(id) {
|
|
|
575
577
|
return viteId.startsWith("__x00__") ? `\0${viteId.slice(7)}` : viteId;
|
|
576
578
|
}
|
|
577
579
|
function assertModuleFound(tag, str = "") {
|
|
578
|
-
const module = VirtualModule.findModule(tag, str);
|
|
580
|
+
const module = VirtualModule.findById(str) ?? VirtualModule.findModule(tag, str);
|
|
579
581
|
if (!module) throw createModuleFederationError(`Module Federation shared module '${str}' not found. Please ensure it's installed as a dependency in your package.json.`);
|
|
580
582
|
return module;
|
|
581
583
|
}
|
|
@@ -594,6 +596,7 @@ var VirtualModule = class VirtualModule {
|
|
|
594
596
|
code;
|
|
595
597
|
importId;
|
|
596
598
|
importIdKey;
|
|
599
|
+
scopeName;
|
|
597
600
|
static findName(tag, str = "") {
|
|
598
601
|
if (!patternMap[tag]) patternMap[tag] = new RegExp(`(.*${packageNameEncode(tag)}(.+?)${packageNameEncode(tag)}.*)`);
|
|
599
602
|
const moduleName = (normalizeVirtualModuleId(str).match(patternMap[tag]) || [])[2];
|
|
@@ -607,16 +610,16 @@ var VirtualModule = class VirtualModule {
|
|
|
607
610
|
const normalized = normalizeVirtualModuleId(id);
|
|
608
611
|
return normalized.startsWith("virtual:mf:") ? idCacheMap[normalized] : void 0;
|
|
609
612
|
}
|
|
610
|
-
constructor(name, tag = "__mf_v__", suffix = "") {
|
|
613
|
+
constructor(name, tag = "__mf_v__", suffix = "", scopeName) {
|
|
611
614
|
this.name = name;
|
|
612
615
|
this.tag = tag;
|
|
613
616
|
this.suffix = suffix || getSuffix(name);
|
|
617
|
+
this.scopeName = scopeName;
|
|
614
618
|
if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
|
|
615
619
|
cacheMap[this.tag][this.name] = this;
|
|
616
620
|
}
|
|
617
621
|
getImportId() {
|
|
618
|
-
const
|
|
619
|
-
const importIdKey = `${mfName}${this.tag}${this.name}${this.tag}`;
|
|
622
|
+
const importIdKey = `${this.scopeName ?? getNormalizeModuleFederationOptions().internalName}${this.tag}${this.name}${this.tag}`;
|
|
620
623
|
if (this.importId && this.importIdKey === importIdKey) return this.importId;
|
|
621
624
|
if (this.importId) delete idCacheMap[this.importId];
|
|
622
625
|
this.importIdKey = importIdKey;
|
|
@@ -774,7 +777,7 @@ function generateExposes(options, remoteDependencyMap = {}, command = "build") {
|
|
|
774
777
|
export default {
|
|
775
778
|
${Object.keys(options.exposes).map((key) => {
|
|
776
779
|
const remoteDependencyPreloads = (remoteDependencyMap[key] ?? []).map((remoteId) => {
|
|
777
|
-
const virtualRemote = getRemoteVirtualModule(remoteId, command);
|
|
780
|
+
const virtualRemote = getRemoteVirtualModule(remoteId, command, false, "unified", options);
|
|
778
781
|
return `import(${JSON.stringify(virtualRemote.getImportId())})
|
|
779
782
|
.then((mod) => mod.__mf_remote_pending)`;
|
|
780
783
|
}).join(",");
|
|
@@ -805,9 +808,40 @@ function generateExposes(options, remoteDependencyMap = {}, command = "build") {
|
|
|
805
808
|
//#endregion
|
|
806
809
|
//#region src/virtualModules/virtualRuntimeInitStatus.ts
|
|
807
810
|
const virtualRuntimeInitStatus = new VirtualModule("runtimeInit");
|
|
811
|
+
const runtimeInitModules = /* @__PURE__ */ new WeakMap();
|
|
812
|
+
const runtimeInitOwnerIds = /* @__PURE__ */ new WeakMap();
|
|
813
|
+
let nextRuntimeInitOwnerId = 1;
|
|
808
814
|
const MODULE_CACHE_GLOBAL_KEY = "__mf_module_cache__";
|
|
809
|
-
function
|
|
810
|
-
|
|
815
|
+
function getRuntimeInitOwnerId(options) {
|
|
816
|
+
let ownerId = runtimeInitOwnerIds.get(options);
|
|
817
|
+
if (!ownerId) {
|
|
818
|
+
ownerId = nextRuntimeInitOwnerId++;
|
|
819
|
+
runtimeInitOwnerIds.set(options, ownerId);
|
|
820
|
+
}
|
|
821
|
+
return ownerId;
|
|
822
|
+
}
|
|
823
|
+
function getRuntimeInitModule(options) {
|
|
824
|
+
if (!options) return virtualRuntimeInitStatus;
|
|
825
|
+
let runtimeInitModule = runtimeInitModules.get(options);
|
|
826
|
+
if (!runtimeInitModule) {
|
|
827
|
+
const ownerId = getRuntimeInitOwnerId(options);
|
|
828
|
+
runtimeInitModule = new VirtualModule("runtimeInit", "__mf_v__", "", `${options.internalName}__mf_owner__${ownerId}`);
|
|
829
|
+
runtimeInitModules.set(options, runtimeInitModule);
|
|
830
|
+
}
|
|
831
|
+
return runtimeInitModule;
|
|
832
|
+
}
|
|
833
|
+
function getRuntimeInitStatusImportId(options) {
|
|
834
|
+
return getRuntimeInitModule(options).getImportId();
|
|
835
|
+
}
|
|
836
|
+
function getRuntimeRemoteCachePrefix(options) {
|
|
837
|
+
return options ? `${getRuntimeInitStatusImportId(options)}::` : "";
|
|
838
|
+
}
|
|
839
|
+
function getRuntimeRemoteAlias(alias, options) {
|
|
840
|
+
if (!options) return alias;
|
|
841
|
+
return `${options.internalName}__mf_owner__${getRuntimeInitOwnerId(options)}__${alias}`;
|
|
842
|
+
}
|
|
843
|
+
function getRuntimeInitGlobalKey(ownerImportId) {
|
|
844
|
+
return `__mf_init__${ownerImportId ?? virtualRuntimeInitStatus.getImportId()}__`;
|
|
811
845
|
}
|
|
812
846
|
function getDeferredInitPromiseCode() {
|
|
813
847
|
return `let initResolve, initReject;
|
|
@@ -817,10 +851,7 @@ function getDeferredInitPromiseCode() {
|
|
|
817
851
|
});`;
|
|
818
852
|
}
|
|
819
853
|
let _ssrRemotes = [];
|
|
820
|
-
function
|
|
821
|
-
_ssrRemotes = remotes;
|
|
822
|
-
}
|
|
823
|
-
function getSsrNoopResolveCode(enableSsrInit, hostInitImportId, initResolveExpression = "initResolve") {
|
|
854
|
+
function getSsrNoopResolveCode(enableSsrInit, hostInitImportId, initResolveExpression = "initResolve", ssrRemotes = _ssrRemotes) {
|
|
824
855
|
if (!enableSsrInit) return "";
|
|
825
856
|
return `if (${SERVER_ENV_GUARD}) {
|
|
826
857
|
var _noop = { loadRemote: function() { return Promise.resolve(undefined); }, loadShare: function() { return Promise.resolve(undefined); } };
|
|
@@ -840,7 +871,7 @@ function getSsrNoopResolveCode(enableSsrInit, hostInitImportId, initResolveExpre
|
|
|
840
871
|
function() { return [runtimeMod, []]; }
|
|
841
872
|
);
|
|
842
873
|
}).then(function(pair) {
|
|
843
|
-
var runtime = pair[0].init({ name: '__mf_ssr_host__', remotes: ${JSON.stringify(
|
|
874
|
+
var runtime = pair[0].init({ name: '__mf_ssr_host__', remotes: ${JSON.stringify(ssrRemotes)}, shared: {}, plugins: pair[1] });
|
|
844
875
|
${initResolveExpression}(runtime);
|
|
845
876
|
}, function() {
|
|
846
877
|
${initResolveExpression}(_noop);
|
|
@@ -850,7 +881,7 @@ function getSsrNoopResolveCode(enableSsrInit, hostInitImportId, initResolveExpre
|
|
|
850
881
|
}
|
|
851
882
|
function getRuntimeInitStateBootstrapCode(options) {
|
|
852
883
|
return `
|
|
853
|
-
const ${options.globalKeyVar} = ${JSON.stringify(getRuntimeInitGlobalKey())};
|
|
884
|
+
const ${options.globalKeyVar} = ${JSON.stringify(getRuntimeInitGlobalKey(options.ownerImportId))};
|
|
854
885
|
let ${options.stateVar} = globalThis[${options.globalKeyVar}];
|
|
855
886
|
if (!${options.stateVar}) {
|
|
856
887
|
${getDeferredInitPromiseCode()}
|
|
@@ -859,14 +890,14 @@ if (!${options.stateVar}) {
|
|
|
859
890
|
initResolve,
|
|
860
891
|
initReject,
|
|
861
892
|
};
|
|
862
|
-
${getSsrNoopResolveCode(options.enableSsrInit)}
|
|
893
|
+
${getSsrNoopResolveCode(options.enableSsrInit, options.hostInitImportId, "initResolve", options.ssrRemotes)}
|
|
863
894
|
}
|
|
864
895
|
const ${options.exposedConst} = ${options.stateVar}.${options.exposedProperty};
|
|
865
896
|
`;
|
|
866
897
|
}
|
|
867
|
-
function getRuntimeInitBootstrapCode(enableSsrInit = false, hostInitImportId) {
|
|
898
|
+
function getRuntimeInitBootstrapCode(enableSsrInit = false, ownerImportId, ssrRemotes, hostInitImportId = ownerImportId) {
|
|
868
899
|
return `
|
|
869
|
-
const globalKey = ${JSON.stringify(getRuntimeInitGlobalKey())};
|
|
900
|
+
const globalKey = ${JSON.stringify(getRuntimeInitGlobalKey(ownerImportId))};
|
|
870
901
|
const moduleCacheGlobalKey = ${JSON.stringify(MODULE_CACHE_GLOBAL_KEY)};
|
|
871
902
|
globalThis[moduleCacheGlobalKey] ||= { share: {}, remote: {} };
|
|
872
903
|
globalThis[moduleCacheGlobalKey].share ||= {};
|
|
@@ -883,7 +914,7 @@ globalThis[globalKey] = {
|
|
|
883
914
|
${enableSsrInit ? `
|
|
884
915
|
if (${SERVER_ENV_GUARD} && !globalThis[globalKey].ssrInitStarted) {
|
|
885
916
|
globalThis[globalKey].ssrInitStarted = true;
|
|
886
|
-
${getSsrNoopResolveCode(enableSsrInit, hostInitImportId, "globalThis[globalKey].initResolve")}
|
|
917
|
+
${getSsrNoopResolveCode(enableSsrInit, hostInitImportId, "globalThis[globalKey].initResolve", ssrRemotes)}
|
|
887
918
|
}` : ""}
|
|
888
919
|
globalThis[globalKey].moduleCache ||= globalThis[moduleCacheGlobalKey];
|
|
889
920
|
globalThis[globalKey].moduleCache.share ||= {};
|
|
@@ -912,29 +943,36 @@ for (const __mfShareKey of Object.keys(__mfModuleCache.share)) {
|
|
|
912
943
|
}
|
|
913
944
|
`;
|
|
914
945
|
}
|
|
915
|
-
function getRuntimeInitPromiseBootstrapCode(enableSsrInit = false) {
|
|
946
|
+
function getRuntimeInitPromiseBootstrapCode(enableSsrInit = false, ownerImportId, ssrRemotes, hostInitImportId = ownerImportId) {
|
|
916
947
|
return getRuntimeInitStateBootstrapCode({
|
|
917
948
|
globalKeyVar: "__mfPromiseGlobalKey",
|
|
918
949
|
stateVar: "__mfPromiseState",
|
|
919
950
|
exposedConst: "initPromise",
|
|
920
951
|
exposedProperty: "initPromise",
|
|
921
|
-
enableSsrInit
|
|
952
|
+
enableSsrInit,
|
|
953
|
+
ownerImportId,
|
|
954
|
+
hostInitImportId,
|
|
955
|
+
ssrRemotes
|
|
922
956
|
});
|
|
923
957
|
}
|
|
924
|
-
function getRuntimeInitResolveBootstrapCode(enableSsrInit = false) {
|
|
958
|
+
function getRuntimeInitResolveBootstrapCode(enableSsrInit = false, ownerImportId, ssrRemotes, hostInitImportId = ownerImportId) {
|
|
925
959
|
return getRuntimeInitStateBootstrapCode({
|
|
926
960
|
globalKeyVar: "__mfResolveGlobalKey",
|
|
927
961
|
stateVar: "__mfResolveState",
|
|
928
962
|
exposedConst: "initResolve",
|
|
929
963
|
exposedProperty: "initResolve",
|
|
930
|
-
enableSsrInit
|
|
964
|
+
enableSsrInit,
|
|
965
|
+
ownerImportId,
|
|
966
|
+
hostInitImportId,
|
|
967
|
+
ssrRemotes
|
|
931
968
|
});
|
|
932
969
|
}
|
|
933
|
-
function writeRuntimeInitStatus(command, enableSsrInit = false, hostInitImportId) {
|
|
970
|
+
function writeRuntimeInitStatus(command, enableSsrInit = false, hostInitImportId, options, ssrRemotes = _ssrRemotes) {
|
|
934
971
|
const exportStatement = command === "build" ? `const { initPromise, initResolve, initReject, moduleCache } = globalThis[globalKey];
|
|
935
972
|
export { initPromise, initResolve, initReject, moduleCache };` : `module.exports = globalThis[globalKey];`;
|
|
936
|
-
|
|
937
|
-
|
|
973
|
+
const ownerImportId = options ? getRuntimeInitStatusImportId(options) : hostInitImportId;
|
|
974
|
+
getRuntimeInitModule(options).writeSync(`
|
|
975
|
+
${getRuntimeInitBootstrapCode(enableSsrInit, ownerImportId, ssrRemotes, hostInitImportId)}
|
|
938
976
|
${exportStatement}
|
|
939
977
|
`);
|
|
940
978
|
}
|
|
@@ -1079,25 +1117,35 @@ function createCodePositionMap(code) {
|
|
|
1079
1117
|
}
|
|
1080
1118
|
//#endregion
|
|
1081
1119
|
//#region src/utils/treeShaking.ts
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
let
|
|
1090
|
-
|
|
1091
|
-
|
|
1120
|
+
const legacyTreeShakingState = {
|
|
1121
|
+
inferredUsage: /* @__PURE__ */ new Map(),
|
|
1122
|
+
buildMode: false
|
|
1123
|
+
};
|
|
1124
|
+
const treeShakingStates = /* @__PURE__ */ new WeakMap();
|
|
1125
|
+
function getTreeShakingState(options) {
|
|
1126
|
+
if (!options) return legacyTreeShakingState;
|
|
1127
|
+
let state = treeShakingStates.get(options);
|
|
1128
|
+
if (!state) {
|
|
1129
|
+
state = {
|
|
1130
|
+
inferredUsage: /* @__PURE__ */ new Map(),
|
|
1131
|
+
buildMode: false
|
|
1132
|
+
};
|
|
1133
|
+
treeShakingStates.set(options, state);
|
|
1134
|
+
}
|
|
1135
|
+
return state;
|
|
1136
|
+
}
|
|
1137
|
+
function setTreeShakingBuildMode(enabled, options) {
|
|
1138
|
+
getTreeShakingState(options).buildMode = enabled;
|
|
1092
1139
|
}
|
|
1093
|
-
function resetTreeShakingExports() {
|
|
1094
|
-
|
|
1140
|
+
function resetTreeShakingExports(options) {
|
|
1141
|
+
getTreeShakingState(options).inferredUsage.clear();
|
|
1095
1142
|
}
|
|
1096
|
-
function getOrCreateExportRecord(sharedKey, request) {
|
|
1097
|
-
|
|
1143
|
+
function getOrCreateExportRecord(sharedKey, request, options) {
|
|
1144
|
+
const inferredUsage = getTreeShakingState(options).inferredUsage;
|
|
1145
|
+
let byRequest = inferredUsage.get(sharedKey);
|
|
1098
1146
|
if (!byRequest) {
|
|
1099
1147
|
byRequest = /* @__PURE__ */ new Map();
|
|
1100
|
-
|
|
1148
|
+
inferredUsage.set(sharedKey, byRequest);
|
|
1101
1149
|
}
|
|
1102
1150
|
let record = byRequest.get(request);
|
|
1103
1151
|
if (!record) {
|
|
@@ -1109,22 +1157,23 @@ function getOrCreateExportRecord(sharedKey, request) {
|
|
|
1109
1157
|
}
|
|
1110
1158
|
return record;
|
|
1111
1159
|
}
|
|
1112
|
-
function recordTreeShakingExports(sharedKey, exports, request = sharedKey) {
|
|
1113
|
-
const record = getOrCreateExportRecord(sharedKey, request);
|
|
1160
|
+
function recordTreeShakingExports(sharedKey, exports, request = sharedKey, options) {
|
|
1161
|
+
const record = getOrCreateExportRecord(sharedKey, request, options);
|
|
1114
1162
|
exports.forEach((name) => record.usedExports.add(name));
|
|
1115
1163
|
}
|
|
1116
|
-
function markTreeShakingPackageUnsafe(sharedKey, request = sharedKey) {
|
|
1117
|
-
getOrCreateExportRecord(sharedKey, request).requiresFullBundle = true;
|
|
1164
|
+
function markTreeShakingPackageUnsafe(sharedKey, request = sharedKey, options) {
|
|
1165
|
+
getOrCreateExportRecord(sharedKey, request, options).requiresFullBundle = true;
|
|
1118
1166
|
}
|
|
1119
|
-
function getExportRecords(sharedKey, request) {
|
|
1167
|
+
function getExportRecords(sharedKey, request, options) {
|
|
1168
|
+
const inferredUsage = getTreeShakingState(options).inferredUsage;
|
|
1120
1169
|
if (sharedKey) {
|
|
1121
|
-
const records =
|
|
1170
|
+
const records = inferredUsage.get(sharedKey);
|
|
1122
1171
|
const wildcard = records?.get("*");
|
|
1123
1172
|
const exact = records?.get(request);
|
|
1124
1173
|
return [wildcard, exact === wildcard ? void 0 : exact].filter((record) => !!record);
|
|
1125
1174
|
}
|
|
1126
1175
|
const records = [];
|
|
1127
|
-
|
|
1176
|
+
inferredUsage.forEach((byRequest, configuredKey) => {
|
|
1128
1177
|
const wildcard = byRequest.get("*");
|
|
1129
1178
|
const exact = byRequest.get(request);
|
|
1130
1179
|
const keyBase = configuredKey.endsWith("/") ? configuredKey.slice(0, -1) : configuredKey;
|
|
@@ -1141,10 +1190,10 @@ function getExportRecords(sharedKey, request) {
|
|
|
1141
1190
|
* fallback lookup across keys keeps aliases/backwards-compatible callers
|
|
1142
1191
|
* working, while still keeping each concrete request's exports isolated.
|
|
1143
1192
|
*/
|
|
1144
|
-
function getTreeShakingExportUsage(request, shareItem, sharedKey) {
|
|
1193
|
+
function getTreeShakingExportUsage(request, shareItem, sharedKey, options) {
|
|
1145
1194
|
const treeShaking = shareItem?.shareConfig.treeShaking;
|
|
1146
|
-
if (!treeShaking || !
|
|
1147
|
-
const records = getExportRecords(sharedKey, request);
|
|
1195
|
+
if (!treeShaking || !getTreeShakingState(options).buildMode) return void 0;
|
|
1196
|
+
const records = getExportRecords(sharedKey, request, options);
|
|
1148
1197
|
if (records.some((record) => record.requiresFullBundle)) return { kind: "full" };
|
|
1149
1198
|
const configured = treeShaking.usedExports ?? [];
|
|
1150
1199
|
const result = new Set(configured);
|
|
@@ -1661,7 +1710,7 @@ function getNamedExportsViaRegex(source, filePath, visited, scanState = { comple
|
|
|
1661
1710
|
}
|
|
1662
1711
|
const name = asMatch[1];
|
|
1663
1712
|
if (isValidEsmExportName(name)) names.add(name);
|
|
1664
|
-
else scanState.complete = false;
|
|
1713
|
+
else if (name === "default" || name === "__esModule") {} else scanState.complete = false;
|
|
1665
1714
|
}
|
|
1666
1715
|
}
|
|
1667
1716
|
const namespaceReExportRegex = new RegExp(`export\\s+\\*\\s+as\\s+(${JS_IDENTIFIER_PATTERN})\\s+from\\s+['"][^'"]+['"]`, "gu");
|
|
@@ -1852,8 +1901,8 @@ function getDependencyNames(packageJson) {
|
|
|
1852
1901
|
}
|
|
1853
1902
|
return Array.from(names);
|
|
1854
1903
|
}
|
|
1855
|
-
function isSharedSingletonConsumedByPeer(pkg) {
|
|
1856
|
-
const shared =
|
|
1904
|
+
function isSharedSingletonConsumedByPeer(pkg, options = getNormalizeModuleFederationOptions()) {
|
|
1905
|
+
const shared = options?.shared || {};
|
|
1857
1906
|
if (Object.entries(shared).some(([key, item]) => key !== pkg && key.startsWith(`${pkg}/`) && item.shareConfig.singleton === true)) return true;
|
|
1858
1907
|
const sharedKeyByPackageName = /* @__PURE__ */ new Map();
|
|
1859
1908
|
Object.entries(shared).filter(([, item]) => item.shareConfig.singleton === true).forEach(([key]) => {
|
|
@@ -1874,8 +1923,7 @@ function isSharedSingletonConsumedByPeer(pkg) {
|
|
|
1874
1923
|
};
|
|
1875
1924
|
return Array.from(sharedKeyByPackageName.values()).some((sharedPkg) => sharedPkg !== pkg && reachesPkg(sharedPkg, new Set([sharedPkg])));
|
|
1876
1925
|
}
|
|
1877
|
-
function isRemoteOnlyContainer() {
|
|
1878
|
-
const options = getNormalizeModuleFederationOptions();
|
|
1926
|
+
function isRemoteOnlyContainer(options = getNormalizeModuleFederationOptions()) {
|
|
1879
1927
|
return Object.keys(options.exposes || {}).length > 0 && Object.keys(options.remotes || {}).length === 0;
|
|
1880
1928
|
}
|
|
1881
1929
|
function tryResolveImportFromPackageRoot(pkg, root) {
|
|
@@ -1898,13 +1946,42 @@ function getConcreteSharedImportSource(pkg, shareItem) {
|
|
|
1898
1946
|
}
|
|
1899
1947
|
return tryResolveImportFromPackageRoot(pkg, currentDir);
|
|
1900
1948
|
}
|
|
1901
|
-
const preBuildCacheMap = {};
|
|
1902
|
-
const preBuildShareItemMap = {};
|
|
1903
1949
|
const PREBUILD_TAG = "__prebuild__";
|
|
1904
|
-
const treeShakingProviderCacheMap = {};
|
|
1905
|
-
const materializedTreeShakingProviders = /* @__PURE__ */ new Set();
|
|
1906
1950
|
const TREE_SHAKING_PROVIDER_TAG = "__treeShakingProvider__";
|
|
1907
1951
|
const TREE_SHAKING_GRAPH_QUERY = "__mf_tree_shaking_graph__";
|
|
1952
|
+
const legacySharedVirtualModuleState = {
|
|
1953
|
+
preBuildCacheMap: {},
|
|
1954
|
+
preBuildShareItemMap: {},
|
|
1955
|
+
treeShakingProviderCacheMap: {},
|
|
1956
|
+
materializedTreeShakingProviders: /* @__PURE__ */ new Set(),
|
|
1957
|
+
loadShareCacheMap: {}
|
|
1958
|
+
};
|
|
1959
|
+
const sharedVirtualModuleStates = /* @__PURE__ */ new WeakMap();
|
|
1960
|
+
let nextSharedVirtualModuleOwnerId = 1;
|
|
1961
|
+
function getSharedVirtualModuleState(options) {
|
|
1962
|
+
if (!options) try {
|
|
1963
|
+
const currentOptions = getNormalizeModuleFederationOptions();
|
|
1964
|
+
return sharedVirtualModuleStates.get(currentOptions) ?? legacySharedVirtualModuleState;
|
|
1965
|
+
} catch {
|
|
1966
|
+
return legacySharedVirtualModuleState;
|
|
1967
|
+
}
|
|
1968
|
+
let state = sharedVirtualModuleStates.get(options);
|
|
1969
|
+
if (!state) {
|
|
1970
|
+
state = {
|
|
1971
|
+
preBuildCacheMap: {},
|
|
1972
|
+
preBuildShareItemMap: {},
|
|
1973
|
+
treeShakingProviderCacheMap: {},
|
|
1974
|
+
materializedTreeShakingProviders: /* @__PURE__ */ new Set(),
|
|
1975
|
+
loadShareCacheMap: {},
|
|
1976
|
+
ownerKey: `${options.internalName}__mf_owner__${nextSharedVirtualModuleOwnerId++}`
|
|
1977
|
+
};
|
|
1978
|
+
sharedVirtualModuleStates.set(options, state);
|
|
1979
|
+
}
|
|
1980
|
+
return state;
|
|
1981
|
+
}
|
|
1982
|
+
function createScopedSharedVirtualModule(pkg, tag, options) {
|
|
1983
|
+
return new VirtualModule(pkg, tag, ".js", getSharedVirtualModuleState(options).ownerKey);
|
|
1984
|
+
}
|
|
1908
1985
|
function getTreeShakingGraphToken(id) {
|
|
1909
1986
|
if (!id) return void 0;
|
|
1910
1987
|
const queryStart = id.indexOf("?");
|
|
@@ -1935,19 +2012,21 @@ function addTreeShakingGraphQuery(id, token) {
|
|
|
1935
2012
|
const hash = hashStart === -1 ? "" : cleanId.slice(hashStart);
|
|
1936
2013
|
return `${base}${base.includes("?") ? "&" : "?"}${TREE_SHAKING_GRAPH_QUERY}=${encodeURIComponent(token)}${hash}`;
|
|
1937
2014
|
}
|
|
1938
|
-
function getConcreteTreeShakingExportUsage(pkg, shareItem) {
|
|
1939
|
-
return getTreeShakingExportUsage(pkg, shareItem, shareItem?.name);
|
|
2015
|
+
function getConcreteTreeShakingExportUsage(pkg, shareItem, options) {
|
|
2016
|
+
return getTreeShakingExportUsage(pkg, shareItem, shareItem?.name, options);
|
|
1940
2017
|
}
|
|
1941
|
-
function getTreeShakingSharedProviderName(pkg) {
|
|
1942
|
-
const
|
|
1943
|
-
return `${internalName
|
|
2018
|
+
function getTreeShakingSharedProviderName(pkg, options) {
|
|
2019
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2020
|
+
return `${getSharedVirtualModuleState(options).ownerKey ?? resolvedOptions.internalName ?? resolvedOptions.name}__tree_shaking__${packageNameEncode(pkg)}`;
|
|
1944
2021
|
}
|
|
1945
|
-
function getTreeShakingSharedProviderImportId(pkg) {
|
|
1946
|
-
|
|
2022
|
+
function getTreeShakingSharedProviderImportId(pkg, options) {
|
|
2023
|
+
const { treeShakingProviderCacheMap } = getSharedVirtualModuleState(options);
|
|
2024
|
+
if (!treeShakingProviderCacheMap[pkg]) treeShakingProviderCacheMap[pkg] = createScopedSharedVirtualModule(pkg, TREE_SHAKING_PROVIDER_TAG, options);
|
|
1947
2025
|
return treeShakingProviderCacheMap[pkg].getImportId();
|
|
1948
2026
|
}
|
|
1949
|
-
function hasTreeShakingSharedProvider(pkg, shareItem) {
|
|
1950
|
-
const
|
|
2027
|
+
function hasTreeShakingSharedProvider(pkg, shareItem, options) {
|
|
2028
|
+
const { materializedTreeShakingProviders } = getSharedVirtualModuleState(options);
|
|
2029
|
+
const usage = getConcreteTreeShakingExportUsage(pkg, shareItem, options);
|
|
1951
2030
|
return materializedTreeShakingProviders.has(pkg) && usage?.kind === "exports";
|
|
1952
2031
|
}
|
|
1953
2032
|
/**
|
|
@@ -1959,8 +2038,9 @@ function hasTreeShakingSharedProvider(pkg, shareItem) {
|
|
|
1959
2038
|
* perform its normal usedExports compatibility check and safely choose the full
|
|
1960
2039
|
* provider when the optimized one is insufficient.
|
|
1961
2040
|
*/
|
|
1962
|
-
function writeTreeShakingSharedProvider(pkg, shareItem) {
|
|
1963
|
-
const
|
|
2041
|
+
function writeTreeShakingSharedProvider(pkg, shareItem, options) {
|
|
2042
|
+
const { materializedTreeShakingProviders, treeShakingProviderCacheMap } = getSharedVirtualModuleState(options);
|
|
2043
|
+
const usage = getConcreteTreeShakingExportUsage(pkg, shareItem, options);
|
|
1964
2044
|
if (usage?.kind !== "exports" || !usage.usedExports.length || shareItem?.shareConfig.import === false) {
|
|
1965
2045
|
materializedTreeShakingProviders.delete(pkg);
|
|
1966
2046
|
return;
|
|
@@ -1972,7 +2052,7 @@ function writeTreeShakingSharedProvider(pkg, shareItem) {
|
|
|
1972
2052
|
mfWarn(`Tree-shaking shared dependency "${pkg}" was disabled because export "${unsupportedExport}" cannot be represented by the generated ESM provider.`);
|
|
1973
2053
|
return;
|
|
1974
2054
|
}
|
|
1975
|
-
const provider = treeShakingProviderCacheMap[pkg] || (treeShakingProviderCacheMap[pkg] =
|
|
2055
|
+
const provider = treeShakingProviderCacheMap[pkg] || (treeShakingProviderCacheMap[pkg] = createScopedSharedVirtualModule(pkg, "__treeShakingProvider__", options));
|
|
1976
2056
|
const optimizedImportSource = addTreeShakingGraphQuery(getConcreteSharedImportSource(pkg, shareItem) || pkg, pkg);
|
|
1977
2057
|
const namedExports = usedExports.filter((name) => name !== "default");
|
|
1978
2058
|
const namedImports = namedExports.map((name, index) => `${name} as __mfTreeShaken_${index}`).join(", ");
|
|
@@ -1994,11 +2074,12 @@ export default { get, init };
|
|
|
1994
2074
|
`, true);
|
|
1995
2075
|
materializedTreeShakingProviders.add(pkg);
|
|
1996
2076
|
}
|
|
1997
|
-
function writePreBuildLibPath(pkg, shareItem) {
|
|
1998
|
-
|
|
2077
|
+
function writePreBuildLibPath(pkg, shareItem, options) {
|
|
2078
|
+
const { preBuildCacheMap, preBuildShareItemMap } = getSharedVirtualModuleState(options);
|
|
2079
|
+
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = createScopedSharedVirtualModule(pkg, PREBUILD_TAG, options);
|
|
1999
2080
|
preBuildShareItemMap[pkg] = shareItem;
|
|
2000
2081
|
const importSource = getConcreteSharedImportSource(pkg, shareItem) || pkg;
|
|
2001
|
-
writeTreeShakingSharedProvider(pkg, shareItem);
|
|
2082
|
+
writeTreeShakingSharedProvider(pkg, shareItem, options);
|
|
2002
2083
|
if (pkg === "react/compiler-runtime") {
|
|
2003
2084
|
const reactCacheDescriptor = getSharedCacheDescriptorLiteral("react", shareItem ?? {
|
|
2004
2085
|
name: "react",
|
|
@@ -2067,36 +2148,36 @@ function writePreBuildLibPath(pkg, shareItem) {
|
|
|
2067
2148
|
`, true);
|
|
2068
2149
|
}
|
|
2069
2150
|
/** Re-render already materialized wrappers after import analysis discovers exports. */
|
|
2070
|
-
function refreshTreeShakingModules() {
|
|
2151
|
+
function refreshTreeShakingModules(options) {
|
|
2152
|
+
const { preBuildShareItemMap } = getSharedVirtualModuleState(options);
|
|
2071
2153
|
for (const [pkg, shareItem] of Object.entries(preBuildShareItemMap)) {
|
|
2072
2154
|
if (!shareItem?.shareConfig.treeShaking) continue;
|
|
2073
|
-
writePreBuildLibPath(pkg, shareItem);
|
|
2074
|
-
writeLoadShareModule(pkg, shareItem, "build", false);
|
|
2155
|
+
writePreBuildLibPath(pkg, shareItem, options);
|
|
2156
|
+
writeLoadShareModule(pkg, shareItem, "build", false, options);
|
|
2075
2157
|
}
|
|
2076
2158
|
}
|
|
2077
|
-
function getPreBuildLibImportId(pkg) {
|
|
2078
|
-
|
|
2159
|
+
function getPreBuildLibImportId(pkg, options) {
|
|
2160
|
+
const { preBuildCacheMap } = getSharedVirtualModuleState(options);
|
|
2161
|
+
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = createScopedSharedVirtualModule(pkg, PREBUILD_TAG, options);
|
|
2079
2162
|
return preBuildCacheMap[pkg].getImportId();
|
|
2080
2163
|
}
|
|
2081
|
-
function getPreBuildShareItem(pkg) {
|
|
2082
|
-
return preBuildShareItemMap[pkg];
|
|
2164
|
+
function getPreBuildShareItem(pkg, options) {
|
|
2165
|
+
return getSharedVirtualModuleState(options).preBuildShareItemMap[pkg];
|
|
2083
2166
|
}
|
|
2084
|
-
function getSharedImportSource(pkg, shareItem) {
|
|
2085
|
-
return getConcreteSharedImportSource(pkg, shareItem) || getPreBuildLibImportId(pkg);
|
|
2167
|
+
function getSharedImportSource(pkg, shareItem, options) {
|
|
2168
|
+
return getConcreteSharedImportSource(pkg, shareItem) || getPreBuildLibImportId(pkg, options);
|
|
2086
2169
|
}
|
|
2087
2170
|
const LOAD_SHARE_TAG = "__loadShare__";
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] =
|
|
2171
|
+
function getLoadShareImportId(pkg, _isRolldown, options) {
|
|
2172
|
+
const { loadShareCacheMap } = getSharedVirtualModuleState(options);
|
|
2173
|
+
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = createScopedSharedVirtualModule(pkg, LOAD_SHARE_TAG, options);
|
|
2091
2174
|
return loadShareCacheMap[pkg].getImportId();
|
|
2092
2175
|
}
|
|
2093
|
-
function getLoadShareModulePath(pkg, isRolldown) {
|
|
2094
|
-
|
|
2176
|
+
function getLoadShareModulePath(pkg, isRolldown, options) {
|
|
2177
|
+
const { loadShareCacheMap } = getSharedVirtualModuleState(options);
|
|
2178
|
+
if (!loadShareCacheMap[pkg]) getLoadShareImportId(pkg, isRolldown, options);
|
|
2095
2179
|
return loadShareCacheMap[pkg].getImportId();
|
|
2096
2180
|
}
|
|
2097
|
-
function toViteOptimizedDepVirtualId(id) {
|
|
2098
|
-
return toViteEncodedId(id);
|
|
2099
|
-
}
|
|
2100
2181
|
function getCachedLoadSharePkg(id) {
|
|
2101
2182
|
if (!id.includes("__loadShare__")) return;
|
|
2102
2183
|
const normalized = normalizeVirtualModuleId(id);
|
|
@@ -2114,8 +2195,8 @@ function materializeCachedLoadShareModule(options) {
|
|
|
2114
2195
|
const key = options.findSharedKey(pkg, options.shared);
|
|
2115
2196
|
if (!key) return;
|
|
2116
2197
|
const shareItem = options.shared[key];
|
|
2117
|
-
writeLoadShareModule(pkg, shareItem, options.command, options.isRolldown);
|
|
2118
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(pkg, shareItem);
|
|
2198
|
+
writeLoadShareModule(pkg, shareItem, options.command, options.isRolldown, options.federationOptions);
|
|
2199
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(pkg, shareItem, options.federationOptions);
|
|
2119
2200
|
options.addUsedShares(pkg);
|
|
2120
2201
|
options.writeLocalSharedImportMap();
|
|
2121
2202
|
}
|
|
@@ -2242,12 +2323,15 @@ const normalizeLocalShareModuleCode = `const __mfNormalizeShareModule = (mod) =>
|
|
|
2242
2323
|
? Object.assign({}, normalized)
|
|
2243
2324
|
: normalized;
|
|
2244
2325
|
};`;
|
|
2245
|
-
function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
2246
|
-
|
|
2326
|
+
function writeLoadShareModule(pkg, shareItem, command, _isRolldown, options) {
|
|
2327
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2328
|
+
const { loadShareCacheMap } = getSharedVirtualModuleState(options);
|
|
2329
|
+
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = createScopedSharedVirtualModule(pkg, LOAD_SHARE_TAG, options);
|
|
2247
2330
|
let importLine = getRuntimeModuleCacheBootstrapCode();
|
|
2248
2331
|
const cacheDescriptor = getSharedCacheDescriptorLiteral(pkg, shareItem);
|
|
2249
|
-
const cacheOwner = JSON.stringify(
|
|
2250
|
-
const
|
|
2332
|
+
const cacheOwner = JSON.stringify(resolvedOptions.name);
|
|
2333
|
+
const runtimeInitOwnerImportId = options ? getRuntimeInitStatusImportId(options) : void 0;
|
|
2334
|
+
const treeShakingConsumer = command === "build" && shareItem.shareConfig.treeShaking ? resolvedOptions.name : void 0;
|
|
2251
2335
|
if (shareItem.shareConfig.import === false) {
|
|
2252
2336
|
const detectedNamedExports = getPackageNamedExports(pkg);
|
|
2253
2337
|
const namedExports = detectedNamedExports ?? [];
|
|
@@ -2258,7 +2342,7 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
2258
2342
|
exportLine = generateDeferredHostProvidedExports([], pkg, cacheDescriptor, treeShakingConsumer);
|
|
2259
2343
|
}
|
|
2260
2344
|
loadShareCacheMap[pkg].writeSync(`
|
|
2261
|
-
${getRuntimeInitPromiseBootstrapCode()}
|
|
2345
|
+
${getRuntimeInitPromiseBootstrapCode(false, runtimeInitOwnerImportId)}
|
|
2262
2346
|
${importLine}
|
|
2263
2347
|
${sharedCacheHelperCode}
|
|
2264
2348
|
${exportLine}
|
|
@@ -2266,7 +2350,7 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
2266
2350
|
return;
|
|
2267
2351
|
}
|
|
2268
2352
|
const concreteSharedImportSource = getConcreteSharedImportSource(pkg, shareItem);
|
|
2269
|
-
const sharedImportSource = concreteSharedImportSource || getPreBuildLibImportId(pkg);
|
|
2353
|
+
const sharedImportSource = concreteSharedImportSource || getPreBuildLibImportId(pkg, options);
|
|
2270
2354
|
const devImportSource = concreteSharedImportSource || pkg;
|
|
2271
2355
|
const localProviderPath = getLocalProviderImportPath(pkg);
|
|
2272
2356
|
const coherentLocalSource = concreteSharedImportSource || localProviderPath || devImportSource;
|
|
@@ -2278,20 +2362,20 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
2278
2362
|
const hasCompleteExportCoverage = detectedNamedExports !== void 0;
|
|
2279
2363
|
const isWorkspaceSingleton = isWorkspacePackage && shareItem.shareConfig.singleton === true;
|
|
2280
2364
|
const isDefaultShareScope = shareItem.scope === void 0 || shareItem.scope === "default" || Array.isArray(shareItem.scope) && shareItem.scope[0] === "default";
|
|
2281
|
-
const usesDeferredSingletonFallback = hasCompleteExportCoverage && (isWorkspaceSingleton || command !== "build" && isRemoteOnlyContainer() && shareItem.shareConfig.singleton === true || command === "build" && isRemoteOnlyContainer() && shareItem.shareConfig.singleton === true && !isDefaultShareScope);
|
|
2282
|
-
const servesRemoteSingletonFallback = command !== "build" && isRemoteOnlyContainer() && shareItem.shareConfig.singleton === true;
|
|
2283
|
-
const isConsumedByPeerSingleton = isSharedSingletonConsumedByPeer(pkg);
|
|
2284
|
-
const usesEntryInjectedRemoteFallback = hasCompleteExportCoverage && command !== "build" && !isWorkspaceSingleton && isRemoteOnlyContainer() && shareItem.shareConfig.singleton === true &&
|
|
2365
|
+
const usesDeferredSingletonFallback = hasCompleteExportCoverage && (isWorkspaceSingleton || command !== "build" && isRemoteOnlyContainer(resolvedOptions) && shareItem.shareConfig.singleton === true || command === "build" && isRemoteOnlyContainer(resolvedOptions) && shareItem.shareConfig.singleton === true && !isDefaultShareScope);
|
|
2366
|
+
const servesRemoteSingletonFallback = command !== "build" && isRemoteOnlyContainer(resolvedOptions) && shareItem.shareConfig.singleton === true;
|
|
2367
|
+
const isConsumedByPeerSingleton = isSharedSingletonConsumedByPeer(pkg, resolvedOptions);
|
|
2368
|
+
const usesEntryInjectedRemoteFallback = hasCompleteExportCoverage && command !== "build" && !isWorkspaceSingleton && isRemoteOnlyContainer(resolvedOptions) && shareItem.shareConfig.singleton === true && resolvedOptions.hostInitInjectLocation === "entry" && isConsumedByPeerSingleton;
|
|
2285
2369
|
const usesEagerWorkspaceFallback = hasCompleteExportCoverage && isWorkspaceSingleton && isConsumedByPeerSingleton;
|
|
2286
2370
|
const usesDeferredTreeShakingFallback = hasCompleteExportCoverage && Boolean(treeShakingConsumer);
|
|
2287
2371
|
let exportLine;
|
|
2288
2372
|
let initBlock = "";
|
|
2289
2373
|
if (usesDeferredTreeShakingFallback) {
|
|
2290
|
-
importLine = `${getRuntimeInitPromiseBootstrapCode()}\n ${importLine}`;
|
|
2374
|
+
importLine = `${getRuntimeInitPromiseBootstrapCode(false, runtimeInitOwnerImportId)}\n ${importLine}`;
|
|
2291
2375
|
exportLine = generateLazyWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor, cacheOwner, treeShakingConsumer, command !== "build" && (isWorkspaceSingleton || isWorkspacePackage || servesRemoteSingletonFallback));
|
|
2292
2376
|
} else if (usesEagerWorkspaceFallback || usesEntryInjectedRemoteFallback) exportLine = generateEagerWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor, cacheOwner, treeShakingConsumer);
|
|
2293
2377
|
else if (usesDeferredSingletonFallback) {
|
|
2294
|
-
importLine = `${getRuntimeInitPromiseBootstrapCode()}\n ${importLine}`;
|
|
2378
|
+
importLine = `${getRuntimeInitPromiseBootstrapCode(false, runtimeInitOwnerImportId)}\n ${importLine}`;
|
|
2295
2379
|
exportLine = generateLazyWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor, cacheOwner, treeShakingConsumer, command !== "build" && (isWorkspaceSingleton || isWorkspacePackage || servesRemoteSingletonFallback));
|
|
2296
2380
|
} else if (detectedNamedExports === void 0) {
|
|
2297
2381
|
exportLine = `const __mfDefaultExport = (() => {
|
|
@@ -2380,74 +2464,98 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
2380
2464
|
//#endregion
|
|
2381
2465
|
//#region src/virtualModules/virtualRemoteEntry.ts
|
|
2382
2466
|
let usedShares = /* @__PURE__ */ new Set();
|
|
2383
|
-
|
|
2467
|
+
const usedSharesByOptions = /* @__PURE__ */ new WeakMap();
|
|
2468
|
+
function getScopedUsedShares(options) {
|
|
2469
|
+
let scoped = usedSharesByOptions.get(options);
|
|
2470
|
+
if (!scoped) {
|
|
2471
|
+
scoped = /* @__PURE__ */ new Set();
|
|
2472
|
+
usedSharesByOptions.set(options, scoped);
|
|
2473
|
+
}
|
|
2474
|
+
return scoped;
|
|
2475
|
+
}
|
|
2476
|
+
function getUsedShares(options) {
|
|
2477
|
+
if (options) return getScopedUsedShares(options);
|
|
2384
2478
|
return usedShares;
|
|
2385
2479
|
}
|
|
2386
|
-
function addUsedShares(pkg) {
|
|
2480
|
+
function addUsedShares(pkg, options) {
|
|
2387
2481
|
usedShares.add(pkg);
|
|
2482
|
+
if (options) getScopedUsedShares(options).add(pkg);
|
|
2388
2483
|
}
|
|
2389
2484
|
const LOCAL_SHARED_IMPORT_MAP_ID = "virtual:mf-localSharedImportMap";
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2485
|
+
const localOwnerIds = /* @__PURE__ */ new WeakMap();
|
|
2486
|
+
let nextLocalOwnerId = 1;
|
|
2487
|
+
function getLocalOwnerKey(options) {
|
|
2488
|
+
let ownerId = localOwnerIds.get(options);
|
|
2489
|
+
if (!ownerId) {
|
|
2490
|
+
ownerId = nextLocalOwnerId++;
|
|
2491
|
+
localOwnerIds.set(options, ownerId);
|
|
2492
|
+
}
|
|
2493
|
+
return `${options.internalName}__mf_owner__${ownerId}`;
|
|
2393
2494
|
}
|
|
2394
|
-
function
|
|
2395
|
-
|
|
2495
|
+
function getLocalSharedImportMapPath(options) {
|
|
2496
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2497
|
+
return `${LOCAL_SHARED_IMPORT_MAP_ID}:${packageNameEncode(options ? getLocalOwnerKey(resolvedOptions) : resolvedOptions.internalName || resolvedOptions.name)}`;
|
|
2498
|
+
}
|
|
2499
|
+
function getResolvedLocalSharedImportMapId(options) {
|
|
2500
|
+
return `\0${getLocalSharedImportMapPath(options)}`;
|
|
2396
2501
|
}
|
|
2397
2502
|
let invalidateLocalSharedImportMap;
|
|
2398
|
-
|
|
2399
|
-
|
|
2503
|
+
const localSharedImportMapInvalidators = /* @__PURE__ */ new WeakMap();
|
|
2504
|
+
function setLocalSharedImportMapInvalidator(invalidator, options) {
|
|
2505
|
+
if (!options) invalidateLocalSharedImportMap = invalidator;
|
|
2506
|
+
else if (invalidator) localSharedImportMapInvalidators.set(options, invalidator);
|
|
2507
|
+
else localSharedImportMapInvalidators.delete(options);
|
|
2400
2508
|
}
|
|
2401
|
-
function writeLocalSharedImportMap() {
|
|
2402
|
-
invalidateLocalSharedImportMap?.();
|
|
2509
|
+
function writeLocalSharedImportMap(options) {
|
|
2510
|
+
(options ? localSharedImportMapInvalidators.get(options) : invalidateLocalSharedImportMap)?.();
|
|
2403
2511
|
}
|
|
2404
2512
|
function shouldUseDirectReactImport() {
|
|
2405
2513
|
const isVinext = hasPackageDependency("vinext");
|
|
2406
2514
|
const isAstro = hasPackageDependency("astro");
|
|
2407
2515
|
return isVinext || isAstro;
|
|
2408
2516
|
}
|
|
2409
|
-
function getLocalSharedPackagePath(pkg, shareItem) {
|
|
2517
|
+
function getLocalSharedPackagePath(pkg, shareItem, options) {
|
|
2410
2518
|
if (shouldUseDirectReactImport() && pkg === "react") return "react";
|
|
2411
|
-
return getConcreteSharedImportSource(pkg, shareItem) || getLocalProviderImportPath(pkg) || getSharedImportSource(pkg, shareItem);
|
|
2519
|
+
return getConcreteSharedImportSource(pkg, shareItem) || getLocalProviderImportPath(pkg) || getSharedImportSource(pkg, shareItem, options);
|
|
2412
2520
|
}
|
|
2413
2521
|
function getDirectSharedCacheSeedImportPath(pkg, shareItem) {
|
|
2414
2522
|
return getConcreteSharedImportSource(pkg, shareItem) || getProjectResolvedImportPath(pkg) || getLocalProviderImportPath(pkg) || pkg;
|
|
2415
2523
|
}
|
|
2416
|
-
function generateLocalSharedImportMap() {
|
|
2524
|
+
function generateLocalSharedImportMap(options) {
|
|
2525
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2417
2526
|
const useDirectReactImport = shouldUseDirectReactImport();
|
|
2418
|
-
const
|
|
2419
|
-
const orderedShares = getOrderedUsedShares();
|
|
2527
|
+
const orderedShares = getOrderedUsedShares(options);
|
|
2420
2528
|
return `
|
|
2421
2529
|
import {loadShare} from "@module-federation/runtime";
|
|
2422
2530
|
${orderedShares.map((pkg, index) => {
|
|
2423
|
-
const shareItem = getNormalizeShareItem(pkg);
|
|
2531
|
+
const shareItem = getNormalizeShareItem(pkg, resolvedOptions);
|
|
2424
2532
|
if (!shareItem?.shareConfig.eager || shareItem.shareConfig.import === false) return "";
|
|
2425
|
-
return `import * as __mfEagerShare_${index} from ${JSON.stringify(getLocalSharedPackagePath(pkg, shareItem))};`;
|
|
2533
|
+
return `import * as __mfEagerShare_${index} from ${JSON.stringify(getLocalSharedPackagePath(pkg, shareItem, options))};`;
|
|
2426
2534
|
}).filter(Boolean).join("\n")}
|
|
2427
2535
|
const importMap = {
|
|
2428
2536
|
${orderedShares.map((pkg, index) => {
|
|
2429
|
-
const shareItem = getNormalizeShareItem(pkg);
|
|
2537
|
+
const shareItem = getNormalizeShareItem(pkg, resolvedOptions);
|
|
2430
2538
|
return `
|
|
2431
2539
|
${JSON.stringify(pkg)}: async () => {
|
|
2432
2540
|
${shareItem?.shareConfig.import === false ? `throw new Error(\`[Module Federation] Shared module '\${${JSON.stringify(pkg)}}' must be provided by host\`);` : shareItem?.shareConfig.eager ? `let pkg = __mfEagerShare_${index};
|
|
2433
|
-
return pkg;` : `let pkg = await import(${JSON.stringify(getLocalSharedPackagePath(pkg, shareItem))});
|
|
2541
|
+
return pkg;` : `let pkg = await import(${JSON.stringify(getLocalSharedPackagePath(pkg, shareItem, options))});
|
|
2434
2542
|
return pkg;`}
|
|
2435
2543
|
}
|
|
2436
2544
|
`;
|
|
2437
2545
|
}).join(",")}
|
|
2438
2546
|
}
|
|
2439
2547
|
const usedShared = {
|
|
2440
|
-
${getOrderedUsedShares().map((key) => {
|
|
2441
|
-
const shareItem = getNormalizeShareItem(key);
|
|
2548
|
+
${getOrderedUsedShares(options).map((key) => {
|
|
2549
|
+
const shareItem = getNormalizeShareItem(key, resolvedOptions);
|
|
2442
2550
|
if (!shareItem) return null;
|
|
2443
2551
|
const detectedNamedExports = getSharedNamedExports(key, shareItem);
|
|
2444
2552
|
const canLiveRebind = shareItem.shareConfig.import === false || detectedNamedExports !== void 0;
|
|
2445
2553
|
const treeShakingConfig = canLiveRebind ? shareItem.shareConfig.treeShaking : void 0;
|
|
2446
|
-
const treeShakingUsage = treeShakingConfig ? getTreeShakingExportUsage(key, shareItem, shareItem.name) : void 0;
|
|
2554
|
+
const treeShakingUsage = treeShakingConfig ? getTreeShakingExportUsage(key, shareItem, shareItem.name, options) : void 0;
|
|
2447
2555
|
const treeShakingProviderExports = treeShakingUsage?.kind === "exports" ? treeShakingUsage.usedExports : [];
|
|
2448
|
-
const treeShakingUsedExports =
|
|
2449
|
-
const disableRuntimeInference = treeShakingConfig?.mode === "runtime-infer" &&
|
|
2450
|
-
const treeShakingProviderImportId = treeShakingConfig && !disableRuntimeInference && hasTreeShakingSharedProvider(key, shareItem) ? getTreeShakingSharedProviderImportId(key) : void 0;
|
|
2556
|
+
const treeShakingUsedExports = resolvedOptions.injectTreeShakingUsedExports === false ? treeShakingConfig?.usedExports || [] : treeShakingProviderExports;
|
|
2557
|
+
const disableRuntimeInference = treeShakingConfig?.mode === "runtime-infer" && resolvedOptions.injectTreeShakingUsedExports === false;
|
|
2558
|
+
const treeShakingProviderImportId = treeShakingConfig && !disableRuntimeInference && hasTreeShakingSharedProvider(key, shareItem, options) ? getTreeShakingSharedProviderImportId(key, options) : void 0;
|
|
2451
2559
|
const treeShakingStatus = treeShakingUsage?.kind === "full" || disableRuntimeInference || treeShakingConfig?.mode === "runtime-infer" && !treeShakingProviderImportId && shareItem.shareConfig.import !== false ? 0 : 1;
|
|
2452
2560
|
return `
|
|
2453
2561
|
${JSON.stringify(key)}: {
|
|
@@ -2456,7 +2564,7 @@ function generateLocalSharedImportMap() {
|
|
|
2456
2564
|
scope: [${JSON.stringify(shareItem.scope)}],
|
|
2457
2565
|
loaded: false,
|
|
2458
2566
|
eager: ${Boolean(shareItem.shareConfig.eager)},
|
|
2459
|
-
from: ${JSON.stringify(
|
|
2567
|
+
from: ${JSON.stringify(resolvedOptions.name)},
|
|
2460
2568
|
canLiveRebind: ${canLiveRebind},
|
|
2461
2569
|
async get () {
|
|
2462
2570
|
if (${shareItem.shareConfig.import === false}) {
|
|
@@ -2501,14 +2609,14 @@ function generateLocalSharedImportMap() {
|
|
|
2501
2609
|
`;
|
|
2502
2610
|
}).filter((x) => x !== null).join(",")}
|
|
2503
2611
|
}
|
|
2504
|
-
const usedRemotes = [${Object.keys(getUsedRemotesMap()).map((key) => {
|
|
2505
|
-
const remote =
|
|
2612
|
+
const usedRemotes = [${Object.keys(getUsedRemotesMap(options)).map((key) => {
|
|
2613
|
+
const remote = resolvedOptions.remotes[key];
|
|
2506
2614
|
if (!remote) return null;
|
|
2507
2615
|
return `
|
|
2508
2616
|
{
|
|
2509
|
-
alias: ${JSON.stringify(key)},
|
|
2617
|
+
alias: ${JSON.stringify(getRuntimeRemoteAlias(key, options))},
|
|
2510
2618
|
entryGlobalName: ${JSON.stringify(remote.entryGlobalName)},
|
|
2511
|
-
name: ${JSON.stringify(remote.name)},
|
|
2619
|
+
name: ${JSON.stringify(options ? getRuntimeRemoteAlias(key, options) : remote.name)},
|
|
2512
2620
|
type: ${JSON.stringify(remote.type)},
|
|
2513
2621
|
entry: ${JSON.stringify(remote.entry)},
|
|
2514
2622
|
shareScope: ${JSON.stringify(remote.shareScope ?? "default")},
|
|
@@ -2522,16 +2630,12 @@ function generateLocalSharedImportMap() {
|
|
|
2522
2630
|
}
|
|
2523
2631
|
`;
|
|
2524
2632
|
}
|
|
2525
|
-
function getOrderedUsedShares() {
|
|
2526
|
-
const
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
return;
|
|
2532
|
-
}
|
|
2533
|
-
});
|
|
2534
|
-
} catch {}
|
|
2633
|
+
function getOrderedUsedShares(options) {
|
|
2634
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2635
|
+
const shares = new Set(getUsedShares(options));
|
|
2636
|
+
Object.keys(resolvedOptions.shared).forEach((pkg) => {
|
|
2637
|
+
if (!pkg.endsWith("/")) shares.add(pkg);
|
|
2638
|
+
});
|
|
2535
2639
|
return orderSharedDependenciesFirst(Array.from(shares).sort((a, b) => {
|
|
2536
2640
|
const priority = (pkg) => pkg === "react" ? 0 : pkg === "react-dom" ? 1 : pkg.startsWith("react/") ? 2 : 3;
|
|
2537
2641
|
return priority(a) - priority(b) || a.localeCompare(b);
|
|
@@ -2585,15 +2689,15 @@ function orderSharedDependenciesFirst(sharedPackages) {
|
|
|
2585
2689
|
sharedPackages.forEach(visit);
|
|
2586
2690
|
return ordered;
|
|
2587
2691
|
}
|
|
2588
|
-
function getShareItemForPreload(pkg) {
|
|
2589
|
-
const shared =
|
|
2692
|
+
function getShareItemForPreload(pkg, options = getNormalizeModuleFederationOptions()) {
|
|
2693
|
+
const shared = options.shared;
|
|
2590
2694
|
const wildcardKey = `${pkg.startsWith("@") ? pkg.split("/").slice(0, 2).join("/") : pkg.split("/")[0]}/`;
|
|
2591
|
-
if (isExplicitSharedKey(pkg)) return shared[pkg];
|
|
2592
|
-
if (isExplicitSharedKey(wildcardKey)) return shared[wildcardKey];
|
|
2695
|
+
if (isExplicitSharedKey(pkg, options)) return shared[pkg];
|
|
2696
|
+
if (isExplicitSharedKey(wildcardKey, options)) return shared[wildcardKey];
|
|
2593
2697
|
}
|
|
2594
|
-
function generateSharedCacheSeedItem(pkg, shareItem, importPath) {
|
|
2698
|
+
function generateSharedCacheSeedItem(pkg, shareItem, importPath, options = getNormalizeModuleFederationOptions()) {
|
|
2595
2699
|
const cacheDescriptor = getSharedCacheDescriptor(pkg, shareItem);
|
|
2596
|
-
const cacheOwner =
|
|
2700
|
+
const cacheOwner = options.name;
|
|
2597
2701
|
return `if (__mfReadSharedCache(__mfModuleCache.share, ${JSON.stringify(cacheDescriptor)}) === undefined) {
|
|
2598
2702
|
const mod = await import(${JSON.stringify(importPath)});
|
|
2599
2703
|
${normalizeRuntimeShareCode}
|
|
@@ -2886,10 +2990,11 @@ function getBrowserImportPath(importPath) {
|
|
|
2886
2990
|
if (/^(?:[a-zA-Z]:[\\/]|\/)/.test(importPath) && !importPath.startsWith("/@")) return `/@fs/${importPath}`;
|
|
2887
2991
|
return importPath;
|
|
2888
2992
|
}
|
|
2889
|
-
function getHostAutoInitSharedSeedItems() {
|
|
2890
|
-
|
|
2993
|
+
function getHostAutoInitSharedSeedItems(options) {
|
|
2994
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2995
|
+
return getOrderedUsedShares(options).map((pkg) => ({
|
|
2891
2996
|
pkg,
|
|
2892
|
-
shareItem: getShareItemForPreload(pkg)
|
|
2997
|
+
shareItem: getShareItemForPreload(pkg, resolvedOptions)
|
|
2893
2998
|
})).filter(({ shareItem }) => shareItem?.shareConfig?.import === false).sort((a, b) => {
|
|
2894
2999
|
const priority = (pkg) => pkg === "vue" ? 0 : pkg === "pinia" ? 1 : 2;
|
|
2895
3000
|
const aIsLocal = !!getLocalProviderImportPath(a.pkg);
|
|
@@ -2897,11 +3002,12 @@ function getHostAutoInitSharedSeedItems() {
|
|
|
2897
3002
|
return priority(a.pkg) - priority(b.pkg) || Number(aIsLocal) - Number(bIsLocal) || a.pkg.localeCompare(b.pkg);
|
|
2898
3003
|
});
|
|
2899
3004
|
}
|
|
2900
|
-
function generateHostAutoInitSharedCacheSeedCode(command = "build") {
|
|
3005
|
+
function generateHostAutoInitSharedCacheSeedCode(command = "build", options) {
|
|
2901
3006
|
if (command === "build") return "";
|
|
2902
|
-
|
|
3007
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
3008
|
+
return getHostAutoInitSharedSeedItems(options).map(({ pkg, shareItem }) => {
|
|
2903
3009
|
if (!shareItem) return null;
|
|
2904
|
-
return generateSharedCacheSeedItem(pkg, shareItem, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
3010
|
+
return generateSharedCacheSeedItem(pkg, shareItem, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)), resolvedOptions);
|
|
2905
3011
|
}).filter((item) => item !== null).join("\n");
|
|
2906
3012
|
}
|
|
2907
3013
|
const REMOTE_ENTRY_ID = "virtual:mf-REMOTE_ENTRY_ID";
|
|
@@ -3126,10 +3232,10 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
3126
3232
|
globalThis.__VUE_HMR_RUNTIME__ = { createRecord() {}, rerender() {}, reload() {} };
|
|
3127
3233
|
}
|
|
3128
3234
|
import {${runtimeImports}} from "@module-federation/runtime";
|
|
3129
|
-
${hasEagerShared ? `import * as __mfLocalSharedImportMap from "${getLocalSharedImportMapPath()}";` : ""}
|
|
3235
|
+
${hasEagerShared ? `import * as __mfLocalSharedImportMap from "${getLocalSharedImportMapPath(options)}";` : ""}
|
|
3130
3236
|
${runtimeHelperImports.length ? `import {${runtimeHelperImports.join(", ")}} from "@module-federation/runtime/helpers";` : ""}
|
|
3131
3237
|
${pluginImportNames.filter((item) => !isSsrOnlyPlugin(item[1])).map((item) => item[1]).join("\n")}
|
|
3132
|
-
${command === "build" ? getRuntimeInitResolveBootstrapCode() : getRuntimeInitBootstrapCode() + "\n const { initResolve } = globalThis[globalKey];"}
|
|
3238
|
+
${command === "build" ? getRuntimeInitResolveBootstrapCode(false, options ? getRuntimeInitStatusImportId(options) : void 0) : getRuntimeInitBootstrapCode(false, options ? getRuntimeInitStatusImportId(options) : void 0) + "\n const { initResolve } = globalThis[globalKey];"}
|
|
3133
3239
|
${getRuntimeModuleCacheBootstrapCode()}
|
|
3134
3240
|
const initTokens = {}
|
|
3135
3241
|
const shareScopeNames = Array.isArray(${JSON.stringify(options.shareScope)}) ? ${JSON.stringify(options.shareScope)} : [${JSON.stringify(options.shareScope)}]
|
|
@@ -3163,7 +3269,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
3163
3269
|
async function getLocalSharedImportMap() {
|
|
3164
3270
|
${hasEagerShared ? "return __mfLocalSharedImportMap;" : ""}
|
|
3165
3271
|
${hasEagerShared ? "" : `if (!localSharedImportMapPromise) {
|
|
3166
|
-
localSharedImportMapPromise = retrySharedInit(() => import("${getLocalSharedImportMapPath()}"))
|
|
3272
|
+
localSharedImportMapPromise = retrySharedInit(() => import("${getLocalSharedImportMapPath(options)}"))
|
|
3167
3273
|
.catch((e) => { localSharedImportMapPromise = undefined; throw e; });
|
|
3168
3274
|
}
|
|
3169
3275
|
return localSharedImportMapPromise`}
|
|
@@ -3859,13 +3965,35 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
3859
3965
|
}
|
|
3860
3966
|
`;
|
|
3861
3967
|
}
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3968
|
+
/**
|
|
3969
|
+
* Inject entry file, automatically init when used as host,
|
|
3970
|
+
* and will not inject remoteEntry
|
|
3971
|
+
*/
|
|
3972
|
+
const HOST_AUTO_INIT_TAG = "__H_A_I__";
|
|
3973
|
+
const hostAutoInitStates = /* @__PURE__ */ new WeakMap();
|
|
3974
|
+
const legacyHostAutoInitState = {
|
|
3975
|
+
module: new VirtualModule("hostAutoInit", HOST_AUTO_INIT_TAG),
|
|
3976
|
+
remoteEntryId: REMOTE_ENTRY_ID,
|
|
3977
|
+
command: "build"
|
|
3978
|
+
};
|
|
3979
|
+
function getHostAutoInitState(options) {
|
|
3980
|
+
if (!options) return legacyHostAutoInitState;
|
|
3981
|
+
let state = hostAutoInitStates.get(options);
|
|
3982
|
+
if (!state) {
|
|
3983
|
+
state = {
|
|
3984
|
+
module: new VirtualModule("hostAutoInit", HOST_AUTO_INIT_TAG, "", getLocalOwnerKey(options)),
|
|
3985
|
+
remoteEntryId: REMOTE_ENTRY_ID,
|
|
3986
|
+
command: "build"
|
|
3987
|
+
};
|
|
3988
|
+
hostAutoInitStates.set(options, state);
|
|
3989
|
+
}
|
|
3990
|
+
return state;
|
|
3991
|
+
}
|
|
3992
|
+
function generateHostAutoInitCode(remoteEntryImport, _command = "build", options) {
|
|
3993
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
3994
|
+
const shouldPreloadShares = resolvedOptions.shareStrategy !== "loaded-first";
|
|
3995
|
+
const hostInitShareOrder = JSON.stringify(getOrderedUsedShares(options));
|
|
3996
|
+
const cacheOwner = JSON.stringify(resolvedOptions.name);
|
|
3869
3997
|
return `
|
|
3870
3998
|
${getRuntimeModuleCacheBootstrapCode()}
|
|
3871
3999
|
let hostInitPromise;
|
|
@@ -3873,10 +4001,10 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
3873
4001
|
if (!hostInitPromise) {
|
|
3874
4002
|
hostInitPromise = (async () => {
|
|
3875
4003
|
${sharedCacheHelperCode}
|
|
3876
|
-
${generateHostAutoInitSharedCacheSeedCode(_command)}
|
|
4004
|
+
${generateHostAutoInitSharedCacheSeedCode(_command, options)}
|
|
3877
4005
|
const remoteEntry = await import(${remoteEntryImport});
|
|
3878
4006
|
const runtime = await remoteEntry.init();
|
|
3879
|
-
const {usedShared} = await import("${getLocalSharedImportMapPath()}");
|
|
4007
|
+
const {usedShared} = await import("${getLocalSharedImportMapPath(options)}");
|
|
3880
4008
|
${normalizeRuntimeShareCode}
|
|
3881
4009
|
${shouldPreloadShares ? `
|
|
3882
4010
|
const __mfHostInitShareOrder = ${hostInitShareOrder}
|
|
@@ -3918,46 +4046,82 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
3918
4046
|
export { initHost, hostInitPromise };
|
|
3919
4047
|
`;
|
|
3920
4048
|
}
|
|
3921
|
-
function writeHostAutoInit(remoteEntryId = REMOTE_ENTRY_ID, command = "build") {
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
4049
|
+
function writeHostAutoInit(remoteEntryId = REMOTE_ENTRY_ID, command = "build", options) {
|
|
4050
|
+
const state = getHostAutoInitState(options);
|
|
4051
|
+
state.remoteEntryId = remoteEntryId;
|
|
4052
|
+
state.command = command;
|
|
4053
|
+
state.module.writeSync(generateHostAutoInitCode(JSON.stringify(remoteEntryId), command, options), true);
|
|
3925
4054
|
}
|
|
3926
|
-
function refreshHostAutoInit() {
|
|
4055
|
+
function refreshHostAutoInit(options) {
|
|
3927
4056
|
try {
|
|
3928
|
-
|
|
4057
|
+
const state = getHostAutoInitState(options);
|
|
4058
|
+
writeHostAutoInit(state.remoteEntryId, state.command, options);
|
|
3929
4059
|
} catch {}
|
|
3930
4060
|
}
|
|
3931
|
-
function getHostAutoInitImportId() {
|
|
3932
|
-
return
|
|
4061
|
+
function getHostAutoInitImportId(options) {
|
|
4062
|
+
return getHostAutoInitState(options).module.getImportId();
|
|
3933
4063
|
}
|
|
3934
|
-
function getHostAutoInitPath() {
|
|
3935
|
-
return
|
|
4064
|
+
function getHostAutoInitPath(options) {
|
|
4065
|
+
return getHostAutoInitState(options).module.getImportId();
|
|
3936
4066
|
}
|
|
3937
4067
|
//#endregion
|
|
3938
4068
|
//#region src/virtualModules/virtualRemotes.ts
|
|
3939
|
-
const cacheRemoteMap =
|
|
4069
|
+
const cacheRemoteMap = /* @__PURE__ */ new WeakMap();
|
|
4070
|
+
const remoteOptionsIds = /* @__PURE__ */ new WeakMap();
|
|
4071
|
+
let nextRemoteOptionsId = 1;
|
|
4072
|
+
function getRemoteOptionsId(options) {
|
|
4073
|
+
let id = remoteOptionsIds.get(options);
|
|
4074
|
+
if (id === void 0) {
|
|
4075
|
+
id = nextRemoteOptionsId++;
|
|
4076
|
+
remoteOptionsIds.set(options, id);
|
|
4077
|
+
}
|
|
4078
|
+
return id;
|
|
4079
|
+
}
|
|
3940
4080
|
const LOAD_REMOTE_TAG = "__loadRemote__";
|
|
3941
|
-
function getRemoteVirtualModule(remote, command, enableSsrInit = false, consumer = "unified") {
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
cacheRemoteMap
|
|
3946
|
-
cacheRemoteMap[cacheKey].writeSync(generateRemotes(remote, command, enableSsrInit, consumer));
|
|
4081
|
+
function getRemoteVirtualModule(remote, command, enableSsrInit = false, consumer = "unified", options = getNormalizeModuleFederationOptions()) {
|
|
4082
|
+
let instanceCache = cacheRemoteMap.get(options);
|
|
4083
|
+
if (!instanceCache) {
|
|
4084
|
+
instanceCache = /* @__PURE__ */ new Map();
|
|
4085
|
+
cacheRemoteMap.set(options, instanceCache);
|
|
3947
4086
|
}
|
|
3948
|
-
|
|
4087
|
+
const cacheKey = `${remote}__${command}__${options.shareStrategy}__${consumer}__${enableSsrInit ? "ssr-init" : "no-ssr-init"}`;
|
|
4088
|
+
if (!instanceCache.has(cacheKey)) {
|
|
4089
|
+
const virtual = new VirtualModule(`${consumer === "unified" ? remote : `${remote}__mf_consumer__${consumer}`}__mf_owner__${getRemoteOptionsId(options)}`, LOAD_REMOTE_TAG, ".js", options.internalName);
|
|
4090
|
+
virtual.writeSync(generateRemotes(remote, command, enableSsrInit, consumer, options));
|
|
4091
|
+
instanceCache.set(cacheKey, virtual);
|
|
4092
|
+
}
|
|
4093
|
+
return instanceCache.get(cacheKey);
|
|
3949
4094
|
}
|
|
3950
4095
|
const usedRemotesMap = {};
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
4096
|
+
const usedRemotesByOptions = /* @__PURE__ */ new WeakMap();
|
|
4097
|
+
function getScopedUsedRemotesMap(options) {
|
|
4098
|
+
let scoped = usedRemotesByOptions.get(options);
|
|
4099
|
+
if (!scoped) {
|
|
4100
|
+
scoped = {};
|
|
4101
|
+
usedRemotesByOptions.set(options, scoped);
|
|
4102
|
+
}
|
|
4103
|
+
return scoped;
|
|
4104
|
+
}
|
|
4105
|
+
function recordUsedRemote(map, remoteKey, remoteModule) {
|
|
4106
|
+
if (!map[remoteKey]) map[remoteKey] = /* @__PURE__ */ new Set();
|
|
4107
|
+
map[remoteKey].add(remoteModule);
|
|
3954
4108
|
}
|
|
3955
|
-
function
|
|
4109
|
+
function addUsedRemote(remoteKey, remoteModule, options) {
|
|
4110
|
+
recordUsedRemote(usedRemotesMap, remoteKey, remoteModule);
|
|
4111
|
+
if (options) recordUsedRemote(getScopedUsedRemotesMap(options), remoteKey, remoteModule);
|
|
4112
|
+
}
|
|
4113
|
+
function getUsedRemotesMap(options) {
|
|
4114
|
+
if (options) return getScopedUsedRemotesMap(options);
|
|
3956
4115
|
return usedRemotesMap;
|
|
3957
4116
|
}
|
|
3958
4117
|
function getRemoteAliasFromId(id, remotes) {
|
|
3959
4118
|
return Object.keys(remotes).filter((name) => id === name || id.startsWith(name + "/")).sort((a, b) => b.length - a.length)[0];
|
|
3960
4119
|
}
|
|
4120
|
+
function getRuntimeRemoteId(id, remotes, options) {
|
|
4121
|
+
const alias = getRemoteAliasFromId(id, remotes);
|
|
4122
|
+
if (!alias) return id;
|
|
4123
|
+
return `${getRuntimeRemoteAlias(alias, options)}${id.slice(alias.length)}`;
|
|
4124
|
+
}
|
|
3961
4125
|
function resolveRemoteInitMode(shareStrategy, consumer) {
|
|
3962
4126
|
if (shareStrategy !== "loaded-first") return "eager";
|
|
3963
4127
|
if (consumer === "server") return "loaded-first-ssr";
|
|
@@ -4016,7 +4180,7 @@ function getRemoteModuleRuntimeHelpers() {
|
|
|
4016
4180
|
return exportModule;
|
|
4017
4181
|
}`;
|
|
4018
4182
|
}
|
|
4019
|
-
function getDeferredProxyHelper(
|
|
4183
|
+
function getDeferredProxyHelper(remoteCacheKey) {
|
|
4020
4184
|
return `
|
|
4021
4185
|
function __mfCreateDeferredRemoteProxy() {
|
|
4022
4186
|
let pendingPromise;
|
|
@@ -4024,7 +4188,7 @@ function getDeferredProxyHelper(remoteId) {
|
|
|
4024
4188
|
pendingPromise ||= __mfStartRemoteLoad();
|
|
4025
4189
|
return pendingPromise;
|
|
4026
4190
|
};
|
|
4027
|
-
const getModule = () => __mfModuleCache.remote[${JSON.stringify(
|
|
4191
|
+
const getModule = () => __mfModuleCache.remote[${JSON.stringify(remoteCacheKey)}];
|
|
4028
4192
|
const proxyTarget = function (...args) {
|
|
4029
4193
|
pendingPromise ||= __mfStartRemoteLoad();
|
|
4030
4194
|
const mod = getModule();
|
|
@@ -4122,30 +4286,39 @@ ${deferRemoteLoad ? getLazyRemotePendingExport() : getEagerRemotePendingExport()
|
|
|
4122
4286
|
${command === "serve" && consumer === "server" ? getServerThenExport() : ""}
|
|
4123
4287
|
export { __mfDefaultExport as default };`;
|
|
4124
4288
|
}
|
|
4125
|
-
function generateRemotes(id, command, enableSsrInit = false, consumer = "unified") {
|
|
4126
|
-
const
|
|
4127
|
-
const isLoadedFirst =
|
|
4128
|
-
const initMode = resolveRemoteInitMode(
|
|
4289
|
+
function generateRemotes(id, command, enableSsrInit = false, consumer = "unified", options) {
|
|
4290
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
4291
|
+
const isLoadedFirst = resolvedOptions.shareStrategy === "loaded-first";
|
|
4292
|
+
const initMode = resolveRemoteInitMode(resolvedOptions.shareStrategy, consumer);
|
|
4129
4293
|
const deferRemoteLoad = shouldDeferRemoteLoad(initMode);
|
|
4130
|
-
const remoteAlias = getRemoteAliasFromId(id,
|
|
4131
|
-
const remote = remoteAlias ?
|
|
4294
|
+
const remoteAlias = getRemoteAliasFromId(id, resolvedOptions.remotes);
|
|
4295
|
+
const remote = remoteAlias ? resolvedOptions.remotes[remoteAlias] : void 0;
|
|
4296
|
+
const runtimeRemoteAlias = remoteAlias ? getRuntimeRemoteAlias(remoteAlias, options) : void 0;
|
|
4297
|
+
const runtimeRemoteId = getRuntimeRemoteId(id, resolvedOptions.remotes, options);
|
|
4132
4298
|
const registerRemoteCode = isLoadedFirst && remote ? `runtime.registerRemotes([${JSON.stringify({
|
|
4133
4299
|
entryGlobalName: remote.entryGlobalName,
|
|
4134
|
-
name: remote.name,
|
|
4135
|
-
alias:
|
|
4300
|
+
name: options ? runtimeRemoteAlias : remote.name,
|
|
4301
|
+
alias: runtimeRemoteAlias,
|
|
4136
4302
|
type: remote.type,
|
|
4137
4303
|
entry: remote.entry,
|
|
4138
4304
|
shareScope: remote.shareScope ?? "default"
|
|
4139
4305
|
})}]);` : "";
|
|
4140
|
-
const
|
|
4306
|
+
const hostAutoInitPath = getHostAutoInitPath(options);
|
|
4307
|
+
const ssrRemotes = Object.entries(resolvedOptions.remotes).map(([name, item]) => ({
|
|
4308
|
+
name: getRuntimeRemoteAlias(name, options),
|
|
4309
|
+
entry: item.entry,
|
|
4310
|
+
type: item.type ?? "module"
|
|
4311
|
+
}));
|
|
4312
|
+
const browserHostInitCode = `import(${JSON.stringify(hostAutoInitPath)})
|
|
4141
4313
|
.then((mod) => mod.hostInitPromise)
|
|
4142
4314
|
.then(initResolve, initReject);`;
|
|
4143
|
-
const devRuntimeBootstrap = `${getRuntimeInitBootstrapCode(enableSsrInit,
|
|
4315
|
+
const devRuntimeBootstrap = `${getRuntimeInitBootstrapCode(enableSsrInit, getRuntimeInitStatusImportId(options), ssrRemotes, hostAutoInitPath)}
|
|
4144
4316
|
const { initPromise, initResolve, initReject, moduleCache: __mfModuleCache } = globalThis[globalKey];`;
|
|
4145
4317
|
const importLine = command === "build" ? `${getRuntimeModuleCacheBootstrapCode()}
|
|
4146
|
-
import { hostInitPromise as __mfHostInitPromise } from ${JSON.stringify(
|
|
4318
|
+
import { hostInitPromise as __mfHostInitPromise } from ${JSON.stringify(hostAutoInitPath)};` : `${devRuntimeBootstrap}
|
|
4147
4319
|
${command === "serve" && consumer !== "server" ? browserHostInitCode : ""}`;
|
|
4148
4320
|
const remoteLoadRuntimePromise = command === "build" ? "__mfHostInitPromise" : "initPromise";
|
|
4321
|
+
const remoteCacheKey = `${getRuntimeRemoteCachePrefix(options)}${id}`;
|
|
4149
4322
|
const remoteLoadFailureHandler = command === "build" ? `.catch((error) => {
|
|
4150
4323
|
delete __mfModuleCache.remote[pendingKey];
|
|
4151
4324
|
throw error;
|
|
@@ -4156,16 +4329,17 @@ function generateRemotes(id, command, enableSsrInit = false, consumer = "unified
|
|
|
4156
4329
|
const remoteLoadCode = `
|
|
4157
4330
|
function __mfStartRemoteLoad() {
|
|
4158
4331
|
${`
|
|
4159
|
-
const
|
|
4332
|
+
const remoteCacheKey = ${JSON.stringify(remoteCacheKey)};
|
|
4333
|
+
const pendingKey = "__mf_pending__" + remoteCacheKey;
|
|
4160
4334
|
if (!__mfModuleCache.remote[pendingKey]) {
|
|
4161
4335
|
__mfModuleCache.remote[pendingKey] = ${remoteLoadRuntimePromise}
|
|
4162
4336
|
.then((runtime) => {
|
|
4163
4337
|
${registerRemoteCode}
|
|
4164
|
-
return runtime.loadRemote(${JSON.stringify(
|
|
4338
|
+
return runtime.loadRemote(${JSON.stringify(runtimeRemoteId)});
|
|
4165
4339
|
})
|
|
4166
4340
|
.then((mod) => Promise.resolve(mod?.__mf_remote_dependency_pending).then(() => mod))
|
|
4167
4341
|
.then((mod) => {
|
|
4168
|
-
__mfModuleCache.remote[
|
|
4342
|
+
__mfModuleCache.remote[remoteCacheKey] = mod;
|
|
4169
4343
|
delete __mfModuleCache.remote[pendingKey];
|
|
4170
4344
|
return mod;
|
|
4171
4345
|
})
|
|
@@ -4185,14 +4359,14 @@ function generateRemotes(id, command, enableSsrInit = false, consumer = "unified
|
|
|
4185
4359
|
}`;
|
|
4186
4360
|
const initExportModule = initMode === "eager" ? environmentSplitInit(eagerClientInit, realRemoteInit) : environmentSplitInit(loadedFirstClientInit, realRemoteInit);
|
|
4187
4361
|
const includeProxyHelper = shouldIncludeDeferredProxy(initMode, consumer, eagerLoadClientRemote, deferRemoteLoad);
|
|
4188
|
-
const deferredProxyCode = getDeferredProxyHelper(
|
|
4362
|
+
const deferredProxyCode = getDeferredProxyHelper(remoteCacheKey);
|
|
4189
4363
|
return `
|
|
4190
4364
|
${importLine}
|
|
4191
4365
|
${remoteLoadCode}
|
|
4192
4366
|
${includeProxyHelper ? deferredProxyCode : ""}
|
|
4193
4367
|
${getRemoteModuleRuntimeHelpers()}
|
|
4194
4368
|
let __mfRemotePending;
|
|
4195
|
-
let exportModule = __mfModuleCache.remote[${JSON.stringify(
|
|
4369
|
+
let exportModule = __mfModuleCache.remote[${JSON.stringify(remoteCacheKey)}]
|
|
4196
4370
|
if (exportModule === undefined) {
|
|
4197
4371
|
${initExportModule}
|
|
4198
4372
|
}
|
|
@@ -4271,7 +4445,7 @@ function patchHashEntryFileNames(config, entryName, fileName) {
|
|
|
4271
4445
|
patchBundlerOutput(config.build.rollupOptions);
|
|
4272
4446
|
patchBundlerOutput(config.build.rolldownOptions);
|
|
4273
4447
|
}
|
|
4274
|
-
const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClientInjected, skipTransformFor = [] }) => {
|
|
4448
|
+
const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClientInjected, skipTransformFor = [], federationOptions }) => {
|
|
4275
4449
|
const DEV_HTML_PROXY_PREFIX = "virtual:mf-html-entry-proxy?";
|
|
4276
4450
|
const ENTRY_BOOTSTRAP_PARAM = "mf-entry-bootstrap";
|
|
4277
4451
|
const ENTRY_BOOTSTRAP_QUERY = `?${ENTRY_BOOTSTRAP_PARAM}`;
|
|
@@ -4355,15 +4529,17 @@ const __mfCurrentScript = document.currentScript;
|
|
|
4355
4529
|
: import(src);
|
|
4356
4530
|
` : "";
|
|
4357
4531
|
const importExpression = (src) => useSystemImportFallback ? `__mfImport(${JSON.stringify(src)})` : `import(${JSON.stringify(src)})`;
|
|
4358
|
-
const remotePreloads = !options?.skipRemotePreload && getNormalizeModuleFederationOptions()?.shareStrategy !== "loaded-first" ? Object.entries(getUsedRemotesMap()).flatMap(([remoteKey, remotes]) => Array.from(remotes).filter((remote) => remote !== remoteKey)).sort().map((remote) => `__mfPreloadRemote(${JSON.stringify(remote)})`).join(",") : "";
|
|
4532
|
+
const remotePreloads = !options?.skipRemotePreload && (federationOptions ?? getNormalizeModuleFederationOptions())?.shareStrategy !== "loaded-first" ? Object.entries(getUsedRemotesMap(federationOptions)).flatMap(([remoteKey, remotes]) => Array.from(remotes).filter((remote) => remote !== remoteKey)).sort().map((remote) => `__mfPreloadRemote(${JSON.stringify(getRuntimeRemoteId(remote, (federationOptions ?? getNormalizeModuleFederationOptions()).remotes, federationOptions))}, ${JSON.stringify(remote)})`).join(",") : "";
|
|
4533
|
+
const remoteCachePrefix = getRuntimeRemoteCachePrefix(federationOptions);
|
|
4359
4534
|
const preloadBlock = remotePreloads ? `
|
|
4360
4535
|
const runtime = await initHost();
|
|
4361
|
-
const __mfPreloadRemote = (remote) => {
|
|
4362
|
-
const
|
|
4536
|
+
const __mfPreloadRemote = (runtimeRemote, remote) => {
|
|
4537
|
+
const remoteCacheKey = ${JSON.stringify(remoteCachePrefix)} + remote;
|
|
4538
|
+
const pendingKey = "__mf_pending__" + remoteCacheKey;
|
|
4363
4539
|
if (!__mfModuleCache.remote[pendingKey]) {
|
|
4364
|
-
__mfModuleCache.remote[pendingKey] = runtime.loadRemote(
|
|
4540
|
+
__mfModuleCache.remote[pendingKey] = runtime.loadRemote(runtimeRemote)
|
|
4365
4541
|
.then((mod) => {
|
|
4366
|
-
__mfModuleCache.remote[
|
|
4542
|
+
__mfModuleCache.remote[remoteCacheKey] = mod;
|
|
4367
4543
|
delete __mfModuleCache.remote[pendingKey];
|
|
4368
4544
|
return mod;
|
|
4369
4545
|
})
|
|
@@ -4610,7 +4786,7 @@ const __mfCurrentScript = document.currentScript;
|
|
|
4610
4786
|
htmlContent = htmlContent.replace("<head>", `<head>${scriptContent}`);
|
|
4611
4787
|
}
|
|
4612
4788
|
}
|
|
4613
|
-
if (waitsForInit) htmlContent = injectHostInitPreloads(htmlContent, bundle, (builtFileName) => resolvePath(builtFileName, fileName));
|
|
4789
|
+
if (waitsForInit && viteConfig.build.modulePreload !== false) htmlContent = injectHostInitPreloads(htmlContent, bundle, (builtFileName) => resolvePath(builtFileName, fileName));
|
|
4614
4790
|
htmlAsset.source = htmlContent;
|
|
4615
4791
|
}
|
|
4616
4792
|
},
|
|
@@ -5186,10 +5362,14 @@ function pluginDevRemoteHmr(options) {
|
|
|
5186
5362
|
}
|
|
5187
5363
|
//#endregion
|
|
5188
5364
|
//#region src/virtualModules/index.ts
|
|
5189
|
-
function initVirtualModules(command, remoteEntryId, enableSsrInit = false) {
|
|
5190
|
-
writeLocalSharedImportMap();
|
|
5191
|
-
writeHostAutoInit(remoteEntryId, command);
|
|
5192
|
-
writeRuntimeInitStatus(command, enableSsrInit, getHostAutoInitPath())
|
|
5365
|
+
function initVirtualModules(command, remoteEntryId, enableSsrInit = false, options) {
|
|
5366
|
+
writeLocalSharedImportMap(options);
|
|
5367
|
+
writeHostAutoInit(remoteEntryId, command, options);
|
|
5368
|
+
writeRuntimeInitStatus(command, enableSsrInit, getHostAutoInitPath(options), options, options ? Object.entries(options.remotes).map(([name, item]) => ({
|
|
5369
|
+
name,
|
|
5370
|
+
entry: item.entry,
|
|
5371
|
+
type: item.type ?? "module"
|
|
5372
|
+
})) : void 0);
|
|
5193
5373
|
}
|
|
5194
5374
|
//#endregion
|
|
5195
5375
|
//#region src/utils/bundleHelpers.ts
|
|
@@ -5524,9 +5704,9 @@ const deduplicateAssets = (filesMap) => {
|
|
|
5524
5704
|
* @param resolveFn - Function to resolve module paths
|
|
5525
5705
|
* @returns Map of file paths to their corresponding share keys
|
|
5526
5706
|
*/
|
|
5527
|
-
const buildFileToShareKeyMap = async (shareKeys, resolveFn) => {
|
|
5707
|
+
const buildFileToShareKeyMap = async (shareKeys, resolveFn, options) => {
|
|
5528
5708
|
const fileToShareKey = /* @__PURE__ */ new Map();
|
|
5529
|
-
const resolutions = await Promise.all(Array.from(shareKeys).map((shareKey) => resolveFn(getPreBuildLibImportId(shareKey)).then((resolution) => ({
|
|
5709
|
+
const resolutions = await Promise.all(Array.from(shareKeys).map((shareKey) => resolveFn(getPreBuildLibImportId(shareKey, options)).then((resolution) => ({
|
|
5530
5710
|
shareKey,
|
|
5531
5711
|
file: resolution?.id?.split("?")[0]
|
|
5532
5712
|
})).catch(() => null)));
|
|
@@ -5732,8 +5912,8 @@ function getRemoteContainerName(remoteKey, remote) {
|
|
|
5732
5912
|
if (entryGlobalName && entryGlobalName !== remoteKey && entryGlobalName !== remote.entry) return entryGlobalName;
|
|
5733
5913
|
return remote.name;
|
|
5734
5914
|
}
|
|
5735
|
-
const Manifest = () => {
|
|
5736
|
-
const mfOptions = getNormalizeModuleFederationOptions();
|
|
5915
|
+
const Manifest = (providedOptions) => {
|
|
5916
|
+
const mfOptions = providedOptions ?? getNormalizeModuleFederationOptions();
|
|
5737
5917
|
const { name, filename, getPublicPath, manifest: manifestOptions, varFilename } = mfOptions;
|
|
5738
5918
|
let mfManifestName = manifestOptions === true ? "mf-manifest.json" : typeof manifestOptions === "object" ? normalizePathForImport(path$1.join(manifestOptions?.filePath || "", manifestOptions?.fileName || "mf-manifest.json")) : void 0;
|
|
5739
5919
|
let mfManifestStatsName = mfManifestName ? getStatsFileName(mfManifestName) : void 0;
|
|
@@ -5860,7 +6040,7 @@ const Manifest = () => {
|
|
|
5860
6040
|
root,
|
|
5861
6041
|
stripKnownJsExtensions: true
|
|
5862
6042
|
});
|
|
5863
|
-
const fileToShareKey = await buildFileToShareKeyMap(getUsedShares(), this.resolve.bind(this));
|
|
6043
|
+
const fileToShareKey = await buildFileToShareKeyMap(getUsedShares(mfOptions), this.resolve.bind(this), mfOptions);
|
|
5864
6044
|
processModuleAssets(Object.fromEntries(Object.entries(bundle).filter(([, file]) => !isTreeShakingProviderChunk(file))), filesMap, (modulePath) => fileToShareKey.get(modulePath));
|
|
5865
6045
|
if (mfOptions.bundleAllCSS) addCssAssetsToAllExports(filesMap, allCssAssets);
|
|
5866
6046
|
filesMap = deduplicateAssets(filesMap);
|
|
@@ -5887,7 +6067,7 @@ const Manifest = () => {
|
|
|
5887
6067
|
* @returns Complete manifest object
|
|
5888
6068
|
*/
|
|
5889
6069
|
function generateMFManifest(preloadMap, disableAssetsAnalyze = false) {
|
|
5890
|
-
const options =
|
|
6070
|
+
const options = mfOptions;
|
|
5891
6071
|
const { name, varFilename } = options;
|
|
5892
6072
|
const resolvedRemoteEntryFile = _command === "serve" ? remoteEntryFile || resolveDevRemoteEntryFileName(filename) : remoteEntryFile;
|
|
5893
6073
|
const remoteEntry = {
|
|
@@ -5905,7 +6085,7 @@ const Manifest = () => {
|
|
|
5905
6085
|
path: "",
|
|
5906
6086
|
type: "var"
|
|
5907
6087
|
} : void 0;
|
|
5908
|
-
const remotes = Array.from(Object.entries(getUsedRemotesMap())).flatMap(([remoteKey, modules]) => {
|
|
6088
|
+
const remotes = Array.from(Object.entries(getUsedRemotesMap(options))).flatMap(([remoteKey, modules]) => {
|
|
5909
6089
|
const remote = options.remotes[remoteKey];
|
|
5910
6090
|
return Array.from(modules).map((moduleKey) => ({
|
|
5911
6091
|
federationContainerName: getRemoteContainerName(remoteKey, remote),
|
|
@@ -5914,11 +6094,11 @@ const Manifest = () => {
|
|
|
5914
6094
|
entry: "*"
|
|
5915
6095
|
}));
|
|
5916
6096
|
});
|
|
5917
|
-
const shared = Array.from(getUsedShares()).flatMap((shareKey) => {
|
|
5918
|
-
const shareItem = getNormalizeShareItem(shareKey);
|
|
6097
|
+
const shared = Array.from(getUsedShares(options)).flatMap((shareKey) => {
|
|
6098
|
+
const shareItem = getNormalizeShareItem(shareKey, options);
|
|
5919
6099
|
if (!shareItem) return [];
|
|
5920
6100
|
const assets = preloadMap[shareKey] || (_command === "serve" && resolvedRemoteEntryFile ? createRemoteEntryAssetMap(resolvedRemoteEntryFile) : createEmptyAssetMap());
|
|
5921
|
-
const treeShakingUsage = getTreeShakingExportUsage(shareKey, shareItem, shareItem.name);
|
|
6101
|
+
const treeShakingUsage = getTreeShakingExportUsage(shareKey, shareItem, shareItem.name, options);
|
|
5922
6102
|
const treeShakingUsedExports = treeShakingUsage?.kind === "exports" ? treeShakingUsage.usedExports : [];
|
|
5923
6103
|
const treeShakingStatus = treeShakingUsage?.kind === "full" ? 0 : 1;
|
|
5924
6104
|
return [{
|
|
@@ -6150,6 +6330,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6150
6330
|
let exposeRemoteDependenciesDirty = true;
|
|
6151
6331
|
let refreshPromise;
|
|
6152
6332
|
let dependencyInvalidationVersion = 0;
|
|
6333
|
+
const isHostAutoInitId = (id) => id.includes(getHostAutoInitPath(options)) || id.includes(getHostAutoInitPath());
|
|
6153
6334
|
function isRemoteImport(source) {
|
|
6154
6335
|
return Object.keys(options.remotes).some((name) => source === name || source.startsWith(name + "/"));
|
|
6155
6336
|
}
|
|
@@ -6239,7 +6420,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6239
6420
|
async resolveId(id, importer) {
|
|
6240
6421
|
if (id === remoteEntryId) return remoteEntryId;
|
|
6241
6422
|
if (id === virtualExposesId) return virtualExposesId;
|
|
6242
|
-
if (_command === "serve" && id
|
|
6423
|
+
if (_command === "serve" && isHostAutoInitId(id)) return id;
|
|
6243
6424
|
if (importer === remoteEntryId && !id.startsWith(".") && !id.startsWith("/") && !id.startsWith("\0") && !id.startsWith("virtual:")) {
|
|
6244
6425
|
const importPath = typeof __filename === "string" ? __filename : fileURLToPath(import.meta.url);
|
|
6245
6426
|
const resolved = await this.resolve(id, importPath, { skipSelf: true });
|
|
@@ -6252,7 +6433,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6252
6433
|
await refreshExposeRemoteDependencies(this);
|
|
6253
6434
|
return generateExposes(options, exposeRemoteDependencies, _command);
|
|
6254
6435
|
}
|
|
6255
|
-
if (_command === "serve" && id
|
|
6436
|
+
if (_command === "serve" && isHostAutoInitId(id)) return id;
|
|
6256
6437
|
},
|
|
6257
6438
|
async transform(code, id) {
|
|
6258
6439
|
return mapCodeToCodeWithSourcemap(await (async () => {
|
|
@@ -6262,7 +6443,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6262
6443
|
await refreshExposeRemoteDependencies(this);
|
|
6263
6444
|
return generateExposes(options, exposeRemoteDependencies, _command);
|
|
6264
6445
|
}
|
|
6265
|
-
if (id
|
|
6446
|
+
if (isHostAutoInitId(id)) {
|
|
6266
6447
|
if (_command === "serve") {
|
|
6267
6448
|
const host = typeof viteConfig.server?.host === "string" && viteConfig.server.host !== "0.0.0.0" ? viteConfig.server.host : "localhost";
|
|
6268
6449
|
const resolvedPublicPath = resolvePublicPath(options, viteConfig.base);
|
|
@@ -6272,7 +6453,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6272
6453
|
return `
|
|
6273
6454
|
const origin = typeof window !== 'undefined' && (${!options.ignoreOrigin}) ? window.origin : ${JSON.stringify(fallbackOrigin)};
|
|
6274
6455
|
const remoteEntryImport = typeof window !== 'undefined' ? origin + ${publicPath} : ${JSON.stringify(ssrRemoteEntry)};
|
|
6275
|
-
${generateHostAutoInitCode("remoteEntryImport", "serve")}
|
|
6456
|
+
${generateHostAutoInitCode("remoteEntryImport", "serve", options)}
|
|
6276
6457
|
`;
|
|
6277
6458
|
}
|
|
6278
6459
|
return code;
|
|
@@ -6367,9 +6548,9 @@ function pluginProxyRemotes_default(options) {
|
|
|
6367
6548
|
if (installedPackageEntry && (importer === void 0 || isNodeModulesImporter(importer))) return installedPackageEntry;
|
|
6368
6549
|
}
|
|
6369
6550
|
const consumer = resolveRemoteConsumer(pluginContext, hasMultiEnvironment);
|
|
6370
|
-
const remoteModule = getRemoteVirtualModule(source, command, enableSsrInit, consumer);
|
|
6371
|
-
addUsedRemote(remoteName, source);
|
|
6372
|
-
refreshHostAutoInit();
|
|
6551
|
+
const remoteModule = getRemoteVirtualModule(source, command, enableSsrInit, consumer, options);
|
|
6552
|
+
addUsedRemote(remoteName, source, options);
|
|
6553
|
+
refreshHostAutoInit(options);
|
|
6373
6554
|
return remoteModule.getImportId();
|
|
6374
6555
|
}
|
|
6375
6556
|
return {
|
|
@@ -6546,7 +6727,7 @@ function excludeSharedSubDependencies(shared) {
|
|
|
6546
6727
|
}
|
|
6547
6728
|
}
|
|
6548
6729
|
function proxySharedModule(options) {
|
|
6549
|
-
const { shared = {} } = options;
|
|
6730
|
+
const { shared = {}, federationOptions } = options;
|
|
6550
6731
|
let _config;
|
|
6551
6732
|
let _command = "serve";
|
|
6552
6733
|
let useDirectReactImport = false;
|
|
@@ -6566,20 +6747,20 @@ function proxySharedModule(options) {
|
|
|
6566
6747
|
};
|
|
6567
6748
|
const getTreeShakingProviderFileName = (pkg, shareItem) => {
|
|
6568
6749
|
if (!shareItem.shareConfig.treeShaking) return void 0;
|
|
6569
|
-
const normalizedOptions = getNormalizeModuleFederationOptions();
|
|
6750
|
+
const normalizedOptions = federationOptions ?? getNormalizeModuleFederationOptions();
|
|
6570
6751
|
const outputDir = normalizedOptions.treeShakingDir ? normalizeTreeShakingOutputPath(normalizedOptions.treeShakingDir) : void 0;
|
|
6571
|
-
const fileName = outputDir ? path$1.posix.join(outputDir, `${getTreeShakingSharedProviderName(pkg)}.js`) : void 0;
|
|
6752
|
+
const fileName = outputDir ? path$1.posix.join(outputDir, `${getTreeShakingSharedProviderName(pkg, federationOptions)}.js`) : void 0;
|
|
6572
6753
|
if (!fileName) return void 0;
|
|
6573
6754
|
return fileName;
|
|
6574
6755
|
};
|
|
6575
6756
|
const emitTreeShakingProvider = (context, pkg, shareItem) => {
|
|
6576
6757
|
if (_command !== "build" || emittedTreeShakingProviders.has(pkg)) return;
|
|
6577
|
-
if (!hasTreeShakingSharedProvider(pkg, shareItem)) return;
|
|
6758
|
+
if (!hasTreeShakingSharedProvider(pkg, shareItem, federationOptions)) return;
|
|
6578
6759
|
const fileName = getTreeShakingProviderFileName(pkg, shareItem);
|
|
6579
6760
|
context.emitFile({
|
|
6580
6761
|
type: "chunk",
|
|
6581
|
-
id: getTreeShakingSharedProviderImportId(pkg),
|
|
6582
|
-
name: getTreeShakingSharedProviderName(pkg),
|
|
6762
|
+
id: getTreeShakingSharedProviderImportId(pkg, federationOptions),
|
|
6763
|
+
name: getTreeShakingSharedProviderName(pkg, federationOptions),
|
|
6583
6764
|
...fileName ? { fileName } : {}
|
|
6584
6765
|
});
|
|
6585
6766
|
emittedTreeShakingProviders.add(pkg);
|
|
@@ -6591,28 +6772,28 @@ function proxySharedModule(options) {
|
|
|
6591
6772
|
configureServer(server) {
|
|
6592
6773
|
devServer = server;
|
|
6593
6774
|
setLocalSharedImportMapInvalidator(() => {
|
|
6594
|
-
const module = server.moduleGraph.getModuleById(getResolvedLocalSharedImportMapId());
|
|
6775
|
+
const module = server.moduleGraph.getModuleById(getResolvedLocalSharedImportMapId(federationOptions));
|
|
6595
6776
|
if (module) server.moduleGraph.invalidateModule(module);
|
|
6596
|
-
});
|
|
6777
|
+
}, federationOptions);
|
|
6597
6778
|
},
|
|
6598
6779
|
resolveId(source) {
|
|
6599
|
-
if (source === getLocalSharedImportMapPath()) return getResolvedLocalSharedImportMapId();
|
|
6780
|
+
if (source === getLocalSharedImportMapPath(federationOptions)) return getResolvedLocalSharedImportMapId(federationOptions);
|
|
6600
6781
|
},
|
|
6601
6782
|
load(id) {
|
|
6602
|
-
if (id === getResolvedLocalSharedImportMapId()) return parsePromise.then((_) => {
|
|
6603
|
-
refreshTreeShakingModules();
|
|
6604
|
-
const providerPackages = new Set([...Object.keys(shared).filter((pkg) => !pkg.endsWith("/")), ...getUsedShares()]);
|
|
6783
|
+
if (id === getResolvedLocalSharedImportMapId(federationOptions)) return parsePromise.then((_) => {
|
|
6784
|
+
refreshTreeShakingModules(federationOptions);
|
|
6785
|
+
const providerPackages = new Set([...Object.keys(shared).filter((pkg) => !pkg.endsWith("/")), ...getUsedShares(federationOptions)]);
|
|
6605
6786
|
for (const pkg of providerPackages) {
|
|
6606
6787
|
const sharedKey = findSharedKeyForSource(pkg, shared);
|
|
6607
6788
|
const shareItem = shared[pkg] || (sharedKey ? shared[sharedKey] : void 0);
|
|
6608
6789
|
if (shareItem) emitTreeShakingProvider(this, pkg, shareItem);
|
|
6609
6790
|
}
|
|
6610
|
-
return generateLocalSharedImportMap();
|
|
6791
|
+
return generateLocalSharedImportMap(federationOptions);
|
|
6611
6792
|
});
|
|
6612
6793
|
},
|
|
6613
6794
|
closeBundle() {
|
|
6614
6795
|
if (devServer) return;
|
|
6615
|
-
setLocalSharedImportMapInvalidator(void 0);
|
|
6796
|
+
setLocalSharedImportMapInvalidator(void 0, federationOptions);
|
|
6616
6797
|
}
|
|
6617
6798
|
},
|
|
6618
6799
|
{
|
|
@@ -6620,8 +6801,8 @@ function proxySharedModule(options) {
|
|
|
6620
6801
|
enforce: "post",
|
|
6621
6802
|
config(config, { command }) {
|
|
6622
6803
|
setPackageDetectionCwd(config.root || process.cwd());
|
|
6623
|
-
setTreeShakingBuildMode(command === "build");
|
|
6624
|
-
resetTreeShakingExports();
|
|
6804
|
+
setTreeShakingBuildMode(command === "build", federationOptions);
|
|
6805
|
+
resetTreeShakingExports(federationOptions);
|
|
6625
6806
|
emittedTreeShakingProviders.clear();
|
|
6626
6807
|
const isVinext = hasPackageDependency("vinext");
|
|
6627
6808
|
const isAstro = hasPackageDependency("astro");
|
|
@@ -6637,29 +6818,29 @@ function proxySharedModule(options) {
|
|
|
6637
6818
|
Object.keys(shared).forEach((key) => {
|
|
6638
6819
|
if (key.endsWith("/")) return;
|
|
6639
6820
|
if (useDirectReactImport && key === "react") {
|
|
6640
|
-
addUsedShares(key);
|
|
6821
|
+
addUsedShares(key, federationOptions);
|
|
6641
6822
|
return;
|
|
6642
6823
|
}
|
|
6643
|
-
writeLoadShareModule(key, shared[key], _command, isRolldown);
|
|
6644
|
-
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(key, shared[key]);
|
|
6645
|
-
addUsedShares(key);
|
|
6824
|
+
writeLoadShareModule(key, shared[key], _command, isRolldown, federationOptions);
|
|
6825
|
+
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(key, shared[key], federationOptions);
|
|
6826
|
+
addUsedShares(key, federationOptions);
|
|
6646
6827
|
});
|
|
6647
|
-
writeLocalSharedImportMap();
|
|
6648
|
-
refreshHostAutoInit();
|
|
6828
|
+
writeLocalSharedImportMap(federationOptions);
|
|
6829
|
+
refreshHostAutoInit(federationOptions);
|
|
6649
6830
|
},
|
|
6650
6831
|
buildStart() {
|
|
6651
6832
|
if (_command !== "build") return;
|
|
6652
|
-
resetTreeShakingExports();
|
|
6833
|
+
resetTreeShakingExports(federationOptions);
|
|
6653
6834
|
emittedTreeShakingProviders.clear();
|
|
6654
|
-
refreshTreeShakingModules();
|
|
6835
|
+
refreshTreeShakingModules(federationOptions);
|
|
6655
6836
|
},
|
|
6656
6837
|
shouldTransformCachedModule() {
|
|
6657
6838
|
return _command === "build" && Object.values(shared).some((share) => !!share.shareConfig.treeShaking);
|
|
6658
6839
|
},
|
|
6659
6840
|
transform(code, id) {
|
|
6660
6841
|
if (_command !== "build" || !Object.keys(shared).some((key) => shared[key].shareConfig.treeShaking)) return;
|
|
6661
|
-
collectTreeShakingImports(code, id, shared, findSharedKeyForSource, recordTreeShakingExports, markTreeShakingPackageUnsafe);
|
|
6662
|
-
refreshTreeShakingModules();
|
|
6842
|
+
collectTreeShakingImports(code, id, shared, findSharedKeyForSource, (sharedKey, exports, request) => recordTreeShakingExports(sharedKey, exports, request, federationOptions), (sharedKey, request) => markTreeShakingPackageUnsafe(sharedKey, request, federationOptions));
|
|
6843
|
+
refreshTreeShakingModules(federationOptions);
|
|
6663
6844
|
}
|
|
6664
6845
|
},
|
|
6665
6846
|
{
|
|
@@ -6719,14 +6900,14 @@ function proxySharedModule(options) {
|
|
|
6719
6900
|
if (shouldSkipTaggedImporterProxy(key, "__loadShare__")) return;
|
|
6720
6901
|
if (shouldSkipTaggedImporterProxy(key, "__prebuild__")) return;
|
|
6721
6902
|
const shareSource = key === "vue" && source.startsWith("vue/dist/") ? key : isNodeModulePath(source) ? getCommonSharedSubpathFromNodeModulePath(source, key) || key : source;
|
|
6722
|
-
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown);
|
|
6903
|
+
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown, federationOptions);
|
|
6723
6904
|
if (!materializedLoadShareSources.has(shareSource)) {
|
|
6724
6905
|
materializedLoadShareSources.add(shareSource);
|
|
6725
|
-
writeLoadShareModule(shareSource, shared[key], _command, useRolldown);
|
|
6726
|
-
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key]);
|
|
6727
|
-
addUsedShares(shareSource);
|
|
6728
|
-
writeLocalSharedImportMap();
|
|
6729
|
-
refreshHostAutoInit();
|
|
6906
|
+
writeLoadShareModule(shareSource, shared[key], _command, useRolldown, federationOptions);
|
|
6907
|
+
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key], federationOptions);
|
|
6908
|
+
addUsedShares(shareSource, federationOptions);
|
|
6909
|
+
writeLocalSharedImportMap(federationOptions);
|
|
6910
|
+
refreshHostAutoInit(federationOptions);
|
|
6730
6911
|
}
|
|
6731
6912
|
return this.resolve(loadSharePath, importer, { skipSelf: true });
|
|
6732
6913
|
}
|
|
@@ -6738,7 +6919,7 @@ function proxySharedModule(options) {
|
|
|
6738
6919
|
if (!source.includes("__prebuild__")) return;
|
|
6739
6920
|
if (source.startsWith(".")) return;
|
|
6740
6921
|
const pkgName = assertModuleFound(PREBUILD_TAG, source).name;
|
|
6741
|
-
const importSource = getPrebuildResolutionSource(pkgName, getPreBuildShareItem(pkgName));
|
|
6922
|
+
const importSource = getPrebuildResolutionSource(pkgName, getPreBuildShareItem(pkgName, federationOptions));
|
|
6742
6923
|
if (_command === "build") return this.resolve(importSource, importer, { skipSelf: true });
|
|
6743
6924
|
const direct = tryResolveFromProjectRoot(importSource);
|
|
6744
6925
|
const directSource = direct && !isNodeModulePath(direct) ? direct : void 0;
|
|
@@ -7395,8 +7576,8 @@ function isWithinDirectory(filePath, directory) {
|
|
|
7395
7576
|
}
|
|
7396
7577
|
//#endregion
|
|
7397
7578
|
//#region src/plugins/pluginVarRemoteEntry.ts
|
|
7398
|
-
const VarRemoteEntry = () => {
|
|
7399
|
-
const mfOptions = getNormalizeModuleFederationOptions();
|
|
7579
|
+
const VarRemoteEntry = (providedOptions) => {
|
|
7580
|
+
const mfOptions = providedOptions ?? getNormalizeModuleFederationOptions();
|
|
7400
7581
|
const { name, varFilename, filename } = mfOptions;
|
|
7401
7582
|
let viteConfig;
|
|
7402
7583
|
return [{
|
|
@@ -7448,7 +7629,7 @@ const VarRemoteEntry = () => {
|
|
|
7448
7629
|
* @returns Complete "var" remoteEntry.js file source
|
|
7449
7630
|
*/
|
|
7450
7631
|
function generateVarRemoteEntry(remoteEntryFile) {
|
|
7451
|
-
const { name, varFilename } =
|
|
7632
|
+
const { name, varFilename } = mfOptions;
|
|
7452
7633
|
const isValidName = isValidVarName(name);
|
|
7453
7634
|
return `
|
|
7454
7635
|
${isValidName ? `var ${name};` : ""}
|
|
@@ -7721,15 +7902,10 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
7721
7902
|
const root = config.root || process.cwd();
|
|
7722
7903
|
setPackageDetectionCwd(root);
|
|
7723
7904
|
const isVinext = hasPackageDependency("vinext");
|
|
7724
|
-
|
|
7725
|
-
name: key,
|
|
7726
|
-
entry: r.entry,
|
|
7727
|
-
type: r.type ?? "module"
|
|
7728
|
-
})));
|
|
7729
|
-
initVirtualModules(_command, getRemoteEntryId(options));
|
|
7905
|
+
initVirtualModules(_command, getRemoteEntryId(options), false, options);
|
|
7730
7906
|
const isRolldown = getIsRolldown(this);
|
|
7731
7907
|
if (remotes && Object.keys(remotes).length > 0) {
|
|
7732
|
-
for (const key of Object.keys(remotes)) addUsedRemote(key, key);
|
|
7908
|
+
for (const key of Object.keys(remotes)) addUsedRemote(key, key, options);
|
|
7733
7909
|
if (_command === "serve") {
|
|
7734
7910
|
config.optimizeDeps = config.optimizeDeps || {};
|
|
7735
7911
|
config.optimizeDeps.exclude = config.optimizeDeps.exclude || [];
|
|
@@ -7749,11 +7925,11 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
7749
7925
|
name: "module-federation:optimize-shared-resolver",
|
|
7750
7926
|
load(id) {
|
|
7751
7927
|
if (id !== "module-federation:optimized-require-react") return;
|
|
7752
|
-
const loadSharePath = getLoadShareModulePath("react", isRolldown);
|
|
7928
|
+
const loadSharePath = getLoadShareModulePath("react", isRolldown, options);
|
|
7753
7929
|
const source = JSON.stringify(loadSharePath);
|
|
7754
7930
|
return "import * as __mfShared from " + source + ";\nexport * from " + source + ";\nexport default __mfShared.default ?? __mfShared;";
|
|
7755
7931
|
},
|
|
7756
|
-
resolveId(source, importer,
|
|
7932
|
+
resolveId(source, importer, resolveOptions) {
|
|
7757
7933
|
if (createViteEncodedIdPrefixRegExp("virtual:mf:").test(source)) return {
|
|
7758
7934
|
id: source,
|
|
7759
7935
|
external: true
|
|
@@ -7764,19 +7940,19 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
7764
7940
|
if (isAssetLikeImport(source)) return;
|
|
7765
7941
|
const shareItem = shared[key];
|
|
7766
7942
|
const isReactSingleton = source === "react" && key === "react" && shareItem.shareConfig?.singleton === true;
|
|
7767
|
-
const isReactRequire =
|
|
7768
|
-
if (
|
|
7943
|
+
const isReactRequire = resolveOptions?.kind?.startsWith("require") && isReactSingleton;
|
|
7944
|
+
if (resolveOptions?.kind?.startsWith("require") && !isReactSingleton) return;
|
|
7769
7945
|
if (isCommonJsImporter(importer) && !isReactSingleton) return;
|
|
7770
7946
|
if (isReactRequire) {
|
|
7771
|
-
writeLoadShareModule(source, shareItem, _command, isRolldown);
|
|
7772
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(source, shareItem);
|
|
7773
|
-
addUsedShares(source);
|
|
7947
|
+
writeLoadShareModule(source, shareItem, _command, isRolldown, options);
|
|
7948
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(source, shareItem, options);
|
|
7949
|
+
addUsedShares(source, options);
|
|
7774
7950
|
return { id: "module-federation:optimized-require-react" };
|
|
7775
7951
|
}
|
|
7776
|
-
const loadSharePath = getLoadShareModulePath(source, isRolldown);
|
|
7777
|
-
writeLoadShareModule(source, shareItem, _command, isRolldown);
|
|
7778
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(source, shareItem);
|
|
7779
|
-
addUsedShares(source);
|
|
7952
|
+
const loadSharePath = getLoadShareModulePath(source, isRolldown, options);
|
|
7953
|
+
writeLoadShareModule(source, shareItem, _command, isRolldown, options);
|
|
7954
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(source, shareItem, options);
|
|
7955
|
+
addUsedShares(source, options);
|
|
7780
7956
|
return {
|
|
7781
7957
|
id: loadSharePath,
|
|
7782
7958
|
external: true
|
|
@@ -7810,15 +7986,15 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
7810
7986
|
const key = findSharedKey(args.path, shared);
|
|
7811
7987
|
if (!key) return;
|
|
7812
7988
|
const shareItem = shared[key];
|
|
7813
|
-
const
|
|
7814
|
-
writeLoadShareModule(args.path, shareItem, _command, isRolldown);
|
|
7815
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(args.path, shareItem);
|
|
7816
|
-
addUsedShares(args.path);
|
|
7989
|
+
const loadSharePath = getLoadShareModulePath(args.path, isRolldown, options);
|
|
7990
|
+
writeLoadShareModule(args.path, shareItem, _command, isRolldown, options);
|
|
7991
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(args.path, shareItem, options);
|
|
7992
|
+
addUsedShares(args.path, options);
|
|
7817
7993
|
return {
|
|
7818
7994
|
loader: "js",
|
|
7819
7995
|
resolveDir: root,
|
|
7820
|
-
contents: `import * as __mfShared from ${JSON.stringify(
|
|
7821
|
-
export * from ${JSON.stringify(
|
|
7996
|
+
contents: `import * as __mfShared from ${JSON.stringify(loadSharePath)};
|
|
7997
|
+
export * from ${JSON.stringify(loadSharePath)};
|
|
7822
7998
|
export default __mfShared.default ?? __mfShared;`
|
|
7823
7999
|
};
|
|
7824
8000
|
});
|
|
@@ -7834,7 +8010,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
7834
8010
|
optimizeDeps.include ??= [];
|
|
7835
8011
|
optimizeDeps.exclude ??= [];
|
|
7836
8012
|
for (const subpath of getCommonSharedSubpaths(key)) {
|
|
7837
|
-
writePreBuildLibPath(subpath, shareItem);
|
|
8013
|
+
writePreBuildLibPath(subpath, shareItem, options);
|
|
7838
8014
|
if (canResolveSharedSubpath(subpath, root)) optimizeDeps.include.push(subpath);
|
|
7839
8015
|
else optimizeDeps.exclude.push(subpath);
|
|
7840
8016
|
}
|
|
@@ -7842,13 +8018,13 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
7842
8018
|
continue;
|
|
7843
8019
|
}
|
|
7844
8020
|
if (isVinext && key === "react") {
|
|
7845
|
-
addUsedShares(key);
|
|
8021
|
+
addUsedShares(key, options);
|
|
7846
8022
|
continue;
|
|
7847
8023
|
}
|
|
7848
|
-
getLoadShareModulePath(key, isRolldown);
|
|
7849
|
-
writeLoadShareModule(key, shareItem, _command, isRolldown);
|
|
7850
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(key, shareItem);
|
|
7851
|
-
addUsedShares(key);
|
|
8024
|
+
getLoadShareModulePath(key, isRolldown, options);
|
|
8025
|
+
writeLoadShareModule(key, shareItem, _command, isRolldown, options);
|
|
8026
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(key, shareItem, options);
|
|
8027
|
+
addUsedShares(key, options);
|
|
7852
8028
|
if (_command === "serve" && shareItem.shareConfig?.import !== false) {
|
|
7853
8029
|
const optimizeDeps = config.optimizeDeps ??= {};
|
|
7854
8030
|
optimizeDeps.include ??= [];
|
|
@@ -7858,16 +8034,16 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
7858
8034
|
else if (shouldBypassOptimizeDep) optimizeDeps.exclude.push(key);
|
|
7859
8035
|
else optimizeDeps.include.push(key);
|
|
7860
8036
|
for (const subpath of getCommonSharedSubpaths(key)) {
|
|
7861
|
-
getLoadShareModulePath(subpath, isRolldown);
|
|
7862
|
-
writeLoadShareModule(subpath, shareItem, _command, isRolldown);
|
|
7863
|
-
writePreBuildLibPath(subpath, shareItem);
|
|
7864
|
-
addUsedShares(subpath);
|
|
8037
|
+
getLoadShareModulePath(subpath, isRolldown, options);
|
|
8038
|
+
writeLoadShareModule(subpath, shareItem, _command, isRolldown, options);
|
|
8039
|
+
writePreBuildLibPath(subpath, shareItem, options);
|
|
8040
|
+
addUsedShares(subpath, options);
|
|
7865
8041
|
if (canResolveSharedSubpath(subpath, root)) optimizeDeps.include.push(subpath);
|
|
7866
8042
|
else optimizeDeps.exclude.push(subpath);
|
|
7867
8043
|
}
|
|
7868
8044
|
}
|
|
7869
8045
|
}
|
|
7870
|
-
writeLocalSharedImportMap();
|
|
8046
|
+
writeLocalSharedImportMap(options);
|
|
7871
8047
|
}
|
|
7872
8048
|
if (_command === "serve") {
|
|
7873
8049
|
config.optimizeDeps ??= {};
|
|
@@ -7939,8 +8115,9 @@ function federation(mfUserOptions) {
|
|
|
7939
8115
|
command,
|
|
7940
8116
|
isRolldown: getIsRolldown(this),
|
|
7941
8117
|
findSharedKey,
|
|
7942
|
-
addUsedShares,
|
|
7943
|
-
writeLocalSharedImportMap
|
|
8118
|
+
addUsedShares: (pkg) => addUsedShares(pkg, options),
|
|
8119
|
+
writeLocalSharedImportMap: () => writeLocalSharedImportMap(options),
|
|
8120
|
+
federationOptions: options
|
|
7944
8121
|
});
|
|
7945
8122
|
virtualModule = VirtualModule.findById(id);
|
|
7946
8123
|
}
|
|
@@ -7981,7 +8158,7 @@ function federation(mfUserOptions) {
|
|
|
7981
8158
|
},
|
|
7982
8159
|
configResolved() {
|
|
7983
8160
|
const ssrCapabilities = getSsrCapabilities(parseInt(version, 10), command, Object.keys(options.remotes).length > 0);
|
|
7984
|
-
initVirtualModules(command, remoteEntryId, ssrCapabilities.enableSsrInitBootstrap);
|
|
8161
|
+
initVirtualModules(command, remoteEntryId, ssrCapabilities.enableSsrInitBootstrap, options);
|
|
7985
8162
|
}
|
|
7986
8163
|
},
|
|
7987
8164
|
aliasToArrayPlugin_default,
|
|
@@ -8004,18 +8181,21 @@ function federation(mfUserOptions) {
|
|
|
8004
8181
|
...addEntry({
|
|
8005
8182
|
entryName: "remoteEntry",
|
|
8006
8183
|
entryPath: remoteEntryId,
|
|
8007
|
-
fileName: filename
|
|
8184
|
+
fileName: filename,
|
|
8185
|
+
federationOptions: options
|
|
8008
8186
|
}),
|
|
8009
8187
|
...addEntry({
|
|
8010
8188
|
entryName: "hostInit",
|
|
8011
|
-
entryPath: () => getHostAutoInitPath(),
|
|
8189
|
+
entryPath: () => getHostAutoInitPath(options),
|
|
8012
8190
|
inject: hostInitInjectLocation,
|
|
8013
8191
|
forceClientInjected: Object.keys(options.exposes).length > 0,
|
|
8014
|
-
skipTransformFor: Object.values(options.exposes).map((expose) => expose.import)
|
|
8192
|
+
skipTransformFor: Object.values(options.exposes).map((expose) => expose.import),
|
|
8193
|
+
federationOptions: options
|
|
8015
8194
|
}),
|
|
8016
8195
|
...addEntry({
|
|
8017
8196
|
entryName: "virtualExposes",
|
|
8018
|
-
entryPath: virtualExposesId
|
|
8197
|
+
entryPath: virtualExposesId,
|
|
8198
|
+
federationOptions: options
|
|
8019
8199
|
}),
|
|
8020
8200
|
pluginProxyRemoteEntry_default({
|
|
8021
8201
|
options,
|
|
@@ -8025,20 +8205,23 @@ function federation(mfUserOptions) {
|
|
|
8025
8205
|
pluginProxyRemotes_default(options),
|
|
8026
8206
|
pluginRemoteNamedExports(options),
|
|
8027
8207
|
...pluginModuleParseEnd_default((id) => {
|
|
8028
|
-
return id.includes(getHostAutoInitImportId()) || id.includes(remoteEntryId) || id.includes(virtualExposesId) || id.includes(getLocalSharedImportMapPath()) || id.includes("__loadShare__") || id.includes("__prebuild__") || id.includes("__treeShakingProvider__") || id.includes("__mf_tree_shaking_graph__");
|
|
8208
|
+
return id.includes(getHostAutoInitImportId(options)) || id.includes(remoteEntryId) || id.includes(virtualExposesId) || id.includes(getLocalSharedImportMapPath(options)) || id.includes("__loadShare__") || id.includes("__prebuild__") || id.includes("__treeShakingProvider__") || id.includes("__mf_tree_shaking_graph__");
|
|
8029
8209
|
}, {
|
|
8030
8210
|
moduleParseTimeout: options.moduleParseTimeout,
|
|
8031
8211
|
moduleParseIdleTimeout: options.moduleParseIdleTimeout,
|
|
8032
8212
|
exposedModuleImports: Object.values(options.exposes).map((expose) => expose.import)
|
|
8033
8213
|
}),
|
|
8034
|
-
...proxySharedModule({
|
|
8214
|
+
...proxySharedModule({
|
|
8215
|
+
shared,
|
|
8216
|
+
federationOptions: options
|
|
8217
|
+
}),
|
|
8035
8218
|
{
|
|
8036
8219
|
name: "module-federation-esm-shims",
|
|
8037
8220
|
enforce: "pre",
|
|
8038
8221
|
apply: "build",
|
|
8039
8222
|
config(config) {
|
|
8040
8223
|
isSsrBuild = config.build?.ssr === true;
|
|
8041
|
-
const runtimeInitId =
|
|
8224
|
+
const runtimeInitId = getRuntimeInitStatusImportId(options);
|
|
8042
8225
|
config.build = config.build || {};
|
|
8043
8226
|
if (config.build.modulePreload !== false) {
|
|
8044
8227
|
const currentModulePreload = config.build.modulePreload && typeof config.build.modulePreload === "object" ? config.build.modulePreload : {};
|
|
@@ -8085,7 +8268,7 @@ function federation(mfUserOptions) {
|
|
|
8085
8268
|
mfWarn("Ignoring `output.manualChunks` because it conflicts with module federation. Module federation transforms shared dependency imports with async init wrappers, and grouping these transformed modules into a single chunk creates circular async dependencies that cause the application to silently hang.");
|
|
8086
8269
|
}
|
|
8087
8270
|
const mfChunkName = function(id) {
|
|
8088
|
-
if (id.includes(runtimeInitId)) return "runtimeInit";
|
|
8271
|
+
if (id.includes(runtimeInitId) || id.includes("__mf_v__runtimeInit__mf_v__")) return "runtimeInit";
|
|
8089
8272
|
if (id.includes("__loadShare__")) {
|
|
8090
8273
|
const match = id.match(/([^/\\]+__loadShare__[^/\\]+)/);
|
|
8091
8274
|
return match ? match[1] : "loadShare";
|
|
@@ -8282,9 +8465,9 @@ function federation(mfUserOptions) {
|
|
|
8282
8465
|
if (options.target && config.define["ENV_TARGET"] !== JSON.stringify(options.target)) mfWarn(`ENV_TARGET define (${config.define["ENV_TARGET"]}) differs from target option ("${options.target}"). ENV_TARGET will not be overridden.`);
|
|
8283
8466
|
}
|
|
8284
8467
|
},
|
|
8285
|
-
...Manifest(),
|
|
8468
|
+
...Manifest(options),
|
|
8286
8469
|
...pluginSSRRemoteEntry(options),
|
|
8287
|
-
...VarRemoteEntry(),
|
|
8470
|
+
...VarRemoteEntry(options),
|
|
8288
8471
|
{
|
|
8289
8472
|
name: "module-federation-vinext-fix-rsc-preload-as",
|
|
8290
8473
|
enforce: "post",
|