@module-federation/vite 1.16.1 → 1.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +55 -38
- package/lib/index.mjs +55 -38
- package/lib/{packageUtils-DOekOsFz.mjs → packageUtils-Bbc6r6__.mjs} +4 -1
- package/lib/{packageUtils-CbbnJvKu.cjs → packageUtils-se-UDhCa.cjs} +9 -0
- package/lib/{pluginDts-BEOKyKQ-.cjs → pluginDts-7EoFboCu.cjs} +1 -1
- package/lib/{pluginDts-B5pcUmam.mjs → pluginDts-DDx4TPN8.mjs} +1 -1
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_packageUtils = require("./packageUtils-
|
|
2
|
+
const require_packageUtils = require("./packageUtils-se-UDhCa.cjs");
|
|
3
3
|
let fs = require("fs");
|
|
4
4
|
fs = require_packageUtils.__toESM(fs);
|
|
5
5
|
let module$1 = require("module");
|
|
@@ -970,7 +970,7 @@ function getLoadShareModulePath(pkg, isRolldown) {
|
|
|
970
970
|
if (!loadShareCacheMap[pkg]) getLoadShareImportId(pkg, isRolldown);
|
|
971
971
|
return loadShareCacheMap[pkg].getImportId();
|
|
972
972
|
}
|
|
973
|
-
function generateDeferredHostProvidedExports(namedExports, pkg) {
|
|
973
|
+
function generateDeferredHostProvidedExports(namedExports, pkg, cacheKey) {
|
|
974
974
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
975
975
|
const declarations = ["let __mf_default;", ...namedExportVars.map((name) => `let ${name};`)].join("\n ");
|
|
976
976
|
const assignments = [...namedExports.map((name, i) => `${namedExportVars[i]} = exportModule[${escapeGeneratedStringLiteral(name)}];`), "__mf_default = exportModule.default ?? exportModule;"].join("\n ");
|
|
@@ -979,10 +979,10 @@ function generateDeferredHostProvidedExports(namedExports, pkg) {
|
|
|
979
979
|
const __mfApplyHostProvidedExports = (exportModule) => {
|
|
980
980
|
${assignments}
|
|
981
981
|
};
|
|
982
|
-
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
982
|
+
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}];
|
|
983
983
|
if (exportModule === undefined) {
|
|
984
984
|
initPromise.then(() => {
|
|
985
|
-
exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
985
|
+
exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}];
|
|
986
986
|
if (exportModule === undefined) {
|
|
987
987
|
throw new Error("[Module Federation] Shared module ${pkg} was imported before federation bootstrap finished.");
|
|
988
988
|
}
|
|
@@ -1013,13 +1013,14 @@ const normalizeLocalShareModuleCode = `const __mfNormalizeShareModule = (mod) =>
|
|
|
1013
1013
|
function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
1014
1014
|
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = new VirtualModule(pkg, LOAD_SHARE_TAG, ".mjs");
|
|
1015
1015
|
const importLine = getRuntimeModuleCacheBootstrapCode();
|
|
1016
|
+
const cacheKey = require_packageUtils.getSharedCacheKey(pkg, shareItem);
|
|
1016
1017
|
if (shareItem.shareConfig.import === false) {
|
|
1017
1018
|
const namedExports = getPackageNamedExports(pkg);
|
|
1018
1019
|
let exportLine;
|
|
1019
|
-
if (namedExports.length > 0) exportLine = generateDeferredHostProvidedExports(namedExports, pkg);
|
|
1020
|
+
if (namedExports.length > 0) exportLine = generateDeferredHostProvidedExports(namedExports, pkg, cacheKey);
|
|
1020
1021
|
else {
|
|
1021
1022
|
require_packageUtils.mfWarn(`Shared dependency "${pkg}" has import: false but is not installed locally.\n Named imports (e.g. import { ... } from '${pkg}') will not work in production builds.\n Install it as a devDependency to enable named export detection.`);
|
|
1022
|
-
exportLine = generateDeferredHostProvidedExports([], pkg);
|
|
1023
|
+
exportLine = generateDeferredHostProvidedExports([], pkg, cacheKey);
|
|
1023
1024
|
}
|
|
1024
1025
|
loadShareCacheMap[pkg].writeSync(`
|
|
1025
1026
|
${getRuntimeInitPromiseBootstrapCode()}
|
|
@@ -1060,11 +1061,11 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
1060
1061
|
${devDynamicImportLine}
|
|
1061
1062
|
${importLine}
|
|
1062
1063
|
${normalizeLocalShareModuleCode}
|
|
1063
|
-
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1064
|
+
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}]
|
|
1064
1065
|
if (exportModule === undefined) {
|
|
1065
1066
|
${usesLazyLocalFallback ? `exportModule = __mfNormalizeShareModule(await import(${escapeGeneratedStringLiteral(lazyLocalFallbackSource)}));
|
|
1066
|
-
__mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1067
|
-
__mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1067
|
+
__mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}] = exportModule;` : `exportModule = __mfNormalizeShareModule(__mfLocalShare);
|
|
1068
|
+
__mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}] = exportModule;`}
|
|
1068
1069
|
}
|
|
1069
1070
|
${exportLine}
|
|
1070
1071
|
`, true);
|
|
@@ -1218,8 +1219,9 @@ function getShareItemForPreload(pkg) {
|
|
|
1218
1219
|
if (isExplicitSharedKey(pkg)) return shared[pkg];
|
|
1219
1220
|
if (isExplicitSharedKey(wildcardKey)) return shared[wildcardKey];
|
|
1220
1221
|
}
|
|
1221
|
-
function generateSharedCacheSeedItem(pkg, importPath) {
|
|
1222
|
-
|
|
1222
|
+
function generateSharedCacheSeedItem(pkg, shareItem, importPath) {
|
|
1223
|
+
const cacheKey = require_packageUtils.getSharedCacheKey(pkg, shareItem);
|
|
1224
|
+
return `if (__mfModuleCache.share[${JSON.stringify(cacheKey)}] === undefined) {
|
|
1223
1225
|
const mod = await import(${JSON.stringify(importPath)});
|
|
1224
1226
|
${normalizeRuntimeShareCode}
|
|
1225
1227
|
const normalizedModule = __mfNormalizeRuntimeShare(mod);
|
|
@@ -1228,7 +1230,7 @@ function generateSharedCacheSeedItem(pkg, importPath) {
|
|
|
1228
1230
|
value: true,
|
|
1229
1231
|
enumerable: false
|
|
1230
1232
|
});
|
|
1231
|
-
__mfModuleCache.share[${JSON.stringify(
|
|
1233
|
+
__mfModuleCache.share[${JSON.stringify(cacheKey)}] = exportModule;
|
|
1232
1234
|
}`;
|
|
1233
1235
|
}
|
|
1234
1236
|
const normalizeRuntimeShareCode = `const __mfNormalizeRuntimeShare = (mod) => {
|
|
@@ -1246,7 +1248,7 @@ function generateDirectSharedCacheSeedCode(command = "build") {
|
|
|
1246
1248
|
return getOrderedUsedShares().map((pkg) => {
|
|
1247
1249
|
const shareItem = getShareItemForPreload(pkg);
|
|
1248
1250
|
if (!shareItem || shareItem.shareConfig.import === false) return null;
|
|
1249
|
-
return generateSharedCacheSeedItem(pkg, command === "serve" ? getLocalSharedPackagePath(pkg, shareItem) : getDirectSharedCacheSeedImportPath(pkg, shareItem));
|
|
1251
|
+
return generateSharedCacheSeedItem(pkg, shareItem, command === "serve" ? getLocalSharedPackagePath(pkg, shareItem) : getDirectSharedCacheSeedImportPath(pkg, shareItem));
|
|
1250
1252
|
}).filter((item) => item !== null).join("\n");
|
|
1251
1253
|
}
|
|
1252
1254
|
function getBrowserImportPath(importPath) {
|
|
@@ -1268,7 +1270,7 @@ function generateHostAutoInitSharedCacheSeedCode(command = "build") {
|
|
|
1268
1270
|
if (command === "build") return "";
|
|
1269
1271
|
return getHostAutoInitSharedSeedItems().map(({ pkg, shareItem }) => {
|
|
1270
1272
|
if (!shareItem) return null;
|
|
1271
|
-
return generateSharedCacheSeedItem(pkg, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
1273
|
+
return generateSharedCacheSeedItem(pkg, shareItem, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
1272
1274
|
}).filter((item) => item !== null).join("\n");
|
|
1273
1275
|
}
|
|
1274
1276
|
const REMOTE_ENTRY_ID = "virtual:mf-REMOTE_ENTRY_ID";
|
|
@@ -1384,7 +1386,8 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1384
1386
|
console.error('[Module Federation]', e)
|
|
1385
1387
|
}
|
|
1386
1388
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1387
|
-
|
|
1389
|
+
const cacheKey = share.shareConfig?.singleton || !share.version ? pkg : \`\${pkg}@\${share.version}\`;
|
|
1390
|
+
if (share.shareConfig?.import !== false || __mfModuleCache.share[cacheKey] !== undefined) continue;
|
|
1388
1391
|
${normalizeRuntimeShareCode}
|
|
1389
1392
|
const versions = shared?.[pkg];
|
|
1390
1393
|
const provider = versions && versions[Object.keys(versions)[0]];
|
|
@@ -1392,7 +1395,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1392
1395
|
const factory = provider.lib || (provider.loading ? await provider.loading : await provider.get?.());
|
|
1393
1396
|
const mod = typeof factory === "function" ? factory() : factory;
|
|
1394
1397
|
const resolved = await Promise.resolve(mod);
|
|
1395
|
-
__mfModuleCache.share[
|
|
1398
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1396
1399
|
}
|
|
1397
1400
|
return initRes
|
|
1398
1401
|
}
|
|
@@ -1426,7 +1429,8 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1426
1429
|
${normalizeRuntimeShareCode}
|
|
1427
1430
|
${shouldPreloadShares ? `
|
|
1428
1431
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1429
|
-
|
|
1432
|
+
const cacheKey = share.shareConfig?.singleton || !share.version ? pkg : \`\${pkg}@\${share.version}\`;
|
|
1433
|
+
if (__mfModuleCache.share[cacheKey] !== undefined) {
|
|
1430
1434
|
continue;
|
|
1431
1435
|
}
|
|
1432
1436
|
await runtime.loadShare(pkg, {
|
|
@@ -1434,7 +1438,7 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1434
1438
|
}).then((factory) => {
|
|
1435
1439
|
const mod = typeof factory === "function" ? factory() : factory;
|
|
1436
1440
|
return Promise.resolve(mod).then((resolved) => {
|
|
1437
|
-
__mfModuleCache.share[
|
|
1441
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1438
1442
|
});
|
|
1439
1443
|
});
|
|
1440
1444
|
}
|
|
@@ -2929,7 +2933,7 @@ function getCommonSharedSubpathFromNodeModulePath(source, sharedKey) {
|
|
|
2929
2933
|
*/
|
|
2930
2934
|
function resolvePublicPath(options, viteBase, originalBase) {
|
|
2931
2935
|
if (options.publicPath && options.publicPath !== "auto") return options.publicPath;
|
|
2932
|
-
if (originalBase
|
|
2936
|
+
if (!originalBase) return "auto";
|
|
2933
2937
|
if (viteBase) return ensureTrailingSlash(viteBase);
|
|
2934
2938
|
return "auto";
|
|
2935
2939
|
}
|
|
@@ -3431,7 +3435,8 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
3431
3435
|
if (id.includes(getHostAutoInitPath())) {
|
|
3432
3436
|
if (_command === "serve") {
|
|
3433
3437
|
const host = typeof viteConfig.server?.host === "string" && viteConfig.server.host !== "0.0.0.0" ? viteConfig.server.host : "localhost";
|
|
3434
|
-
const
|
|
3438
|
+
const resolvedPublicPath = resolvePublicPath(options, viteConfig.base);
|
|
3439
|
+
const publicPath = JSON.stringify((resolvedPublicPath === "auto" ? "/" : resolvedPublicPath) + options.filename);
|
|
3435
3440
|
const fallbackOrigin = `//${host}:${viteConfig.server?.port}`;
|
|
3436
3441
|
const ssrRemoteEntry = "data:text/javascript," + encodeURIComponent("export async function init(){return {loadRemote:async()=>({}),loadShare:async()=>({})}}");
|
|
3437
3442
|
return `
|
|
@@ -3595,6 +3600,7 @@ function isBuildConfigImporter(importer) {
|
|
|
3595
3600
|
}
|
|
3596
3601
|
function matchesSharedSource(source, key) {
|
|
3597
3602
|
const keyBase = key.endsWith("/") ? key.slice(0, -1) : key;
|
|
3603
|
+
if (keyBase === "vue" && (source === "vue/dist/vue.esm-bundler.js" || source === "vue/dist/vue.runtime.esm-bundler.js")) return true;
|
|
3598
3604
|
if (key.endsWith("/")) return source === keyBase || source.startsWith(`${keyBase}/`);
|
|
3599
3605
|
if (getCommonSharedSubpaths(keyBase).includes(source)) return true;
|
|
3600
3606
|
return source === keyBase;
|
|
@@ -3709,6 +3715,12 @@ function proxySharedModule(options) {
|
|
|
3709
3715
|
name: "proxyPreBuildShared:resolve-shared-loadShare",
|
|
3710
3716
|
enforce: "pre",
|
|
3711
3717
|
async resolveId(source, importer) {
|
|
3718
|
+
function shouldSkipTaggedImporterProxy(sharedKey, tag) {
|
|
3719
|
+
if (!importer?.includes(tag)) return false;
|
|
3720
|
+
const taggedModule = VirtualModule.findModule(tag, importer);
|
|
3721
|
+
if (!taggedModule) return true;
|
|
3722
|
+
return taggedModule.name === sharedKey || matchesSharedSource(source, taggedModule.name);
|
|
3723
|
+
}
|
|
3712
3724
|
const key = findSharedKeyForSource(source, shared);
|
|
3713
3725
|
if (!key) return;
|
|
3714
3726
|
if (useDirectReactImport && key === "react") return;
|
|
@@ -3717,9 +3729,9 @@ function proxySharedModule(options) {
|
|
|
3717
3729
|
if (useDirectReactImport && source === "react") return;
|
|
3718
3730
|
if (importer && importer.includes("localSharedImportMap")) return;
|
|
3719
3731
|
if (importer && (importer.includes("hostAutoInit") || importer.includes("__H_A_I__"))) return;
|
|
3720
|
-
if (
|
|
3721
|
-
if (
|
|
3722
|
-
const shareSource = isNodeModulePath(source) ? getCommonSharedSubpathFromNodeModulePath(source, key) || key : source;
|
|
3732
|
+
if (shouldSkipTaggedImporterProxy(key, "__loadShare__")) return;
|
|
3733
|
+
if (shouldSkipTaggedImporterProxy(key, "__prebuild__")) return;
|
|
3734
|
+
const shareSource = key === "vue" && source.startsWith("vue/dist/") ? key : isNodeModulePath(source) ? getCommonSharedSubpathFromNodeModulePath(source, key) || key : source;
|
|
3723
3735
|
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown);
|
|
3724
3736
|
writeLoadShareModule(shareSource, shared[key], _command, useRolldown);
|
|
3725
3737
|
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key]);
|
|
@@ -3806,19 +3818,8 @@ function applyRewrites(code, imports, id) {
|
|
|
3806
3818
|
const ms = new CodeRewriter(code);
|
|
3807
3819
|
let changed = false;
|
|
3808
3820
|
let counter = 0;
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
const src = JSON.stringify(imp.source);
|
|
3812
|
-
if (imp.namespaceLocal && !imp.defaultLocal && imp.named.length === 0) ms.overwrite(imp.start, imp.end, `import { __moduleExports as ${imp.namespaceLocal} } from ${src};`);
|
|
3813
|
-
else {
|
|
3814
|
-
const nsId = `__mf_ns_${counter++}`;
|
|
3815
|
-
const importParts = [];
|
|
3816
|
-
if (imp.defaultLocal) importParts.push(`default as ${imp.defaultLocal}`);
|
|
3817
|
-
importParts.push(`__moduleExports as ${nsId}`);
|
|
3818
|
-
let rewrite = `import { ${importParts.join(", ")} } from ${src};`;
|
|
3819
|
-
if (imp.named.length > 0) {
|
|
3820
|
-
const isProxyId = `__mf_is_proxy_${counter++}`;
|
|
3821
|
-
const namedProxyHelper = `function __mfCreateNamedRemoteProxy(ns, key) {
|
|
3821
|
+
let namedProxyHelperDeclared = false;
|
|
3822
|
+
const namedProxyHelper = `function __mfCreateNamedRemoteProxy(ns, key) {
|
|
3822
3823
|
const target = function (...args) {
|
|
3823
3824
|
const value = ns[key];
|
|
3824
3825
|
return typeof value === "function" ? value.apply(this, args) : value;
|
|
@@ -3836,13 +3837,29 @@ function applyRewrites(code, imports, id) {
|
|
|
3836
3837
|
}
|
|
3837
3838
|
});
|
|
3838
3839
|
}`;
|
|
3840
|
+
for (const imp of imports) switch (imp.kind) {
|
|
3841
|
+
case "static": {
|
|
3842
|
+
const src = JSON.stringify(imp.source);
|
|
3843
|
+
if (imp.namespaceLocal && !imp.defaultLocal && imp.named.length === 0) ms.overwrite(imp.start, imp.end, `import { __moduleExports as ${imp.namespaceLocal} } from ${src};`);
|
|
3844
|
+
else {
|
|
3845
|
+
const nsId = `__mf_ns_${counter++}`;
|
|
3846
|
+
const importParts = [];
|
|
3847
|
+
if (imp.defaultLocal) importParts.push(`default as ${imp.defaultLocal}`);
|
|
3848
|
+
importParts.push(`__moduleExports as ${nsId}`);
|
|
3849
|
+
let rewrite = `import { ${importParts.join(", ")} } from ${src};`;
|
|
3850
|
+
if (imp.named.length > 0) {
|
|
3851
|
+
const isProxyId = `__mf_is_proxy_${counter++}`;
|
|
3839
3852
|
const tempNames = imp.named.map((_s) => `__mf_named_${counter++}`);
|
|
3840
3853
|
const destructParts = imp.named.map((s, index) => `${s.imported}: ${tempNames[index]}`);
|
|
3841
3854
|
const bindingLines = imp.named.map((s, index) => {
|
|
3842
3855
|
const temp = tempNames[index];
|
|
3843
3856
|
return `const ${s.local} = ${isProxyId} ? __mfCreateNamedRemoteProxy(${nsId}, ${JSON.stringify(s.imported)}) : ${temp};`;
|
|
3844
3857
|
});
|
|
3845
|
-
|
|
3858
|
+
if (!namedProxyHelperDeclared) {
|
|
3859
|
+
rewrite += `\n${namedProxyHelper}`;
|
|
3860
|
+
namedProxyHelperDeclared = true;
|
|
3861
|
+
}
|
|
3862
|
+
rewrite += `\nconst ${isProxyId} = ${nsId} && ${nsId}.__mf_is_remote_proxy;`;
|
|
3846
3863
|
rewrite += `\nconst { ${destructParts.join(", ")} } = ${isProxyId} ? {} : ${nsId};`;
|
|
3847
3864
|
rewrite += `\n${bindingLines.join("\n")}`;
|
|
3848
3865
|
}
|
|
@@ -4744,7 +4761,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4744
4761
|
const SSR_ONLY_PLUGINS = new Set(["@module-federation/vite/ssrEntryLoader"]);
|
|
4745
4762
|
function loadPluginDts(options) {
|
|
4746
4763
|
if (options.dts === false) return [];
|
|
4747
|
-
return [Promise.resolve().then(() => require("./pluginDts-
|
|
4764
|
+
return [Promise.resolve().then(() => require("./pluginDts-7EoFboCu.cjs")).then(({ default: pluginDts }) => pluginDts(options))];
|
|
4748
4765
|
}
|
|
4749
4766
|
function federation(mfUserOptions) {
|
|
4750
4767
|
if (isTestEnv()) return [];
|
package/lib/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as getPackageName, c as
|
|
1
|
+
import { a as getPackageName, c as hasPackageDependency, d as packageNameEncode, f as setPackageDetectionCwd, g as __require, h as mfWarn, i as getPackageDetectionCwd, l as isNuxtProjectRoot, n as getInstalledPackageJson, o as getPackageNameFromNodeModulePath, p as createModuleFederationError, r as getIsRolldown, s as getSharedCacheKey, t as getInstalledPackageEntry, u as packageNameDecode } from "./packageUtils-Bbc6r6__.mjs";
|
|
2
2
|
import * as fs$1 from "fs";
|
|
3
3
|
import { existsSync, readFileSync, realpathSync, statSync, writeFileSync } from "fs";
|
|
4
4
|
import { createRequire } from "module";
|
|
@@ -969,7 +969,7 @@ function getLoadShareModulePath(pkg, isRolldown) {
|
|
|
969
969
|
if (!loadShareCacheMap[pkg]) getLoadShareImportId(pkg, isRolldown);
|
|
970
970
|
return loadShareCacheMap[pkg].getImportId();
|
|
971
971
|
}
|
|
972
|
-
function generateDeferredHostProvidedExports(namedExports, pkg) {
|
|
972
|
+
function generateDeferredHostProvidedExports(namedExports, pkg, cacheKey) {
|
|
973
973
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
974
974
|
const declarations = ["let __mf_default;", ...namedExportVars.map((name) => `let ${name};`)].join("\n ");
|
|
975
975
|
const assignments = [...namedExports.map((name, i) => `${namedExportVars[i]} = exportModule[${escapeGeneratedStringLiteral(name)}];`), "__mf_default = exportModule.default ?? exportModule;"].join("\n ");
|
|
@@ -978,10 +978,10 @@ function generateDeferredHostProvidedExports(namedExports, pkg) {
|
|
|
978
978
|
const __mfApplyHostProvidedExports = (exportModule) => {
|
|
979
979
|
${assignments}
|
|
980
980
|
};
|
|
981
|
-
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
981
|
+
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}];
|
|
982
982
|
if (exportModule === undefined) {
|
|
983
983
|
initPromise.then(() => {
|
|
984
|
-
exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
984
|
+
exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}];
|
|
985
985
|
if (exportModule === undefined) {
|
|
986
986
|
throw new Error("[Module Federation] Shared module ${pkg} was imported before federation bootstrap finished.");
|
|
987
987
|
}
|
|
@@ -1012,13 +1012,14 @@ const normalizeLocalShareModuleCode = `const __mfNormalizeShareModule = (mod) =>
|
|
|
1012
1012
|
function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
1013
1013
|
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = new VirtualModule(pkg, LOAD_SHARE_TAG, ".mjs");
|
|
1014
1014
|
const importLine = getRuntimeModuleCacheBootstrapCode();
|
|
1015
|
+
const cacheKey = getSharedCacheKey(pkg, shareItem);
|
|
1015
1016
|
if (shareItem.shareConfig.import === false) {
|
|
1016
1017
|
const namedExports = getPackageNamedExports(pkg);
|
|
1017
1018
|
let exportLine;
|
|
1018
|
-
if (namedExports.length > 0) exportLine = generateDeferredHostProvidedExports(namedExports, pkg);
|
|
1019
|
+
if (namedExports.length > 0) exportLine = generateDeferredHostProvidedExports(namedExports, pkg, cacheKey);
|
|
1019
1020
|
else {
|
|
1020
1021
|
mfWarn(`Shared dependency "${pkg}" has import: false but is not installed locally.\n Named imports (e.g. import { ... } from '${pkg}') will not work in production builds.\n Install it as a devDependency to enable named export detection.`);
|
|
1021
|
-
exportLine = generateDeferredHostProvidedExports([], pkg);
|
|
1022
|
+
exportLine = generateDeferredHostProvidedExports([], pkg, cacheKey);
|
|
1022
1023
|
}
|
|
1023
1024
|
loadShareCacheMap[pkg].writeSync(`
|
|
1024
1025
|
${getRuntimeInitPromiseBootstrapCode()}
|
|
@@ -1059,11 +1060,11 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
1059
1060
|
${devDynamicImportLine}
|
|
1060
1061
|
${importLine}
|
|
1061
1062
|
${normalizeLocalShareModuleCode}
|
|
1062
|
-
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1063
|
+
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}]
|
|
1063
1064
|
if (exportModule === undefined) {
|
|
1064
1065
|
${usesLazyLocalFallback ? `exportModule = __mfNormalizeShareModule(await import(${escapeGeneratedStringLiteral(lazyLocalFallbackSource)}));
|
|
1065
|
-
__mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1066
|
-
__mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1066
|
+
__mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}] = exportModule;` : `exportModule = __mfNormalizeShareModule(__mfLocalShare);
|
|
1067
|
+
__mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}] = exportModule;`}
|
|
1067
1068
|
}
|
|
1068
1069
|
${exportLine}
|
|
1069
1070
|
`, true);
|
|
@@ -1217,8 +1218,9 @@ function getShareItemForPreload(pkg) {
|
|
|
1217
1218
|
if (isExplicitSharedKey(pkg)) return shared[pkg];
|
|
1218
1219
|
if (isExplicitSharedKey(wildcardKey)) return shared[wildcardKey];
|
|
1219
1220
|
}
|
|
1220
|
-
function generateSharedCacheSeedItem(pkg, importPath) {
|
|
1221
|
-
|
|
1221
|
+
function generateSharedCacheSeedItem(pkg, shareItem, importPath) {
|
|
1222
|
+
const cacheKey = getSharedCacheKey(pkg, shareItem);
|
|
1223
|
+
return `if (__mfModuleCache.share[${JSON.stringify(cacheKey)}] === undefined) {
|
|
1222
1224
|
const mod = await import(${JSON.stringify(importPath)});
|
|
1223
1225
|
${normalizeRuntimeShareCode}
|
|
1224
1226
|
const normalizedModule = __mfNormalizeRuntimeShare(mod);
|
|
@@ -1227,7 +1229,7 @@ function generateSharedCacheSeedItem(pkg, importPath) {
|
|
|
1227
1229
|
value: true,
|
|
1228
1230
|
enumerable: false
|
|
1229
1231
|
});
|
|
1230
|
-
__mfModuleCache.share[${JSON.stringify(
|
|
1232
|
+
__mfModuleCache.share[${JSON.stringify(cacheKey)}] = exportModule;
|
|
1231
1233
|
}`;
|
|
1232
1234
|
}
|
|
1233
1235
|
const normalizeRuntimeShareCode = `const __mfNormalizeRuntimeShare = (mod) => {
|
|
@@ -1245,7 +1247,7 @@ function generateDirectSharedCacheSeedCode(command = "build") {
|
|
|
1245
1247
|
return getOrderedUsedShares().map((pkg) => {
|
|
1246
1248
|
const shareItem = getShareItemForPreload(pkg);
|
|
1247
1249
|
if (!shareItem || shareItem.shareConfig.import === false) return null;
|
|
1248
|
-
return generateSharedCacheSeedItem(pkg, command === "serve" ? getLocalSharedPackagePath(pkg, shareItem) : getDirectSharedCacheSeedImportPath(pkg, shareItem));
|
|
1250
|
+
return generateSharedCacheSeedItem(pkg, shareItem, command === "serve" ? getLocalSharedPackagePath(pkg, shareItem) : getDirectSharedCacheSeedImportPath(pkg, shareItem));
|
|
1249
1251
|
}).filter((item) => item !== null).join("\n");
|
|
1250
1252
|
}
|
|
1251
1253
|
function getBrowserImportPath(importPath) {
|
|
@@ -1267,7 +1269,7 @@ function generateHostAutoInitSharedCacheSeedCode(command = "build") {
|
|
|
1267
1269
|
if (command === "build") return "";
|
|
1268
1270
|
return getHostAutoInitSharedSeedItems().map(({ pkg, shareItem }) => {
|
|
1269
1271
|
if (!shareItem) return null;
|
|
1270
|
-
return generateSharedCacheSeedItem(pkg, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
1272
|
+
return generateSharedCacheSeedItem(pkg, shareItem, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
1271
1273
|
}).filter((item) => item !== null).join("\n");
|
|
1272
1274
|
}
|
|
1273
1275
|
const REMOTE_ENTRY_ID = "virtual:mf-REMOTE_ENTRY_ID";
|
|
@@ -1383,7 +1385,8 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1383
1385
|
console.error('[Module Federation]', e)
|
|
1384
1386
|
}
|
|
1385
1387
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1386
|
-
|
|
1388
|
+
const cacheKey = share.shareConfig?.singleton || !share.version ? pkg : \`\${pkg}@\${share.version}\`;
|
|
1389
|
+
if (share.shareConfig?.import !== false || __mfModuleCache.share[cacheKey] !== undefined) continue;
|
|
1387
1390
|
${normalizeRuntimeShareCode}
|
|
1388
1391
|
const versions = shared?.[pkg];
|
|
1389
1392
|
const provider = versions && versions[Object.keys(versions)[0]];
|
|
@@ -1391,7 +1394,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1391
1394
|
const factory = provider.lib || (provider.loading ? await provider.loading : await provider.get?.());
|
|
1392
1395
|
const mod = typeof factory === "function" ? factory() : factory;
|
|
1393
1396
|
const resolved = await Promise.resolve(mod);
|
|
1394
|
-
__mfModuleCache.share[
|
|
1397
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1395
1398
|
}
|
|
1396
1399
|
return initRes
|
|
1397
1400
|
}
|
|
@@ -1425,7 +1428,8 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1425
1428
|
${normalizeRuntimeShareCode}
|
|
1426
1429
|
${shouldPreloadShares ? `
|
|
1427
1430
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1428
|
-
|
|
1431
|
+
const cacheKey = share.shareConfig?.singleton || !share.version ? pkg : \`\${pkg}@\${share.version}\`;
|
|
1432
|
+
if (__mfModuleCache.share[cacheKey] !== undefined) {
|
|
1429
1433
|
continue;
|
|
1430
1434
|
}
|
|
1431
1435
|
await runtime.loadShare(pkg, {
|
|
@@ -1433,7 +1437,7 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1433
1437
|
}).then((factory) => {
|
|
1434
1438
|
const mod = typeof factory === "function" ? factory() : factory;
|
|
1435
1439
|
return Promise.resolve(mod).then((resolved) => {
|
|
1436
|
-
__mfModuleCache.share[
|
|
1440
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1437
1441
|
});
|
|
1438
1442
|
});
|
|
1439
1443
|
}
|
|
@@ -2928,7 +2932,7 @@ function getCommonSharedSubpathFromNodeModulePath(source, sharedKey) {
|
|
|
2928
2932
|
*/
|
|
2929
2933
|
function resolvePublicPath(options, viteBase, originalBase) {
|
|
2930
2934
|
if (options.publicPath && options.publicPath !== "auto") return options.publicPath;
|
|
2931
|
-
if (originalBase
|
|
2935
|
+
if (!originalBase) return "auto";
|
|
2932
2936
|
if (viteBase) return ensureTrailingSlash(viteBase);
|
|
2933
2937
|
return "auto";
|
|
2934
2938
|
}
|
|
@@ -3430,7 +3434,8 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
3430
3434
|
if (id.includes(getHostAutoInitPath())) {
|
|
3431
3435
|
if (_command === "serve") {
|
|
3432
3436
|
const host = typeof viteConfig.server?.host === "string" && viteConfig.server.host !== "0.0.0.0" ? viteConfig.server.host : "localhost";
|
|
3433
|
-
const
|
|
3437
|
+
const resolvedPublicPath = resolvePublicPath(options, viteConfig.base);
|
|
3438
|
+
const publicPath = JSON.stringify((resolvedPublicPath === "auto" ? "/" : resolvedPublicPath) + options.filename);
|
|
3434
3439
|
const fallbackOrigin = `//${host}:${viteConfig.server?.port}`;
|
|
3435
3440
|
const ssrRemoteEntry = "data:text/javascript," + encodeURIComponent("export async function init(){return {loadRemote:async()=>({}),loadShare:async()=>({})}}");
|
|
3436
3441
|
return `
|
|
@@ -3594,6 +3599,7 @@ function isBuildConfigImporter(importer) {
|
|
|
3594
3599
|
}
|
|
3595
3600
|
function matchesSharedSource(source, key) {
|
|
3596
3601
|
const keyBase = key.endsWith("/") ? key.slice(0, -1) : key;
|
|
3602
|
+
if (keyBase === "vue" && (source === "vue/dist/vue.esm-bundler.js" || source === "vue/dist/vue.runtime.esm-bundler.js")) return true;
|
|
3597
3603
|
if (key.endsWith("/")) return source === keyBase || source.startsWith(`${keyBase}/`);
|
|
3598
3604
|
if (getCommonSharedSubpaths(keyBase).includes(source)) return true;
|
|
3599
3605
|
return source === keyBase;
|
|
@@ -3708,6 +3714,12 @@ function proxySharedModule(options) {
|
|
|
3708
3714
|
name: "proxyPreBuildShared:resolve-shared-loadShare",
|
|
3709
3715
|
enforce: "pre",
|
|
3710
3716
|
async resolveId(source, importer) {
|
|
3717
|
+
function shouldSkipTaggedImporterProxy(sharedKey, tag) {
|
|
3718
|
+
if (!importer?.includes(tag)) return false;
|
|
3719
|
+
const taggedModule = VirtualModule.findModule(tag, importer);
|
|
3720
|
+
if (!taggedModule) return true;
|
|
3721
|
+
return taggedModule.name === sharedKey || matchesSharedSource(source, taggedModule.name);
|
|
3722
|
+
}
|
|
3711
3723
|
const key = findSharedKeyForSource(source, shared);
|
|
3712
3724
|
if (!key) return;
|
|
3713
3725
|
if (useDirectReactImport && key === "react") return;
|
|
@@ -3716,9 +3728,9 @@ function proxySharedModule(options) {
|
|
|
3716
3728
|
if (useDirectReactImport && source === "react") return;
|
|
3717
3729
|
if (importer && importer.includes("localSharedImportMap")) return;
|
|
3718
3730
|
if (importer && (importer.includes("hostAutoInit") || importer.includes("__H_A_I__"))) return;
|
|
3719
|
-
if (
|
|
3720
|
-
if (
|
|
3721
|
-
const shareSource = isNodeModulePath(source) ? getCommonSharedSubpathFromNodeModulePath(source, key) || key : source;
|
|
3731
|
+
if (shouldSkipTaggedImporterProxy(key, "__loadShare__")) return;
|
|
3732
|
+
if (shouldSkipTaggedImporterProxy(key, "__prebuild__")) return;
|
|
3733
|
+
const shareSource = key === "vue" && source.startsWith("vue/dist/") ? key : isNodeModulePath(source) ? getCommonSharedSubpathFromNodeModulePath(source, key) || key : source;
|
|
3722
3734
|
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown);
|
|
3723
3735
|
writeLoadShareModule(shareSource, shared[key], _command, useRolldown);
|
|
3724
3736
|
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key]);
|
|
@@ -3805,19 +3817,8 @@ function applyRewrites(code, imports, id) {
|
|
|
3805
3817
|
const ms = new CodeRewriter(code);
|
|
3806
3818
|
let changed = false;
|
|
3807
3819
|
let counter = 0;
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
const src = JSON.stringify(imp.source);
|
|
3811
|
-
if (imp.namespaceLocal && !imp.defaultLocal && imp.named.length === 0) ms.overwrite(imp.start, imp.end, `import { __moduleExports as ${imp.namespaceLocal} } from ${src};`);
|
|
3812
|
-
else {
|
|
3813
|
-
const nsId = `__mf_ns_${counter++}`;
|
|
3814
|
-
const importParts = [];
|
|
3815
|
-
if (imp.defaultLocal) importParts.push(`default as ${imp.defaultLocal}`);
|
|
3816
|
-
importParts.push(`__moduleExports as ${nsId}`);
|
|
3817
|
-
let rewrite = `import { ${importParts.join(", ")} } from ${src};`;
|
|
3818
|
-
if (imp.named.length > 0) {
|
|
3819
|
-
const isProxyId = `__mf_is_proxy_${counter++}`;
|
|
3820
|
-
const namedProxyHelper = `function __mfCreateNamedRemoteProxy(ns, key) {
|
|
3820
|
+
let namedProxyHelperDeclared = false;
|
|
3821
|
+
const namedProxyHelper = `function __mfCreateNamedRemoteProxy(ns, key) {
|
|
3821
3822
|
const target = function (...args) {
|
|
3822
3823
|
const value = ns[key];
|
|
3823
3824
|
return typeof value === "function" ? value.apply(this, args) : value;
|
|
@@ -3835,13 +3836,29 @@ function applyRewrites(code, imports, id) {
|
|
|
3835
3836
|
}
|
|
3836
3837
|
});
|
|
3837
3838
|
}`;
|
|
3839
|
+
for (const imp of imports) switch (imp.kind) {
|
|
3840
|
+
case "static": {
|
|
3841
|
+
const src = JSON.stringify(imp.source);
|
|
3842
|
+
if (imp.namespaceLocal && !imp.defaultLocal && imp.named.length === 0) ms.overwrite(imp.start, imp.end, `import { __moduleExports as ${imp.namespaceLocal} } from ${src};`);
|
|
3843
|
+
else {
|
|
3844
|
+
const nsId = `__mf_ns_${counter++}`;
|
|
3845
|
+
const importParts = [];
|
|
3846
|
+
if (imp.defaultLocal) importParts.push(`default as ${imp.defaultLocal}`);
|
|
3847
|
+
importParts.push(`__moduleExports as ${nsId}`);
|
|
3848
|
+
let rewrite = `import { ${importParts.join(", ")} } from ${src};`;
|
|
3849
|
+
if (imp.named.length > 0) {
|
|
3850
|
+
const isProxyId = `__mf_is_proxy_${counter++}`;
|
|
3838
3851
|
const tempNames = imp.named.map((_s) => `__mf_named_${counter++}`);
|
|
3839
3852
|
const destructParts = imp.named.map((s, index) => `${s.imported}: ${tempNames[index]}`);
|
|
3840
3853
|
const bindingLines = imp.named.map((s, index) => {
|
|
3841
3854
|
const temp = tempNames[index];
|
|
3842
3855
|
return `const ${s.local} = ${isProxyId} ? __mfCreateNamedRemoteProxy(${nsId}, ${JSON.stringify(s.imported)}) : ${temp};`;
|
|
3843
3856
|
});
|
|
3844
|
-
|
|
3857
|
+
if (!namedProxyHelperDeclared) {
|
|
3858
|
+
rewrite += `\n${namedProxyHelper}`;
|
|
3859
|
+
namedProxyHelperDeclared = true;
|
|
3860
|
+
}
|
|
3861
|
+
rewrite += `\nconst ${isProxyId} = ${nsId} && ${nsId}.__mf_is_remote_proxy;`;
|
|
3845
3862
|
rewrite += `\nconst { ${destructParts.join(", ")} } = ${isProxyId} ? {} : ${nsId};`;
|
|
3846
3863
|
rewrite += `\n${bindingLines.join("\n")}`;
|
|
3847
3864
|
}
|
|
@@ -4743,7 +4760,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4743
4760
|
const SSR_ONLY_PLUGINS = new Set(["@module-federation/vite/ssrEntryLoader"]);
|
|
4744
4761
|
function loadPluginDts(options) {
|
|
4745
4762
|
if (options.dts === false) return [];
|
|
4746
|
-
return [import("./pluginDts-
|
|
4763
|
+
return [import("./pluginDts-DDx4TPN8.mjs").then(({ default: pluginDts }) => pluginDts(options))];
|
|
4747
4764
|
}
|
|
4748
4765
|
function federation(mfUserOptions) {
|
|
4749
4766
|
if (isTestEnv()) return [];
|
|
@@ -123,6 +123,9 @@ function getPackageNameFromNodeModulePath(source) {
|
|
|
123
123
|
if (parts[0].startsWith("@")) return parts[1] ? `${parts[0]}/${parts[1]}` : void 0;
|
|
124
124
|
return parts[0];
|
|
125
125
|
}
|
|
126
|
+
function getSharedCacheKey(pkg, shareItem) {
|
|
127
|
+
return shareItem.shareConfig.singleton || !shareItem.version ? pkg : `${pkg}@${shareItem.version}`;
|
|
128
|
+
}
|
|
126
129
|
function getInstalledPackageJson(pkg, opts) {
|
|
127
130
|
const cwd = opts?.cwd || getPackageDetectionCwd();
|
|
128
131
|
const packageName = opts?.packageName || getPackageName(pkg);
|
|
@@ -247,4 +250,4 @@ function hasPackageDependency(dependencyName, cwd = packageDetectionCwd || proce
|
|
|
247
250
|
}
|
|
248
251
|
}
|
|
249
252
|
//#endregion
|
|
250
|
-
export { getPackageName as a,
|
|
253
|
+
export { getPackageName as a, hasPackageDependency as c, packageNameEncode as d, setPackageDetectionCwd as f, __require as g, mfWarn as h, getPackageDetectionCwd as i, isNuxtProjectRoot as l, mfError as m, getInstalledPackageJson as n, getPackageNameFromNodeModulePath as o, createModuleFederationError as p, getIsRolldown as r, getSharedCacheKey as s, getInstalledPackageEntry as t, packageNameDecode as u };
|
|
@@ -142,6 +142,9 @@ function getPackageNameFromNodeModulePath(source) {
|
|
|
142
142
|
if (parts[0].startsWith("@")) return parts[1] ? `${parts[0]}/${parts[1]}` : void 0;
|
|
143
143
|
return parts[0];
|
|
144
144
|
}
|
|
145
|
+
function getSharedCacheKey(pkg, shareItem) {
|
|
146
|
+
return shareItem.shareConfig.singleton || !shareItem.version ? pkg : `${pkg}@${shareItem.version}`;
|
|
147
|
+
}
|
|
145
148
|
function getInstalledPackageJson(pkg, opts) {
|
|
146
149
|
const cwd = opts?.cwd || getPackageDetectionCwd();
|
|
147
150
|
const packageName = opts?.packageName || getPackageName(pkg);
|
|
@@ -314,6 +317,12 @@ Object.defineProperty(exports, "getPackageNameFromNodeModulePath", {
|
|
|
314
317
|
return getPackageNameFromNodeModulePath;
|
|
315
318
|
}
|
|
316
319
|
});
|
|
320
|
+
Object.defineProperty(exports, "getSharedCacheKey", {
|
|
321
|
+
enumerable: true,
|
|
322
|
+
get: function() {
|
|
323
|
+
return getSharedCacheKey;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
317
326
|
Object.defineProperty(exports, "hasPackageDependency", {
|
|
318
327
|
enumerable: true,
|
|
319
328
|
get: function() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as hasPackageDependency, g as __require, m as mfError, p as createModuleFederationError } from "./packageUtils-Bbc6r6__.mjs";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import * as path$1 from "pathe";
|
|
4
4
|
import { normalizeOptions } from "@module-federation/sdk";
|