@module-federation/vite 1.18.1 → 1.19.0
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 +23 -0
- package/lib/index.d.ts +21 -0
- package/lib/index.js +632 -361
- package/lib/{pluginDts-CGDIZCsD.js → pluginDts-Cd2opGcZ.js} +8 -0
- package/lib/{ssrEntryLoader-DgsCQOqq.js → ssrEntryLoader-CuZVKlRm.js} +114 -40
- package/lib/{ssrVmStrategy-ChF8MB7l.js → ssrVmStrategy-Dpw20xiw.js} +5 -4
- package/lib/utils/ssrEntryLoader.d.ts +5 -0
- package/lib/utils/ssrEntryLoader.js +1 -1
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _ as createModuleFederationError, a as getIsRolldown, b as rebaseImport, c as getPackageNameFromNodeModulePath, d as isNuxtProjectRoot, f as packageNameDecode, g as sharedCacheHelperCode, h as setPackageDetectionCwd, i as getInstalledPackageJson, l as getSharedCacheDescriptor, m as resolveImportPath, o as getPackageDetectionCwd, p as packageNameEncode, r as getInstalledPackageEntry, s as getPackageName, u as hasPackageDependency, v as mfWarn, y as normalizePathForImport } from "./pluginDts-
|
|
1
|
+
import { _ as createModuleFederationError, a as getIsRolldown, b as rebaseImport, c as getPackageNameFromNodeModulePath, d as isNuxtProjectRoot, f as packageNameDecode, g as sharedCacheHelperCode, h as setPackageDetectionCwd, i as getInstalledPackageJson, l as getSharedCacheDescriptor, m as resolveImportPath, o as getPackageDetectionCwd, p as packageNameEncode, r as getInstalledPackageEntry, s as getPackageName, u as hasPackageDependency, v as mfWarn, y as normalizePathForImport } from "./pluginDts-Cd2opGcZ.js";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import * as fs$2 from "fs";
|
|
4
4
|
import { existsSync, readFileSync, realpathSync, statSync, writeFileSync } from "fs";
|
|
@@ -237,6 +237,10 @@ const ASSET_LIKE_IMPORT_RE = new RegExp(`\\.(${[...[
|
|
|
237
237
|
function isAssetLikeImport(source) {
|
|
238
238
|
return ASSET_LIKE_IMPORT_RE.test(source);
|
|
239
239
|
}
|
|
240
|
+
const VITE_OPTIMIZABLE_ENTRY_RE = /\.[cm]?[jt]s$/;
|
|
241
|
+
function isViteOptimizableEntry(resolvedPath) {
|
|
242
|
+
return VITE_OPTIMIZABLE_ENTRY_RE.test(resolvedPath);
|
|
243
|
+
}
|
|
240
244
|
function removeTrailingSlash(value) {
|
|
241
245
|
return value.endsWith("/") ? value.slice(0, -1) : value;
|
|
242
246
|
}
|
|
@@ -490,6 +494,7 @@ function normalizeManifest(manifest) {
|
|
|
490
494
|
}
|
|
491
495
|
let config;
|
|
492
496
|
let explicitSharedKeys = /* @__PURE__ */ new Set();
|
|
497
|
+
const explicitSharedKeysByOptions = /* @__PURE__ */ new WeakMap();
|
|
493
498
|
function resolveRuntimeImplementation() {
|
|
494
499
|
const fallback = resolveImportPath("@module-federation/runtime");
|
|
495
500
|
try {
|
|
@@ -505,17 +510,16 @@ function resolveRuntimeImplementation() {
|
|
|
505
510
|
function getNormalizeModuleFederationOptions() {
|
|
506
511
|
return config;
|
|
507
512
|
}
|
|
508
|
-
function isExplicitSharedKey(key) {
|
|
509
|
-
return explicitSharedKeys
|
|
513
|
+
function isExplicitSharedKey(key, options) {
|
|
514
|
+
return (options ? explicitSharedKeysByOptions.get(options) : explicitSharedKeys)?.has(key) ?? false;
|
|
510
515
|
}
|
|
511
|
-
function getNormalizeShareItem(key) {
|
|
512
|
-
const options = getNormalizeModuleFederationOptions();
|
|
516
|
+
function getNormalizeShareItem(key, options = getNormalizeModuleFederationOptions()) {
|
|
513
517
|
return options.shared[key] || options.shared[getPackageName(key)] || options.shared[getPackageName(key) + "/"];
|
|
514
518
|
}
|
|
515
519
|
function normalizeModuleFederationOptions(options) {
|
|
516
520
|
warnOnReservedInternalNamePrefix(options.name, "containerName");
|
|
517
521
|
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
|
-
|
|
522
|
+
const normalized = {
|
|
519
523
|
exposes: normalizeExposes(options.exposes),
|
|
520
524
|
filename: options.filename || "remoteEntry-[hash]",
|
|
521
525
|
internalName: toInternalModuleFederationName(options.name),
|
|
@@ -544,8 +548,13 @@ function normalizeModuleFederationOptions(options) {
|
|
|
544
548
|
moduleParseTimeout: options.moduleParseTimeout || 10,
|
|
545
549
|
moduleParseIdleTimeout: options.moduleParseIdleTimeout,
|
|
546
550
|
varFilename: options.varFilename,
|
|
547
|
-
target: options.target
|
|
551
|
+
target: options.target,
|
|
552
|
+
disableRemote: options.disableRemote,
|
|
553
|
+
disableShared: options.disableShared,
|
|
554
|
+
disableSnapshot: options.disableSnapshot
|
|
548
555
|
};
|
|
556
|
+
explicitSharedKeysByOptions.set(normalized, new Set(explicitSharedKeys));
|
|
557
|
+
return config = normalized;
|
|
549
558
|
}
|
|
550
559
|
//#endregion
|
|
551
560
|
//#region src/utils/VirtualModule.ts
|
|
@@ -575,7 +584,7 @@ function decodeViteId(id) {
|
|
|
575
584
|
return viteId.startsWith("__x00__") ? `\0${viteId.slice(7)}` : viteId;
|
|
576
585
|
}
|
|
577
586
|
function assertModuleFound(tag, str = "") {
|
|
578
|
-
const module = VirtualModule.findModule(tag, str);
|
|
587
|
+
const module = VirtualModule.findById(str) ?? VirtualModule.findModule(tag, str);
|
|
579
588
|
if (!module) throw createModuleFederationError(`Module Federation shared module '${str}' not found. Please ensure it's installed as a dependency in your package.json.`);
|
|
580
589
|
return module;
|
|
581
590
|
}
|
|
@@ -594,6 +603,7 @@ var VirtualModule = class VirtualModule {
|
|
|
594
603
|
code;
|
|
595
604
|
importId;
|
|
596
605
|
importIdKey;
|
|
606
|
+
scopeName;
|
|
597
607
|
static findName(tag, str = "") {
|
|
598
608
|
if (!patternMap[tag]) patternMap[tag] = new RegExp(`(.*${packageNameEncode(tag)}(.+?)${packageNameEncode(tag)}.*)`);
|
|
599
609
|
const moduleName = (normalizeVirtualModuleId(str).match(patternMap[tag]) || [])[2];
|
|
@@ -607,16 +617,16 @@ var VirtualModule = class VirtualModule {
|
|
|
607
617
|
const normalized = normalizeVirtualModuleId(id);
|
|
608
618
|
return normalized.startsWith("virtual:mf:") ? idCacheMap[normalized] : void 0;
|
|
609
619
|
}
|
|
610
|
-
constructor(name, tag = "__mf_v__", suffix = "") {
|
|
620
|
+
constructor(name, tag = "__mf_v__", suffix = "", scopeName) {
|
|
611
621
|
this.name = name;
|
|
612
622
|
this.tag = tag;
|
|
613
623
|
this.suffix = suffix || getSuffix(name);
|
|
624
|
+
this.scopeName = scopeName;
|
|
614
625
|
if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
|
|
615
626
|
cacheMap[this.tag][this.name] = this;
|
|
616
627
|
}
|
|
617
628
|
getImportId() {
|
|
618
|
-
const
|
|
619
|
-
const importIdKey = `${mfName}${this.tag}${this.name}${this.tag}`;
|
|
629
|
+
const importIdKey = `${this.scopeName ?? getNormalizeModuleFederationOptions().internalName}${this.tag}${this.name}${this.tag}`;
|
|
620
630
|
if (this.importId && this.importIdKey === importIdKey) return this.importId;
|
|
621
631
|
if (this.importId) delete idCacheMap[this.importId];
|
|
622
632
|
this.importIdKey = importIdKey;
|
|
@@ -774,7 +784,7 @@ function generateExposes(options, remoteDependencyMap = {}, command = "build") {
|
|
|
774
784
|
export default {
|
|
775
785
|
${Object.keys(options.exposes).map((key) => {
|
|
776
786
|
const remoteDependencyPreloads = (remoteDependencyMap[key] ?? []).map((remoteId) => {
|
|
777
|
-
const virtualRemote = getRemoteVirtualModule(remoteId, command);
|
|
787
|
+
const virtualRemote = getRemoteVirtualModule(remoteId, command, false, "unified", options);
|
|
778
788
|
return `import(${JSON.stringify(virtualRemote.getImportId())})
|
|
779
789
|
.then((mod) => mod.__mf_remote_pending)`;
|
|
780
790
|
}).join(",");
|
|
@@ -805,9 +815,40 @@ function generateExposes(options, remoteDependencyMap = {}, command = "build") {
|
|
|
805
815
|
//#endregion
|
|
806
816
|
//#region src/virtualModules/virtualRuntimeInitStatus.ts
|
|
807
817
|
const virtualRuntimeInitStatus = new VirtualModule("runtimeInit");
|
|
818
|
+
const runtimeInitModules = /* @__PURE__ */ new WeakMap();
|
|
819
|
+
const runtimeInitOwnerIds = /* @__PURE__ */ new WeakMap();
|
|
820
|
+
let nextRuntimeInitOwnerId = 1;
|
|
808
821
|
const MODULE_CACHE_GLOBAL_KEY = "__mf_module_cache__";
|
|
809
|
-
function
|
|
810
|
-
|
|
822
|
+
function getRuntimeInitOwnerId(options) {
|
|
823
|
+
let ownerId = runtimeInitOwnerIds.get(options);
|
|
824
|
+
if (!ownerId) {
|
|
825
|
+
ownerId = nextRuntimeInitOwnerId++;
|
|
826
|
+
runtimeInitOwnerIds.set(options, ownerId);
|
|
827
|
+
}
|
|
828
|
+
return ownerId;
|
|
829
|
+
}
|
|
830
|
+
function getRuntimeInitModule(options) {
|
|
831
|
+
if (!options) return virtualRuntimeInitStatus;
|
|
832
|
+
let runtimeInitModule = runtimeInitModules.get(options);
|
|
833
|
+
if (!runtimeInitModule) {
|
|
834
|
+
const ownerId = getRuntimeInitOwnerId(options);
|
|
835
|
+
runtimeInitModule = new VirtualModule("runtimeInit", "__mf_v__", "", `${options.internalName}__mf_owner__${ownerId}`);
|
|
836
|
+
runtimeInitModules.set(options, runtimeInitModule);
|
|
837
|
+
}
|
|
838
|
+
return runtimeInitModule;
|
|
839
|
+
}
|
|
840
|
+
function getRuntimeInitStatusImportId(options) {
|
|
841
|
+
return getRuntimeInitModule(options).getImportId();
|
|
842
|
+
}
|
|
843
|
+
function getRuntimeRemoteCachePrefix(options) {
|
|
844
|
+
return options ? `${getRuntimeInitStatusImportId(options)}::` : "";
|
|
845
|
+
}
|
|
846
|
+
function getRuntimeRemoteAlias(alias, options) {
|
|
847
|
+
if (!options) return alias;
|
|
848
|
+
return `${options.internalName}__mf_owner__${getRuntimeInitOwnerId(options)}__${alias}`;
|
|
849
|
+
}
|
|
850
|
+
function getRuntimeInitGlobalKey(ownerImportId) {
|
|
851
|
+
return `__mf_init__${ownerImportId ?? virtualRuntimeInitStatus.getImportId()}__`;
|
|
811
852
|
}
|
|
812
853
|
function getDeferredInitPromiseCode() {
|
|
813
854
|
return `let initResolve, initReject;
|
|
@@ -817,10 +858,7 @@ function getDeferredInitPromiseCode() {
|
|
|
817
858
|
});`;
|
|
818
859
|
}
|
|
819
860
|
let _ssrRemotes = [];
|
|
820
|
-
function
|
|
821
|
-
_ssrRemotes = remotes;
|
|
822
|
-
}
|
|
823
|
-
function getSsrNoopResolveCode(enableSsrInit, hostInitImportId, initResolveExpression = "initResolve") {
|
|
861
|
+
function getSsrNoopResolveCode(enableSsrInit, hostInitImportId, initResolveExpression = "initResolve", ssrRemotes = _ssrRemotes) {
|
|
824
862
|
if (!enableSsrInit) return "";
|
|
825
863
|
return `if (${SERVER_ENV_GUARD}) {
|
|
826
864
|
var _noop = { loadRemote: function() { return Promise.resolve(undefined); }, loadShare: function() { return Promise.resolve(undefined); } };
|
|
@@ -840,7 +878,7 @@ function getSsrNoopResolveCode(enableSsrInit, hostInitImportId, initResolveExpre
|
|
|
840
878
|
function() { return [runtimeMod, []]; }
|
|
841
879
|
);
|
|
842
880
|
}).then(function(pair) {
|
|
843
|
-
var runtime = pair[0].init({ name: '__mf_ssr_host__', remotes: ${JSON.stringify(
|
|
881
|
+
var runtime = pair[0].init({ name: '__mf_ssr_host__', remotes: ${JSON.stringify(ssrRemotes)}, shared: {}, plugins: pair[1] });
|
|
844
882
|
${initResolveExpression}(runtime);
|
|
845
883
|
}, function() {
|
|
846
884
|
${initResolveExpression}(_noop);
|
|
@@ -850,7 +888,7 @@ function getSsrNoopResolveCode(enableSsrInit, hostInitImportId, initResolveExpre
|
|
|
850
888
|
}
|
|
851
889
|
function getRuntimeInitStateBootstrapCode(options) {
|
|
852
890
|
return `
|
|
853
|
-
const ${options.globalKeyVar} = ${JSON.stringify(getRuntimeInitGlobalKey())};
|
|
891
|
+
const ${options.globalKeyVar} = ${JSON.stringify(getRuntimeInitGlobalKey(options.ownerImportId))};
|
|
854
892
|
let ${options.stateVar} = globalThis[${options.globalKeyVar}];
|
|
855
893
|
if (!${options.stateVar}) {
|
|
856
894
|
${getDeferredInitPromiseCode()}
|
|
@@ -859,14 +897,14 @@ if (!${options.stateVar}) {
|
|
|
859
897
|
initResolve,
|
|
860
898
|
initReject,
|
|
861
899
|
};
|
|
862
|
-
${getSsrNoopResolveCode(options.enableSsrInit)}
|
|
900
|
+
${getSsrNoopResolveCode(options.enableSsrInit, options.hostInitImportId, "initResolve", options.ssrRemotes)}
|
|
863
901
|
}
|
|
864
902
|
const ${options.exposedConst} = ${options.stateVar}.${options.exposedProperty};
|
|
865
903
|
`;
|
|
866
904
|
}
|
|
867
|
-
function getRuntimeInitBootstrapCode(enableSsrInit = false, hostInitImportId) {
|
|
905
|
+
function getRuntimeInitBootstrapCode(enableSsrInit = false, ownerImportId, ssrRemotes, hostInitImportId = ownerImportId) {
|
|
868
906
|
return `
|
|
869
|
-
const globalKey = ${JSON.stringify(getRuntimeInitGlobalKey())};
|
|
907
|
+
const globalKey = ${JSON.stringify(getRuntimeInitGlobalKey(ownerImportId))};
|
|
870
908
|
const moduleCacheGlobalKey = ${JSON.stringify(MODULE_CACHE_GLOBAL_KEY)};
|
|
871
909
|
globalThis[moduleCacheGlobalKey] ||= { share: {}, remote: {} };
|
|
872
910
|
globalThis[moduleCacheGlobalKey].share ||= {};
|
|
@@ -883,7 +921,7 @@ globalThis[globalKey] = {
|
|
|
883
921
|
${enableSsrInit ? `
|
|
884
922
|
if (${SERVER_ENV_GUARD} && !globalThis[globalKey].ssrInitStarted) {
|
|
885
923
|
globalThis[globalKey].ssrInitStarted = true;
|
|
886
|
-
${getSsrNoopResolveCode(enableSsrInit, hostInitImportId, "globalThis[globalKey].initResolve")}
|
|
924
|
+
${getSsrNoopResolveCode(enableSsrInit, hostInitImportId, "globalThis[globalKey].initResolve", ssrRemotes)}
|
|
887
925
|
}` : ""}
|
|
888
926
|
globalThis[globalKey].moduleCache ||= globalThis[moduleCacheGlobalKey];
|
|
889
927
|
globalThis[globalKey].moduleCache.share ||= {};
|
|
@@ -912,29 +950,36 @@ for (const __mfShareKey of Object.keys(__mfModuleCache.share)) {
|
|
|
912
950
|
}
|
|
913
951
|
`;
|
|
914
952
|
}
|
|
915
|
-
function getRuntimeInitPromiseBootstrapCode(enableSsrInit = false) {
|
|
953
|
+
function getRuntimeInitPromiseBootstrapCode(enableSsrInit = false, ownerImportId, ssrRemotes, hostInitImportId = ownerImportId) {
|
|
916
954
|
return getRuntimeInitStateBootstrapCode({
|
|
917
955
|
globalKeyVar: "__mfPromiseGlobalKey",
|
|
918
956
|
stateVar: "__mfPromiseState",
|
|
919
957
|
exposedConst: "initPromise",
|
|
920
958
|
exposedProperty: "initPromise",
|
|
921
|
-
enableSsrInit
|
|
959
|
+
enableSsrInit,
|
|
960
|
+
ownerImportId,
|
|
961
|
+
hostInitImportId,
|
|
962
|
+
ssrRemotes
|
|
922
963
|
});
|
|
923
964
|
}
|
|
924
|
-
function getRuntimeInitResolveBootstrapCode(enableSsrInit = false) {
|
|
965
|
+
function getRuntimeInitResolveBootstrapCode(enableSsrInit = false, ownerImportId, ssrRemotes, hostInitImportId = ownerImportId) {
|
|
925
966
|
return getRuntimeInitStateBootstrapCode({
|
|
926
967
|
globalKeyVar: "__mfResolveGlobalKey",
|
|
927
968
|
stateVar: "__mfResolveState",
|
|
928
969
|
exposedConst: "initResolve",
|
|
929
970
|
exposedProperty: "initResolve",
|
|
930
|
-
enableSsrInit
|
|
971
|
+
enableSsrInit,
|
|
972
|
+
ownerImportId,
|
|
973
|
+
hostInitImportId,
|
|
974
|
+
ssrRemotes
|
|
931
975
|
});
|
|
932
976
|
}
|
|
933
|
-
function writeRuntimeInitStatus(command, enableSsrInit = false, hostInitImportId) {
|
|
977
|
+
function writeRuntimeInitStatus(command, enableSsrInit = false, hostInitImportId, options, ssrRemotes = _ssrRemotes) {
|
|
934
978
|
const exportStatement = command === "build" ? `const { initPromise, initResolve, initReject, moduleCache } = globalThis[globalKey];
|
|
935
979
|
export { initPromise, initResolve, initReject, moduleCache };` : `module.exports = globalThis[globalKey];`;
|
|
936
|
-
|
|
937
|
-
|
|
980
|
+
const ownerImportId = options ? getRuntimeInitStatusImportId(options) : hostInitImportId;
|
|
981
|
+
getRuntimeInitModule(options).writeSync(`
|
|
982
|
+
${getRuntimeInitBootstrapCode(enableSsrInit, ownerImportId, ssrRemotes, hostInitImportId)}
|
|
938
983
|
${exportStatement}
|
|
939
984
|
`);
|
|
940
985
|
}
|
|
@@ -1079,25 +1124,35 @@ function createCodePositionMap(code) {
|
|
|
1079
1124
|
}
|
|
1080
1125
|
//#endregion
|
|
1081
1126
|
//#region src/utils/treeShaking.ts
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
let
|
|
1090
|
-
|
|
1091
|
-
|
|
1127
|
+
const legacyTreeShakingState = {
|
|
1128
|
+
inferredUsage: /* @__PURE__ */ new Map(),
|
|
1129
|
+
buildMode: false
|
|
1130
|
+
};
|
|
1131
|
+
const treeShakingStates = /* @__PURE__ */ new WeakMap();
|
|
1132
|
+
function getTreeShakingState(options) {
|
|
1133
|
+
if (!options) return legacyTreeShakingState;
|
|
1134
|
+
let state = treeShakingStates.get(options);
|
|
1135
|
+
if (!state) {
|
|
1136
|
+
state = {
|
|
1137
|
+
inferredUsage: /* @__PURE__ */ new Map(),
|
|
1138
|
+
buildMode: false
|
|
1139
|
+
};
|
|
1140
|
+
treeShakingStates.set(options, state);
|
|
1141
|
+
}
|
|
1142
|
+
return state;
|
|
1143
|
+
}
|
|
1144
|
+
function setTreeShakingBuildMode(enabled, options) {
|
|
1145
|
+
getTreeShakingState(options).buildMode = enabled;
|
|
1092
1146
|
}
|
|
1093
|
-
function resetTreeShakingExports() {
|
|
1094
|
-
|
|
1147
|
+
function resetTreeShakingExports(options) {
|
|
1148
|
+
getTreeShakingState(options).inferredUsage.clear();
|
|
1095
1149
|
}
|
|
1096
|
-
function getOrCreateExportRecord(sharedKey, request) {
|
|
1097
|
-
|
|
1150
|
+
function getOrCreateExportRecord(sharedKey, request, options) {
|
|
1151
|
+
const inferredUsage = getTreeShakingState(options).inferredUsage;
|
|
1152
|
+
let byRequest = inferredUsage.get(sharedKey);
|
|
1098
1153
|
if (!byRequest) {
|
|
1099
1154
|
byRequest = /* @__PURE__ */ new Map();
|
|
1100
|
-
|
|
1155
|
+
inferredUsage.set(sharedKey, byRequest);
|
|
1101
1156
|
}
|
|
1102
1157
|
let record = byRequest.get(request);
|
|
1103
1158
|
if (!record) {
|
|
@@ -1109,22 +1164,23 @@ function getOrCreateExportRecord(sharedKey, request) {
|
|
|
1109
1164
|
}
|
|
1110
1165
|
return record;
|
|
1111
1166
|
}
|
|
1112
|
-
function recordTreeShakingExports(sharedKey, exports, request = sharedKey) {
|
|
1113
|
-
const record = getOrCreateExportRecord(sharedKey, request);
|
|
1167
|
+
function recordTreeShakingExports(sharedKey, exports, request = sharedKey, options) {
|
|
1168
|
+
const record = getOrCreateExportRecord(sharedKey, request, options);
|
|
1114
1169
|
exports.forEach((name) => record.usedExports.add(name));
|
|
1115
1170
|
}
|
|
1116
|
-
function markTreeShakingPackageUnsafe(sharedKey, request = sharedKey) {
|
|
1117
|
-
getOrCreateExportRecord(sharedKey, request).requiresFullBundle = true;
|
|
1171
|
+
function markTreeShakingPackageUnsafe(sharedKey, request = sharedKey, options) {
|
|
1172
|
+
getOrCreateExportRecord(sharedKey, request, options).requiresFullBundle = true;
|
|
1118
1173
|
}
|
|
1119
|
-
function getExportRecords(sharedKey, request) {
|
|
1174
|
+
function getExportRecords(sharedKey, request, options) {
|
|
1175
|
+
const inferredUsage = getTreeShakingState(options).inferredUsage;
|
|
1120
1176
|
if (sharedKey) {
|
|
1121
|
-
const records =
|
|
1177
|
+
const records = inferredUsage.get(sharedKey);
|
|
1122
1178
|
const wildcard = records?.get("*");
|
|
1123
1179
|
const exact = records?.get(request);
|
|
1124
1180
|
return [wildcard, exact === wildcard ? void 0 : exact].filter((record) => !!record);
|
|
1125
1181
|
}
|
|
1126
1182
|
const records = [];
|
|
1127
|
-
|
|
1183
|
+
inferredUsage.forEach((byRequest, configuredKey) => {
|
|
1128
1184
|
const wildcard = byRequest.get("*");
|
|
1129
1185
|
const exact = byRequest.get(request);
|
|
1130
1186
|
const keyBase = configuredKey.endsWith("/") ? configuredKey.slice(0, -1) : configuredKey;
|
|
@@ -1141,10 +1197,10 @@ function getExportRecords(sharedKey, request) {
|
|
|
1141
1197
|
* fallback lookup across keys keeps aliases/backwards-compatible callers
|
|
1142
1198
|
* working, while still keeping each concrete request's exports isolated.
|
|
1143
1199
|
*/
|
|
1144
|
-
function getTreeShakingExportUsage(request, shareItem, sharedKey) {
|
|
1200
|
+
function getTreeShakingExportUsage(request, shareItem, sharedKey, options) {
|
|
1145
1201
|
const treeShaking = shareItem?.shareConfig.treeShaking;
|
|
1146
|
-
if (!treeShaking || !
|
|
1147
|
-
const records = getExportRecords(sharedKey, request);
|
|
1202
|
+
if (!treeShaking || !getTreeShakingState(options).buildMode) return void 0;
|
|
1203
|
+
const records = getExportRecords(sharedKey, request, options);
|
|
1148
1204
|
if (records.some((record) => record.requiresFullBundle)) return { kind: "full" };
|
|
1149
1205
|
const configured = treeShaking.usedExports ?? [];
|
|
1150
1206
|
const result = new Set(configured);
|
|
@@ -1661,7 +1717,7 @@ function getNamedExportsViaRegex(source, filePath, visited, scanState = { comple
|
|
|
1661
1717
|
}
|
|
1662
1718
|
const name = asMatch[1];
|
|
1663
1719
|
if (isValidEsmExportName(name)) names.add(name);
|
|
1664
|
-
else scanState.complete = false;
|
|
1720
|
+
else if (name === "default" || name === "__esModule") {} else scanState.complete = false;
|
|
1665
1721
|
}
|
|
1666
1722
|
}
|
|
1667
1723
|
const namespaceReExportRegex = new RegExp(`export\\s+\\*\\s+as\\s+(${JS_IDENTIFIER_PATTERN})\\s+from\\s+['"][^'"]+['"]`, "gu");
|
|
@@ -1852,8 +1908,8 @@ function getDependencyNames(packageJson) {
|
|
|
1852
1908
|
}
|
|
1853
1909
|
return Array.from(names);
|
|
1854
1910
|
}
|
|
1855
|
-
function isSharedSingletonConsumedByPeer(pkg) {
|
|
1856
|
-
const shared =
|
|
1911
|
+
function isSharedSingletonConsumedByPeer(pkg, options = getNormalizeModuleFederationOptions()) {
|
|
1912
|
+
const shared = options?.shared || {};
|
|
1857
1913
|
if (Object.entries(shared).some(([key, item]) => key !== pkg && key.startsWith(`${pkg}/`) && item.shareConfig.singleton === true)) return true;
|
|
1858
1914
|
const sharedKeyByPackageName = /* @__PURE__ */ new Map();
|
|
1859
1915
|
Object.entries(shared).filter(([, item]) => item.shareConfig.singleton === true).forEach(([key]) => {
|
|
@@ -1874,8 +1930,7 @@ function isSharedSingletonConsumedByPeer(pkg) {
|
|
|
1874
1930
|
};
|
|
1875
1931
|
return Array.from(sharedKeyByPackageName.values()).some((sharedPkg) => sharedPkg !== pkg && reachesPkg(sharedPkg, new Set([sharedPkg])));
|
|
1876
1932
|
}
|
|
1877
|
-
function isRemoteOnlyContainer() {
|
|
1878
|
-
const options = getNormalizeModuleFederationOptions();
|
|
1933
|
+
function isRemoteOnlyContainer(options = getNormalizeModuleFederationOptions()) {
|
|
1879
1934
|
return Object.keys(options.exposes || {}).length > 0 && Object.keys(options.remotes || {}).length === 0;
|
|
1880
1935
|
}
|
|
1881
1936
|
function tryResolveImportFromPackageRoot(pkg, root) {
|
|
@@ -1898,13 +1953,42 @@ function getConcreteSharedImportSource(pkg, shareItem) {
|
|
|
1898
1953
|
}
|
|
1899
1954
|
return tryResolveImportFromPackageRoot(pkg, currentDir);
|
|
1900
1955
|
}
|
|
1901
|
-
const preBuildCacheMap = {};
|
|
1902
|
-
const preBuildShareItemMap = {};
|
|
1903
1956
|
const PREBUILD_TAG = "__prebuild__";
|
|
1904
|
-
const treeShakingProviderCacheMap = {};
|
|
1905
|
-
const materializedTreeShakingProviders = /* @__PURE__ */ new Set();
|
|
1906
1957
|
const TREE_SHAKING_PROVIDER_TAG = "__treeShakingProvider__";
|
|
1907
1958
|
const TREE_SHAKING_GRAPH_QUERY = "__mf_tree_shaking_graph__";
|
|
1959
|
+
const legacySharedVirtualModuleState = {
|
|
1960
|
+
preBuildCacheMap: {},
|
|
1961
|
+
preBuildShareItemMap: {},
|
|
1962
|
+
treeShakingProviderCacheMap: {},
|
|
1963
|
+
materializedTreeShakingProviders: /* @__PURE__ */ new Set(),
|
|
1964
|
+
loadShareCacheMap: {}
|
|
1965
|
+
};
|
|
1966
|
+
const sharedVirtualModuleStates = /* @__PURE__ */ new WeakMap();
|
|
1967
|
+
let nextSharedVirtualModuleOwnerId = 1;
|
|
1968
|
+
function getSharedVirtualModuleState(options) {
|
|
1969
|
+
if (!options) try {
|
|
1970
|
+
const currentOptions = getNormalizeModuleFederationOptions();
|
|
1971
|
+
return sharedVirtualModuleStates.get(currentOptions) ?? legacySharedVirtualModuleState;
|
|
1972
|
+
} catch {
|
|
1973
|
+
return legacySharedVirtualModuleState;
|
|
1974
|
+
}
|
|
1975
|
+
let state = sharedVirtualModuleStates.get(options);
|
|
1976
|
+
if (!state) {
|
|
1977
|
+
state = {
|
|
1978
|
+
preBuildCacheMap: {},
|
|
1979
|
+
preBuildShareItemMap: {},
|
|
1980
|
+
treeShakingProviderCacheMap: {},
|
|
1981
|
+
materializedTreeShakingProviders: /* @__PURE__ */ new Set(),
|
|
1982
|
+
loadShareCacheMap: {},
|
|
1983
|
+
ownerKey: `${options.internalName}__mf_owner__${nextSharedVirtualModuleOwnerId++}`
|
|
1984
|
+
};
|
|
1985
|
+
sharedVirtualModuleStates.set(options, state);
|
|
1986
|
+
}
|
|
1987
|
+
return state;
|
|
1988
|
+
}
|
|
1989
|
+
function createScopedSharedVirtualModule(pkg, tag, options) {
|
|
1990
|
+
return new VirtualModule(pkg, tag, ".js", getSharedVirtualModuleState(options).ownerKey);
|
|
1991
|
+
}
|
|
1908
1992
|
function getTreeShakingGraphToken(id) {
|
|
1909
1993
|
if (!id) return void 0;
|
|
1910
1994
|
const queryStart = id.indexOf("?");
|
|
@@ -1935,19 +2019,21 @@ function addTreeShakingGraphQuery(id, token) {
|
|
|
1935
2019
|
const hash = hashStart === -1 ? "" : cleanId.slice(hashStart);
|
|
1936
2020
|
return `${base}${base.includes("?") ? "&" : "?"}${TREE_SHAKING_GRAPH_QUERY}=${encodeURIComponent(token)}${hash}`;
|
|
1937
2021
|
}
|
|
1938
|
-
function getConcreteTreeShakingExportUsage(pkg, shareItem) {
|
|
1939
|
-
return getTreeShakingExportUsage(pkg, shareItem, shareItem?.name);
|
|
2022
|
+
function getConcreteTreeShakingExportUsage(pkg, shareItem, options) {
|
|
2023
|
+
return getTreeShakingExportUsage(pkg, shareItem, shareItem?.name, options);
|
|
1940
2024
|
}
|
|
1941
|
-
function getTreeShakingSharedProviderName(pkg) {
|
|
1942
|
-
const
|
|
1943
|
-
return `${internalName
|
|
2025
|
+
function getTreeShakingSharedProviderName(pkg, options) {
|
|
2026
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2027
|
+
return `${getSharedVirtualModuleState(options).ownerKey ?? resolvedOptions.internalName ?? resolvedOptions.name}__tree_shaking__${packageNameEncode(pkg)}`;
|
|
1944
2028
|
}
|
|
1945
|
-
function getTreeShakingSharedProviderImportId(pkg) {
|
|
1946
|
-
|
|
2029
|
+
function getTreeShakingSharedProviderImportId(pkg, options) {
|
|
2030
|
+
const { treeShakingProviderCacheMap } = getSharedVirtualModuleState(options);
|
|
2031
|
+
if (!treeShakingProviderCacheMap[pkg]) treeShakingProviderCacheMap[pkg] = createScopedSharedVirtualModule(pkg, TREE_SHAKING_PROVIDER_TAG, options);
|
|
1947
2032
|
return treeShakingProviderCacheMap[pkg].getImportId();
|
|
1948
2033
|
}
|
|
1949
|
-
function hasTreeShakingSharedProvider(pkg, shareItem) {
|
|
1950
|
-
const
|
|
2034
|
+
function hasTreeShakingSharedProvider(pkg, shareItem, options) {
|
|
2035
|
+
const { materializedTreeShakingProviders } = getSharedVirtualModuleState(options);
|
|
2036
|
+
const usage = getConcreteTreeShakingExportUsage(pkg, shareItem, options);
|
|
1951
2037
|
return materializedTreeShakingProviders.has(pkg) && usage?.kind === "exports";
|
|
1952
2038
|
}
|
|
1953
2039
|
/**
|
|
@@ -1959,8 +2045,9 @@ function hasTreeShakingSharedProvider(pkg, shareItem) {
|
|
|
1959
2045
|
* perform its normal usedExports compatibility check and safely choose the full
|
|
1960
2046
|
* provider when the optimized one is insufficient.
|
|
1961
2047
|
*/
|
|
1962
|
-
function writeTreeShakingSharedProvider(pkg, shareItem) {
|
|
1963
|
-
const
|
|
2048
|
+
function writeTreeShakingSharedProvider(pkg, shareItem, options) {
|
|
2049
|
+
const { materializedTreeShakingProviders, treeShakingProviderCacheMap } = getSharedVirtualModuleState(options);
|
|
2050
|
+
const usage = getConcreteTreeShakingExportUsage(pkg, shareItem, options);
|
|
1964
2051
|
if (usage?.kind !== "exports" || !usage.usedExports.length || shareItem?.shareConfig.import === false) {
|
|
1965
2052
|
materializedTreeShakingProviders.delete(pkg);
|
|
1966
2053
|
return;
|
|
@@ -1972,7 +2059,7 @@ function writeTreeShakingSharedProvider(pkg, shareItem) {
|
|
|
1972
2059
|
mfWarn(`Tree-shaking shared dependency "${pkg}" was disabled because export "${unsupportedExport}" cannot be represented by the generated ESM provider.`);
|
|
1973
2060
|
return;
|
|
1974
2061
|
}
|
|
1975
|
-
const provider = treeShakingProviderCacheMap[pkg] || (treeShakingProviderCacheMap[pkg] =
|
|
2062
|
+
const provider = treeShakingProviderCacheMap[pkg] || (treeShakingProviderCacheMap[pkg] = createScopedSharedVirtualModule(pkg, "__treeShakingProvider__", options));
|
|
1976
2063
|
const optimizedImportSource = addTreeShakingGraphQuery(getConcreteSharedImportSource(pkg, shareItem) || pkg, pkg);
|
|
1977
2064
|
const namedExports = usedExports.filter((name) => name !== "default");
|
|
1978
2065
|
const namedImports = namedExports.map((name, index) => `${name} as __mfTreeShaken_${index}`).join(", ");
|
|
@@ -1994,11 +2081,12 @@ export default { get, init };
|
|
|
1994
2081
|
`, true);
|
|
1995
2082
|
materializedTreeShakingProviders.add(pkg);
|
|
1996
2083
|
}
|
|
1997
|
-
function writePreBuildLibPath(pkg, shareItem) {
|
|
1998
|
-
|
|
2084
|
+
function writePreBuildLibPath(pkg, shareItem, options) {
|
|
2085
|
+
const { preBuildCacheMap, preBuildShareItemMap } = getSharedVirtualModuleState(options);
|
|
2086
|
+
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = createScopedSharedVirtualModule(pkg, PREBUILD_TAG, options);
|
|
1999
2087
|
preBuildShareItemMap[pkg] = shareItem;
|
|
2000
2088
|
const importSource = getConcreteSharedImportSource(pkg, shareItem) || pkg;
|
|
2001
|
-
writeTreeShakingSharedProvider(pkg, shareItem);
|
|
2089
|
+
writeTreeShakingSharedProvider(pkg, shareItem, options);
|
|
2002
2090
|
if (pkg === "react/compiler-runtime") {
|
|
2003
2091
|
const reactCacheDescriptor = getSharedCacheDescriptorLiteral("react", shareItem ?? {
|
|
2004
2092
|
name: "react",
|
|
@@ -2067,36 +2155,36 @@ function writePreBuildLibPath(pkg, shareItem) {
|
|
|
2067
2155
|
`, true);
|
|
2068
2156
|
}
|
|
2069
2157
|
/** Re-render already materialized wrappers after import analysis discovers exports. */
|
|
2070
|
-
function refreshTreeShakingModules() {
|
|
2158
|
+
function refreshTreeShakingModules(options) {
|
|
2159
|
+
const { preBuildShareItemMap } = getSharedVirtualModuleState(options);
|
|
2071
2160
|
for (const [pkg, shareItem] of Object.entries(preBuildShareItemMap)) {
|
|
2072
2161
|
if (!shareItem?.shareConfig.treeShaking) continue;
|
|
2073
|
-
writePreBuildLibPath(pkg, shareItem);
|
|
2074
|
-
writeLoadShareModule(pkg, shareItem, "build", false);
|
|
2162
|
+
writePreBuildLibPath(pkg, shareItem, options);
|
|
2163
|
+
writeLoadShareModule(pkg, shareItem, "build", false, options);
|
|
2075
2164
|
}
|
|
2076
2165
|
}
|
|
2077
|
-
function getPreBuildLibImportId(pkg) {
|
|
2078
|
-
|
|
2166
|
+
function getPreBuildLibImportId(pkg, options) {
|
|
2167
|
+
const { preBuildCacheMap } = getSharedVirtualModuleState(options);
|
|
2168
|
+
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = createScopedSharedVirtualModule(pkg, PREBUILD_TAG, options);
|
|
2079
2169
|
return preBuildCacheMap[pkg].getImportId();
|
|
2080
2170
|
}
|
|
2081
|
-
function getPreBuildShareItem(pkg) {
|
|
2082
|
-
return preBuildShareItemMap[pkg];
|
|
2171
|
+
function getPreBuildShareItem(pkg, options) {
|
|
2172
|
+
return getSharedVirtualModuleState(options).preBuildShareItemMap[pkg];
|
|
2083
2173
|
}
|
|
2084
|
-
function getSharedImportSource(pkg, shareItem) {
|
|
2085
|
-
return getConcreteSharedImportSource(pkg, shareItem) || getPreBuildLibImportId(pkg);
|
|
2174
|
+
function getSharedImportSource(pkg, shareItem, options) {
|
|
2175
|
+
return getConcreteSharedImportSource(pkg, shareItem) || getPreBuildLibImportId(pkg, options);
|
|
2086
2176
|
}
|
|
2087
2177
|
const LOAD_SHARE_TAG = "__loadShare__";
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] =
|
|
2178
|
+
function getLoadShareImportId(pkg, _isRolldown, options) {
|
|
2179
|
+
const { loadShareCacheMap } = getSharedVirtualModuleState(options);
|
|
2180
|
+
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = createScopedSharedVirtualModule(pkg, LOAD_SHARE_TAG, options);
|
|
2091
2181
|
return loadShareCacheMap[pkg].getImportId();
|
|
2092
2182
|
}
|
|
2093
|
-
function getLoadShareModulePath(pkg, isRolldown) {
|
|
2094
|
-
|
|
2183
|
+
function getLoadShareModulePath(pkg, isRolldown, options) {
|
|
2184
|
+
const { loadShareCacheMap } = getSharedVirtualModuleState(options);
|
|
2185
|
+
if (!loadShareCacheMap[pkg]) getLoadShareImportId(pkg, isRolldown, options);
|
|
2095
2186
|
return loadShareCacheMap[pkg].getImportId();
|
|
2096
2187
|
}
|
|
2097
|
-
function toViteOptimizedDepVirtualId(id) {
|
|
2098
|
-
return toViteEncodedId(id);
|
|
2099
|
-
}
|
|
2100
2188
|
function getCachedLoadSharePkg(id) {
|
|
2101
2189
|
if (!id.includes("__loadShare__")) return;
|
|
2102
2190
|
const normalized = normalizeVirtualModuleId(id);
|
|
@@ -2114,8 +2202,8 @@ function materializeCachedLoadShareModule(options) {
|
|
|
2114
2202
|
const key = options.findSharedKey(pkg, options.shared);
|
|
2115
2203
|
if (!key) return;
|
|
2116
2204
|
const shareItem = options.shared[key];
|
|
2117
|
-
writeLoadShareModule(pkg, shareItem, options.command, options.isRolldown);
|
|
2118
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(pkg, shareItem);
|
|
2205
|
+
writeLoadShareModule(pkg, shareItem, options.command, options.isRolldown, options.federationOptions);
|
|
2206
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(pkg, shareItem, options.federationOptions);
|
|
2119
2207
|
options.addUsedShares(pkg);
|
|
2120
2208
|
options.writeLocalSharedImportMap();
|
|
2121
2209
|
}
|
|
@@ -2242,12 +2330,15 @@ const normalizeLocalShareModuleCode = `const __mfNormalizeShareModule = (mod) =>
|
|
|
2242
2330
|
? Object.assign({}, normalized)
|
|
2243
2331
|
: normalized;
|
|
2244
2332
|
};`;
|
|
2245
|
-
function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
2246
|
-
|
|
2333
|
+
function writeLoadShareModule(pkg, shareItem, command, _isRolldown, options) {
|
|
2334
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2335
|
+
const { loadShareCacheMap } = getSharedVirtualModuleState(options);
|
|
2336
|
+
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = createScopedSharedVirtualModule(pkg, LOAD_SHARE_TAG, options);
|
|
2247
2337
|
let importLine = getRuntimeModuleCacheBootstrapCode();
|
|
2248
2338
|
const cacheDescriptor = getSharedCacheDescriptorLiteral(pkg, shareItem);
|
|
2249
|
-
const cacheOwner = JSON.stringify(
|
|
2250
|
-
const
|
|
2339
|
+
const cacheOwner = JSON.stringify(resolvedOptions.name);
|
|
2340
|
+
const runtimeInitOwnerImportId = options ? getRuntimeInitStatusImportId(options) : void 0;
|
|
2341
|
+
const treeShakingConsumer = command === "build" && shareItem.shareConfig.treeShaking ? resolvedOptions.name : void 0;
|
|
2251
2342
|
if (shareItem.shareConfig.import === false) {
|
|
2252
2343
|
const detectedNamedExports = getPackageNamedExports(pkg);
|
|
2253
2344
|
const namedExports = detectedNamedExports ?? [];
|
|
@@ -2258,7 +2349,7 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
2258
2349
|
exportLine = generateDeferredHostProvidedExports([], pkg, cacheDescriptor, treeShakingConsumer);
|
|
2259
2350
|
}
|
|
2260
2351
|
loadShareCacheMap[pkg].writeSync(`
|
|
2261
|
-
${getRuntimeInitPromiseBootstrapCode()}
|
|
2352
|
+
${getRuntimeInitPromiseBootstrapCode(false, runtimeInitOwnerImportId)}
|
|
2262
2353
|
${importLine}
|
|
2263
2354
|
${sharedCacheHelperCode}
|
|
2264
2355
|
${exportLine}
|
|
@@ -2266,7 +2357,7 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
2266
2357
|
return;
|
|
2267
2358
|
}
|
|
2268
2359
|
const concreteSharedImportSource = getConcreteSharedImportSource(pkg, shareItem);
|
|
2269
|
-
const sharedImportSource = concreteSharedImportSource || getPreBuildLibImportId(pkg);
|
|
2360
|
+
const sharedImportSource = concreteSharedImportSource || getPreBuildLibImportId(pkg, options);
|
|
2270
2361
|
const devImportSource = concreteSharedImportSource || pkg;
|
|
2271
2362
|
const localProviderPath = getLocalProviderImportPath(pkg);
|
|
2272
2363
|
const coherentLocalSource = concreteSharedImportSource || localProviderPath || devImportSource;
|
|
@@ -2278,20 +2369,20 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
2278
2369
|
const hasCompleteExportCoverage = detectedNamedExports !== void 0;
|
|
2279
2370
|
const isWorkspaceSingleton = isWorkspacePackage && shareItem.shareConfig.singleton === true;
|
|
2280
2371
|
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 &&
|
|
2372
|
+
const usesDeferredSingletonFallback = hasCompleteExportCoverage && (isWorkspaceSingleton || command !== "build" && isRemoteOnlyContainer(resolvedOptions) && shareItem.shareConfig.singleton === true || command === "build" && isRemoteOnlyContainer(resolvedOptions) && shareItem.shareConfig.singleton === true && !isDefaultShareScope);
|
|
2373
|
+
const servesRemoteSingletonFallback = command !== "build" && isRemoteOnlyContainer(resolvedOptions) && shareItem.shareConfig.singleton === true;
|
|
2374
|
+
const isConsumedByPeerSingleton = isSharedSingletonConsumedByPeer(pkg, resolvedOptions);
|
|
2375
|
+
const usesEntryInjectedRemoteFallback = hasCompleteExportCoverage && command !== "build" && !isWorkspaceSingleton && isRemoteOnlyContainer(resolvedOptions) && shareItem.shareConfig.singleton === true && resolvedOptions.hostInitInjectLocation === "entry" && isConsumedByPeerSingleton;
|
|
2285
2376
|
const usesEagerWorkspaceFallback = hasCompleteExportCoverage && isWorkspaceSingleton && isConsumedByPeerSingleton;
|
|
2286
2377
|
const usesDeferredTreeShakingFallback = hasCompleteExportCoverage && Boolean(treeShakingConsumer);
|
|
2287
2378
|
let exportLine;
|
|
2288
2379
|
let initBlock = "";
|
|
2289
2380
|
if (usesDeferredTreeShakingFallback) {
|
|
2290
|
-
importLine = `${getRuntimeInitPromiseBootstrapCode()}\n ${importLine}`;
|
|
2381
|
+
importLine = `${getRuntimeInitPromiseBootstrapCode(false, runtimeInitOwnerImportId)}\n ${importLine}`;
|
|
2291
2382
|
exportLine = generateLazyWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor, cacheOwner, treeShakingConsumer, command !== "build" && (isWorkspaceSingleton || isWorkspacePackage || servesRemoteSingletonFallback));
|
|
2292
2383
|
} else if (usesEagerWorkspaceFallback || usesEntryInjectedRemoteFallback) exportLine = generateEagerWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor, cacheOwner, treeShakingConsumer);
|
|
2293
2384
|
else if (usesDeferredSingletonFallback) {
|
|
2294
|
-
importLine = `${getRuntimeInitPromiseBootstrapCode()}\n ${importLine}`;
|
|
2385
|
+
importLine = `${getRuntimeInitPromiseBootstrapCode(false, runtimeInitOwnerImportId)}\n ${importLine}`;
|
|
2295
2386
|
exportLine = generateLazyWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor, cacheOwner, treeShakingConsumer, command !== "build" && (isWorkspaceSingleton || isWorkspacePackage || servesRemoteSingletonFallback));
|
|
2296
2387
|
} else if (detectedNamedExports === void 0) {
|
|
2297
2388
|
exportLine = `const __mfDefaultExport = (() => {
|
|
@@ -2380,74 +2471,98 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
2380
2471
|
//#endregion
|
|
2381
2472
|
//#region src/virtualModules/virtualRemoteEntry.ts
|
|
2382
2473
|
let usedShares = /* @__PURE__ */ new Set();
|
|
2383
|
-
|
|
2474
|
+
const usedSharesByOptions = /* @__PURE__ */ new WeakMap();
|
|
2475
|
+
function getScopedUsedShares(options) {
|
|
2476
|
+
let scoped = usedSharesByOptions.get(options);
|
|
2477
|
+
if (!scoped) {
|
|
2478
|
+
scoped = /* @__PURE__ */ new Set();
|
|
2479
|
+
usedSharesByOptions.set(options, scoped);
|
|
2480
|
+
}
|
|
2481
|
+
return scoped;
|
|
2482
|
+
}
|
|
2483
|
+
function getUsedShares(options) {
|
|
2484
|
+
if (options) return getScopedUsedShares(options);
|
|
2384
2485
|
return usedShares;
|
|
2385
2486
|
}
|
|
2386
|
-
function addUsedShares(pkg) {
|
|
2487
|
+
function addUsedShares(pkg, options) {
|
|
2387
2488
|
usedShares.add(pkg);
|
|
2489
|
+
if (options) getScopedUsedShares(options).add(pkg);
|
|
2388
2490
|
}
|
|
2389
2491
|
const LOCAL_SHARED_IMPORT_MAP_ID = "virtual:mf-localSharedImportMap";
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2492
|
+
const localOwnerIds = /* @__PURE__ */ new WeakMap();
|
|
2493
|
+
let nextLocalOwnerId = 1;
|
|
2494
|
+
function getLocalOwnerKey(options) {
|
|
2495
|
+
let ownerId = localOwnerIds.get(options);
|
|
2496
|
+
if (!ownerId) {
|
|
2497
|
+
ownerId = nextLocalOwnerId++;
|
|
2498
|
+
localOwnerIds.set(options, ownerId);
|
|
2499
|
+
}
|
|
2500
|
+
return `${options.internalName}__mf_owner__${ownerId}`;
|
|
2393
2501
|
}
|
|
2394
|
-
function
|
|
2395
|
-
|
|
2502
|
+
function getLocalSharedImportMapPath(options) {
|
|
2503
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2504
|
+
return `${LOCAL_SHARED_IMPORT_MAP_ID}:${packageNameEncode(options ? getLocalOwnerKey(resolvedOptions) : resolvedOptions.internalName || resolvedOptions.name)}`;
|
|
2505
|
+
}
|
|
2506
|
+
function getResolvedLocalSharedImportMapId(options) {
|
|
2507
|
+
return `\0${getLocalSharedImportMapPath(options)}`;
|
|
2396
2508
|
}
|
|
2397
2509
|
let invalidateLocalSharedImportMap;
|
|
2398
|
-
|
|
2399
|
-
|
|
2510
|
+
const localSharedImportMapInvalidators = /* @__PURE__ */ new WeakMap();
|
|
2511
|
+
function setLocalSharedImportMapInvalidator(invalidator, options) {
|
|
2512
|
+
if (!options) invalidateLocalSharedImportMap = invalidator;
|
|
2513
|
+
else if (invalidator) localSharedImportMapInvalidators.set(options, invalidator);
|
|
2514
|
+
else localSharedImportMapInvalidators.delete(options);
|
|
2400
2515
|
}
|
|
2401
|
-
function writeLocalSharedImportMap() {
|
|
2402
|
-
invalidateLocalSharedImportMap?.();
|
|
2516
|
+
function writeLocalSharedImportMap(options) {
|
|
2517
|
+
(options ? localSharedImportMapInvalidators.get(options) : invalidateLocalSharedImportMap)?.();
|
|
2403
2518
|
}
|
|
2404
2519
|
function shouldUseDirectReactImport() {
|
|
2405
2520
|
const isVinext = hasPackageDependency("vinext");
|
|
2406
2521
|
const isAstro = hasPackageDependency("astro");
|
|
2407
2522
|
return isVinext || isAstro;
|
|
2408
2523
|
}
|
|
2409
|
-
function getLocalSharedPackagePath(pkg, shareItem) {
|
|
2524
|
+
function getLocalSharedPackagePath(pkg, shareItem, options) {
|
|
2410
2525
|
if (shouldUseDirectReactImport() && pkg === "react") return "react";
|
|
2411
|
-
return getConcreteSharedImportSource(pkg, shareItem) || getLocalProviderImportPath(pkg) || getSharedImportSource(pkg, shareItem);
|
|
2526
|
+
return getConcreteSharedImportSource(pkg, shareItem) || getLocalProviderImportPath(pkg) || getSharedImportSource(pkg, shareItem, options);
|
|
2412
2527
|
}
|
|
2413
2528
|
function getDirectSharedCacheSeedImportPath(pkg, shareItem) {
|
|
2414
2529
|
return getConcreteSharedImportSource(pkg, shareItem) || getProjectResolvedImportPath(pkg) || getLocalProviderImportPath(pkg) || pkg;
|
|
2415
2530
|
}
|
|
2416
|
-
function generateLocalSharedImportMap() {
|
|
2531
|
+
function generateLocalSharedImportMap(options) {
|
|
2532
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2417
2533
|
const useDirectReactImport = shouldUseDirectReactImport();
|
|
2418
|
-
const
|
|
2419
|
-
const orderedShares = getOrderedUsedShares();
|
|
2534
|
+
const orderedShares = getOrderedUsedShares(options);
|
|
2420
2535
|
return `
|
|
2421
2536
|
import {loadShare} from "@module-federation/runtime";
|
|
2422
2537
|
${orderedShares.map((pkg, index) => {
|
|
2423
|
-
const shareItem = getNormalizeShareItem(pkg);
|
|
2538
|
+
const shareItem = getNormalizeShareItem(pkg, resolvedOptions);
|
|
2424
2539
|
if (!shareItem?.shareConfig.eager || shareItem.shareConfig.import === false) return "";
|
|
2425
|
-
return `import * as __mfEagerShare_${index} from ${JSON.stringify(getLocalSharedPackagePath(pkg, shareItem))};`;
|
|
2540
|
+
return `import * as __mfEagerShare_${index} from ${JSON.stringify(getLocalSharedPackagePath(pkg, shareItem, options))};`;
|
|
2426
2541
|
}).filter(Boolean).join("\n")}
|
|
2427
2542
|
const importMap = {
|
|
2428
2543
|
${orderedShares.map((pkg, index) => {
|
|
2429
|
-
const shareItem = getNormalizeShareItem(pkg);
|
|
2544
|
+
const shareItem = getNormalizeShareItem(pkg, resolvedOptions);
|
|
2430
2545
|
return `
|
|
2431
2546
|
${JSON.stringify(pkg)}: async () => {
|
|
2432
2547
|
${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))});
|
|
2548
|
+
return pkg;` : `let pkg = await import(${JSON.stringify(getLocalSharedPackagePath(pkg, shareItem, options))});
|
|
2434
2549
|
return pkg;`}
|
|
2435
2550
|
}
|
|
2436
2551
|
`;
|
|
2437
2552
|
}).join(",")}
|
|
2438
2553
|
}
|
|
2439
2554
|
const usedShared = {
|
|
2440
|
-
${getOrderedUsedShares().map((key) => {
|
|
2441
|
-
const shareItem = getNormalizeShareItem(key);
|
|
2555
|
+
${getOrderedUsedShares(options).map((key) => {
|
|
2556
|
+
const shareItem = getNormalizeShareItem(key, resolvedOptions);
|
|
2442
2557
|
if (!shareItem) return null;
|
|
2443
2558
|
const detectedNamedExports = getSharedNamedExports(key, shareItem);
|
|
2444
2559
|
const canLiveRebind = shareItem.shareConfig.import === false || detectedNamedExports !== void 0;
|
|
2445
2560
|
const treeShakingConfig = canLiveRebind ? shareItem.shareConfig.treeShaking : void 0;
|
|
2446
|
-
const treeShakingUsage = treeShakingConfig ? getTreeShakingExportUsage(key, shareItem, shareItem.name) : void 0;
|
|
2561
|
+
const treeShakingUsage = treeShakingConfig ? getTreeShakingExportUsage(key, shareItem, shareItem.name, options) : void 0;
|
|
2447
2562
|
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;
|
|
2563
|
+
const treeShakingUsedExports = resolvedOptions.injectTreeShakingUsedExports === false ? treeShakingConfig?.usedExports || [] : treeShakingProviderExports;
|
|
2564
|
+
const disableRuntimeInference = treeShakingConfig?.mode === "runtime-infer" && resolvedOptions.injectTreeShakingUsedExports === false;
|
|
2565
|
+
const treeShakingProviderImportId = treeShakingConfig && !disableRuntimeInference && hasTreeShakingSharedProvider(key, shareItem, options) ? getTreeShakingSharedProviderImportId(key, options) : void 0;
|
|
2451
2566
|
const treeShakingStatus = treeShakingUsage?.kind === "full" || disableRuntimeInference || treeShakingConfig?.mode === "runtime-infer" && !treeShakingProviderImportId && shareItem.shareConfig.import !== false ? 0 : 1;
|
|
2452
2567
|
return `
|
|
2453
2568
|
${JSON.stringify(key)}: {
|
|
@@ -2456,7 +2571,7 @@ function generateLocalSharedImportMap() {
|
|
|
2456
2571
|
scope: [${JSON.stringify(shareItem.scope)}],
|
|
2457
2572
|
loaded: false,
|
|
2458
2573
|
eager: ${Boolean(shareItem.shareConfig.eager)},
|
|
2459
|
-
from: ${JSON.stringify(
|
|
2574
|
+
from: ${JSON.stringify(resolvedOptions.name)},
|
|
2460
2575
|
canLiveRebind: ${canLiveRebind},
|
|
2461
2576
|
async get () {
|
|
2462
2577
|
if (${shareItem.shareConfig.import === false}) {
|
|
@@ -2501,14 +2616,14 @@ function generateLocalSharedImportMap() {
|
|
|
2501
2616
|
`;
|
|
2502
2617
|
}).filter((x) => x !== null).join(",")}
|
|
2503
2618
|
}
|
|
2504
|
-
const usedRemotes = [${Object.keys(getUsedRemotesMap()).map((key) => {
|
|
2505
|
-
const remote =
|
|
2619
|
+
const usedRemotes = [${Object.keys(getUsedRemotesMap(options)).map((key) => {
|
|
2620
|
+
const remote = resolvedOptions.remotes[key];
|
|
2506
2621
|
if (!remote) return null;
|
|
2507
2622
|
return `
|
|
2508
2623
|
{
|
|
2509
|
-
alias: ${JSON.stringify(key)},
|
|
2624
|
+
alias: ${JSON.stringify(getRuntimeRemoteAlias(key, options))},
|
|
2510
2625
|
entryGlobalName: ${JSON.stringify(remote.entryGlobalName)},
|
|
2511
|
-
name: ${JSON.stringify(remote.name)},
|
|
2626
|
+
name: ${JSON.stringify(options ? getRuntimeRemoteAlias(key, options) : remote.name)},
|
|
2512
2627
|
type: ${JSON.stringify(remote.type)},
|
|
2513
2628
|
entry: ${JSON.stringify(remote.entry)},
|
|
2514
2629
|
shareScope: ${JSON.stringify(remote.shareScope ?? "default")},
|
|
@@ -2522,16 +2637,12 @@ function generateLocalSharedImportMap() {
|
|
|
2522
2637
|
}
|
|
2523
2638
|
`;
|
|
2524
2639
|
}
|
|
2525
|
-
function getOrderedUsedShares() {
|
|
2526
|
-
const
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
return;
|
|
2532
|
-
}
|
|
2533
|
-
});
|
|
2534
|
-
} catch {}
|
|
2640
|
+
function getOrderedUsedShares(options) {
|
|
2641
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
2642
|
+
const shares = new Set(getUsedShares(options));
|
|
2643
|
+
Object.keys(resolvedOptions.shared).forEach((pkg) => {
|
|
2644
|
+
if (!pkg.endsWith("/")) shares.add(pkg);
|
|
2645
|
+
});
|
|
2535
2646
|
return orderSharedDependenciesFirst(Array.from(shares).sort((a, b) => {
|
|
2536
2647
|
const priority = (pkg) => pkg === "react" ? 0 : pkg === "react-dom" ? 1 : pkg.startsWith("react/") ? 2 : 3;
|
|
2537
2648
|
return priority(a) - priority(b) || a.localeCompare(b);
|
|
@@ -2585,15 +2696,15 @@ function orderSharedDependenciesFirst(sharedPackages) {
|
|
|
2585
2696
|
sharedPackages.forEach(visit);
|
|
2586
2697
|
return ordered;
|
|
2587
2698
|
}
|
|
2588
|
-
function getShareItemForPreload(pkg) {
|
|
2589
|
-
const shared =
|
|
2699
|
+
function getShareItemForPreload(pkg, options = getNormalizeModuleFederationOptions()) {
|
|
2700
|
+
const shared = options.shared;
|
|
2590
2701
|
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];
|
|
2702
|
+
if (isExplicitSharedKey(pkg, options)) return shared[pkg];
|
|
2703
|
+
if (isExplicitSharedKey(wildcardKey, options)) return shared[wildcardKey];
|
|
2593
2704
|
}
|
|
2594
|
-
function generateSharedCacheSeedItem(pkg, shareItem, importPath) {
|
|
2705
|
+
function generateSharedCacheSeedItem(pkg, shareItem, importPath, options = getNormalizeModuleFederationOptions()) {
|
|
2595
2706
|
const cacheDescriptor = getSharedCacheDescriptor(pkg, shareItem);
|
|
2596
|
-
const cacheOwner =
|
|
2707
|
+
const cacheOwner = options.name;
|
|
2597
2708
|
return `if (__mfReadSharedCache(__mfModuleCache.share, ${JSON.stringify(cacheDescriptor)}) === undefined) {
|
|
2598
2709
|
const mod = await import(${JSON.stringify(importPath)});
|
|
2599
2710
|
${normalizeRuntimeShareCode}
|
|
@@ -2886,10 +2997,11 @@ function getBrowserImportPath(importPath) {
|
|
|
2886
2997
|
if (/^(?:[a-zA-Z]:[\\/]|\/)/.test(importPath) && !importPath.startsWith("/@")) return `/@fs/${importPath}`;
|
|
2887
2998
|
return importPath;
|
|
2888
2999
|
}
|
|
2889
|
-
function getHostAutoInitSharedSeedItems() {
|
|
2890
|
-
|
|
3000
|
+
function getHostAutoInitSharedSeedItems(options) {
|
|
3001
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
3002
|
+
return getOrderedUsedShares(options).map((pkg) => ({
|
|
2891
3003
|
pkg,
|
|
2892
|
-
shareItem: getShareItemForPreload(pkg)
|
|
3004
|
+
shareItem: getShareItemForPreload(pkg, resolvedOptions)
|
|
2893
3005
|
})).filter(({ shareItem }) => shareItem?.shareConfig?.import === false).sort((a, b) => {
|
|
2894
3006
|
const priority = (pkg) => pkg === "vue" ? 0 : pkg === "pinia" ? 1 : 2;
|
|
2895
3007
|
const aIsLocal = !!getLocalProviderImportPath(a.pkg);
|
|
@@ -2897,11 +3009,12 @@ function getHostAutoInitSharedSeedItems() {
|
|
|
2897
3009
|
return priority(a.pkg) - priority(b.pkg) || Number(aIsLocal) - Number(bIsLocal) || a.pkg.localeCompare(b.pkg);
|
|
2898
3010
|
});
|
|
2899
3011
|
}
|
|
2900
|
-
function generateHostAutoInitSharedCacheSeedCode(command = "build") {
|
|
3012
|
+
function generateHostAutoInitSharedCacheSeedCode(command = "build", options) {
|
|
2901
3013
|
if (command === "build") return "";
|
|
2902
|
-
|
|
3014
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
3015
|
+
return getHostAutoInitSharedSeedItems(options).map(({ pkg, shareItem }) => {
|
|
2903
3016
|
if (!shareItem) return null;
|
|
2904
|
-
return generateSharedCacheSeedItem(pkg, shareItem, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
3017
|
+
return generateSharedCacheSeedItem(pkg, shareItem, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)), resolvedOptions);
|
|
2905
3018
|
}).filter((item) => item !== null).join("\n");
|
|
2906
3019
|
}
|
|
2907
3020
|
const REMOTE_ENTRY_ID = "virtual:mf-REMOTE_ENTRY_ID";
|
|
@@ -3126,10 +3239,10 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
3126
3239
|
globalThis.__VUE_HMR_RUNTIME__ = { createRecord() {}, rerender() {}, reload() {} };
|
|
3127
3240
|
}
|
|
3128
3241
|
import {${runtimeImports}} from "@module-federation/runtime";
|
|
3129
|
-
${hasEagerShared ? `import * as __mfLocalSharedImportMap from "${getLocalSharedImportMapPath()}";` : ""}
|
|
3242
|
+
${hasEagerShared ? `import * as __mfLocalSharedImportMap from "${getLocalSharedImportMapPath(options)}";` : ""}
|
|
3130
3243
|
${runtimeHelperImports.length ? `import {${runtimeHelperImports.join(", ")}} from "@module-federation/runtime/helpers";` : ""}
|
|
3131
3244
|
${pluginImportNames.filter((item) => !isSsrOnlyPlugin(item[1])).map((item) => item[1]).join("\n")}
|
|
3132
|
-
${command === "build" ? getRuntimeInitResolveBootstrapCode() : getRuntimeInitBootstrapCode() + "\n const { initResolve } = globalThis[globalKey];"}
|
|
3245
|
+
${command === "build" ? getRuntimeInitResolveBootstrapCode(false, options ? getRuntimeInitStatusImportId(options) : void 0) : getRuntimeInitBootstrapCode(false, options ? getRuntimeInitStatusImportId(options) : void 0) + "\n const { initResolve } = globalThis[globalKey];"}
|
|
3133
3246
|
${getRuntimeModuleCacheBootstrapCode()}
|
|
3134
3247
|
const initTokens = {}
|
|
3135
3248
|
const shareScopeNames = Array.isArray(${JSON.stringify(options.shareScope)}) ? ${JSON.stringify(options.shareScope)} : [${JSON.stringify(options.shareScope)}]
|
|
@@ -3163,7 +3276,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
3163
3276
|
async function getLocalSharedImportMap() {
|
|
3164
3277
|
${hasEagerShared ? "return __mfLocalSharedImportMap;" : ""}
|
|
3165
3278
|
${hasEagerShared ? "" : `if (!localSharedImportMapPromise) {
|
|
3166
|
-
localSharedImportMapPromise = retrySharedInit(() => import("${getLocalSharedImportMapPath()}"))
|
|
3279
|
+
localSharedImportMapPromise = retrySharedInit(() => import("${getLocalSharedImportMapPath(options)}"))
|
|
3167
3280
|
.catch((e) => { localSharedImportMapPromise = undefined; throw e; });
|
|
3168
3281
|
}
|
|
3169
3282
|
return localSharedImportMapPromise`}
|
|
@@ -3859,13 +3972,35 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
3859
3972
|
}
|
|
3860
3973
|
`;
|
|
3861
3974
|
}
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3975
|
+
/**
|
|
3976
|
+
* Inject entry file, automatically init when used as host,
|
|
3977
|
+
* and will not inject remoteEntry
|
|
3978
|
+
*/
|
|
3979
|
+
const HOST_AUTO_INIT_TAG = "__H_A_I__";
|
|
3980
|
+
const hostAutoInitStates = /* @__PURE__ */ new WeakMap();
|
|
3981
|
+
const legacyHostAutoInitState = {
|
|
3982
|
+
module: new VirtualModule("hostAutoInit", HOST_AUTO_INIT_TAG),
|
|
3983
|
+
remoteEntryId: REMOTE_ENTRY_ID,
|
|
3984
|
+
command: "build"
|
|
3985
|
+
};
|
|
3986
|
+
function getHostAutoInitState(options) {
|
|
3987
|
+
if (!options) return legacyHostAutoInitState;
|
|
3988
|
+
let state = hostAutoInitStates.get(options);
|
|
3989
|
+
if (!state) {
|
|
3990
|
+
state = {
|
|
3991
|
+
module: new VirtualModule("hostAutoInit", HOST_AUTO_INIT_TAG, "", getLocalOwnerKey(options)),
|
|
3992
|
+
remoteEntryId: REMOTE_ENTRY_ID,
|
|
3993
|
+
command: "build"
|
|
3994
|
+
};
|
|
3995
|
+
hostAutoInitStates.set(options, state);
|
|
3996
|
+
}
|
|
3997
|
+
return state;
|
|
3998
|
+
}
|
|
3999
|
+
function generateHostAutoInitCode(remoteEntryImport, _command = "build", options) {
|
|
4000
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
4001
|
+
const shouldPreloadShares = resolvedOptions.shareStrategy !== "loaded-first";
|
|
4002
|
+
const hostInitShareOrder = JSON.stringify(getOrderedUsedShares(options));
|
|
4003
|
+
const cacheOwner = JSON.stringify(resolvedOptions.name);
|
|
3869
4004
|
return `
|
|
3870
4005
|
${getRuntimeModuleCacheBootstrapCode()}
|
|
3871
4006
|
let hostInitPromise;
|
|
@@ -3873,10 +4008,10 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
3873
4008
|
if (!hostInitPromise) {
|
|
3874
4009
|
hostInitPromise = (async () => {
|
|
3875
4010
|
${sharedCacheHelperCode}
|
|
3876
|
-
${generateHostAutoInitSharedCacheSeedCode(_command)}
|
|
4011
|
+
${generateHostAutoInitSharedCacheSeedCode(_command, options)}
|
|
3877
4012
|
const remoteEntry = await import(${remoteEntryImport});
|
|
3878
4013
|
const runtime = await remoteEntry.init();
|
|
3879
|
-
const {usedShared} = await import("${getLocalSharedImportMapPath()}");
|
|
4014
|
+
const {usedShared} = await import("${getLocalSharedImportMapPath(options)}");
|
|
3880
4015
|
${normalizeRuntimeShareCode}
|
|
3881
4016
|
${shouldPreloadShares ? `
|
|
3882
4017
|
const __mfHostInitShareOrder = ${hostInitShareOrder}
|
|
@@ -3918,46 +4053,82 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
3918
4053
|
export { initHost, hostInitPromise };
|
|
3919
4054
|
`;
|
|
3920
4055
|
}
|
|
3921
|
-
function writeHostAutoInit(remoteEntryId = REMOTE_ENTRY_ID, command = "build") {
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
4056
|
+
function writeHostAutoInit(remoteEntryId = REMOTE_ENTRY_ID, command = "build", options) {
|
|
4057
|
+
const state = getHostAutoInitState(options);
|
|
4058
|
+
state.remoteEntryId = remoteEntryId;
|
|
4059
|
+
state.command = command;
|
|
4060
|
+
state.module.writeSync(generateHostAutoInitCode(JSON.stringify(remoteEntryId), command, options), true);
|
|
3925
4061
|
}
|
|
3926
|
-
function refreshHostAutoInit() {
|
|
4062
|
+
function refreshHostAutoInit(options) {
|
|
3927
4063
|
try {
|
|
3928
|
-
|
|
4064
|
+
const state = getHostAutoInitState(options);
|
|
4065
|
+
writeHostAutoInit(state.remoteEntryId, state.command, options);
|
|
3929
4066
|
} catch {}
|
|
3930
4067
|
}
|
|
3931
|
-
function getHostAutoInitImportId() {
|
|
3932
|
-
return
|
|
4068
|
+
function getHostAutoInitImportId(options) {
|
|
4069
|
+
return getHostAutoInitState(options).module.getImportId();
|
|
3933
4070
|
}
|
|
3934
|
-
function getHostAutoInitPath() {
|
|
3935
|
-
return
|
|
4071
|
+
function getHostAutoInitPath(options) {
|
|
4072
|
+
return getHostAutoInitState(options).module.getImportId();
|
|
3936
4073
|
}
|
|
3937
4074
|
//#endregion
|
|
3938
4075
|
//#region src/virtualModules/virtualRemotes.ts
|
|
3939
|
-
const cacheRemoteMap =
|
|
4076
|
+
const cacheRemoteMap = /* @__PURE__ */ new WeakMap();
|
|
4077
|
+
const remoteOptionsIds = /* @__PURE__ */ new WeakMap();
|
|
4078
|
+
let nextRemoteOptionsId = 1;
|
|
4079
|
+
function getRemoteOptionsId(options) {
|
|
4080
|
+
let id = remoteOptionsIds.get(options);
|
|
4081
|
+
if (id === void 0) {
|
|
4082
|
+
id = nextRemoteOptionsId++;
|
|
4083
|
+
remoteOptionsIds.set(options, id);
|
|
4084
|
+
}
|
|
4085
|
+
return id;
|
|
4086
|
+
}
|
|
3940
4087
|
const LOAD_REMOTE_TAG = "__loadRemote__";
|
|
3941
|
-
function getRemoteVirtualModule(remote, command, enableSsrInit = false, consumer = "unified") {
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
cacheRemoteMap
|
|
3946
|
-
|
|
4088
|
+
function getRemoteVirtualModule(remote, command, enableSsrInit = false, consumer = "unified", options = getNormalizeModuleFederationOptions()) {
|
|
4089
|
+
let instanceCache = cacheRemoteMap.get(options);
|
|
4090
|
+
if (!instanceCache) {
|
|
4091
|
+
instanceCache = /* @__PURE__ */ new Map();
|
|
4092
|
+
cacheRemoteMap.set(options, instanceCache);
|
|
4093
|
+
}
|
|
4094
|
+
const cacheKey = `${remote}__${command}__${options.shareStrategy}__${consumer}__${enableSsrInit ? "ssr-init" : "no-ssr-init"}`;
|
|
4095
|
+
if (!instanceCache.has(cacheKey)) {
|
|
4096
|
+
const virtual = new VirtualModule(`${consumer === "unified" ? remote : `${remote}__mf_consumer__${consumer}`}__mf_owner__${getRemoteOptionsId(options)}`, LOAD_REMOTE_TAG, ".js", options.internalName);
|
|
4097
|
+
virtual.writeSync(generateRemotes(remote, command, enableSsrInit, consumer, options));
|
|
4098
|
+
instanceCache.set(cacheKey, virtual);
|
|
3947
4099
|
}
|
|
3948
|
-
return
|
|
4100
|
+
return instanceCache.get(cacheKey);
|
|
3949
4101
|
}
|
|
3950
4102
|
const usedRemotesMap = {};
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
4103
|
+
const usedRemotesByOptions = /* @__PURE__ */ new WeakMap();
|
|
4104
|
+
function getScopedUsedRemotesMap(options) {
|
|
4105
|
+
let scoped = usedRemotesByOptions.get(options);
|
|
4106
|
+
if (!scoped) {
|
|
4107
|
+
scoped = {};
|
|
4108
|
+
usedRemotesByOptions.set(options, scoped);
|
|
4109
|
+
}
|
|
4110
|
+
return scoped;
|
|
4111
|
+
}
|
|
4112
|
+
function recordUsedRemote(map, remoteKey, remoteModule) {
|
|
4113
|
+
if (!map[remoteKey]) map[remoteKey] = /* @__PURE__ */ new Set();
|
|
4114
|
+
map[remoteKey].add(remoteModule);
|
|
4115
|
+
}
|
|
4116
|
+
function addUsedRemote(remoteKey, remoteModule, options) {
|
|
4117
|
+
recordUsedRemote(usedRemotesMap, remoteKey, remoteModule);
|
|
4118
|
+
if (options) recordUsedRemote(getScopedUsedRemotesMap(options), remoteKey, remoteModule);
|
|
3954
4119
|
}
|
|
3955
|
-
function getUsedRemotesMap() {
|
|
4120
|
+
function getUsedRemotesMap(options) {
|
|
4121
|
+
if (options) return getScopedUsedRemotesMap(options);
|
|
3956
4122
|
return usedRemotesMap;
|
|
3957
4123
|
}
|
|
3958
4124
|
function getRemoteAliasFromId(id, remotes) {
|
|
3959
4125
|
return Object.keys(remotes).filter((name) => id === name || id.startsWith(name + "/")).sort((a, b) => b.length - a.length)[0];
|
|
3960
4126
|
}
|
|
4127
|
+
function getRuntimeRemoteId(id, remotes, options) {
|
|
4128
|
+
const alias = getRemoteAliasFromId(id, remotes);
|
|
4129
|
+
if (!alias) return id;
|
|
4130
|
+
return `${getRuntimeRemoteAlias(alias, options)}${id.slice(alias.length)}`;
|
|
4131
|
+
}
|
|
3961
4132
|
function resolveRemoteInitMode(shareStrategy, consumer) {
|
|
3962
4133
|
if (shareStrategy !== "loaded-first") return "eager";
|
|
3963
4134
|
if (consumer === "server") return "loaded-first-ssr";
|
|
@@ -4016,7 +4187,7 @@ function getRemoteModuleRuntimeHelpers() {
|
|
|
4016
4187
|
return exportModule;
|
|
4017
4188
|
}`;
|
|
4018
4189
|
}
|
|
4019
|
-
function getDeferredProxyHelper(
|
|
4190
|
+
function getDeferredProxyHelper(remoteCacheKey) {
|
|
4020
4191
|
return `
|
|
4021
4192
|
function __mfCreateDeferredRemoteProxy() {
|
|
4022
4193
|
let pendingPromise;
|
|
@@ -4024,7 +4195,7 @@ function getDeferredProxyHelper(remoteId) {
|
|
|
4024
4195
|
pendingPromise ||= __mfStartRemoteLoad();
|
|
4025
4196
|
return pendingPromise;
|
|
4026
4197
|
};
|
|
4027
|
-
const getModule = () => __mfModuleCache.remote[${JSON.stringify(
|
|
4198
|
+
const getModule = () => __mfModuleCache.remote[${JSON.stringify(remoteCacheKey)}];
|
|
4028
4199
|
const proxyTarget = function (...args) {
|
|
4029
4200
|
pendingPromise ||= __mfStartRemoteLoad();
|
|
4030
4201
|
const mod = getModule();
|
|
@@ -4122,30 +4293,39 @@ ${deferRemoteLoad ? getLazyRemotePendingExport() : getEagerRemotePendingExport()
|
|
|
4122
4293
|
${command === "serve" && consumer === "server" ? getServerThenExport() : ""}
|
|
4123
4294
|
export { __mfDefaultExport as default };`;
|
|
4124
4295
|
}
|
|
4125
|
-
function generateRemotes(id, command, enableSsrInit = false, consumer = "unified") {
|
|
4126
|
-
const
|
|
4127
|
-
const isLoadedFirst =
|
|
4128
|
-
const initMode = resolveRemoteInitMode(
|
|
4296
|
+
function generateRemotes(id, command, enableSsrInit = false, consumer = "unified", options) {
|
|
4297
|
+
const resolvedOptions = options ?? getNormalizeModuleFederationOptions();
|
|
4298
|
+
const isLoadedFirst = resolvedOptions.shareStrategy === "loaded-first";
|
|
4299
|
+
const initMode = resolveRemoteInitMode(resolvedOptions.shareStrategy, consumer);
|
|
4129
4300
|
const deferRemoteLoad = shouldDeferRemoteLoad(initMode);
|
|
4130
|
-
const remoteAlias = getRemoteAliasFromId(id,
|
|
4131
|
-
const remote = remoteAlias ?
|
|
4301
|
+
const remoteAlias = getRemoteAliasFromId(id, resolvedOptions.remotes);
|
|
4302
|
+
const remote = remoteAlias ? resolvedOptions.remotes[remoteAlias] : void 0;
|
|
4303
|
+
const runtimeRemoteAlias = remoteAlias ? getRuntimeRemoteAlias(remoteAlias, options) : void 0;
|
|
4304
|
+
const runtimeRemoteId = getRuntimeRemoteId(id, resolvedOptions.remotes, options);
|
|
4132
4305
|
const registerRemoteCode = isLoadedFirst && remote ? `runtime.registerRemotes([${JSON.stringify({
|
|
4133
4306
|
entryGlobalName: remote.entryGlobalName,
|
|
4134
|
-
name: remote.name,
|
|
4135
|
-
alias:
|
|
4307
|
+
name: options ? runtimeRemoteAlias : remote.name,
|
|
4308
|
+
alias: runtimeRemoteAlias,
|
|
4136
4309
|
type: remote.type,
|
|
4137
4310
|
entry: remote.entry,
|
|
4138
4311
|
shareScope: remote.shareScope ?? "default"
|
|
4139
4312
|
})}]);` : "";
|
|
4140
|
-
const
|
|
4313
|
+
const hostAutoInitPath = getHostAutoInitPath(options);
|
|
4314
|
+
const ssrRemotes = Object.entries(resolvedOptions.remotes).map(([name, item]) => ({
|
|
4315
|
+
name: getRuntimeRemoteAlias(name, options),
|
|
4316
|
+
entry: item.entry,
|
|
4317
|
+
type: item.type ?? "module"
|
|
4318
|
+
}));
|
|
4319
|
+
const browserHostInitCode = `import(${JSON.stringify(hostAutoInitPath)})
|
|
4141
4320
|
.then((mod) => mod.hostInitPromise)
|
|
4142
4321
|
.then(initResolve, initReject);`;
|
|
4143
|
-
const devRuntimeBootstrap = `${getRuntimeInitBootstrapCode(enableSsrInit,
|
|
4322
|
+
const devRuntimeBootstrap = `${getRuntimeInitBootstrapCode(enableSsrInit, getRuntimeInitStatusImportId(options), ssrRemotes, hostAutoInitPath)}
|
|
4144
4323
|
const { initPromise, initResolve, initReject, moduleCache: __mfModuleCache } = globalThis[globalKey];`;
|
|
4145
4324
|
const importLine = command === "build" ? `${getRuntimeModuleCacheBootstrapCode()}
|
|
4146
|
-
import { hostInitPromise as __mfHostInitPromise } from ${JSON.stringify(
|
|
4325
|
+
import { hostInitPromise as __mfHostInitPromise } from ${JSON.stringify(hostAutoInitPath)};` : `${devRuntimeBootstrap}
|
|
4147
4326
|
${command === "serve" && consumer !== "server" ? browserHostInitCode : ""}`;
|
|
4148
4327
|
const remoteLoadRuntimePromise = command === "build" ? "__mfHostInitPromise" : "initPromise";
|
|
4328
|
+
const remoteCacheKey = `${getRuntimeRemoteCachePrefix(options)}${id}`;
|
|
4149
4329
|
const remoteLoadFailureHandler = command === "build" ? `.catch((error) => {
|
|
4150
4330
|
delete __mfModuleCache.remote[pendingKey];
|
|
4151
4331
|
throw error;
|
|
@@ -4156,16 +4336,17 @@ function generateRemotes(id, command, enableSsrInit = false, consumer = "unified
|
|
|
4156
4336
|
const remoteLoadCode = `
|
|
4157
4337
|
function __mfStartRemoteLoad() {
|
|
4158
4338
|
${`
|
|
4159
|
-
const
|
|
4339
|
+
const remoteCacheKey = ${JSON.stringify(remoteCacheKey)};
|
|
4340
|
+
const pendingKey = "__mf_pending__" + remoteCacheKey;
|
|
4160
4341
|
if (!__mfModuleCache.remote[pendingKey]) {
|
|
4161
4342
|
__mfModuleCache.remote[pendingKey] = ${remoteLoadRuntimePromise}
|
|
4162
4343
|
.then((runtime) => {
|
|
4163
4344
|
${registerRemoteCode}
|
|
4164
|
-
return runtime.loadRemote(${JSON.stringify(
|
|
4345
|
+
return runtime.loadRemote(${JSON.stringify(runtimeRemoteId)});
|
|
4165
4346
|
})
|
|
4166
4347
|
.then((mod) => Promise.resolve(mod?.__mf_remote_dependency_pending).then(() => mod))
|
|
4167
4348
|
.then((mod) => {
|
|
4168
|
-
__mfModuleCache.remote[
|
|
4349
|
+
__mfModuleCache.remote[remoteCacheKey] = mod;
|
|
4169
4350
|
delete __mfModuleCache.remote[pendingKey];
|
|
4170
4351
|
return mod;
|
|
4171
4352
|
})
|
|
@@ -4185,14 +4366,14 @@ function generateRemotes(id, command, enableSsrInit = false, consumer = "unified
|
|
|
4185
4366
|
}`;
|
|
4186
4367
|
const initExportModule = initMode === "eager" ? environmentSplitInit(eagerClientInit, realRemoteInit) : environmentSplitInit(loadedFirstClientInit, realRemoteInit);
|
|
4187
4368
|
const includeProxyHelper = shouldIncludeDeferredProxy(initMode, consumer, eagerLoadClientRemote, deferRemoteLoad);
|
|
4188
|
-
const deferredProxyCode = getDeferredProxyHelper(
|
|
4369
|
+
const deferredProxyCode = getDeferredProxyHelper(remoteCacheKey);
|
|
4189
4370
|
return `
|
|
4190
4371
|
${importLine}
|
|
4191
4372
|
${remoteLoadCode}
|
|
4192
4373
|
${includeProxyHelper ? deferredProxyCode : ""}
|
|
4193
4374
|
${getRemoteModuleRuntimeHelpers()}
|
|
4194
4375
|
let __mfRemotePending;
|
|
4195
|
-
let exportModule = __mfModuleCache.remote[${JSON.stringify(
|
|
4376
|
+
let exportModule = __mfModuleCache.remote[${JSON.stringify(remoteCacheKey)}]
|
|
4196
4377
|
if (exportModule === undefined) {
|
|
4197
4378
|
${initExportModule}
|
|
4198
4379
|
}
|
|
@@ -4271,7 +4452,7 @@ function patchHashEntryFileNames(config, entryName, fileName) {
|
|
|
4271
4452
|
patchBundlerOutput(config.build.rollupOptions);
|
|
4272
4453
|
patchBundlerOutput(config.build.rolldownOptions);
|
|
4273
4454
|
}
|
|
4274
|
-
const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClientInjected, skipTransformFor = [] }) => {
|
|
4455
|
+
const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClientInjected, skipTransformFor = [], federationOptions }) => {
|
|
4275
4456
|
const DEV_HTML_PROXY_PREFIX = "virtual:mf-html-entry-proxy?";
|
|
4276
4457
|
const ENTRY_BOOTSTRAP_PARAM = "mf-entry-bootstrap";
|
|
4277
4458
|
const ENTRY_BOOTSTRAP_QUERY = `?${ENTRY_BOOTSTRAP_PARAM}`;
|
|
@@ -4355,15 +4536,17 @@ const __mfCurrentScript = document.currentScript;
|
|
|
4355
4536
|
: import(src);
|
|
4356
4537
|
` : "";
|
|
4357
4538
|
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(",") : "";
|
|
4539
|
+
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(",") : "";
|
|
4540
|
+
const remoteCachePrefix = getRuntimeRemoteCachePrefix(federationOptions);
|
|
4359
4541
|
const preloadBlock = remotePreloads ? `
|
|
4360
4542
|
const runtime = await initHost();
|
|
4361
|
-
const __mfPreloadRemote = (remote) => {
|
|
4362
|
-
const
|
|
4543
|
+
const __mfPreloadRemote = (runtimeRemote, remote) => {
|
|
4544
|
+
const remoteCacheKey = ${JSON.stringify(remoteCachePrefix)} + remote;
|
|
4545
|
+
const pendingKey = "__mf_pending__" + remoteCacheKey;
|
|
4363
4546
|
if (!__mfModuleCache.remote[pendingKey]) {
|
|
4364
|
-
__mfModuleCache.remote[pendingKey] = runtime.loadRemote(
|
|
4547
|
+
__mfModuleCache.remote[pendingKey] = runtime.loadRemote(runtimeRemote)
|
|
4365
4548
|
.then((mod) => {
|
|
4366
|
-
__mfModuleCache.remote[
|
|
4549
|
+
__mfModuleCache.remote[remoteCacheKey] = mod;
|
|
4367
4550
|
delete __mfModuleCache.remote[pendingKey];
|
|
4368
4551
|
return mod;
|
|
4369
4552
|
})
|
|
@@ -4610,7 +4793,7 @@ const __mfCurrentScript = document.currentScript;
|
|
|
4610
4793
|
htmlContent = htmlContent.replace("<head>", `<head>${scriptContent}`);
|
|
4611
4794
|
}
|
|
4612
4795
|
}
|
|
4613
|
-
if (waitsForInit) htmlContent = injectHostInitPreloads(htmlContent, bundle, (builtFileName) => resolvePath(builtFileName, fileName));
|
|
4796
|
+
if (waitsForInit && viteConfig.build.modulePreload !== false) htmlContent = injectHostInitPreloads(htmlContent, bundle, (builtFileName) => resolvePath(builtFileName, fileName));
|
|
4614
4797
|
htmlAsset.source = htmlContent;
|
|
4615
4798
|
}
|
|
4616
4799
|
},
|
|
@@ -4739,8 +4922,8 @@ function resolveReactRefreshRuntime(root) {
|
|
|
4739
4922
|
* falls back to this remote's local runtime when the remote is opened directly.
|
|
4740
4923
|
*/
|
|
4741
4924
|
const REACT_REFRESH_PROXY_MODULE = [
|
|
4742
|
-
`const
|
|
4743
|
-
`const __target = window.location.origin ===
|
|
4925
|
+
`const __remoteUrl = new URL(import.meta.url);`,
|
|
4926
|
+
`const __target = window.location.origin === __remoteUrl.origin ? new URL('.${LOCAL_REACT_REFRESH_PATH}', __remoteUrl).href : window.location.origin + '${REACT_REFRESH_PATH}';`,
|
|
4744
4927
|
`const __rt = await import(__target);`,
|
|
4745
4928
|
`export const injectIntoGlobalHook = __rt.injectIntoGlobalHook;`,
|
|
4746
4929
|
`export const register = __rt.register;`,
|
|
@@ -4757,14 +4940,14 @@ const reactAdapter = {
|
|
|
4757
4940
|
let reactRefreshRuntime;
|
|
4758
4941
|
server.middlewares.use((req, res, next) => {
|
|
4759
4942
|
const url = stripQuery(req.url);
|
|
4760
|
-
if (url
|
|
4943
|
+
if (url?.endsWith(LOCAL_REACT_REFRESH_PATH)) {
|
|
4761
4944
|
reactRefreshRuntime ??= resolveReactRefreshRuntime(server.config.root);
|
|
4762
4945
|
res.setHeader("Content-Type", "application/javascript; charset=utf-8");
|
|
4763
4946
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
4764
4947
|
res.end(reactRefreshRuntime);
|
|
4765
4948
|
return;
|
|
4766
4949
|
}
|
|
4767
|
-
if (url
|
|
4950
|
+
if (!url?.endsWith(REACT_REFRESH_PATH)) return next();
|
|
4768
4951
|
res.setHeader("Content-Type", "application/javascript; charset=utf-8");
|
|
4769
4952
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
4770
4953
|
res.end(REACT_REFRESH_PROXY_MODULE);
|
|
@@ -5186,10 +5369,14 @@ function pluginDevRemoteHmr(options) {
|
|
|
5186
5369
|
}
|
|
5187
5370
|
//#endregion
|
|
5188
5371
|
//#region src/virtualModules/index.ts
|
|
5189
|
-
function initVirtualModules(command, remoteEntryId, enableSsrInit = false) {
|
|
5190
|
-
writeLocalSharedImportMap();
|
|
5191
|
-
writeHostAutoInit(remoteEntryId, command);
|
|
5192
|
-
writeRuntimeInitStatus(command, enableSsrInit, getHostAutoInitPath())
|
|
5372
|
+
function initVirtualModules(command, remoteEntryId, enableSsrInit = false, options) {
|
|
5373
|
+
writeLocalSharedImportMap(options);
|
|
5374
|
+
writeHostAutoInit(remoteEntryId, command, options);
|
|
5375
|
+
writeRuntimeInitStatus(command, enableSsrInit, getHostAutoInitPath(options), options, options ? Object.entries(options.remotes).map(([name, item]) => ({
|
|
5376
|
+
name,
|
|
5377
|
+
entry: item.entry,
|
|
5378
|
+
type: item.type ?? "module"
|
|
5379
|
+
})) : void 0);
|
|
5193
5380
|
}
|
|
5194
5381
|
//#endregion
|
|
5195
5382
|
//#region src/utils/bundleHelpers.ts
|
|
@@ -5450,6 +5637,35 @@ const chunkContainsCssModules = (modules) => {
|
|
|
5450
5637
|
return false;
|
|
5451
5638
|
};
|
|
5452
5639
|
/**
|
|
5640
|
+
* Analyzes assets associated with a chunk without mutating the output map.
|
|
5641
|
+
* The static-import traversal is cycle-safe and ignores missing bundle entries.
|
|
5642
|
+
*/
|
|
5643
|
+
const analyzeChunkAssets = (bundle, fileName, chunk) => {
|
|
5644
|
+
const dynamicAssets = [];
|
|
5645
|
+
const visited = /* @__PURE__ */ new Set();
|
|
5646
|
+
const queue = [fileName];
|
|
5647
|
+
for (let queueIndex = 0; queueIndex < queue.length; queueIndex++) {
|
|
5648
|
+
const currentFileName = queue[queueIndex];
|
|
5649
|
+
if (visited.has(currentFileName)) continue;
|
|
5650
|
+
visited.add(currentFileName);
|
|
5651
|
+
const currentChunk = bundle[currentFileName];
|
|
5652
|
+
if (!currentChunk || currentChunk.type !== "chunk") continue;
|
|
5653
|
+
for (const dynamicImport of currentChunk.dynamicImports ?? []) {
|
|
5654
|
+
if (!bundle[dynamicImport]) continue;
|
|
5655
|
+
dynamicAssets.push({
|
|
5656
|
+
fileName: dynamicImport,
|
|
5657
|
+
type: isCSSFile(dynamicImport) ? "css" : "js"
|
|
5658
|
+
});
|
|
5659
|
+
}
|
|
5660
|
+
for (const staticImport of currentChunk.imports ?? []) queue.push(staticImport);
|
|
5661
|
+
}
|
|
5662
|
+
return {
|
|
5663
|
+
importedCss: Array.from(chunk.viteMetadata?.importedCss ?? []),
|
|
5664
|
+
containsCssModules: chunkContainsCssModules(chunk.modules),
|
|
5665
|
+
dynamicAssets
|
|
5666
|
+
};
|
|
5667
|
+
};
|
|
5668
|
+
/**
|
|
5453
5669
|
* Processes module assets and tracks them in the files map
|
|
5454
5670
|
* @param bundle - The Rollup output bundle
|
|
5455
5671
|
* @param filesMap - The preload map to populate
|
|
@@ -5457,6 +5673,7 @@ const chunkContainsCssModules = (modules) => {
|
|
|
5457
5673
|
*/
|
|
5458
5674
|
const processModuleAssets = (bundle, filesMap, moduleMatcher, options = {}) => {
|
|
5459
5675
|
const bundleCssAssets = collectCssAssets(bundle);
|
|
5676
|
+
const chunkAnalysisCache = /* @__PURE__ */ new Map();
|
|
5460
5677
|
for (const [fileName, fileData] of Object.entries(bundle)) {
|
|
5461
5678
|
if (fileData.type !== "chunk") continue;
|
|
5462
5679
|
if (!fileData.modules) continue;
|
|
@@ -5469,27 +5686,19 @@ const processModuleAssets = (bundle, filesMap, moduleMatcher, options = {}) => {
|
|
|
5469
5686
|
}
|
|
5470
5687
|
const matchKey = comparableModulePaths.map(moduleMatcher).find(Boolean);
|
|
5471
5688
|
if (!matchKey) continue;
|
|
5689
|
+
let analysis = chunkAnalysisCache.get(fileName);
|
|
5690
|
+
if (!analysis) {
|
|
5691
|
+
analysis = analyzeChunkAssets(bundle, fileName, fileData);
|
|
5692
|
+
chunkAnalysisCache.set(fileName, analysis);
|
|
5693
|
+
}
|
|
5472
5694
|
trackAsset(filesMap, matchKey, fileName, false, "js");
|
|
5473
5695
|
let foundCssViaMetadata = false;
|
|
5474
|
-
|
|
5696
|
+
for (const cssFile of analysis.importedCss) {
|
|
5475
5697
|
trackAsset(filesMap, matchKey, cssFile, false, "css");
|
|
5476
5698
|
foundCssViaMetadata = true;
|
|
5477
5699
|
}
|
|
5478
|
-
if (!foundCssViaMetadata &&
|
|
5479
|
-
const
|
|
5480
|
-
const queue = [fileName];
|
|
5481
|
-
for (let queueIndex = 0; queueIndex < queue.length; queueIndex++) {
|
|
5482
|
-
const cur = queue[queueIndex];
|
|
5483
|
-
if (visited.has(cur)) continue;
|
|
5484
|
-
visited.add(cur);
|
|
5485
|
-
const chunk = bundle[cur];
|
|
5486
|
-
if (!chunk || chunk.type !== "chunk") continue;
|
|
5487
|
-
if (chunk.dynamicImports) for (const dynamicImport of chunk.dynamicImports) {
|
|
5488
|
-
if (!bundle[dynamicImport]) continue;
|
|
5489
|
-
trackAsset(filesMap, matchKey, dynamicImport, true, isCSSFile(dynamicImport) ? "css" : "js");
|
|
5490
|
-
}
|
|
5491
|
-
if (chunk.imports) for (const imp of chunk.imports) queue.push(imp);
|
|
5492
|
-
}
|
|
5700
|
+
if (!foundCssViaMetadata && analysis.containsCssModules) for (const cssAsset of Array.from(bundleCssAssets)) trackAsset(filesMap, matchKey, cssAsset, false, "css");
|
|
5701
|
+
for (const asset of analysis.dynamicAssets) trackAsset(filesMap, matchKey, asset.fileName, true, asset.type);
|
|
5493
5702
|
}
|
|
5494
5703
|
}
|
|
5495
5704
|
};
|
|
@@ -5524,9 +5733,9 @@ const deduplicateAssets = (filesMap) => {
|
|
|
5524
5733
|
* @param resolveFn - Function to resolve module paths
|
|
5525
5734
|
* @returns Map of file paths to their corresponding share keys
|
|
5526
5735
|
*/
|
|
5527
|
-
const buildFileToShareKeyMap = async (shareKeys, resolveFn) => {
|
|
5736
|
+
const buildFileToShareKeyMap = async (shareKeys, resolveFn, options) => {
|
|
5528
5737
|
const fileToShareKey = /* @__PURE__ */ new Map();
|
|
5529
|
-
const resolutions = await Promise.all(Array.from(shareKeys).map((shareKey) => resolveFn(getPreBuildLibImportId(shareKey)).then((resolution) => ({
|
|
5738
|
+
const resolutions = await Promise.all(Array.from(shareKeys).map((shareKey) => resolveFn(getPreBuildLibImportId(shareKey, options)).then((resolution) => ({
|
|
5530
5739
|
shareKey,
|
|
5531
5740
|
file: resolution?.id?.split("?")[0]
|
|
5532
5741
|
})).catch(() => null)));
|
|
@@ -5732,8 +5941,8 @@ function getRemoteContainerName(remoteKey, remote) {
|
|
|
5732
5941
|
if (entryGlobalName && entryGlobalName !== remoteKey && entryGlobalName !== remote.entry) return entryGlobalName;
|
|
5733
5942
|
return remote.name;
|
|
5734
5943
|
}
|
|
5735
|
-
const Manifest = () => {
|
|
5736
|
-
const mfOptions = getNormalizeModuleFederationOptions();
|
|
5944
|
+
const Manifest = (providedOptions) => {
|
|
5945
|
+
const mfOptions = providedOptions ?? getNormalizeModuleFederationOptions();
|
|
5737
5946
|
const { name, filename, getPublicPath, manifest: manifestOptions, varFilename } = mfOptions;
|
|
5738
5947
|
let mfManifestName = manifestOptions === true ? "mf-manifest.json" : typeof manifestOptions === "object" ? normalizePathForImport(path$1.join(manifestOptions?.filePath || "", manifestOptions?.fileName || "mf-manifest.json")) : void 0;
|
|
5739
5948
|
let mfManifestStatsName = mfManifestName ? getStatsFileName(mfManifestName) : void 0;
|
|
@@ -5860,7 +6069,7 @@ const Manifest = () => {
|
|
|
5860
6069
|
root,
|
|
5861
6070
|
stripKnownJsExtensions: true
|
|
5862
6071
|
});
|
|
5863
|
-
const fileToShareKey = await buildFileToShareKeyMap(getUsedShares(), this.resolve.bind(this));
|
|
6072
|
+
const fileToShareKey = await buildFileToShareKeyMap(getUsedShares(mfOptions), this.resolve.bind(this), mfOptions);
|
|
5864
6073
|
processModuleAssets(Object.fromEntries(Object.entries(bundle).filter(([, file]) => !isTreeShakingProviderChunk(file))), filesMap, (modulePath) => fileToShareKey.get(modulePath));
|
|
5865
6074
|
if (mfOptions.bundleAllCSS) addCssAssetsToAllExports(filesMap, allCssAssets);
|
|
5866
6075
|
filesMap = deduplicateAssets(filesMap);
|
|
@@ -5887,7 +6096,7 @@ const Manifest = () => {
|
|
|
5887
6096
|
* @returns Complete manifest object
|
|
5888
6097
|
*/
|
|
5889
6098
|
function generateMFManifest(preloadMap, disableAssetsAnalyze = false) {
|
|
5890
|
-
const options =
|
|
6099
|
+
const options = mfOptions;
|
|
5891
6100
|
const { name, varFilename } = options;
|
|
5892
6101
|
const resolvedRemoteEntryFile = _command === "serve" ? remoteEntryFile || resolveDevRemoteEntryFileName(filename) : remoteEntryFile;
|
|
5893
6102
|
const remoteEntry = {
|
|
@@ -5905,7 +6114,7 @@ const Manifest = () => {
|
|
|
5905
6114
|
path: "",
|
|
5906
6115
|
type: "var"
|
|
5907
6116
|
} : void 0;
|
|
5908
|
-
const remotes = Array.from(Object.entries(getUsedRemotesMap())).flatMap(([remoteKey, modules]) => {
|
|
6117
|
+
const remotes = Array.from(Object.entries(getUsedRemotesMap(options))).flatMap(([remoteKey, modules]) => {
|
|
5909
6118
|
const remote = options.remotes[remoteKey];
|
|
5910
6119
|
return Array.from(modules).map((moduleKey) => ({
|
|
5911
6120
|
federationContainerName: getRemoteContainerName(remoteKey, remote),
|
|
@@ -5914,11 +6123,11 @@ const Manifest = () => {
|
|
|
5914
6123
|
entry: "*"
|
|
5915
6124
|
}));
|
|
5916
6125
|
});
|
|
5917
|
-
const shared = Array.from(getUsedShares()).flatMap((shareKey) => {
|
|
5918
|
-
const shareItem = getNormalizeShareItem(shareKey);
|
|
6126
|
+
const shared = Array.from(getUsedShares(options)).flatMap((shareKey) => {
|
|
6127
|
+
const shareItem = getNormalizeShareItem(shareKey, options);
|
|
5919
6128
|
if (!shareItem) return [];
|
|
5920
6129
|
const assets = preloadMap[shareKey] || (_command === "serve" && resolvedRemoteEntryFile ? createRemoteEntryAssetMap(resolvedRemoteEntryFile) : createEmptyAssetMap());
|
|
5921
|
-
const treeShakingUsage = getTreeShakingExportUsage(shareKey, shareItem, shareItem.name);
|
|
6130
|
+
const treeShakingUsage = getTreeShakingExportUsage(shareKey, shareItem, shareItem.name, options);
|
|
5922
6131
|
const treeShakingUsedExports = treeShakingUsage?.kind === "exports" ? treeShakingUsage.usedExports : [];
|
|
5923
6132
|
const treeShakingStatus = treeShakingUsage?.kind === "full" ? 0 : 1;
|
|
5924
6133
|
return [{
|
|
@@ -6150,6 +6359,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6150
6359
|
let exposeRemoteDependenciesDirty = true;
|
|
6151
6360
|
let refreshPromise;
|
|
6152
6361
|
let dependencyInvalidationVersion = 0;
|
|
6362
|
+
const isHostAutoInitId = (id) => id.includes(getHostAutoInitPath(options)) || id.includes(getHostAutoInitPath());
|
|
6153
6363
|
function isRemoteImport(source) {
|
|
6154
6364
|
return Object.keys(options.remotes).some((name) => source === name || source.startsWith(name + "/"));
|
|
6155
6365
|
}
|
|
@@ -6239,7 +6449,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6239
6449
|
async resolveId(id, importer) {
|
|
6240
6450
|
if (id === remoteEntryId) return remoteEntryId;
|
|
6241
6451
|
if (id === virtualExposesId) return virtualExposesId;
|
|
6242
|
-
if (_command === "serve" && id
|
|
6452
|
+
if (_command === "serve" && isHostAutoInitId(id)) return id;
|
|
6243
6453
|
if (importer === remoteEntryId && !id.startsWith(".") && !id.startsWith("/") && !id.startsWith("\0") && !id.startsWith("virtual:")) {
|
|
6244
6454
|
const importPath = typeof __filename === "string" ? __filename : fileURLToPath(import.meta.url);
|
|
6245
6455
|
const resolved = await this.resolve(id, importPath, { skipSelf: true });
|
|
@@ -6252,7 +6462,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6252
6462
|
await refreshExposeRemoteDependencies(this);
|
|
6253
6463
|
return generateExposes(options, exposeRemoteDependencies, _command);
|
|
6254
6464
|
}
|
|
6255
|
-
if (_command === "serve" && id
|
|
6465
|
+
if (_command === "serve" && isHostAutoInitId(id)) return id;
|
|
6256
6466
|
},
|
|
6257
6467
|
async transform(code, id) {
|
|
6258
6468
|
return mapCodeToCodeWithSourcemap(await (async () => {
|
|
@@ -6262,7 +6472,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6262
6472
|
await refreshExposeRemoteDependencies(this);
|
|
6263
6473
|
return generateExposes(options, exposeRemoteDependencies, _command);
|
|
6264
6474
|
}
|
|
6265
|
-
if (id
|
|
6475
|
+
if (isHostAutoInitId(id)) {
|
|
6266
6476
|
if (_command === "serve") {
|
|
6267
6477
|
const host = typeof viteConfig.server?.host === "string" && viteConfig.server.host !== "0.0.0.0" ? viteConfig.server.host : "localhost";
|
|
6268
6478
|
const resolvedPublicPath = resolvePublicPath(options, viteConfig.base);
|
|
@@ -6272,7 +6482,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
6272
6482
|
return `
|
|
6273
6483
|
const origin = typeof window !== 'undefined' && (${!options.ignoreOrigin}) ? window.origin : ${JSON.stringify(fallbackOrigin)};
|
|
6274
6484
|
const remoteEntryImport = typeof window !== 'undefined' ? origin + ${publicPath} : ${JSON.stringify(ssrRemoteEntry)};
|
|
6275
|
-
${generateHostAutoInitCode("remoteEntryImport", "serve")}
|
|
6485
|
+
${generateHostAutoInitCode("remoteEntryImport", "serve", options)}
|
|
6276
6486
|
`;
|
|
6277
6487
|
}
|
|
6278
6488
|
return code;
|
|
@@ -6367,9 +6577,9 @@ function pluginProxyRemotes_default(options) {
|
|
|
6367
6577
|
if (installedPackageEntry && (importer === void 0 || isNodeModulesImporter(importer))) return installedPackageEntry;
|
|
6368
6578
|
}
|
|
6369
6579
|
const consumer = resolveRemoteConsumer(pluginContext, hasMultiEnvironment);
|
|
6370
|
-
const remoteModule = getRemoteVirtualModule(source, command, enableSsrInit, consumer);
|
|
6371
|
-
addUsedRemote(remoteName, source);
|
|
6372
|
-
refreshHostAutoInit();
|
|
6580
|
+
const remoteModule = getRemoteVirtualModule(source, command, enableSsrInit, consumer, options);
|
|
6581
|
+
addUsedRemote(remoteName, source, options);
|
|
6582
|
+
refreshHostAutoInit(options);
|
|
6373
6583
|
return remoteModule.getImportId();
|
|
6374
6584
|
}
|
|
6375
6585
|
return {
|
|
@@ -6546,7 +6756,7 @@ function excludeSharedSubDependencies(shared) {
|
|
|
6546
6756
|
}
|
|
6547
6757
|
}
|
|
6548
6758
|
function proxySharedModule(options) {
|
|
6549
|
-
const { shared = {} } = options;
|
|
6759
|
+
const { shared = {}, federationOptions } = options;
|
|
6550
6760
|
let _config;
|
|
6551
6761
|
let _command = "serve";
|
|
6552
6762
|
let useDirectReactImport = false;
|
|
@@ -6566,20 +6776,20 @@ function proxySharedModule(options) {
|
|
|
6566
6776
|
};
|
|
6567
6777
|
const getTreeShakingProviderFileName = (pkg, shareItem) => {
|
|
6568
6778
|
if (!shareItem.shareConfig.treeShaking) return void 0;
|
|
6569
|
-
const normalizedOptions = getNormalizeModuleFederationOptions();
|
|
6779
|
+
const normalizedOptions = federationOptions ?? getNormalizeModuleFederationOptions();
|
|
6570
6780
|
const outputDir = normalizedOptions.treeShakingDir ? normalizeTreeShakingOutputPath(normalizedOptions.treeShakingDir) : void 0;
|
|
6571
|
-
const fileName = outputDir ? path$1.posix.join(outputDir, `${getTreeShakingSharedProviderName(pkg)}.js`) : void 0;
|
|
6781
|
+
const fileName = outputDir ? path$1.posix.join(outputDir, `${getTreeShakingSharedProviderName(pkg, federationOptions)}.js`) : void 0;
|
|
6572
6782
|
if (!fileName) return void 0;
|
|
6573
6783
|
return fileName;
|
|
6574
6784
|
};
|
|
6575
6785
|
const emitTreeShakingProvider = (context, pkg, shareItem) => {
|
|
6576
6786
|
if (_command !== "build" || emittedTreeShakingProviders.has(pkg)) return;
|
|
6577
|
-
if (!hasTreeShakingSharedProvider(pkg, shareItem)) return;
|
|
6787
|
+
if (!hasTreeShakingSharedProvider(pkg, shareItem, federationOptions)) return;
|
|
6578
6788
|
const fileName = getTreeShakingProviderFileName(pkg, shareItem);
|
|
6579
6789
|
context.emitFile({
|
|
6580
6790
|
type: "chunk",
|
|
6581
|
-
id: getTreeShakingSharedProviderImportId(pkg),
|
|
6582
|
-
name: getTreeShakingSharedProviderName(pkg),
|
|
6791
|
+
id: getTreeShakingSharedProviderImportId(pkg, federationOptions),
|
|
6792
|
+
name: getTreeShakingSharedProviderName(pkg, federationOptions),
|
|
6583
6793
|
...fileName ? { fileName } : {}
|
|
6584
6794
|
});
|
|
6585
6795
|
emittedTreeShakingProviders.add(pkg);
|
|
@@ -6591,28 +6801,28 @@ function proxySharedModule(options) {
|
|
|
6591
6801
|
configureServer(server) {
|
|
6592
6802
|
devServer = server;
|
|
6593
6803
|
setLocalSharedImportMapInvalidator(() => {
|
|
6594
|
-
const module = server.moduleGraph.getModuleById(getResolvedLocalSharedImportMapId());
|
|
6804
|
+
const module = server.moduleGraph.getModuleById(getResolvedLocalSharedImportMapId(federationOptions));
|
|
6595
6805
|
if (module) server.moduleGraph.invalidateModule(module);
|
|
6596
|
-
});
|
|
6806
|
+
}, federationOptions);
|
|
6597
6807
|
},
|
|
6598
6808
|
resolveId(source) {
|
|
6599
|
-
if (source === getLocalSharedImportMapPath()) return getResolvedLocalSharedImportMapId();
|
|
6809
|
+
if (source === getLocalSharedImportMapPath(federationOptions)) return getResolvedLocalSharedImportMapId(federationOptions);
|
|
6600
6810
|
},
|
|
6601
6811
|
load(id) {
|
|
6602
|
-
if (id === getResolvedLocalSharedImportMapId()) return parsePromise.then((_) => {
|
|
6603
|
-
refreshTreeShakingModules();
|
|
6604
|
-
const providerPackages = new Set([...Object.keys(shared).filter((pkg) => !pkg.endsWith("/")), ...getUsedShares()]);
|
|
6812
|
+
if (id === getResolvedLocalSharedImportMapId(federationOptions)) return parsePromise.then((_) => {
|
|
6813
|
+
refreshTreeShakingModules(federationOptions);
|
|
6814
|
+
const providerPackages = new Set([...Object.keys(shared).filter((pkg) => !pkg.endsWith("/")), ...getUsedShares(federationOptions)]);
|
|
6605
6815
|
for (const pkg of providerPackages) {
|
|
6606
6816
|
const sharedKey = findSharedKeyForSource(pkg, shared);
|
|
6607
6817
|
const shareItem = shared[pkg] || (sharedKey ? shared[sharedKey] : void 0);
|
|
6608
6818
|
if (shareItem) emitTreeShakingProvider(this, pkg, shareItem);
|
|
6609
6819
|
}
|
|
6610
|
-
return generateLocalSharedImportMap();
|
|
6820
|
+
return generateLocalSharedImportMap(federationOptions);
|
|
6611
6821
|
});
|
|
6612
6822
|
},
|
|
6613
6823
|
closeBundle() {
|
|
6614
6824
|
if (devServer) return;
|
|
6615
|
-
setLocalSharedImportMapInvalidator(void 0);
|
|
6825
|
+
setLocalSharedImportMapInvalidator(void 0, federationOptions);
|
|
6616
6826
|
}
|
|
6617
6827
|
},
|
|
6618
6828
|
{
|
|
@@ -6620,8 +6830,8 @@ function proxySharedModule(options) {
|
|
|
6620
6830
|
enforce: "post",
|
|
6621
6831
|
config(config, { command }) {
|
|
6622
6832
|
setPackageDetectionCwd(config.root || process.cwd());
|
|
6623
|
-
setTreeShakingBuildMode(command === "build");
|
|
6624
|
-
resetTreeShakingExports();
|
|
6833
|
+
setTreeShakingBuildMode(command === "build", federationOptions);
|
|
6834
|
+
resetTreeShakingExports(federationOptions);
|
|
6625
6835
|
emittedTreeShakingProviders.clear();
|
|
6626
6836
|
const isVinext = hasPackageDependency("vinext");
|
|
6627
6837
|
const isAstro = hasPackageDependency("astro");
|
|
@@ -6637,29 +6847,29 @@ function proxySharedModule(options) {
|
|
|
6637
6847
|
Object.keys(shared).forEach((key) => {
|
|
6638
6848
|
if (key.endsWith("/")) return;
|
|
6639
6849
|
if (useDirectReactImport && key === "react") {
|
|
6640
|
-
addUsedShares(key);
|
|
6850
|
+
addUsedShares(key, federationOptions);
|
|
6641
6851
|
return;
|
|
6642
6852
|
}
|
|
6643
|
-
writeLoadShareModule(key, shared[key], _command, isRolldown);
|
|
6644
|
-
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(key, shared[key]);
|
|
6645
|
-
addUsedShares(key);
|
|
6853
|
+
writeLoadShareModule(key, shared[key], _command, isRolldown, federationOptions);
|
|
6854
|
+
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(key, shared[key], federationOptions);
|
|
6855
|
+
addUsedShares(key, federationOptions);
|
|
6646
6856
|
});
|
|
6647
|
-
writeLocalSharedImportMap();
|
|
6648
|
-
refreshHostAutoInit();
|
|
6857
|
+
writeLocalSharedImportMap(federationOptions);
|
|
6858
|
+
refreshHostAutoInit(federationOptions);
|
|
6649
6859
|
},
|
|
6650
6860
|
buildStart() {
|
|
6651
6861
|
if (_command !== "build") return;
|
|
6652
|
-
resetTreeShakingExports();
|
|
6862
|
+
resetTreeShakingExports(federationOptions);
|
|
6653
6863
|
emittedTreeShakingProviders.clear();
|
|
6654
|
-
refreshTreeShakingModules();
|
|
6864
|
+
refreshTreeShakingModules(federationOptions);
|
|
6655
6865
|
},
|
|
6656
6866
|
shouldTransformCachedModule() {
|
|
6657
6867
|
return _command === "build" && Object.values(shared).some((share) => !!share.shareConfig.treeShaking);
|
|
6658
6868
|
},
|
|
6659
6869
|
transform(code, id) {
|
|
6660
6870
|
if (_command !== "build" || !Object.keys(shared).some((key) => shared[key].shareConfig.treeShaking)) return;
|
|
6661
|
-
collectTreeShakingImports(code, id, shared, findSharedKeyForSource, recordTreeShakingExports, markTreeShakingPackageUnsafe);
|
|
6662
|
-
refreshTreeShakingModules();
|
|
6871
|
+
collectTreeShakingImports(code, id, shared, findSharedKeyForSource, (sharedKey, exports, request) => recordTreeShakingExports(sharedKey, exports, request, federationOptions), (sharedKey, request) => markTreeShakingPackageUnsafe(sharedKey, request, federationOptions));
|
|
6872
|
+
refreshTreeShakingModules(federationOptions);
|
|
6663
6873
|
}
|
|
6664
6874
|
},
|
|
6665
6875
|
{
|
|
@@ -6719,14 +6929,14 @@ function proxySharedModule(options) {
|
|
|
6719
6929
|
if (shouldSkipTaggedImporterProxy(key, "__loadShare__")) return;
|
|
6720
6930
|
if (shouldSkipTaggedImporterProxy(key, "__prebuild__")) return;
|
|
6721
6931
|
const shareSource = key === "vue" && source.startsWith("vue/dist/") ? key : isNodeModulePath(source) ? getCommonSharedSubpathFromNodeModulePath(source, key) || key : source;
|
|
6722
|
-
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown);
|
|
6932
|
+
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown, federationOptions);
|
|
6723
6933
|
if (!materializedLoadShareSources.has(shareSource)) {
|
|
6724
6934
|
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();
|
|
6935
|
+
writeLoadShareModule(shareSource, shared[key], _command, useRolldown, federationOptions);
|
|
6936
|
+
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key], federationOptions);
|
|
6937
|
+
addUsedShares(shareSource, federationOptions);
|
|
6938
|
+
writeLocalSharedImportMap(federationOptions);
|
|
6939
|
+
refreshHostAutoInit(federationOptions);
|
|
6730
6940
|
}
|
|
6731
6941
|
return this.resolve(loadSharePath, importer, { skipSelf: true });
|
|
6732
6942
|
}
|
|
@@ -6738,7 +6948,7 @@ function proxySharedModule(options) {
|
|
|
6738
6948
|
if (!source.includes("__prebuild__")) return;
|
|
6739
6949
|
if (source.startsWith(".")) return;
|
|
6740
6950
|
const pkgName = assertModuleFound(PREBUILD_TAG, source).name;
|
|
6741
|
-
const importSource = getPrebuildResolutionSource(pkgName, getPreBuildShareItem(pkgName));
|
|
6951
|
+
const importSource = getPrebuildResolutionSource(pkgName, getPreBuildShareItem(pkgName, federationOptions));
|
|
6742
6952
|
if (_command === "build") return this.resolve(importSource, importer, { skipSelf: true });
|
|
6743
6953
|
const direct = tryResolveFromProjectRoot(importSource);
|
|
6744
6954
|
const directSource = direct && !isNodeModulePath(direct) ? direct : void 0;
|
|
@@ -7395,8 +7605,8 @@ function isWithinDirectory(filePath, directory) {
|
|
|
7395
7605
|
}
|
|
7396
7606
|
//#endregion
|
|
7397
7607
|
//#region src/plugins/pluginVarRemoteEntry.ts
|
|
7398
|
-
const VarRemoteEntry = () => {
|
|
7399
|
-
const mfOptions = getNormalizeModuleFederationOptions();
|
|
7608
|
+
const VarRemoteEntry = (providedOptions) => {
|
|
7609
|
+
const mfOptions = providedOptions ?? getNormalizeModuleFederationOptions();
|
|
7400
7610
|
const { name, varFilename, filename } = mfOptions;
|
|
7401
7611
|
let viteConfig;
|
|
7402
7612
|
return [{
|
|
@@ -7448,7 +7658,7 @@ const VarRemoteEntry = () => {
|
|
|
7448
7658
|
* @returns Complete "var" remoteEntry.js file source
|
|
7449
7659
|
*/
|
|
7450
7660
|
function generateVarRemoteEntry(remoteEntryFile) {
|
|
7451
|
-
const { name, varFilename } =
|
|
7661
|
+
const { name, varFilename } = mfOptions;
|
|
7452
7662
|
const isValidName = isValidVarName(name);
|
|
7453
7663
|
return `
|
|
7454
7664
|
${isValidName ? `var ${name};` : ""}
|
|
@@ -7578,6 +7788,44 @@ var normalizeOptimizeDeps_default = {
|
|
|
7578
7788
|
}
|
|
7579
7789
|
};
|
|
7580
7790
|
//#endregion
|
|
7791
|
+
//#region src/utils/runtimeCapabilityOptimization.ts
|
|
7792
|
+
const RUNTIME_CAPABILITIES = [
|
|
7793
|
+
{
|
|
7794
|
+
option: "disableRemote",
|
|
7795
|
+
define: "FEDERATION_OPTIMIZE_NO_REMOTE"
|
|
7796
|
+
},
|
|
7797
|
+
{
|
|
7798
|
+
option: "disableShared",
|
|
7799
|
+
define: "FEDERATION_OPTIMIZE_NO_SHARED"
|
|
7800
|
+
},
|
|
7801
|
+
{
|
|
7802
|
+
option: "disableSnapshot",
|
|
7803
|
+
define: "FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN"
|
|
7804
|
+
}
|
|
7805
|
+
];
|
|
7806
|
+
function isEquivalentBooleanDefine(value, expected) {
|
|
7807
|
+
return String(value) === JSON.stringify(expected);
|
|
7808
|
+
}
|
|
7809
|
+
function applyRuntimeCapabilityDefines(define, options, { defaultDisableSnapshot, onConflict } = {}) {
|
|
7810
|
+
for (const capability of RUNTIME_CAPABILITIES) {
|
|
7811
|
+
const explicitValue = options[capability.option];
|
|
7812
|
+
const desiredValue = capability.option === "disableSnapshot" ? explicitValue ?? defaultDisableSnapshot : explicitValue;
|
|
7813
|
+
if (desiredValue === void 0) continue;
|
|
7814
|
+
if (!(capability.define in define)) {
|
|
7815
|
+
define[capability.define] = JSON.stringify(desiredValue);
|
|
7816
|
+
continue;
|
|
7817
|
+
}
|
|
7818
|
+
if (explicitValue !== void 0 && !isEquivalentBooleanDefine(define[capability.define], explicitValue)) onConflict?.(`${capability.define} define (${define[capability.define]}) differs from ${capability.option} option (${explicitValue}). The existing define will not be overridden.`);
|
|
7819
|
+
}
|
|
7820
|
+
if (!("FEDERATION_HAS_EXPOSES" in define)) define["FEDERATION_HAS_EXPOSES"] = JSON.stringify(Object.keys(options.exposes).length > 0);
|
|
7821
|
+
}
|
|
7822
|
+
function getRuntimeCapabilityConfigurationWarnings(options) {
|
|
7823
|
+
const warnings = [];
|
|
7824
|
+
if (options.disableRemote && Object.keys(options.remotes).length > 0) warnings.push("disableRemote is true, but remotes are configured. Remote loading will be unavailable at runtime.");
|
|
7825
|
+
if (options.disableShared && Object.keys(options.shared).length > 0) warnings.push("disableShared is true, but shared dependencies are configured. Shared dependency loading will be unavailable at runtime.");
|
|
7826
|
+
return warnings;
|
|
7827
|
+
}
|
|
7828
|
+
//#endregion
|
|
7581
7829
|
//#region src/index.ts
|
|
7582
7830
|
const patchedManualChunks = /* @__PURE__ */ new WeakSet();
|
|
7583
7831
|
const PRELOAD_HELPER_CHUNK = "vite-preload-helper";
|
|
@@ -7662,8 +7910,7 @@ function isFederationHtmlPreloadDependency(dep, includeSharedRuntime = false) {
|
|
|
7662
7910
|
}
|
|
7663
7911
|
function canResolveSharedSubpath(subpath, projectRoot) {
|
|
7664
7912
|
try {
|
|
7665
|
-
createRequire$1(pathToFileURL(path$1.join(projectRoot, "package.json"))).resolve(subpath);
|
|
7666
|
-
return true;
|
|
7913
|
+
return isViteOptimizableEntry(createRequire$1(pathToFileURL(path$1.join(projectRoot, "package.json"))).resolve(subpath));
|
|
7667
7914
|
} catch {
|
|
7668
7915
|
return false;
|
|
7669
7916
|
}
|
|
@@ -7721,15 +7968,10 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
7721
7968
|
const root = config.root || process.cwd();
|
|
7722
7969
|
setPackageDetectionCwd(root);
|
|
7723
7970
|
const isVinext = hasPackageDependency("vinext");
|
|
7724
|
-
|
|
7725
|
-
name: key,
|
|
7726
|
-
entry: r.entry,
|
|
7727
|
-
type: r.type ?? "module"
|
|
7728
|
-
})));
|
|
7729
|
-
initVirtualModules(_command, getRemoteEntryId(options));
|
|
7971
|
+
initVirtualModules(_command, getRemoteEntryId(options), false, options);
|
|
7730
7972
|
const isRolldown = getIsRolldown(this);
|
|
7731
7973
|
if (remotes && Object.keys(remotes).length > 0) {
|
|
7732
|
-
for (const key of Object.keys(remotes)) addUsedRemote(key, key);
|
|
7974
|
+
for (const key of Object.keys(remotes)) addUsedRemote(key, key, options);
|
|
7733
7975
|
if (_command === "serve") {
|
|
7734
7976
|
config.optimizeDeps = config.optimizeDeps || {};
|
|
7735
7977
|
config.optimizeDeps.exclude = config.optimizeDeps.exclude || [];
|
|
@@ -7749,11 +7991,11 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
7749
7991
|
name: "module-federation:optimize-shared-resolver",
|
|
7750
7992
|
load(id) {
|
|
7751
7993
|
if (id !== "module-federation:optimized-require-react") return;
|
|
7752
|
-
const loadSharePath = getLoadShareModulePath("react", isRolldown);
|
|
7994
|
+
const loadSharePath = getLoadShareModulePath("react", isRolldown, options);
|
|
7753
7995
|
const source = JSON.stringify(loadSharePath);
|
|
7754
7996
|
return "import * as __mfShared from " + source + ";\nexport * from " + source + ";\nexport default __mfShared.default ?? __mfShared;";
|
|
7755
7997
|
},
|
|
7756
|
-
resolveId(source, importer,
|
|
7998
|
+
resolveId(source, importer, resolveOptions) {
|
|
7757
7999
|
if (createViteEncodedIdPrefixRegExp("virtual:mf:").test(source)) return {
|
|
7758
8000
|
id: source,
|
|
7759
8001
|
external: true
|
|
@@ -7764,19 +8006,19 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
7764
8006
|
if (isAssetLikeImport(source)) return;
|
|
7765
8007
|
const shareItem = shared[key];
|
|
7766
8008
|
const isReactSingleton = source === "react" && key === "react" && shareItem.shareConfig?.singleton === true;
|
|
7767
|
-
const isReactRequire =
|
|
7768
|
-
if (
|
|
8009
|
+
const isReactRequire = resolveOptions?.kind?.startsWith("require") && isReactSingleton;
|
|
8010
|
+
if (resolveOptions?.kind?.startsWith("require") && !isReactSingleton) return;
|
|
7769
8011
|
if (isCommonJsImporter(importer) && !isReactSingleton) return;
|
|
7770
8012
|
if (isReactRequire) {
|
|
7771
|
-
writeLoadShareModule(source, shareItem, _command, isRolldown);
|
|
7772
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(source, shareItem);
|
|
7773
|
-
addUsedShares(source);
|
|
8013
|
+
writeLoadShareModule(source, shareItem, _command, isRolldown, options);
|
|
8014
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(source, shareItem, options);
|
|
8015
|
+
addUsedShares(source, options);
|
|
7774
8016
|
return { id: "module-federation:optimized-require-react" };
|
|
7775
8017
|
}
|
|
7776
|
-
const loadSharePath = getLoadShareModulePath(source, isRolldown);
|
|
7777
|
-
writeLoadShareModule(source, shareItem, _command, isRolldown);
|
|
7778
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(source, shareItem);
|
|
7779
|
-
addUsedShares(source);
|
|
8018
|
+
const loadSharePath = getLoadShareModulePath(source, isRolldown, options);
|
|
8019
|
+
writeLoadShareModule(source, shareItem, _command, isRolldown, options);
|
|
8020
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(source, shareItem, options);
|
|
8021
|
+
addUsedShares(source, options);
|
|
7780
8022
|
return {
|
|
7781
8023
|
id: loadSharePath,
|
|
7782
8024
|
external: true
|
|
@@ -7810,15 +8052,15 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
7810
8052
|
const key = findSharedKey(args.path, shared);
|
|
7811
8053
|
if (!key) return;
|
|
7812
8054
|
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);
|
|
8055
|
+
const loadSharePath = getLoadShareModulePath(args.path, isRolldown, options);
|
|
8056
|
+
writeLoadShareModule(args.path, shareItem, _command, isRolldown, options);
|
|
8057
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(args.path, shareItem, options);
|
|
8058
|
+
addUsedShares(args.path, options);
|
|
7817
8059
|
return {
|
|
7818
8060
|
loader: "js",
|
|
7819
8061
|
resolveDir: root,
|
|
7820
|
-
contents: `import * as __mfShared from ${JSON.stringify(
|
|
7821
|
-
export * from ${JSON.stringify(
|
|
8062
|
+
contents: `import * as __mfShared from ${JSON.stringify(loadSharePath)};
|
|
8063
|
+
export * from ${JSON.stringify(loadSharePath)};
|
|
7822
8064
|
export default __mfShared.default ?? __mfShared;`
|
|
7823
8065
|
};
|
|
7824
8066
|
});
|
|
@@ -7834,7 +8076,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
7834
8076
|
optimizeDeps.include ??= [];
|
|
7835
8077
|
optimizeDeps.exclude ??= [];
|
|
7836
8078
|
for (const subpath of getCommonSharedSubpaths(key)) {
|
|
7837
|
-
writePreBuildLibPath(subpath, shareItem);
|
|
8079
|
+
writePreBuildLibPath(subpath, shareItem, options);
|
|
7838
8080
|
if (canResolveSharedSubpath(subpath, root)) optimizeDeps.include.push(subpath);
|
|
7839
8081
|
else optimizeDeps.exclude.push(subpath);
|
|
7840
8082
|
}
|
|
@@ -7842,32 +8084,37 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
7842
8084
|
continue;
|
|
7843
8085
|
}
|
|
7844
8086
|
if (isVinext && key === "react") {
|
|
7845
|
-
addUsedShares(key);
|
|
8087
|
+
addUsedShares(key, options);
|
|
7846
8088
|
continue;
|
|
7847
8089
|
}
|
|
7848
|
-
getLoadShareModulePath(key, isRolldown);
|
|
7849
|
-
writeLoadShareModule(key, shareItem, _command, isRolldown);
|
|
7850
|
-
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(key, shareItem);
|
|
7851
|
-
addUsedShares(key);
|
|
8090
|
+
getLoadShareModulePath(key, isRolldown, options);
|
|
8091
|
+
writeLoadShareModule(key, shareItem, _command, isRolldown, options);
|
|
8092
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(key, shareItem, options);
|
|
8093
|
+
addUsedShares(key, options);
|
|
7852
8094
|
if (_command === "serve" && shareItem.shareConfig?.import !== false) {
|
|
7853
8095
|
const optimizeDeps = config.optimizeDeps ??= {};
|
|
7854
8096
|
optimizeDeps.include ??= [];
|
|
7855
8097
|
optimizeDeps.exclude ??= [];
|
|
7856
|
-
const shouldBypassOptimizeDep = isLitShare(key);
|
|
8098
|
+
const shouldBypassOptimizeDep = isLitShare(key) || !canResolveSharedSubpath(key, root);
|
|
7857
8099
|
if (optimizeDeps.include.includes(key)) optimizeDeps.exclude = optimizeDeps.exclude.filter((dep) => dep !== key);
|
|
7858
8100
|
else if (shouldBypassOptimizeDep) optimizeDeps.exclude.push(key);
|
|
7859
8101
|
else optimizeDeps.include.push(key);
|
|
7860
8102
|
for (const subpath of getCommonSharedSubpaths(key)) {
|
|
7861
|
-
|
|
7862
|
-
|
|
7863
|
-
|
|
7864
|
-
|
|
7865
|
-
|
|
8103
|
+
const canResolveSubpath = canResolveSharedSubpath(subpath, root);
|
|
8104
|
+
if (subpath === "react/compiler-runtime" && !canResolveSubpath) {
|
|
8105
|
+
optimizeDeps.exclude.push(subpath);
|
|
8106
|
+
continue;
|
|
8107
|
+
}
|
|
8108
|
+
getLoadShareModulePath(subpath, isRolldown, options);
|
|
8109
|
+
writeLoadShareModule(subpath, shareItem, _command, isRolldown, options);
|
|
8110
|
+
writePreBuildLibPath(subpath, shareItem, options);
|
|
8111
|
+
addUsedShares(subpath, options);
|
|
8112
|
+
if (canResolveSubpath) optimizeDeps.include.push(subpath);
|
|
7866
8113
|
else optimizeDeps.exclude.push(subpath);
|
|
7867
8114
|
}
|
|
7868
8115
|
}
|
|
7869
8116
|
}
|
|
7870
|
-
writeLocalSharedImportMap();
|
|
8117
|
+
writeLocalSharedImportMap(options);
|
|
7871
8118
|
}
|
|
7872
8119
|
if (_command === "serve") {
|
|
7873
8120
|
config.optimizeDeps ??= {};
|
|
@@ -7910,9 +8157,18 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
7910
8157
|
};
|
|
7911
8158
|
}
|
|
7912
8159
|
const SSR_ONLY_PLUGINS = new Set(["@module-federation/vite/ssrEntryLoader"]);
|
|
8160
|
+
function applyBuildTimeRuntimeDefines(define, options, { target, isAstro, defaultDisableSnapshot }) {
|
|
8161
|
+
const envTargetDefineValue = !options.target && isAstro ? "undefined" : JSON.stringify(target);
|
|
8162
|
+
if (!("ENV_TARGET" in define)) define.ENV_TARGET = envTargetDefineValue;
|
|
8163
|
+
applyRuntimeCapabilityDefines(define, options, {
|
|
8164
|
+
defaultDisableSnapshot,
|
|
8165
|
+
onConflict: mfWarn
|
|
8166
|
+
});
|
|
8167
|
+
if (options.target && define.ENV_TARGET !== JSON.stringify(options.target)) mfWarn(`ENV_TARGET define (${define.ENV_TARGET}) differs from target option ("${options.target}"). ENV_TARGET will not be overridden.`);
|
|
8168
|
+
}
|
|
7913
8169
|
function loadPluginDts(options) {
|
|
7914
8170
|
if (options.dts === false) return [];
|
|
7915
|
-
return [import("./pluginDts-
|
|
8171
|
+
return [import("./pluginDts-Cd2opGcZ.js").then((n) => n.n).then(({ default: pluginDts }) => pluginDts(options))];
|
|
7916
8172
|
}
|
|
7917
8173
|
function federation(mfUserOptions) {
|
|
7918
8174
|
if (isTestEnv()) return [];
|
|
@@ -7926,6 +8182,7 @@ function federation(mfUserOptions) {
|
|
|
7926
8182
|
let command;
|
|
7927
8183
|
let desiredRolldownOutput;
|
|
7928
8184
|
let isSsrBuild = false;
|
|
8185
|
+
const emittedRuntimeCapabilityWarnings = /* @__PURE__ */ new Set();
|
|
7929
8186
|
return [
|
|
7930
8187
|
{
|
|
7931
8188
|
name: "vite:module-federation-virtual-modules",
|
|
@@ -7939,8 +8196,9 @@ function federation(mfUserOptions) {
|
|
|
7939
8196
|
command,
|
|
7940
8197
|
isRolldown: getIsRolldown(this),
|
|
7941
8198
|
findSharedKey,
|
|
7942
|
-
addUsedShares,
|
|
7943
|
-
writeLocalSharedImportMap
|
|
8199
|
+
addUsedShares: (pkg) => addUsedShares(pkg, options),
|
|
8200
|
+
writeLocalSharedImportMap: () => writeLocalSharedImportMap(options),
|
|
8201
|
+
federationOptions: options
|
|
7944
8202
|
});
|
|
7945
8203
|
virtualModule = VirtualModule.findById(id);
|
|
7946
8204
|
}
|
|
@@ -7981,7 +8239,7 @@ function federation(mfUserOptions) {
|
|
|
7981
8239
|
},
|
|
7982
8240
|
configResolved() {
|
|
7983
8241
|
const ssrCapabilities = getSsrCapabilities(parseInt(version, 10), command, Object.keys(options.remotes).length > 0);
|
|
7984
|
-
initVirtualModules(command, remoteEntryId, ssrCapabilities.enableSsrInitBootstrap);
|
|
8242
|
+
initVirtualModules(command, remoteEntryId, ssrCapabilities.enableSsrInitBootstrap, options);
|
|
7985
8243
|
}
|
|
7986
8244
|
},
|
|
7987
8245
|
aliasToArrayPlugin_default,
|
|
@@ -8004,18 +8262,21 @@ function federation(mfUserOptions) {
|
|
|
8004
8262
|
...addEntry({
|
|
8005
8263
|
entryName: "remoteEntry",
|
|
8006
8264
|
entryPath: remoteEntryId,
|
|
8007
|
-
fileName: filename
|
|
8265
|
+
fileName: filename,
|
|
8266
|
+
federationOptions: options
|
|
8008
8267
|
}),
|
|
8009
8268
|
...addEntry({
|
|
8010
8269
|
entryName: "hostInit",
|
|
8011
|
-
entryPath: () => getHostAutoInitPath(),
|
|
8270
|
+
entryPath: () => getHostAutoInitPath(options),
|
|
8012
8271
|
inject: hostInitInjectLocation,
|
|
8013
8272
|
forceClientInjected: Object.keys(options.exposes).length > 0,
|
|
8014
|
-
skipTransformFor: Object.values(options.exposes).map((expose) => expose.import)
|
|
8273
|
+
skipTransformFor: Object.values(options.exposes).map((expose) => expose.import),
|
|
8274
|
+
federationOptions: options
|
|
8015
8275
|
}),
|
|
8016
8276
|
...addEntry({
|
|
8017
8277
|
entryName: "virtualExposes",
|
|
8018
|
-
entryPath: virtualExposesId
|
|
8278
|
+
entryPath: virtualExposesId,
|
|
8279
|
+
federationOptions: options
|
|
8019
8280
|
}),
|
|
8020
8281
|
pluginProxyRemoteEntry_default({
|
|
8021
8282
|
options,
|
|
@@ -8025,20 +8286,23 @@ function federation(mfUserOptions) {
|
|
|
8025
8286
|
pluginProxyRemotes_default(options),
|
|
8026
8287
|
pluginRemoteNamedExports(options),
|
|
8027
8288
|
...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__");
|
|
8289
|
+
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
8290
|
}, {
|
|
8030
8291
|
moduleParseTimeout: options.moduleParseTimeout,
|
|
8031
8292
|
moduleParseIdleTimeout: options.moduleParseIdleTimeout,
|
|
8032
8293
|
exposedModuleImports: Object.values(options.exposes).map((expose) => expose.import)
|
|
8033
8294
|
}),
|
|
8034
|
-
...proxySharedModule({
|
|
8295
|
+
...proxySharedModule({
|
|
8296
|
+
shared,
|
|
8297
|
+
federationOptions: options
|
|
8298
|
+
}),
|
|
8035
8299
|
{
|
|
8036
8300
|
name: "module-federation-esm-shims",
|
|
8037
8301
|
enforce: "pre",
|
|
8038
8302
|
apply: "build",
|
|
8039
8303
|
config(config) {
|
|
8040
8304
|
isSsrBuild = config.build?.ssr === true;
|
|
8041
|
-
const runtimeInitId =
|
|
8305
|
+
const runtimeInitId = getRuntimeInitStatusImportId(options);
|
|
8042
8306
|
config.build = config.build || {};
|
|
8043
8307
|
if (config.build.modulePreload !== false) {
|
|
8044
8308
|
const currentModulePreload = config.build.modulePreload && typeof config.build.modulePreload === "object" ? config.build.modulePreload : {};
|
|
@@ -8085,7 +8349,7 @@ function federation(mfUserOptions) {
|
|
|
8085
8349
|
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
8350
|
}
|
|
8087
8351
|
const mfChunkName = function(id) {
|
|
8088
|
-
if (id.includes(runtimeInitId)) return "runtimeInit";
|
|
8352
|
+
if (id.includes(runtimeInitId) || id.includes("__mf_v__runtimeInit__mf_v__")) return "runtimeInit";
|
|
8089
8353
|
if (id.includes("__loadShare__")) {
|
|
8090
8354
|
const match = id.match(/([^/\\]+__loadShare__[^/\\]+)/);
|
|
8091
8355
|
return match ? match[1] : "loadShare";
|
|
@@ -8261,11 +8525,17 @@ function federation(mfUserOptions) {
|
|
|
8261
8525
|
}
|
|
8262
8526
|
const isAstro = hasPackageDependency("astro");
|
|
8263
8527
|
const resolvedTarget = options.target ?? (config.build?.ssr ? "node" : "web");
|
|
8264
|
-
const envTargetDefineValue = !options.target && isAstro ? "undefined" : JSON.stringify(resolvedTarget);
|
|
8265
8528
|
if (!config.define) config.define = {};
|
|
8266
|
-
|
|
8267
|
-
|
|
8268
|
-
|
|
8529
|
+
applyBuildTimeRuntimeDefines(config.define, options, {
|
|
8530
|
+
target: resolvedTarget,
|
|
8531
|
+
isAstro,
|
|
8532
|
+
defaultDisableSnapshot: resolvedTarget === "node" ? true : void 0
|
|
8533
|
+
});
|
|
8534
|
+
for (const warning of getRuntimeCapabilityConfigurationWarnings(options)) {
|
|
8535
|
+
if (emittedRuntimeCapabilityWarnings.has(warning)) continue;
|
|
8536
|
+
emittedRuntimeCapabilityWarnings.add(warning);
|
|
8537
|
+
mfWarn(warning);
|
|
8538
|
+
}
|
|
8269
8539
|
},
|
|
8270
8540
|
configResolved(config) {
|
|
8271
8541
|
if (!hasPackageDependency("nitro")) return;
|
|
@@ -8275,16 +8545,17 @@ function federation(mfUserOptions) {
|
|
|
8275
8545
|
configEnvironment(name, config) {
|
|
8276
8546
|
if (!(config.consumer === "server" || name === "ssr" || name === "server" || config.build?.ssr === true)) return;
|
|
8277
8547
|
const isAstro = hasPackageDependency("astro");
|
|
8278
|
-
const envTargetDefineValue = !options.target && isAstro ? "undefined" : JSON.stringify(options.target ?? "node");
|
|
8279
8548
|
config.define = { ...config.define ?? {} };
|
|
8280
|
-
|
|
8281
|
-
|
|
8282
|
-
|
|
8549
|
+
applyBuildTimeRuntimeDefines(config.define, options, {
|
|
8550
|
+
target: options.target ?? "node",
|
|
8551
|
+
isAstro,
|
|
8552
|
+
defaultDisableSnapshot: true
|
|
8553
|
+
});
|
|
8283
8554
|
}
|
|
8284
8555
|
},
|
|
8285
|
-
...Manifest(),
|
|
8556
|
+
...Manifest(options),
|
|
8286
8557
|
...pluginSSRRemoteEntry(options),
|
|
8287
|
-
...VarRemoteEntry(),
|
|
8558
|
+
...VarRemoteEntry(options),
|
|
8288
8559
|
{
|
|
8289
8560
|
name: "module-federation-vinext-fix-rsc-preload-as",
|
|
8290
8561
|
enforce: "post",
|