@module-federation/vite 1.16.2 → 1.16.3
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/lib/index.cjs +249 -45
- package/lib/index.d.cts +1 -1
- package/lib/index.d.mts +1 -1
- package/lib/index.mjs +249 -45
- package/package.json +16 -16
package/lib/index.cjs
CHANGED
|
@@ -418,24 +418,51 @@ function getSuffix(name) {
|
|
|
418
418
|
}
|
|
419
419
|
const patternMap = {};
|
|
420
420
|
const cacheMap = {};
|
|
421
|
+
const VITE_ID_PREFIX = "/@id/";
|
|
422
|
+
const VITE_ENCODED_NULL_BYTE_PREFIX = `${VITE_ID_PREFIX}__x00__`;
|
|
423
|
+
function escapeRegExp$1(value) {
|
|
424
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
425
|
+
}
|
|
426
|
+
function createViteEncodedIdPrefixRegExp(sourcePrefix = "") {
|
|
427
|
+
return new RegExp(`^(?:${escapeRegExp$1(VITE_ENCODED_NULL_BYTE_PREFIX)})?${sourcePrefix}`);
|
|
428
|
+
}
|
|
429
|
+
function toViteEncodedId(id) {
|
|
430
|
+
return `${VITE_ENCODED_NULL_BYTE_PREFIX}${id}`;
|
|
431
|
+
}
|
|
432
|
+
function decodeViteId(id) {
|
|
433
|
+
if (!id.startsWith("/@id/")) return id;
|
|
434
|
+
const viteId = id.slice(5);
|
|
435
|
+
return viteId.startsWith("__x00__") ? `\0${viteId.slice(7)}` : viteId;
|
|
436
|
+
}
|
|
421
437
|
function assertModuleFound(tag, str = "") {
|
|
422
438
|
const module = VirtualModule.findModule(tag, str);
|
|
423
439
|
if (!module) throw require_packageUtils.createModuleFederationError(`Module Federation shared module '${str}' not found. Please ensure it's installed as a dependency in your package.json.`);
|
|
424
440
|
return module;
|
|
425
441
|
}
|
|
426
|
-
|
|
442
|
+
function normalizeVirtualModuleId(id) {
|
|
443
|
+
const decoded = decodeViteId(id).replace(/^\0+/, "");
|
|
444
|
+
const queryIndex = decoded.indexOf("?");
|
|
445
|
+
const hashIndex = decoded.indexOf("#");
|
|
446
|
+
const endIndex = queryIndex === -1 ? hashIndex : hashIndex === -1 ? queryIndex : Math.min(queryIndex, hashIndex);
|
|
447
|
+
return endIndex === -1 ? decoded : decoded.slice(0, endIndex);
|
|
448
|
+
}
|
|
449
|
+
var VirtualModule = class VirtualModule {
|
|
427
450
|
name;
|
|
428
451
|
tag;
|
|
429
452
|
suffix;
|
|
430
453
|
inited = false;
|
|
431
454
|
code;
|
|
432
|
-
static
|
|
455
|
+
static findName(tag, str = "") {
|
|
433
456
|
if (!patternMap[tag]) patternMap[tag] = new RegExp(`(.*${require_packageUtils.packageNameEncode(tag)}(.+?)${require_packageUtils.packageNameEncode(tag)}.*)`);
|
|
434
|
-
const moduleName = (str.match(patternMap[tag]) || [])[2];
|
|
435
|
-
|
|
457
|
+
const moduleName = (normalizeVirtualModuleId(str).match(patternMap[tag]) || [])[2];
|
|
458
|
+
return moduleName ? require_packageUtils.packageNameDecode(moduleName) : void 0;
|
|
459
|
+
}
|
|
460
|
+
static findModule(tag, str = "") {
|
|
461
|
+
const moduleName = VirtualModule.findName(tag, str);
|
|
462
|
+
return moduleName ? cacheMap[tag][moduleName] : void 0;
|
|
436
463
|
}
|
|
437
464
|
static findById(id) {
|
|
438
|
-
const normalized =
|
|
465
|
+
const normalized = normalizeVirtualModuleId(id);
|
|
439
466
|
for (const modules of Object.values(cacheMap)) for (const module of Object.values(modules)) if (module.getImportId() === normalized) return module;
|
|
440
467
|
}
|
|
441
468
|
constructor(name, tag = "__mf_v__", suffix = "") {
|
|
@@ -749,11 +776,9 @@ function getPackageEsmEntryPath(pkg) {
|
|
|
749
776
|
resolveSubpathWithRequire: false
|
|
750
777
|
}) || resolvePackageEntryFromProjectRoot(pkg);
|
|
751
778
|
}
|
|
752
|
-
function
|
|
779
|
+
function getEsmNamedExportsFromFile(entryPath) {
|
|
753
780
|
let source = "";
|
|
754
|
-
let entryPath;
|
|
755
781
|
try {
|
|
756
|
-
entryPath = getPackageEsmEntryPath(pkg);
|
|
757
782
|
if (!entryPath) return [];
|
|
758
783
|
const { initSync, parse } = localRequire("es-module-lexer");
|
|
759
784
|
initSync();
|
|
@@ -768,6 +793,48 @@ function getEsmNamedExports(pkg) {
|
|
|
768
793
|
return source ? getNamedExportsViaRegex(source, entryPath) : [];
|
|
769
794
|
}
|
|
770
795
|
}
|
|
796
|
+
function getEsmNamedExports(pkg) {
|
|
797
|
+
return getEsmNamedExportsFromFile(getPackageEsmEntryPath(pkg));
|
|
798
|
+
}
|
|
799
|
+
function resolveConfiguredImportPath(importSource) {
|
|
800
|
+
if (pathe.default.isAbsolute(importSource)) return resolveFileLikeModule(importSource);
|
|
801
|
+
const projectRoot = require_packageUtils.getPackageDetectionCwd();
|
|
802
|
+
if (importSource.startsWith(".")) return resolveFileLikeModule(pathe.default.resolve(projectRoot, importSource));
|
|
803
|
+
const esmEntry = require_packageUtils.getInstalledPackageEntry(importSource, {
|
|
804
|
+
conditions: [
|
|
805
|
+
"browser",
|
|
806
|
+
"import",
|
|
807
|
+
"module",
|
|
808
|
+
"default"
|
|
809
|
+
],
|
|
810
|
+
resolveSubpathWithRequire: false
|
|
811
|
+
});
|
|
812
|
+
if (esmEntry) return esmEntry;
|
|
813
|
+
try {
|
|
814
|
+
return (0, module$1.createRequire)(new URL(`file://${pathe.default.join(projectRoot, "package.json")}`)).resolve(importSource);
|
|
815
|
+
} catch {
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
function resolveFileLikeModule(filePath) {
|
|
820
|
+
if ((0, fs.existsSync)(filePath) && !(0, fs.statSync)(filePath).isDirectory()) return filePath;
|
|
821
|
+
const extensions = [
|
|
822
|
+
".ts",
|
|
823
|
+
".tsx",
|
|
824
|
+
".js",
|
|
825
|
+
".jsx",
|
|
826
|
+
".mjs",
|
|
827
|
+
".mts"
|
|
828
|
+
];
|
|
829
|
+
for (const ext of extensions) {
|
|
830
|
+
const candidate = filePath + ext;
|
|
831
|
+
if ((0, fs.existsSync)(candidate) && !(0, fs.statSync)(candidate).isDirectory()) return candidate;
|
|
832
|
+
}
|
|
833
|
+
for (const ext of extensions) {
|
|
834
|
+
const candidate = pathe.default.join(filePath, "index" + ext);
|
|
835
|
+
if ((0, fs.existsSync)(candidate) && !(0, fs.statSync)(candidate).isDirectory()) return candidate;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
771
838
|
function resolveRelativeModule(filePath, specifier) {
|
|
772
839
|
const dir = pathe.default.dirname(filePath);
|
|
773
840
|
const exact = pathe.default.resolve(dir, specifier);
|
|
@@ -837,6 +904,14 @@ function getPackageNamedExports(pkg) {
|
|
|
837
904
|
return getEsmNamedExports(pkg);
|
|
838
905
|
}
|
|
839
906
|
}
|
|
907
|
+
function getSharedNamedExports(pkg, shareItem) {
|
|
908
|
+
const configuredImport = shareItem?.shareConfig.import;
|
|
909
|
+
if (typeof configuredImport === "string") {
|
|
910
|
+
const configuredNamedExports = getEsmNamedExportsFromFile(resolveConfiguredImportPath(configuredImport));
|
|
911
|
+
if (configuredNamedExports.length > 0) return configuredNamedExports;
|
|
912
|
+
}
|
|
913
|
+
return getPackageNamedExports(pkg);
|
|
914
|
+
}
|
|
840
915
|
function getLocalProviderImportPath(pkg) {
|
|
841
916
|
try {
|
|
842
917
|
const resolved = (0, module$1.createRequire)(new URL(`file://${pathe.default.join(require_packageUtils.getPackageDetectionCwd(), "package.json")}`)).resolve(pkg);
|
|
@@ -907,6 +982,20 @@ function writePreBuildLibPath(pkg, shareItem) {
|
|
|
907
982
|
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(pkg, PREBUILD_TAG, ".js");
|
|
908
983
|
preBuildShareItemMap[pkg] = shareItem;
|
|
909
984
|
const importSource = getConcreteSharedImportSource(pkg, shareItem) || pkg;
|
|
985
|
+
if (pkg === "react/compiler-runtime") {
|
|
986
|
+
preBuildCacheMap[pkg].writeSync(`
|
|
987
|
+
const __mfCacheGlobalKey = "__mf_module_cache__";
|
|
988
|
+
export const c = function(size) {
|
|
989
|
+
const cache = globalThis[__mfCacheGlobalKey]?.share;
|
|
990
|
+
const sharedReact = cache?.['react'];
|
|
991
|
+
const reactExports = sharedReact?.default ?? sharedReact;
|
|
992
|
+
const internals = reactExports?.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
|
|
993
|
+
return internals?.H?.useMemoCache(size);
|
|
994
|
+
};
|
|
995
|
+
export default { c };
|
|
996
|
+
`, true);
|
|
997
|
+
return;
|
|
998
|
+
}
|
|
910
999
|
if (pkg === "react/jsx-dev-runtime") {
|
|
911
1000
|
preBuildCacheMap[pkg].writeSync(`
|
|
912
1001
|
import __mfPrebuildDefault from ${escapeGeneratedStringLiteral(importSource)};
|
|
@@ -930,7 +1019,7 @@ function writePreBuildLibPath(pkg, shareItem) {
|
|
|
930
1019
|
`, true);
|
|
931
1020
|
return;
|
|
932
1021
|
}
|
|
933
|
-
const namedExports =
|
|
1022
|
+
const namedExports = getSharedNamedExports(pkg, shareItem);
|
|
934
1023
|
if (namedExports.length > 0) {
|
|
935
1024
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
936
1025
|
const declarations = namedExports.map((name, i) => `const ${namedExportVars[i]} = __mfPrebuildExports[${escapeGeneratedStringLiteral(name)}];`).join("\n ");
|
|
@@ -970,6 +1059,27 @@ function getLoadShareModulePath(pkg, isRolldown) {
|
|
|
970
1059
|
if (!loadShareCacheMap[pkg]) getLoadShareImportId(pkg, isRolldown);
|
|
971
1060
|
return loadShareCacheMap[pkg].getImportId();
|
|
972
1061
|
}
|
|
1062
|
+
function toViteOptimizedDepVirtualId(id) {
|
|
1063
|
+
return toViteEncodedId(id);
|
|
1064
|
+
}
|
|
1065
|
+
function getCachedLoadSharePkg(id) {
|
|
1066
|
+
const normalized = normalizeVirtualModuleId(id);
|
|
1067
|
+
if (!normalized.startsWith("virtual:mf:")) return;
|
|
1068
|
+
const pkg = VirtualModule.findName(LOAD_SHARE_TAG, normalized);
|
|
1069
|
+
if (!pkg) return;
|
|
1070
|
+
return pkg;
|
|
1071
|
+
}
|
|
1072
|
+
function materializeCachedLoadShareModule(options) {
|
|
1073
|
+
const pkg = getCachedLoadSharePkg(options.id);
|
|
1074
|
+
if (!pkg) return;
|
|
1075
|
+
const key = options.findSharedKey(pkg, options.shared);
|
|
1076
|
+
if (!key) return;
|
|
1077
|
+
const shareItem = options.shared[key];
|
|
1078
|
+
writeLoadShareModule(pkg, shareItem, options.command, options.isRolldown);
|
|
1079
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(pkg, shareItem);
|
|
1080
|
+
options.addUsedShares(pkg);
|
|
1081
|
+
options.writeLocalSharedImportMap();
|
|
1082
|
+
}
|
|
973
1083
|
function generateDeferredHostProvidedExports(namedExports, pkg, cacheKey) {
|
|
974
1084
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
975
1085
|
const declarations = ["let __mf_default;", ...namedExportVars.map((name) => `let ${name};`)].join("\n ");
|
|
@@ -1037,7 +1147,7 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
1037
1147
|
const lazyLocalFallbackSource = concreteSharedImportSource || localProviderPath || sharedImportSource;
|
|
1038
1148
|
const skipServePrebuildWarmup = command !== "build" && (pkg === "lit" || pkg.startsWith("lit/"));
|
|
1039
1149
|
const usesLazyLocalFallback = isWorkspacePackage && shareItem.shareConfig.singleton === true;
|
|
1040
|
-
const namedExports =
|
|
1150
|
+
const namedExports = getSharedNamedExports(pkg, shareItem);
|
|
1041
1151
|
let exportLine;
|
|
1042
1152
|
if (namedExports.length > 0) {
|
|
1043
1153
|
const destructure = `const { ${namedExports.map((name, i) => `${name}: __mf_${i}`).join(", ")} } = exportModule;`;
|
|
@@ -1137,7 +1247,7 @@ function generateLocalSharedImportMap() {
|
|
|
1137
1247
|
version: ${JSON.stringify(shareItem.version)},
|
|
1138
1248
|
scope: [${JSON.stringify(shareItem.scope)}],
|
|
1139
1249
|
loaded: false,
|
|
1140
|
-
from: ${JSON.stringify(options.
|
|
1250
|
+
from: ${JSON.stringify(options.name)},
|
|
1141
1251
|
async get () {
|
|
1142
1252
|
if (${shareItem.shareConfig.import === false}) {
|
|
1143
1253
|
throw new Error(\`[Module Federation] Shared module '\${${JSON.stringify(key)}}' must be provided by host\`);
|
|
@@ -1308,7 +1418,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1308
1418
|
${getRuntimeModuleCacheBootstrapCode()}
|
|
1309
1419
|
const initTokens = {}
|
|
1310
1420
|
const shareScopeName = ${JSON.stringify(options.shareScope)}
|
|
1311
|
-
const mfName = ${JSON.stringify(options.
|
|
1421
|
+
const mfName = ${JSON.stringify(options.name)}
|
|
1312
1422
|
let localSharedImportMapPromise
|
|
1313
1423
|
let exposesMapPromise
|
|
1314
1424
|
const shouldRetrySharedInitError = ${command !== "build"} && ((error) => {
|
|
@@ -1350,6 +1460,28 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1350
1460
|
|
|
1351
1461
|
async function init(shared = {}, initScope = []) {
|
|
1352
1462
|
const {usedShared, usedRemotes} = await getLocalSharedImportMap()
|
|
1463
|
+
try {
|
|
1464
|
+
const allInstances = globalThis.__FEDERATION__?.__SHARE__;
|
|
1465
|
+
if (allInstances) {
|
|
1466
|
+
${normalizeRuntimeShareCode}
|
|
1467
|
+
for (const [, scopes] of Object.entries(allInstances)) {
|
|
1468
|
+
const scopeShare = scopes?.['${options.shareScope}'];
|
|
1469
|
+
if (!scopeShare) continue;
|
|
1470
|
+
for (const [pkg, versionMap] of Object.entries(scopeShare)) {
|
|
1471
|
+
for (const [version, provider] of Object.entries(versionMap)) {
|
|
1472
|
+
if (!provider.lib) continue;
|
|
1473
|
+
const cacheKey = provider.shareConfig?.singleton ? pkg : \`\${pkg}@\${version}\`;
|
|
1474
|
+
if (__mfModuleCache.share[cacheKey] !== undefined) continue;
|
|
1475
|
+
const mod = typeof provider.lib === "function" ? provider.lib() : provider.lib;
|
|
1476
|
+
const resolved = await Promise.resolve(mod);
|
|
1477
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
} catch (e) {
|
|
1483
|
+
console.error('[Module Federation] Failed to bridge external shared modules', e)
|
|
1484
|
+
}
|
|
1353
1485
|
${generateDirectSharedCacheSeedCode(command)}
|
|
1354
1486
|
const __browserPlugins = [${pluginImportNames.filter((item) => !isSsrOnlyPlugin(item[1])).map((item) => `${item[0]}(${item[2]})`).join(", ")}];
|
|
1355
1487
|
const __ssrPlugins = typeof globalThis.window === 'undefined'
|
|
@@ -1806,7 +1938,7 @@ const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClien
|
|
|
1806
1938
|
return inject === "entry" || !htmlFilePath;
|
|
1807
1939
|
}
|
|
1808
1940
|
function normalizeDevHtmlProxyId(id) {
|
|
1809
|
-
return id.replace(/^\0/, "")
|
|
1941
|
+
return decodeViteId(id).replace(/^\0/, "");
|
|
1810
1942
|
}
|
|
1811
1943
|
function normalizeModuleId(id) {
|
|
1812
1944
|
return id.split("?")[0].replace(/\\/g, "/");
|
|
@@ -1840,7 +1972,7 @@ const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClien
|
|
|
1840
1972
|
configResolved(config) {
|
|
1841
1973
|
viteConfig = config;
|
|
1842
1974
|
const resolvedEntryPath = getEntryPath();
|
|
1843
|
-
if (resolvedEntryPath.startsWith("virtual:mf")) devEntryPath = config.base +
|
|
1975
|
+
if (resolvedEntryPath.startsWith("virtual:mf")) devEntryPath = config.base + VITE_ID_PREFIX.slice(1) + resolvedEntryPath;
|
|
1844
1976
|
else {
|
|
1845
1977
|
const normalized = resolvedEntryPath.replace(/\\\\?/g, "/");
|
|
1846
1978
|
const root = config.root.replace(/\\\\?/g, "/").replace(/\/$/, "");
|
|
@@ -1881,10 +2013,10 @@ const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClien
|
|
|
1881
2013
|
const base = viteConfig.base.replace(/\/$/, "");
|
|
1882
2014
|
const stripBase = (p) => base && p.startsWith(base + "/") ? p.slice(base.length) : p;
|
|
1883
2015
|
const html = rewriteEntryScripts(c, (originalSrc) => {
|
|
1884
|
-
return
|
|
2016
|
+
return toViteEncodedId(`${DEV_HTML_PROXY_PREFIX}${new URLSearchParams({
|
|
1885
2017
|
init: sanitizeDevEntryPath(stripBase(devEntryPath)),
|
|
1886
2018
|
entry: sanitizeDevEntryPath(stripBase(originalSrc))
|
|
1887
|
-
}).toString()}
|
|
2019
|
+
}).toString()}`);
|
|
1888
2020
|
});
|
|
1889
2021
|
return html === c ? injectEntryScript(c, stripBase(devEntryPath)) : html;
|
|
1890
2022
|
}
|
|
@@ -2830,9 +2962,19 @@ const processModuleAssets = (bundle, filesMap, moduleMatcher, options = {}) => {
|
|
|
2830
2962
|
foundCssViaMetadata = true;
|
|
2831
2963
|
}
|
|
2832
2964
|
if (!foundCssViaMetadata && chunkContainsCssModules(fileData.modules)) for (const cssAsset of Array.from(bundleCssAssets)) trackAsset(filesMap, matchKey, cssAsset, false, "css");
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2965
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2966
|
+
const queue = [fileName];
|
|
2967
|
+
for (let queueIndex = 0; queueIndex < queue.length; queueIndex++) {
|
|
2968
|
+
const cur = queue[queueIndex];
|
|
2969
|
+
if (visited.has(cur)) continue;
|
|
2970
|
+
visited.add(cur);
|
|
2971
|
+
const chunk = bundle[cur];
|
|
2972
|
+
if (!chunk || chunk.type !== "chunk") continue;
|
|
2973
|
+
if (chunk.dynamicImports) for (const dynamicImport of chunk.dynamicImports) {
|
|
2974
|
+
if (!bundle[dynamicImport]) continue;
|
|
2975
|
+
trackAsset(filesMap, matchKey, dynamicImport, true, isCSSFile(dynamicImport) ? "css" : "js");
|
|
2976
|
+
}
|
|
2977
|
+
if (chunk.imports) for (const imp of chunk.imports) queue.push(imp);
|
|
2836
2978
|
}
|
|
2837
2979
|
}
|
|
2838
2980
|
}
|
|
@@ -2880,7 +3022,11 @@ const buildFileToShareKeyMap = async (shareKeys, resolveFn) => {
|
|
|
2880
3022
|
//#endregion
|
|
2881
3023
|
//#region src/utils/pathNormalization.ts
|
|
2882
3024
|
const COMMON_SHARED_SUBPATHS = {
|
|
2883
|
-
react: [
|
|
3025
|
+
react: [
|
|
3026
|
+
"react/jsx-runtime",
|
|
3027
|
+
"react/jsx-dev-runtime",
|
|
3028
|
+
"react/compiler-runtime"
|
|
3029
|
+
],
|
|
2884
3030
|
"react-dom": [
|
|
2885
3031
|
"react-dom/client",
|
|
2886
3032
|
"react-dom/server",
|
|
@@ -3015,11 +3161,11 @@ function generateRemoteEntrySSR(options) {
|
|
|
3015
3161
|
*/
|
|
3016
3162
|
async function init(shared = {}, initScope = []) {
|
|
3017
3163
|
const initRes = runtimeInit({
|
|
3018
|
-
name: ${JSON.stringify(options.
|
|
3164
|
+
name: ${JSON.stringify(options.name)},
|
|
3019
3165
|
remotes: [],
|
|
3020
3166
|
shared: {},
|
|
3021
3167
|
});
|
|
3022
|
-
const initToken = { from: ${JSON.stringify(options.
|
|
3168
|
+
const initToken = { from: ${JSON.stringify(options.name)} };
|
|
3023
3169
|
if (initScope.indexOf(initToken) >= 0) return;
|
|
3024
3170
|
initScope.push(initToken);
|
|
3025
3171
|
initRes.initShareScopeMap(${JSON.stringify(options.shareScope)}, shared);
|
|
@@ -3612,9 +3758,16 @@ function findSharedKey(source, shared) {
|
|
|
3612
3758
|
function findSharedKeyForSource(source, shared) {
|
|
3613
3759
|
const key = findSharedKey(source, shared);
|
|
3614
3760
|
if (key) return key;
|
|
3761
|
+
const explicitSharedSubpathKeys = Object.keys(shared || {}).filter((sharedKey) => require_packageUtils.getPackageName(sharedKey) !== sharedKey && !sharedKey.endsWith("/"));
|
|
3615
3762
|
if (isNodeModulePath(source)) {
|
|
3616
|
-
const explicitSubpathKey = getMatchingNodeModuleSubpath(source,
|
|
3763
|
+
const explicitSubpathKey = getMatchingNodeModuleSubpath(source, explicitSharedSubpathKeys);
|
|
3617
3764
|
if (explicitSubpathKey) return explicitSubpathKey;
|
|
3765
|
+
const normalizedSource = normalizeNodeModulePath(source);
|
|
3766
|
+
const explicitSubpathEntryKey = explicitSharedSubpathKeys.find((sharedKey) => {
|
|
3767
|
+
const entry = require_packageUtils.getInstalledPackageEntry(sharedKey, { cwd: require_packageUtils.getPackageDetectionCwd() });
|
|
3768
|
+
return entry ? normalizeNodeModulePath(entry) === normalizedSource : false;
|
|
3769
|
+
});
|
|
3770
|
+
if (explicitSubpathEntryKey) return explicitSubpathEntryKey;
|
|
3618
3771
|
}
|
|
3619
3772
|
const packageName = require_packageUtils.getPackageNameFromNodeModulePath(source);
|
|
3620
3773
|
return packageName ? findSharedKey(packageName, shared) : void 0;
|
|
@@ -3659,6 +3812,7 @@ function proxySharedModule(options) {
|
|
|
3659
3812
|
let useRolldown = false;
|
|
3660
3813
|
const savePrebuild = new PromiseStore();
|
|
3661
3814
|
let devServer;
|
|
3815
|
+
const materializedLoadShareSources = /* @__PURE__ */ new Set();
|
|
3662
3816
|
return [
|
|
3663
3817
|
{
|
|
3664
3818
|
name: "generateLocalSharedImportMap",
|
|
@@ -3733,11 +3887,14 @@ function proxySharedModule(options) {
|
|
|
3733
3887
|
if (shouldSkipTaggedImporterProxy(key, "__prebuild__")) return;
|
|
3734
3888
|
const shareSource = key === "vue" && source.startsWith("vue/dist/") ? key : isNodeModulePath(source) ? getCommonSharedSubpathFromNodeModulePath(source, key) || key : source;
|
|
3735
3889
|
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown);
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3890
|
+
if (!materializedLoadShareSources.has(shareSource)) {
|
|
3891
|
+
materializedLoadShareSources.add(shareSource);
|
|
3892
|
+
writeLoadShareModule(shareSource, shared[key], _command, useRolldown);
|
|
3893
|
+
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key]);
|
|
3894
|
+
addUsedShares(shareSource);
|
|
3895
|
+
writeLocalSharedImportMap();
|
|
3896
|
+
refreshHostAutoInit();
|
|
3897
|
+
}
|
|
3741
3898
|
return this.resolve(loadSharePath, importer, { skipSelf: true });
|
|
3742
3899
|
}
|
|
3743
3900
|
},
|
|
@@ -4261,7 +4418,7 @@ function pluginSSRRemoteEntry(options) {
|
|
|
4261
4418
|
try {
|
|
4262
4419
|
result = await fetchFn(id, importer, opts);
|
|
4263
4420
|
} catch (fetchErr) {
|
|
4264
|
-
const bareId =
|
|
4421
|
+
const bareId = decodeViteId(id);
|
|
4265
4422
|
try {
|
|
4266
4423
|
const { createRequire } = await import("module");
|
|
4267
4424
|
const resolved = createRequire(new URL(`file://${server.config.root}/package.json`)).resolve(bareId.replace(/^\0/, ""));
|
|
@@ -4516,6 +4673,8 @@ var normalizeOptimizeDeps_default = {
|
|
|
4516
4673
|
//#endregion
|
|
4517
4674
|
//#region src/index.ts
|
|
4518
4675
|
const patchedManualChunks = /* @__PURE__ */ new WeakSet();
|
|
4676
|
+
const PRELOAD_HELPER_CHUNK = "vite-preload-helper";
|
|
4677
|
+
const PRELOAD_HELPER_TEST = /\0?vite\/preload-helper/;
|
|
4519
4678
|
function normalizeVinextRscPreloadHints(code) {
|
|
4520
4679
|
return code.replace(/(:HL\[[^\]\n]*?,)"stylesheet"/g, "$1\"style\"").replace(/(:HL\[[^\]\n]*?,)\\"stylesheet\\"/g, "$1\\\"style\\\"");
|
|
4521
4680
|
}
|
|
@@ -4583,6 +4742,14 @@ function isFederationHtmlPreloadDependency(dep, includeSharedRuntime = false) {
|
|
|
4583
4742
|
if (file.includes("__mfe_internal__") || file.includes("virtual_mf-") || file.includes("virtualExposes") || file.includes("localSharedImportMap") || file.includes("hostInit")) return true;
|
|
4584
4743
|
return includeSharedRuntime && (file.includes("preload-helper") || file.includes("rolldown-runtime") || file.startsWith("dist-"));
|
|
4585
4744
|
}
|
|
4745
|
+
function canResolveSharedSubpath(subpath, projectRoot) {
|
|
4746
|
+
try {
|
|
4747
|
+
(0, module$1.createRequire)(new URL(`file://${projectRoot}/package.json`)).resolve(subpath);
|
|
4748
|
+
return true;
|
|
4749
|
+
} catch {
|
|
4750
|
+
return false;
|
|
4751
|
+
}
|
|
4752
|
+
}
|
|
4586
4753
|
/**
|
|
4587
4754
|
* Plugin that runs FIRST to register generated virtual modules in the config hook.
|
|
4588
4755
|
* This prevents 504 "Outdated Optimize Dep" errors by ensuring ids are known
|
|
@@ -4649,7 +4816,7 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
4649
4816
|
optimizeDeps.esbuildOptions.plugins.push({
|
|
4650
4817
|
name: "module-federation:optimize-shared-proxy",
|
|
4651
4818
|
setup(build) {
|
|
4652
|
-
build.onResolve({ filter:
|
|
4819
|
+
build.onResolve({ filter: createViteEncodedIdPrefixRegExp("virtual:mf:") }, (args) => ({
|
|
4653
4820
|
path: args.path,
|
|
4654
4821
|
external: true
|
|
4655
4822
|
}));
|
|
@@ -4669,15 +4836,15 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
4669
4836
|
const key = findSharedKey(args.path, shared);
|
|
4670
4837
|
if (!key) return;
|
|
4671
4838
|
const shareItem = shared[key];
|
|
4672
|
-
const
|
|
4839
|
+
const optimizedLoadSharePath = toViteOptimizedDepVirtualId(getLoadShareModulePath(args.path, isRolldown));
|
|
4673
4840
|
writeLoadShareModule(args.path, shareItem, _command, isRolldown);
|
|
4674
4841
|
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(args.path, shareItem);
|
|
4675
4842
|
addUsedShares(args.path);
|
|
4676
4843
|
return {
|
|
4677
4844
|
loader: "js",
|
|
4678
4845
|
resolveDir: root,
|
|
4679
|
-
contents: `import * as __mfShared from ${JSON.stringify(
|
|
4680
|
-
export * from ${JSON.stringify(
|
|
4846
|
+
contents: `import * as __mfShared from ${JSON.stringify(optimizedLoadSharePath)};
|
|
4847
|
+
export * from ${JSON.stringify(optimizedLoadSharePath)};
|
|
4681
4848
|
export default __mfShared.default ?? __mfShared;`
|
|
4682
4849
|
};
|
|
4683
4850
|
});
|
|
@@ -4691,9 +4858,11 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4691
4858
|
if (_command === "serve" && shareItem.shareConfig?.import !== false) {
|
|
4692
4859
|
const optimizeDeps = config.optimizeDeps ??= {};
|
|
4693
4860
|
optimizeDeps.include ??= [];
|
|
4861
|
+
optimizeDeps.exclude ??= [];
|
|
4694
4862
|
for (const subpath of getCommonSharedSubpaths(key)) {
|
|
4695
4863
|
writePreBuildLibPath(subpath, shareItem);
|
|
4696
|
-
optimizeDeps.include.push(subpath);
|
|
4864
|
+
if (canResolveSharedSubpath(subpath, root)) optimizeDeps.include.push(subpath);
|
|
4865
|
+
else optimizeDeps.exclude.push(subpath);
|
|
4697
4866
|
}
|
|
4698
4867
|
}
|
|
4699
4868
|
continue;
|
|
@@ -4717,7 +4886,8 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4717
4886
|
writeLoadShareModule(subpath, shareItem, _command, isRolldown);
|
|
4718
4887
|
writePreBuildLibPath(subpath, shareItem);
|
|
4719
4888
|
addUsedShares(subpath);
|
|
4720
|
-
optimizeDeps.include.push(subpath);
|
|
4889
|
+
if (canResolveSharedSubpath(subpath, root)) optimizeDeps.include.push(subpath);
|
|
4890
|
+
else optimizeDeps.exclude.push(subpath);
|
|
4721
4891
|
}
|
|
4722
4892
|
}
|
|
4723
4893
|
}
|
|
@@ -4738,6 +4908,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4738
4908
|
"react-dom",
|
|
4739
4909
|
"react/jsx-runtime",
|
|
4740
4910
|
"react/jsx-dev-runtime",
|
|
4911
|
+
"react/compiler-runtime",
|
|
4741
4912
|
"@module-federation/runtime",
|
|
4742
4913
|
"@module-federation/runtime-core",
|
|
4743
4914
|
"@module-federation/sdk"
|
|
@@ -4778,7 +4949,19 @@ function federation(mfUserOptions) {
|
|
|
4778
4949
|
name: "vite:module-federation-virtual-modules",
|
|
4779
4950
|
enforce: "pre",
|
|
4780
4951
|
resolveId(id) {
|
|
4781
|
-
|
|
4952
|
+
let virtualModule = VirtualModule.findById(id);
|
|
4953
|
+
if (!virtualModule) {
|
|
4954
|
+
materializeCachedLoadShareModule({
|
|
4955
|
+
id,
|
|
4956
|
+
shared: options.shared,
|
|
4957
|
+
command,
|
|
4958
|
+
isRolldown: require_packageUtils.getIsRolldown(this),
|
|
4959
|
+
findSharedKey,
|
|
4960
|
+
addUsedShares,
|
|
4961
|
+
writeLocalSharedImportMap
|
|
4962
|
+
});
|
|
4963
|
+
virtualModule = VirtualModule.findById(id);
|
|
4964
|
+
}
|
|
4782
4965
|
if (!virtualModule) return;
|
|
4783
4966
|
return virtualModule.getResolvedId();
|
|
4784
4967
|
},
|
|
@@ -4797,7 +4980,8 @@ function federation(mfUserOptions) {
|
|
|
4797
4980
|
resolveId(id) {
|
|
4798
4981
|
const reactServerEntryMap = {
|
|
4799
4982
|
"react/jsx-runtime": "react/cjs/react-jsx-runtime.production.js",
|
|
4800
|
-
"react/jsx-dev-runtime": "react/cjs/react-jsx-dev-runtime.production.js"
|
|
4983
|
+
"react/jsx-dev-runtime": "react/cjs/react-jsx-dev-runtime.production.js",
|
|
4984
|
+
"react/compiler-runtime": "react/cjs/react-compiler-runtime.production.js"
|
|
4801
4985
|
};
|
|
4802
4986
|
if (!(id in reactServerEntryMap)) return;
|
|
4803
4987
|
const environmentName = this.environment?.name;
|
|
@@ -4899,6 +5083,8 @@ function federation(mfUserOptions) {
|
|
|
4899
5083
|
}
|
|
4900
5084
|
if (!output?.codeSplitting || typeof output.codeSplitting !== "object") return;
|
|
4901
5085
|
if (!("groups" in output.codeSplitting)) return;
|
|
5086
|
+
const groups = output.codeSplitting.groups;
|
|
5087
|
+
if (Array.isArray(groups) && groups.some((group) => typeof group?.name === "function" && patchedManualChunks.has(group.name))) return;
|
|
4902
5088
|
delete output.codeSplitting.groups;
|
|
4903
5089
|
if (Object.keys(output.codeSplitting).length === 0) delete output.codeSplitting;
|
|
4904
5090
|
if (warnedAboutCodeSplittingGroups) return;
|
|
@@ -4906,27 +5092,45 @@ function federation(mfUserOptions) {
|
|
|
4906
5092
|
require_packageUtils.mfWarn("Ignoring `output.codeSplitting.groups` because it conflicts with module federation. Grouping shared dependency init wrappers with their dependent modules can break runtime init order and cause standalone remotes to fail before mount.");
|
|
4907
5093
|
};
|
|
4908
5094
|
let warnedAboutManualChunks = false;
|
|
4909
|
-
const applyManualChunks = (output) => {
|
|
5095
|
+
const applyManualChunks = (output, useCodeSplitting) => {
|
|
4910
5096
|
ensureCodeSplitting(output);
|
|
4911
5097
|
const isPatchedByPlugin = typeof output.manualChunks === "function" && patchedManualChunks.has(output.manualChunks);
|
|
4912
5098
|
if (output.manualChunks && !isPatchedByPlugin && !warnedAboutManualChunks) {
|
|
4913
5099
|
warnedAboutManualChunks = true;
|
|
4914
5100
|
require_packageUtils.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.");
|
|
4915
5101
|
}
|
|
4916
|
-
const
|
|
5102
|
+
const mfChunkName = function(id) {
|
|
4917
5103
|
if (id.includes(runtimeInitId)) return "runtimeInit";
|
|
4918
5104
|
if (id.includes("__loadShare__")) {
|
|
4919
5105
|
const match = id.match(/([^/\\]+__loadShare__[^/\\]+)/);
|
|
4920
5106
|
return match ? match[1] : "loadShare";
|
|
4921
5107
|
}
|
|
5108
|
+
return null;
|
|
5109
|
+
};
|
|
5110
|
+
patchedManualChunks.add(mfChunkName);
|
|
5111
|
+
if (!useCodeSplitting) {
|
|
5112
|
+
const mfManualChunks = function(id) {
|
|
5113
|
+
return mfChunkName(id) ?? void 0;
|
|
5114
|
+
};
|
|
5115
|
+
patchedManualChunks.add(mfManualChunks);
|
|
5116
|
+
output.manualChunks = mfManualChunks;
|
|
5117
|
+
return;
|
|
5118
|
+
}
|
|
5119
|
+
const groups = [{
|
|
5120
|
+
name: PRELOAD_HELPER_CHUNK,
|
|
5121
|
+
test: PRELOAD_HELPER_TEST,
|
|
5122
|
+
priority: 100
|
|
5123
|
+
}, { name: mfChunkName }];
|
|
5124
|
+
output.codeSplitting = {
|
|
5125
|
+
...output.codeSplitting || {},
|
|
5126
|
+
groups
|
|
4922
5127
|
};
|
|
4923
|
-
|
|
4924
|
-
output.manualChunks = mfManualChunks;
|
|
5128
|
+
delete output.manualChunks;
|
|
4925
5129
|
};
|
|
4926
5130
|
config.build.rollupOptions = config.build.rollupOptions || {};
|
|
4927
5131
|
const rollupOutput = config.build.rollupOptions.output;
|
|
4928
|
-
if (Array.isArray(rollupOutput)) rollupOutput.forEach((output) => applyManualChunks(output));
|
|
4929
|
-
else applyManualChunks(config.build.rollupOptions.output ||= {});
|
|
5132
|
+
if (Array.isArray(rollupOutput)) rollupOutput.forEach((output) => applyManualChunks(output, false));
|
|
5133
|
+
else applyManualChunks(config.build.rollupOptions.output ||= {}, false);
|
|
4930
5134
|
const buildWithRolldown = config.build;
|
|
4931
5135
|
buildWithRolldown.rolldownOptions = buildWithRolldown.rolldownOptions || {};
|
|
4932
5136
|
const rolldownOutput = buildWithRolldown.rolldownOptions.output;
|
|
@@ -4936,10 +5140,10 @@ function federation(mfUserOptions) {
|
|
|
4936
5140
|
assetFileNames: output.assetFileNames
|
|
4937
5141
|
});
|
|
4938
5142
|
if (Array.isArray(rolldownOutput)) {
|
|
4939
|
-
rolldownOutput.forEach((output) => applyManualChunks(output));
|
|
5143
|
+
rolldownOutput.forEach((output) => applyManualChunks(output, true));
|
|
4940
5144
|
desiredRolldownOutput = rolldownOutput.map((output) => snapshotRolldownOutput(output));
|
|
4941
5145
|
} else {
|
|
4942
|
-
applyManualChunks(buildWithRolldown.rolldownOptions.output ||= {});
|
|
5146
|
+
applyManualChunks(buildWithRolldown.rolldownOptions.output ||= {}, true);
|
|
4943
5147
|
desiredRolldownOutput = [snapshotRolldownOutput(buildWithRolldown.rolldownOptions.output)];
|
|
4944
5148
|
}
|
|
4945
5149
|
},
|
package/lib/index.d.cts
CHANGED
package/lib/index.d.mts
CHANGED
package/lib/index.mjs
CHANGED
|
@@ -417,24 +417,51 @@ function getSuffix(name) {
|
|
|
417
417
|
}
|
|
418
418
|
const patternMap = {};
|
|
419
419
|
const cacheMap = {};
|
|
420
|
+
const VITE_ID_PREFIX = "/@id/";
|
|
421
|
+
const VITE_ENCODED_NULL_BYTE_PREFIX = `${VITE_ID_PREFIX}__x00__`;
|
|
422
|
+
function escapeRegExp$1(value) {
|
|
423
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
424
|
+
}
|
|
425
|
+
function createViteEncodedIdPrefixRegExp(sourcePrefix = "") {
|
|
426
|
+
return new RegExp(`^(?:${escapeRegExp$1(VITE_ENCODED_NULL_BYTE_PREFIX)})?${sourcePrefix}`);
|
|
427
|
+
}
|
|
428
|
+
function toViteEncodedId(id) {
|
|
429
|
+
return `${VITE_ENCODED_NULL_BYTE_PREFIX}${id}`;
|
|
430
|
+
}
|
|
431
|
+
function decodeViteId(id) {
|
|
432
|
+
if (!id.startsWith("/@id/")) return id;
|
|
433
|
+
const viteId = id.slice(5);
|
|
434
|
+
return viteId.startsWith("__x00__") ? `\0${viteId.slice(7)}` : viteId;
|
|
435
|
+
}
|
|
420
436
|
function assertModuleFound(tag, str = "") {
|
|
421
437
|
const module = VirtualModule.findModule(tag, str);
|
|
422
438
|
if (!module) throw createModuleFederationError(`Module Federation shared module '${str}' not found. Please ensure it's installed as a dependency in your package.json.`);
|
|
423
439
|
return module;
|
|
424
440
|
}
|
|
425
|
-
|
|
441
|
+
function normalizeVirtualModuleId(id) {
|
|
442
|
+
const decoded = decodeViteId(id).replace(/^\0+/, "");
|
|
443
|
+
const queryIndex = decoded.indexOf("?");
|
|
444
|
+
const hashIndex = decoded.indexOf("#");
|
|
445
|
+
const endIndex = queryIndex === -1 ? hashIndex : hashIndex === -1 ? queryIndex : Math.min(queryIndex, hashIndex);
|
|
446
|
+
return endIndex === -1 ? decoded : decoded.slice(0, endIndex);
|
|
447
|
+
}
|
|
448
|
+
var VirtualModule = class VirtualModule {
|
|
426
449
|
name;
|
|
427
450
|
tag;
|
|
428
451
|
suffix;
|
|
429
452
|
inited = false;
|
|
430
453
|
code;
|
|
431
|
-
static
|
|
454
|
+
static findName(tag, str = "") {
|
|
432
455
|
if (!patternMap[tag]) patternMap[tag] = new RegExp(`(.*${packageNameEncode(tag)}(.+?)${packageNameEncode(tag)}.*)`);
|
|
433
|
-
const moduleName = (str.match(patternMap[tag]) || [])[2];
|
|
434
|
-
|
|
456
|
+
const moduleName = (normalizeVirtualModuleId(str).match(patternMap[tag]) || [])[2];
|
|
457
|
+
return moduleName ? packageNameDecode(moduleName) : void 0;
|
|
458
|
+
}
|
|
459
|
+
static findModule(tag, str = "") {
|
|
460
|
+
const moduleName = VirtualModule.findName(tag, str);
|
|
461
|
+
return moduleName ? cacheMap[tag][moduleName] : void 0;
|
|
435
462
|
}
|
|
436
463
|
static findById(id) {
|
|
437
|
-
const normalized =
|
|
464
|
+
const normalized = normalizeVirtualModuleId(id);
|
|
438
465
|
for (const modules of Object.values(cacheMap)) for (const module of Object.values(modules)) if (module.getImportId() === normalized) return module;
|
|
439
466
|
}
|
|
440
467
|
constructor(name, tag = "__mf_v__", suffix = "") {
|
|
@@ -748,11 +775,9 @@ function getPackageEsmEntryPath(pkg) {
|
|
|
748
775
|
resolveSubpathWithRequire: false
|
|
749
776
|
}) || resolvePackageEntryFromProjectRoot(pkg);
|
|
750
777
|
}
|
|
751
|
-
function
|
|
778
|
+
function getEsmNamedExportsFromFile(entryPath) {
|
|
752
779
|
let source = "";
|
|
753
|
-
let entryPath;
|
|
754
780
|
try {
|
|
755
|
-
entryPath = getPackageEsmEntryPath(pkg);
|
|
756
781
|
if (!entryPath) return [];
|
|
757
782
|
const { initSync, parse } = localRequire("es-module-lexer");
|
|
758
783
|
initSync();
|
|
@@ -767,6 +792,48 @@ function getEsmNamedExports(pkg) {
|
|
|
767
792
|
return source ? getNamedExportsViaRegex(source, entryPath) : [];
|
|
768
793
|
}
|
|
769
794
|
}
|
|
795
|
+
function getEsmNamedExports(pkg) {
|
|
796
|
+
return getEsmNamedExportsFromFile(getPackageEsmEntryPath(pkg));
|
|
797
|
+
}
|
|
798
|
+
function resolveConfiguredImportPath(importSource) {
|
|
799
|
+
if (path.isAbsolute(importSource)) return resolveFileLikeModule(importSource);
|
|
800
|
+
const projectRoot = getPackageDetectionCwd();
|
|
801
|
+
if (importSource.startsWith(".")) return resolveFileLikeModule(path.resolve(projectRoot, importSource));
|
|
802
|
+
const esmEntry = getInstalledPackageEntry(importSource, {
|
|
803
|
+
conditions: [
|
|
804
|
+
"browser",
|
|
805
|
+
"import",
|
|
806
|
+
"module",
|
|
807
|
+
"default"
|
|
808
|
+
],
|
|
809
|
+
resolveSubpathWithRequire: false
|
|
810
|
+
});
|
|
811
|
+
if (esmEntry) return esmEntry;
|
|
812
|
+
try {
|
|
813
|
+
return createRequire(new URL(`file://${path.join(projectRoot, "package.json")}`)).resolve(importSource);
|
|
814
|
+
} catch {
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
function resolveFileLikeModule(filePath) {
|
|
819
|
+
if (existsSync(filePath) && !statSync(filePath).isDirectory()) return filePath;
|
|
820
|
+
const extensions = [
|
|
821
|
+
".ts",
|
|
822
|
+
".tsx",
|
|
823
|
+
".js",
|
|
824
|
+
".jsx",
|
|
825
|
+
".mjs",
|
|
826
|
+
".mts"
|
|
827
|
+
];
|
|
828
|
+
for (const ext of extensions) {
|
|
829
|
+
const candidate = filePath + ext;
|
|
830
|
+
if (existsSync(candidate) && !statSync(candidate).isDirectory()) return candidate;
|
|
831
|
+
}
|
|
832
|
+
for (const ext of extensions) {
|
|
833
|
+
const candidate = path.join(filePath, "index" + ext);
|
|
834
|
+
if (existsSync(candidate) && !statSync(candidate).isDirectory()) return candidate;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
770
837
|
function resolveRelativeModule(filePath, specifier) {
|
|
771
838
|
const dir = path.dirname(filePath);
|
|
772
839
|
const exact = path.resolve(dir, specifier);
|
|
@@ -836,6 +903,14 @@ function getPackageNamedExports(pkg) {
|
|
|
836
903
|
return getEsmNamedExports(pkg);
|
|
837
904
|
}
|
|
838
905
|
}
|
|
906
|
+
function getSharedNamedExports(pkg, shareItem) {
|
|
907
|
+
const configuredImport = shareItem?.shareConfig.import;
|
|
908
|
+
if (typeof configuredImport === "string") {
|
|
909
|
+
const configuredNamedExports = getEsmNamedExportsFromFile(resolveConfiguredImportPath(configuredImport));
|
|
910
|
+
if (configuredNamedExports.length > 0) return configuredNamedExports;
|
|
911
|
+
}
|
|
912
|
+
return getPackageNamedExports(pkg);
|
|
913
|
+
}
|
|
839
914
|
function getLocalProviderImportPath(pkg) {
|
|
840
915
|
try {
|
|
841
916
|
const resolved = createRequire(new URL(`file://${path.join(getPackageDetectionCwd(), "package.json")}`)).resolve(pkg);
|
|
@@ -906,6 +981,20 @@ function writePreBuildLibPath(pkg, shareItem) {
|
|
|
906
981
|
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(pkg, PREBUILD_TAG, ".js");
|
|
907
982
|
preBuildShareItemMap[pkg] = shareItem;
|
|
908
983
|
const importSource = getConcreteSharedImportSource(pkg, shareItem) || pkg;
|
|
984
|
+
if (pkg === "react/compiler-runtime") {
|
|
985
|
+
preBuildCacheMap[pkg].writeSync(`
|
|
986
|
+
const __mfCacheGlobalKey = "__mf_module_cache__";
|
|
987
|
+
export const c = function(size) {
|
|
988
|
+
const cache = globalThis[__mfCacheGlobalKey]?.share;
|
|
989
|
+
const sharedReact = cache?.['react'];
|
|
990
|
+
const reactExports = sharedReact?.default ?? sharedReact;
|
|
991
|
+
const internals = reactExports?.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
|
|
992
|
+
return internals?.H?.useMemoCache(size);
|
|
993
|
+
};
|
|
994
|
+
export default { c };
|
|
995
|
+
`, true);
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
909
998
|
if (pkg === "react/jsx-dev-runtime") {
|
|
910
999
|
preBuildCacheMap[pkg].writeSync(`
|
|
911
1000
|
import __mfPrebuildDefault from ${escapeGeneratedStringLiteral(importSource)};
|
|
@@ -929,7 +1018,7 @@ function writePreBuildLibPath(pkg, shareItem) {
|
|
|
929
1018
|
`, true);
|
|
930
1019
|
return;
|
|
931
1020
|
}
|
|
932
|
-
const namedExports =
|
|
1021
|
+
const namedExports = getSharedNamedExports(pkg, shareItem);
|
|
933
1022
|
if (namedExports.length > 0) {
|
|
934
1023
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
935
1024
|
const declarations = namedExports.map((name, i) => `const ${namedExportVars[i]} = __mfPrebuildExports[${escapeGeneratedStringLiteral(name)}];`).join("\n ");
|
|
@@ -969,6 +1058,27 @@ function getLoadShareModulePath(pkg, isRolldown) {
|
|
|
969
1058
|
if (!loadShareCacheMap[pkg]) getLoadShareImportId(pkg, isRolldown);
|
|
970
1059
|
return loadShareCacheMap[pkg].getImportId();
|
|
971
1060
|
}
|
|
1061
|
+
function toViteOptimizedDepVirtualId(id) {
|
|
1062
|
+
return toViteEncodedId(id);
|
|
1063
|
+
}
|
|
1064
|
+
function getCachedLoadSharePkg(id) {
|
|
1065
|
+
const normalized = normalizeVirtualModuleId(id);
|
|
1066
|
+
if (!normalized.startsWith("virtual:mf:")) return;
|
|
1067
|
+
const pkg = VirtualModule.findName(LOAD_SHARE_TAG, normalized);
|
|
1068
|
+
if (!pkg) return;
|
|
1069
|
+
return pkg;
|
|
1070
|
+
}
|
|
1071
|
+
function materializeCachedLoadShareModule(options) {
|
|
1072
|
+
const pkg = getCachedLoadSharePkg(options.id);
|
|
1073
|
+
if (!pkg) return;
|
|
1074
|
+
const key = options.findSharedKey(pkg, options.shared);
|
|
1075
|
+
if (!key) return;
|
|
1076
|
+
const shareItem = options.shared[key];
|
|
1077
|
+
writeLoadShareModule(pkg, shareItem, options.command, options.isRolldown);
|
|
1078
|
+
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(pkg, shareItem);
|
|
1079
|
+
options.addUsedShares(pkg);
|
|
1080
|
+
options.writeLocalSharedImportMap();
|
|
1081
|
+
}
|
|
972
1082
|
function generateDeferredHostProvidedExports(namedExports, pkg, cacheKey) {
|
|
973
1083
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
974
1084
|
const declarations = ["let __mf_default;", ...namedExportVars.map((name) => `let ${name};`)].join("\n ");
|
|
@@ -1036,7 +1146,7 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
1036
1146
|
const lazyLocalFallbackSource = concreteSharedImportSource || localProviderPath || sharedImportSource;
|
|
1037
1147
|
const skipServePrebuildWarmup = command !== "build" && (pkg === "lit" || pkg.startsWith("lit/"));
|
|
1038
1148
|
const usesLazyLocalFallback = isWorkspacePackage && shareItem.shareConfig.singleton === true;
|
|
1039
|
-
const namedExports =
|
|
1149
|
+
const namedExports = getSharedNamedExports(pkg, shareItem);
|
|
1040
1150
|
let exportLine;
|
|
1041
1151
|
if (namedExports.length > 0) {
|
|
1042
1152
|
const destructure = `const { ${namedExports.map((name, i) => `${name}: __mf_${i}`).join(", ")} } = exportModule;`;
|
|
@@ -1136,7 +1246,7 @@ function generateLocalSharedImportMap() {
|
|
|
1136
1246
|
version: ${JSON.stringify(shareItem.version)},
|
|
1137
1247
|
scope: [${JSON.stringify(shareItem.scope)}],
|
|
1138
1248
|
loaded: false,
|
|
1139
|
-
from: ${JSON.stringify(options.
|
|
1249
|
+
from: ${JSON.stringify(options.name)},
|
|
1140
1250
|
async get () {
|
|
1141
1251
|
if (${shareItem.shareConfig.import === false}) {
|
|
1142
1252
|
throw new Error(\`[Module Federation] Shared module '\${${JSON.stringify(key)}}' must be provided by host\`);
|
|
@@ -1307,7 +1417,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1307
1417
|
${getRuntimeModuleCacheBootstrapCode()}
|
|
1308
1418
|
const initTokens = {}
|
|
1309
1419
|
const shareScopeName = ${JSON.stringify(options.shareScope)}
|
|
1310
|
-
const mfName = ${JSON.stringify(options.
|
|
1420
|
+
const mfName = ${JSON.stringify(options.name)}
|
|
1311
1421
|
let localSharedImportMapPromise
|
|
1312
1422
|
let exposesMapPromise
|
|
1313
1423
|
const shouldRetrySharedInitError = ${command !== "build"} && ((error) => {
|
|
@@ -1349,6 +1459,28 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1349
1459
|
|
|
1350
1460
|
async function init(shared = {}, initScope = []) {
|
|
1351
1461
|
const {usedShared, usedRemotes} = await getLocalSharedImportMap()
|
|
1462
|
+
try {
|
|
1463
|
+
const allInstances = globalThis.__FEDERATION__?.__SHARE__;
|
|
1464
|
+
if (allInstances) {
|
|
1465
|
+
${normalizeRuntimeShareCode}
|
|
1466
|
+
for (const [, scopes] of Object.entries(allInstances)) {
|
|
1467
|
+
const scopeShare = scopes?.['${options.shareScope}'];
|
|
1468
|
+
if (!scopeShare) continue;
|
|
1469
|
+
for (const [pkg, versionMap] of Object.entries(scopeShare)) {
|
|
1470
|
+
for (const [version, provider] of Object.entries(versionMap)) {
|
|
1471
|
+
if (!provider.lib) continue;
|
|
1472
|
+
const cacheKey = provider.shareConfig?.singleton ? pkg : \`\${pkg}@\${version}\`;
|
|
1473
|
+
if (__mfModuleCache.share[cacheKey] !== undefined) continue;
|
|
1474
|
+
const mod = typeof provider.lib === "function" ? provider.lib() : provider.lib;
|
|
1475
|
+
const resolved = await Promise.resolve(mod);
|
|
1476
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
} catch (e) {
|
|
1482
|
+
console.error('[Module Federation] Failed to bridge external shared modules', e)
|
|
1483
|
+
}
|
|
1352
1484
|
${generateDirectSharedCacheSeedCode(command)}
|
|
1353
1485
|
const __browserPlugins = [${pluginImportNames.filter((item) => !isSsrOnlyPlugin(item[1])).map((item) => `${item[0]}(${item[2]})`).join(", ")}];
|
|
1354
1486
|
const __ssrPlugins = typeof globalThis.window === 'undefined'
|
|
@@ -1805,7 +1937,7 @@ const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClien
|
|
|
1805
1937
|
return inject === "entry" || !htmlFilePath;
|
|
1806
1938
|
}
|
|
1807
1939
|
function normalizeDevHtmlProxyId(id) {
|
|
1808
|
-
return id.replace(/^\0/, "")
|
|
1940
|
+
return decodeViteId(id).replace(/^\0/, "");
|
|
1809
1941
|
}
|
|
1810
1942
|
function normalizeModuleId(id) {
|
|
1811
1943
|
return id.split("?")[0].replace(/\\/g, "/");
|
|
@@ -1839,7 +1971,7 @@ const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClien
|
|
|
1839
1971
|
configResolved(config) {
|
|
1840
1972
|
viteConfig = config;
|
|
1841
1973
|
const resolvedEntryPath = getEntryPath();
|
|
1842
|
-
if (resolvedEntryPath.startsWith("virtual:mf")) devEntryPath = config.base +
|
|
1974
|
+
if (resolvedEntryPath.startsWith("virtual:mf")) devEntryPath = config.base + VITE_ID_PREFIX.slice(1) + resolvedEntryPath;
|
|
1843
1975
|
else {
|
|
1844
1976
|
const normalized = resolvedEntryPath.replace(/\\\\?/g, "/");
|
|
1845
1977
|
const root = config.root.replace(/\\\\?/g, "/").replace(/\/$/, "");
|
|
@@ -1880,10 +2012,10 @@ const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClien
|
|
|
1880
2012
|
const base = viteConfig.base.replace(/\/$/, "");
|
|
1881
2013
|
const stripBase = (p) => base && p.startsWith(base + "/") ? p.slice(base.length) : p;
|
|
1882
2014
|
const html = rewriteEntryScripts(c, (originalSrc) => {
|
|
1883
|
-
return
|
|
2015
|
+
return toViteEncodedId(`${DEV_HTML_PROXY_PREFIX}${new URLSearchParams({
|
|
1884
2016
|
init: sanitizeDevEntryPath(stripBase(devEntryPath)),
|
|
1885
2017
|
entry: sanitizeDevEntryPath(stripBase(originalSrc))
|
|
1886
|
-
}).toString()}
|
|
2018
|
+
}).toString()}`);
|
|
1887
2019
|
});
|
|
1888
2020
|
return html === c ? injectEntryScript(c, stripBase(devEntryPath)) : html;
|
|
1889
2021
|
}
|
|
@@ -2829,9 +2961,19 @@ const processModuleAssets = (bundle, filesMap, moduleMatcher, options = {}) => {
|
|
|
2829
2961
|
foundCssViaMetadata = true;
|
|
2830
2962
|
}
|
|
2831
2963
|
if (!foundCssViaMetadata && chunkContainsCssModules(fileData.modules)) for (const cssAsset of Array.from(bundleCssAssets)) trackAsset(filesMap, matchKey, cssAsset, false, "css");
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2964
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2965
|
+
const queue = [fileName];
|
|
2966
|
+
for (let queueIndex = 0; queueIndex < queue.length; queueIndex++) {
|
|
2967
|
+
const cur = queue[queueIndex];
|
|
2968
|
+
if (visited.has(cur)) continue;
|
|
2969
|
+
visited.add(cur);
|
|
2970
|
+
const chunk = bundle[cur];
|
|
2971
|
+
if (!chunk || chunk.type !== "chunk") continue;
|
|
2972
|
+
if (chunk.dynamicImports) for (const dynamicImport of chunk.dynamicImports) {
|
|
2973
|
+
if (!bundle[dynamicImport]) continue;
|
|
2974
|
+
trackAsset(filesMap, matchKey, dynamicImport, true, isCSSFile(dynamicImport) ? "css" : "js");
|
|
2975
|
+
}
|
|
2976
|
+
if (chunk.imports) for (const imp of chunk.imports) queue.push(imp);
|
|
2835
2977
|
}
|
|
2836
2978
|
}
|
|
2837
2979
|
}
|
|
@@ -2879,7 +3021,11 @@ const buildFileToShareKeyMap = async (shareKeys, resolveFn) => {
|
|
|
2879
3021
|
//#endregion
|
|
2880
3022
|
//#region src/utils/pathNormalization.ts
|
|
2881
3023
|
const COMMON_SHARED_SUBPATHS = {
|
|
2882
|
-
react: [
|
|
3024
|
+
react: [
|
|
3025
|
+
"react/jsx-runtime",
|
|
3026
|
+
"react/jsx-dev-runtime",
|
|
3027
|
+
"react/compiler-runtime"
|
|
3028
|
+
],
|
|
2883
3029
|
"react-dom": [
|
|
2884
3030
|
"react-dom/client",
|
|
2885
3031
|
"react-dom/server",
|
|
@@ -3014,11 +3160,11 @@ function generateRemoteEntrySSR(options) {
|
|
|
3014
3160
|
*/
|
|
3015
3161
|
async function init(shared = {}, initScope = []) {
|
|
3016
3162
|
const initRes = runtimeInit({
|
|
3017
|
-
name: ${JSON.stringify(options.
|
|
3163
|
+
name: ${JSON.stringify(options.name)},
|
|
3018
3164
|
remotes: [],
|
|
3019
3165
|
shared: {},
|
|
3020
3166
|
});
|
|
3021
|
-
const initToken = { from: ${JSON.stringify(options.
|
|
3167
|
+
const initToken = { from: ${JSON.stringify(options.name)} };
|
|
3022
3168
|
if (initScope.indexOf(initToken) >= 0) return;
|
|
3023
3169
|
initScope.push(initToken);
|
|
3024
3170
|
initRes.initShareScopeMap(${JSON.stringify(options.shareScope)}, shared);
|
|
@@ -3611,9 +3757,16 @@ function findSharedKey(source, shared) {
|
|
|
3611
3757
|
function findSharedKeyForSource(source, shared) {
|
|
3612
3758
|
const key = findSharedKey(source, shared);
|
|
3613
3759
|
if (key) return key;
|
|
3760
|
+
const explicitSharedSubpathKeys = Object.keys(shared || {}).filter((sharedKey) => getPackageName(sharedKey) !== sharedKey && !sharedKey.endsWith("/"));
|
|
3614
3761
|
if (isNodeModulePath(source)) {
|
|
3615
|
-
const explicitSubpathKey = getMatchingNodeModuleSubpath(source,
|
|
3762
|
+
const explicitSubpathKey = getMatchingNodeModuleSubpath(source, explicitSharedSubpathKeys);
|
|
3616
3763
|
if (explicitSubpathKey) return explicitSubpathKey;
|
|
3764
|
+
const normalizedSource = normalizeNodeModulePath(source);
|
|
3765
|
+
const explicitSubpathEntryKey = explicitSharedSubpathKeys.find((sharedKey) => {
|
|
3766
|
+
const entry = getInstalledPackageEntry(sharedKey, { cwd: getPackageDetectionCwd() });
|
|
3767
|
+
return entry ? normalizeNodeModulePath(entry) === normalizedSource : false;
|
|
3768
|
+
});
|
|
3769
|
+
if (explicitSubpathEntryKey) return explicitSubpathEntryKey;
|
|
3617
3770
|
}
|
|
3618
3771
|
const packageName = getPackageNameFromNodeModulePath(source);
|
|
3619
3772
|
return packageName ? findSharedKey(packageName, shared) : void 0;
|
|
@@ -3658,6 +3811,7 @@ function proxySharedModule(options) {
|
|
|
3658
3811
|
let useRolldown = false;
|
|
3659
3812
|
const savePrebuild = new PromiseStore();
|
|
3660
3813
|
let devServer;
|
|
3814
|
+
const materializedLoadShareSources = /* @__PURE__ */ new Set();
|
|
3661
3815
|
return [
|
|
3662
3816
|
{
|
|
3663
3817
|
name: "generateLocalSharedImportMap",
|
|
@@ -3732,11 +3886,14 @@ function proxySharedModule(options) {
|
|
|
3732
3886
|
if (shouldSkipTaggedImporterProxy(key, "__prebuild__")) return;
|
|
3733
3887
|
const shareSource = key === "vue" && source.startsWith("vue/dist/") ? key : isNodeModulePath(source) ? getCommonSharedSubpathFromNodeModulePath(source, key) || key : source;
|
|
3734
3888
|
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown);
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3889
|
+
if (!materializedLoadShareSources.has(shareSource)) {
|
|
3890
|
+
materializedLoadShareSources.add(shareSource);
|
|
3891
|
+
writeLoadShareModule(shareSource, shared[key], _command, useRolldown);
|
|
3892
|
+
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key]);
|
|
3893
|
+
addUsedShares(shareSource);
|
|
3894
|
+
writeLocalSharedImportMap();
|
|
3895
|
+
refreshHostAutoInit();
|
|
3896
|
+
}
|
|
3740
3897
|
return this.resolve(loadSharePath, importer, { skipSelf: true });
|
|
3741
3898
|
}
|
|
3742
3899
|
},
|
|
@@ -4260,7 +4417,7 @@ function pluginSSRRemoteEntry(options) {
|
|
|
4260
4417
|
try {
|
|
4261
4418
|
result = await fetchFn(id, importer, opts);
|
|
4262
4419
|
} catch (fetchErr) {
|
|
4263
|
-
const bareId =
|
|
4420
|
+
const bareId = decodeViteId(id);
|
|
4264
4421
|
try {
|
|
4265
4422
|
const { createRequire } = await import("module");
|
|
4266
4423
|
const resolved = createRequire(new URL(`file://${server.config.root}/package.json`)).resolve(bareId.replace(/^\0/, ""));
|
|
@@ -4515,6 +4672,8 @@ var normalizeOptimizeDeps_default = {
|
|
|
4515
4672
|
//#endregion
|
|
4516
4673
|
//#region src/index.ts
|
|
4517
4674
|
const patchedManualChunks = /* @__PURE__ */ new WeakSet();
|
|
4675
|
+
const PRELOAD_HELPER_CHUNK = "vite-preload-helper";
|
|
4676
|
+
const PRELOAD_HELPER_TEST = /\0?vite\/preload-helper/;
|
|
4518
4677
|
function normalizeVinextRscPreloadHints(code) {
|
|
4519
4678
|
return code.replace(/(:HL\[[^\]\n]*?,)"stylesheet"/g, "$1\"style\"").replace(/(:HL\[[^\]\n]*?,)\\"stylesheet\\"/g, "$1\\\"style\\\"");
|
|
4520
4679
|
}
|
|
@@ -4582,6 +4741,14 @@ function isFederationHtmlPreloadDependency(dep, includeSharedRuntime = false) {
|
|
|
4582
4741
|
if (file.includes("__mfe_internal__") || file.includes("virtual_mf-") || file.includes("virtualExposes") || file.includes("localSharedImportMap") || file.includes("hostInit")) return true;
|
|
4583
4742
|
return includeSharedRuntime && (file.includes("preload-helper") || file.includes("rolldown-runtime") || file.startsWith("dist-"));
|
|
4584
4743
|
}
|
|
4744
|
+
function canResolveSharedSubpath(subpath, projectRoot) {
|
|
4745
|
+
try {
|
|
4746
|
+
createRequire(new URL(`file://${projectRoot}/package.json`)).resolve(subpath);
|
|
4747
|
+
return true;
|
|
4748
|
+
} catch {
|
|
4749
|
+
return false;
|
|
4750
|
+
}
|
|
4751
|
+
}
|
|
4585
4752
|
/**
|
|
4586
4753
|
* Plugin that runs FIRST to register generated virtual modules in the config hook.
|
|
4587
4754
|
* This prevents 504 "Outdated Optimize Dep" errors by ensuring ids are known
|
|
@@ -4648,7 +4815,7 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
4648
4815
|
optimizeDeps.esbuildOptions.plugins.push({
|
|
4649
4816
|
name: "module-federation:optimize-shared-proxy",
|
|
4650
4817
|
setup(build) {
|
|
4651
|
-
build.onResolve({ filter:
|
|
4818
|
+
build.onResolve({ filter: createViteEncodedIdPrefixRegExp("virtual:mf:") }, (args) => ({
|
|
4652
4819
|
path: args.path,
|
|
4653
4820
|
external: true
|
|
4654
4821
|
}));
|
|
@@ -4668,15 +4835,15 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
4668
4835
|
const key = findSharedKey(args.path, shared);
|
|
4669
4836
|
if (!key) return;
|
|
4670
4837
|
const shareItem = shared[key];
|
|
4671
|
-
const
|
|
4838
|
+
const optimizedLoadSharePath = toViteOptimizedDepVirtualId(getLoadShareModulePath(args.path, isRolldown));
|
|
4672
4839
|
writeLoadShareModule(args.path, shareItem, _command, isRolldown);
|
|
4673
4840
|
if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(args.path, shareItem);
|
|
4674
4841
|
addUsedShares(args.path);
|
|
4675
4842
|
return {
|
|
4676
4843
|
loader: "js",
|
|
4677
4844
|
resolveDir: root,
|
|
4678
|
-
contents: `import * as __mfShared from ${JSON.stringify(
|
|
4679
|
-
export * from ${JSON.stringify(
|
|
4845
|
+
contents: `import * as __mfShared from ${JSON.stringify(optimizedLoadSharePath)};
|
|
4846
|
+
export * from ${JSON.stringify(optimizedLoadSharePath)};
|
|
4680
4847
|
export default __mfShared.default ?? __mfShared;`
|
|
4681
4848
|
};
|
|
4682
4849
|
});
|
|
@@ -4690,9 +4857,11 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4690
4857
|
if (_command === "serve" && shareItem.shareConfig?.import !== false) {
|
|
4691
4858
|
const optimizeDeps = config.optimizeDeps ??= {};
|
|
4692
4859
|
optimizeDeps.include ??= [];
|
|
4860
|
+
optimizeDeps.exclude ??= [];
|
|
4693
4861
|
for (const subpath of getCommonSharedSubpaths(key)) {
|
|
4694
4862
|
writePreBuildLibPath(subpath, shareItem);
|
|
4695
|
-
optimizeDeps.include.push(subpath);
|
|
4863
|
+
if (canResolveSharedSubpath(subpath, root)) optimizeDeps.include.push(subpath);
|
|
4864
|
+
else optimizeDeps.exclude.push(subpath);
|
|
4696
4865
|
}
|
|
4697
4866
|
}
|
|
4698
4867
|
continue;
|
|
@@ -4716,7 +4885,8 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4716
4885
|
writeLoadShareModule(subpath, shareItem, _command, isRolldown);
|
|
4717
4886
|
writePreBuildLibPath(subpath, shareItem);
|
|
4718
4887
|
addUsedShares(subpath);
|
|
4719
|
-
optimizeDeps.include.push(subpath);
|
|
4888
|
+
if (canResolveSharedSubpath(subpath, root)) optimizeDeps.include.push(subpath);
|
|
4889
|
+
else optimizeDeps.exclude.push(subpath);
|
|
4720
4890
|
}
|
|
4721
4891
|
}
|
|
4722
4892
|
}
|
|
@@ -4737,6 +4907,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4737
4907
|
"react-dom",
|
|
4738
4908
|
"react/jsx-runtime",
|
|
4739
4909
|
"react/jsx-dev-runtime",
|
|
4910
|
+
"react/compiler-runtime",
|
|
4740
4911
|
"@module-federation/runtime",
|
|
4741
4912
|
"@module-federation/runtime-core",
|
|
4742
4913
|
"@module-federation/sdk"
|
|
@@ -4777,7 +4948,19 @@ function federation(mfUserOptions) {
|
|
|
4777
4948
|
name: "vite:module-federation-virtual-modules",
|
|
4778
4949
|
enforce: "pre",
|
|
4779
4950
|
resolveId(id) {
|
|
4780
|
-
|
|
4951
|
+
let virtualModule = VirtualModule.findById(id);
|
|
4952
|
+
if (!virtualModule) {
|
|
4953
|
+
materializeCachedLoadShareModule({
|
|
4954
|
+
id,
|
|
4955
|
+
shared: options.shared,
|
|
4956
|
+
command,
|
|
4957
|
+
isRolldown: getIsRolldown(this),
|
|
4958
|
+
findSharedKey,
|
|
4959
|
+
addUsedShares,
|
|
4960
|
+
writeLocalSharedImportMap
|
|
4961
|
+
});
|
|
4962
|
+
virtualModule = VirtualModule.findById(id);
|
|
4963
|
+
}
|
|
4781
4964
|
if (!virtualModule) return;
|
|
4782
4965
|
return virtualModule.getResolvedId();
|
|
4783
4966
|
},
|
|
@@ -4796,7 +4979,8 @@ function federation(mfUserOptions) {
|
|
|
4796
4979
|
resolveId(id) {
|
|
4797
4980
|
const reactServerEntryMap = {
|
|
4798
4981
|
"react/jsx-runtime": "react/cjs/react-jsx-runtime.production.js",
|
|
4799
|
-
"react/jsx-dev-runtime": "react/cjs/react-jsx-dev-runtime.production.js"
|
|
4982
|
+
"react/jsx-dev-runtime": "react/cjs/react-jsx-dev-runtime.production.js",
|
|
4983
|
+
"react/compiler-runtime": "react/cjs/react-compiler-runtime.production.js"
|
|
4800
4984
|
};
|
|
4801
4985
|
if (!(id in reactServerEntryMap)) return;
|
|
4802
4986
|
const environmentName = this.environment?.name;
|
|
@@ -4898,6 +5082,8 @@ function federation(mfUserOptions) {
|
|
|
4898
5082
|
}
|
|
4899
5083
|
if (!output?.codeSplitting || typeof output.codeSplitting !== "object") return;
|
|
4900
5084
|
if (!("groups" in output.codeSplitting)) return;
|
|
5085
|
+
const groups = output.codeSplitting.groups;
|
|
5086
|
+
if (Array.isArray(groups) && groups.some((group) => typeof group?.name === "function" && patchedManualChunks.has(group.name))) return;
|
|
4901
5087
|
delete output.codeSplitting.groups;
|
|
4902
5088
|
if (Object.keys(output.codeSplitting).length === 0) delete output.codeSplitting;
|
|
4903
5089
|
if (warnedAboutCodeSplittingGroups) return;
|
|
@@ -4905,27 +5091,45 @@ function federation(mfUserOptions) {
|
|
|
4905
5091
|
mfWarn("Ignoring `output.codeSplitting.groups` because it conflicts with module federation. Grouping shared dependency init wrappers with their dependent modules can break runtime init order and cause standalone remotes to fail before mount.");
|
|
4906
5092
|
};
|
|
4907
5093
|
let warnedAboutManualChunks = false;
|
|
4908
|
-
const applyManualChunks = (output) => {
|
|
5094
|
+
const applyManualChunks = (output, useCodeSplitting) => {
|
|
4909
5095
|
ensureCodeSplitting(output);
|
|
4910
5096
|
const isPatchedByPlugin = typeof output.manualChunks === "function" && patchedManualChunks.has(output.manualChunks);
|
|
4911
5097
|
if (output.manualChunks && !isPatchedByPlugin && !warnedAboutManualChunks) {
|
|
4912
5098
|
warnedAboutManualChunks = true;
|
|
4913
5099
|
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.");
|
|
4914
5100
|
}
|
|
4915
|
-
const
|
|
5101
|
+
const mfChunkName = function(id) {
|
|
4916
5102
|
if (id.includes(runtimeInitId)) return "runtimeInit";
|
|
4917
5103
|
if (id.includes("__loadShare__")) {
|
|
4918
5104
|
const match = id.match(/([^/\\]+__loadShare__[^/\\]+)/);
|
|
4919
5105
|
return match ? match[1] : "loadShare";
|
|
4920
5106
|
}
|
|
5107
|
+
return null;
|
|
5108
|
+
};
|
|
5109
|
+
patchedManualChunks.add(mfChunkName);
|
|
5110
|
+
if (!useCodeSplitting) {
|
|
5111
|
+
const mfManualChunks = function(id) {
|
|
5112
|
+
return mfChunkName(id) ?? void 0;
|
|
5113
|
+
};
|
|
5114
|
+
patchedManualChunks.add(mfManualChunks);
|
|
5115
|
+
output.manualChunks = mfManualChunks;
|
|
5116
|
+
return;
|
|
5117
|
+
}
|
|
5118
|
+
const groups = [{
|
|
5119
|
+
name: PRELOAD_HELPER_CHUNK,
|
|
5120
|
+
test: PRELOAD_HELPER_TEST,
|
|
5121
|
+
priority: 100
|
|
5122
|
+
}, { name: mfChunkName }];
|
|
5123
|
+
output.codeSplitting = {
|
|
5124
|
+
...output.codeSplitting || {},
|
|
5125
|
+
groups
|
|
4921
5126
|
};
|
|
4922
|
-
|
|
4923
|
-
output.manualChunks = mfManualChunks;
|
|
5127
|
+
delete output.manualChunks;
|
|
4924
5128
|
};
|
|
4925
5129
|
config.build.rollupOptions = config.build.rollupOptions || {};
|
|
4926
5130
|
const rollupOutput = config.build.rollupOptions.output;
|
|
4927
|
-
if (Array.isArray(rollupOutput)) rollupOutput.forEach((output) => applyManualChunks(output));
|
|
4928
|
-
else applyManualChunks(config.build.rollupOptions.output ||= {});
|
|
5131
|
+
if (Array.isArray(rollupOutput)) rollupOutput.forEach((output) => applyManualChunks(output, false));
|
|
5132
|
+
else applyManualChunks(config.build.rollupOptions.output ||= {}, false);
|
|
4929
5133
|
const buildWithRolldown = config.build;
|
|
4930
5134
|
buildWithRolldown.rolldownOptions = buildWithRolldown.rolldownOptions || {};
|
|
4931
5135
|
const rolldownOutput = buildWithRolldown.rolldownOptions.output;
|
|
@@ -4935,10 +5139,10 @@ function federation(mfUserOptions) {
|
|
|
4935
5139
|
assetFileNames: output.assetFileNames
|
|
4936
5140
|
});
|
|
4937
5141
|
if (Array.isArray(rolldownOutput)) {
|
|
4938
|
-
rolldownOutput.forEach((output) => applyManualChunks(output));
|
|
5142
|
+
rolldownOutput.forEach((output) => applyManualChunks(output, true));
|
|
4939
5143
|
desiredRolldownOutput = rolldownOutput.map((output) => snapshotRolldownOutput(output));
|
|
4940
5144
|
} else {
|
|
4941
|
-
applyManualChunks(buildWithRolldown.rolldownOptions.output ||= {});
|
|
5145
|
+
applyManualChunks(buildWithRolldown.rolldownOptions.output ||= {}, true);
|
|
4942
5146
|
desiredRolldownOutput = [snapshotRolldownOutput(buildWithRolldown.rolldownOptions.output)];
|
|
4943
5147
|
}
|
|
4944
5148
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/vite",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.3",
|
|
4
4
|
"description": "Vite plugin for Module Federation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.cjs",
|
|
@@ -77,23 +77,23 @@
|
|
|
77
77
|
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
78
78
|
},
|
|
79
79
|
"dependencies": {
|
|
80
|
-
"@module-federation/dts-plugin": "2.5.
|
|
81
|
-
"@module-federation/runtime": "2.5.
|
|
82
|
-
"@module-federation/sdk": "2.5.
|
|
83
|
-
"es-module-lexer": "
|
|
84
|
-
"estree-walker": "
|
|
85
|
-
"pathe": "
|
|
80
|
+
"@module-federation/dts-plugin": "2.5.1",
|
|
81
|
+
"@module-federation/runtime": "2.5.1",
|
|
82
|
+
"@module-federation/sdk": "2.5.1",
|
|
83
|
+
"es-module-lexer": "2.0.0",
|
|
84
|
+
"estree-walker": "3.0.3",
|
|
85
|
+
"pathe": "2.0.3"
|
|
86
86
|
},
|
|
87
87
|
"devDependencies": {
|
|
88
|
-
"@changesets/cli": "
|
|
89
|
-
"@playwright/test": "
|
|
90
|
-
"@types/node": "
|
|
91
|
-
"husky": "
|
|
92
|
-
"oxfmt": "
|
|
93
|
-
"rollup": "
|
|
94
|
-
"tsdown": "
|
|
88
|
+
"@changesets/cli": "2.30.0",
|
|
89
|
+
"@playwright/test": "1.58.2",
|
|
90
|
+
"@types/node": "25.3.3",
|
|
91
|
+
"husky": "9.1.7",
|
|
92
|
+
"oxfmt": "0.36.0",
|
|
93
|
+
"rollup": "4.47.1",
|
|
94
|
+
"tsdown": "0.21.0",
|
|
95
95
|
"typescript": "5.9.3",
|
|
96
|
-
"vite": "
|
|
97
|
-
"vitest": "
|
|
96
|
+
"vite": "8.0.10",
|
|
97
|
+
"vitest": "4.0.18"
|
|
98
98
|
}
|
|
99
99
|
}
|