@module-federation/vite 1.16.0 → 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 +69 -59
- package/lib/index.mjs +69 -58
- 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 +2 -2
package/lib/index.cjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
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");
|
|
6
6
|
let pathe = require("pathe");
|
|
7
7
|
pathe = require_packageUtils.__toESM(pathe);
|
|
8
8
|
let vite = require("vite");
|
|
9
|
+
let node_crypto = require("node:crypto");
|
|
9
10
|
let url = require("url");
|
|
10
11
|
let es_module_lexer = require("es-module-lexer");
|
|
11
12
|
//#region src/utils/codeRewriter.ts
|
|
@@ -254,23 +255,8 @@ function getLitExportSubpathShares(sharedName) {
|
|
|
254
255
|
return Object.keys(exportsField).filter((key) => key.startsWith("./") && key !== "." && !key.includes("*")).map((key) => `${sharedName}/${key.slice(2)}`);
|
|
255
256
|
}
|
|
256
257
|
function normalizeShareItem(key, shareItem) {
|
|
257
|
-
let version;
|
|
258
258
|
const isImportFalse = typeof shareItem === "object" && shareItem.import === false;
|
|
259
|
-
|
|
260
|
-
try {
|
|
261
|
-
version = require(pathe.join(require_packageUtils.getPackageName(key), "package.json")).version;
|
|
262
|
-
} catch (e1) {
|
|
263
|
-
try {
|
|
264
|
-
const localPath = pathe.join(process.cwd(), "node_modules", require_packageUtils.getPackageName(key), "package.json");
|
|
265
|
-
version = require(localPath).version;
|
|
266
|
-
} catch (e2) {
|
|
267
|
-
version = searchPackageVersion(key);
|
|
268
|
-
if (!version && !isImportFalse) require_packageUtils.mfError(e1);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
} catch (e) {
|
|
272
|
-
if (!isImportFalse) require_packageUtils.mfError(`Unexpected error resolving version for ${key}:`, e);
|
|
273
|
-
}
|
|
259
|
+
const version = (typeof shareItem === "object" ? shareItem.version || inferVersionFromRequiredVersion(shareItem.requiredVersion) : void 0) || searchPackageVersion(key);
|
|
274
260
|
if (typeof shareItem === "string") return {
|
|
275
261
|
name: shareItem,
|
|
276
262
|
version,
|
|
@@ -285,12 +271,12 @@ function normalizeShareItem(key, shareItem) {
|
|
|
285
271
|
return {
|
|
286
272
|
name: key,
|
|
287
273
|
from: "",
|
|
288
|
-
version
|
|
274
|
+
version,
|
|
289
275
|
scope: shareItem.shareScope || "default",
|
|
290
276
|
shareConfig: {
|
|
291
277
|
import: shareItem.import,
|
|
292
278
|
singleton: shareItem.singleton || false,
|
|
293
|
-
requiredVersion: shareItem.requiredVersion || (isImportFalse ? "*" : version ? `^${version}` : "*"),
|
|
279
|
+
requiredVersion: shareItem.requiredVersion || (isImportFalse || shareItem.version ? "*" : version ? `^${version}` : "*"),
|
|
294
280
|
strictVersion: !!shareItem.strictVersion
|
|
295
281
|
}
|
|
296
282
|
};
|
|
@@ -318,11 +304,13 @@ function normalizeShared(shared) {
|
|
|
318
304
|
const result = {};
|
|
319
305
|
const sourceEntries = [];
|
|
320
306
|
if (Array.isArray(shared)) shared.forEach((key) => {
|
|
307
|
+
if (isModuleFederationRuntimePackage(key)) return;
|
|
321
308
|
result[key] = normalizeShareItem(key, key);
|
|
322
309
|
explicitSharedKeys.add(key);
|
|
323
310
|
sourceEntries.push([key, key]);
|
|
324
311
|
});
|
|
325
312
|
else if (typeof shared === "object") Object.keys(shared).forEach((key) => {
|
|
313
|
+
if (isModuleFederationRuntimePackage(key)) return;
|
|
326
314
|
const value = shared[key];
|
|
327
315
|
result[key] = normalizeShareItem(key, value);
|
|
328
316
|
explicitSharedKeys.add(key);
|
|
@@ -336,11 +324,14 @@ function normalizeShared(shared) {
|
|
|
336
324
|
});
|
|
337
325
|
return result;
|
|
338
326
|
}
|
|
327
|
+
function isModuleFederationRuntimePackage(key) {
|
|
328
|
+
return key === "@module-federation/runtime" || key === "@module-federation/runtime-core";
|
|
329
|
+
}
|
|
339
330
|
function shouldAutoShareDependency(key) {
|
|
340
331
|
const pkg = require_packageUtils.getInstalledPackageJson(key)?.packageJson;
|
|
341
332
|
if (!pkg) return false;
|
|
342
333
|
if (!pkg.browser && !pkg.exports && typeof pkg.module !== "string") return false;
|
|
343
|
-
if (key === "@module-federation/vite") return false;
|
|
334
|
+
if (key === "@module-federation/vite" || isModuleFederationRuntimePackage(key)) return false;
|
|
344
335
|
const entry = [
|
|
345
336
|
pkg.module,
|
|
346
337
|
pkg.main,
|
|
@@ -979,7 +970,7 @@ function getLoadShareModulePath(pkg, isRolldown) {
|
|
|
979
970
|
if (!loadShareCacheMap[pkg]) getLoadShareImportId(pkg, isRolldown);
|
|
980
971
|
return loadShareCacheMap[pkg].getImportId();
|
|
981
972
|
}
|
|
982
|
-
function generateDeferredHostProvidedExports(namedExports, pkg) {
|
|
973
|
+
function generateDeferredHostProvidedExports(namedExports, pkg, cacheKey) {
|
|
983
974
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
984
975
|
const declarations = ["let __mf_default;", ...namedExportVars.map((name) => `let ${name};`)].join("\n ");
|
|
985
976
|
const assignments = [...namedExports.map((name, i) => `${namedExportVars[i]} = exportModule[${escapeGeneratedStringLiteral(name)}];`), "__mf_default = exportModule.default ?? exportModule;"].join("\n ");
|
|
@@ -988,10 +979,10 @@ function generateDeferredHostProvidedExports(namedExports, pkg) {
|
|
|
988
979
|
const __mfApplyHostProvidedExports = (exportModule) => {
|
|
989
980
|
${assignments}
|
|
990
981
|
};
|
|
991
|
-
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
982
|
+
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}];
|
|
992
983
|
if (exportModule === undefined) {
|
|
993
984
|
initPromise.then(() => {
|
|
994
|
-
exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
985
|
+
exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}];
|
|
995
986
|
if (exportModule === undefined) {
|
|
996
987
|
throw new Error("[Module Federation] Shared module ${pkg} was imported before federation bootstrap finished.");
|
|
997
988
|
}
|
|
@@ -1022,13 +1013,14 @@ const normalizeLocalShareModuleCode = `const __mfNormalizeShareModule = (mod) =>
|
|
|
1022
1013
|
function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
1023
1014
|
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = new VirtualModule(pkg, LOAD_SHARE_TAG, ".mjs");
|
|
1024
1015
|
const importLine = getRuntimeModuleCacheBootstrapCode();
|
|
1016
|
+
const cacheKey = require_packageUtils.getSharedCacheKey(pkg, shareItem);
|
|
1025
1017
|
if (shareItem.shareConfig.import === false) {
|
|
1026
1018
|
const namedExports = getPackageNamedExports(pkg);
|
|
1027
1019
|
let exportLine;
|
|
1028
|
-
if (namedExports.length > 0) exportLine = generateDeferredHostProvidedExports(namedExports, pkg);
|
|
1020
|
+
if (namedExports.length > 0) exportLine = generateDeferredHostProvidedExports(namedExports, pkg, cacheKey);
|
|
1029
1021
|
else {
|
|
1030
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.`);
|
|
1031
|
-
exportLine = generateDeferredHostProvidedExports([], pkg);
|
|
1023
|
+
exportLine = generateDeferredHostProvidedExports([], pkg, cacheKey);
|
|
1032
1024
|
}
|
|
1033
1025
|
loadShareCacheMap[pkg].writeSync(`
|
|
1034
1026
|
${getRuntimeInitPromiseBootstrapCode()}
|
|
@@ -1069,11 +1061,11 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
1069
1061
|
${devDynamicImportLine}
|
|
1070
1062
|
${importLine}
|
|
1071
1063
|
${normalizeLocalShareModuleCode}
|
|
1072
|
-
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1064
|
+
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}]
|
|
1073
1065
|
if (exportModule === undefined) {
|
|
1074
1066
|
${usesLazyLocalFallback ? `exportModule = __mfNormalizeShareModule(await import(${escapeGeneratedStringLiteral(lazyLocalFallbackSource)}));
|
|
1075
|
-
__mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1076
|
-
__mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1067
|
+
__mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}] = exportModule;` : `exportModule = __mfNormalizeShareModule(__mfLocalShare);
|
|
1068
|
+
__mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}] = exportModule;`}
|
|
1077
1069
|
}
|
|
1078
1070
|
${exportLine}
|
|
1079
1071
|
`, true);
|
|
@@ -1227,8 +1219,9 @@ function getShareItemForPreload(pkg) {
|
|
|
1227
1219
|
if (isExplicitSharedKey(pkg)) return shared[pkg];
|
|
1228
1220
|
if (isExplicitSharedKey(wildcardKey)) return shared[wildcardKey];
|
|
1229
1221
|
}
|
|
1230
|
-
function generateSharedCacheSeedItem(pkg, importPath) {
|
|
1231
|
-
|
|
1222
|
+
function generateSharedCacheSeedItem(pkg, shareItem, importPath) {
|
|
1223
|
+
const cacheKey = require_packageUtils.getSharedCacheKey(pkg, shareItem);
|
|
1224
|
+
return `if (__mfModuleCache.share[${JSON.stringify(cacheKey)}] === undefined) {
|
|
1232
1225
|
const mod = await import(${JSON.stringify(importPath)});
|
|
1233
1226
|
${normalizeRuntimeShareCode}
|
|
1234
1227
|
const normalizedModule = __mfNormalizeRuntimeShare(mod);
|
|
@@ -1237,7 +1230,7 @@ function generateSharedCacheSeedItem(pkg, importPath) {
|
|
|
1237
1230
|
value: true,
|
|
1238
1231
|
enumerable: false
|
|
1239
1232
|
});
|
|
1240
|
-
__mfModuleCache.share[${JSON.stringify(
|
|
1233
|
+
__mfModuleCache.share[${JSON.stringify(cacheKey)}] = exportModule;
|
|
1241
1234
|
}`;
|
|
1242
1235
|
}
|
|
1243
1236
|
const normalizeRuntimeShareCode = `const __mfNormalizeRuntimeShare = (mod) => {
|
|
@@ -1255,7 +1248,7 @@ function generateDirectSharedCacheSeedCode(command = "build") {
|
|
|
1255
1248
|
return getOrderedUsedShares().map((pkg) => {
|
|
1256
1249
|
const shareItem = getShareItemForPreload(pkg);
|
|
1257
1250
|
if (!shareItem || shareItem.shareConfig.import === false) return null;
|
|
1258
|
-
return generateSharedCacheSeedItem(pkg, command === "serve" ? getLocalSharedPackagePath(pkg, shareItem) : getDirectSharedCacheSeedImportPath(pkg, shareItem));
|
|
1251
|
+
return generateSharedCacheSeedItem(pkg, shareItem, command === "serve" ? getLocalSharedPackagePath(pkg, shareItem) : getDirectSharedCacheSeedImportPath(pkg, shareItem));
|
|
1259
1252
|
}).filter((item) => item !== null).join("\n");
|
|
1260
1253
|
}
|
|
1261
1254
|
function getBrowserImportPath(importPath) {
|
|
@@ -1277,7 +1270,7 @@ function generateHostAutoInitSharedCacheSeedCode(command = "build") {
|
|
|
1277
1270
|
if (command === "build") return "";
|
|
1278
1271
|
return getHostAutoInitSharedSeedItems().map(({ pkg, shareItem }) => {
|
|
1279
1272
|
if (!shareItem) return null;
|
|
1280
|
-
return generateSharedCacheSeedItem(pkg, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
1273
|
+
return generateSharedCacheSeedItem(pkg, shareItem, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
1281
1274
|
}).filter((item) => item !== null).join("\n");
|
|
1282
1275
|
}
|
|
1283
1276
|
const REMOTE_ENTRY_ID = "virtual:mf-REMOTE_ENTRY_ID";
|
|
@@ -1393,7 +1386,8 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1393
1386
|
console.error('[Module Federation]', e)
|
|
1394
1387
|
}
|
|
1395
1388
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1396
|
-
|
|
1389
|
+
const cacheKey = share.shareConfig?.singleton || !share.version ? pkg : \`\${pkg}@\${share.version}\`;
|
|
1390
|
+
if (share.shareConfig?.import !== false || __mfModuleCache.share[cacheKey] !== undefined) continue;
|
|
1397
1391
|
${normalizeRuntimeShareCode}
|
|
1398
1392
|
const versions = shared?.[pkg];
|
|
1399
1393
|
const provider = versions && versions[Object.keys(versions)[0]];
|
|
@@ -1401,7 +1395,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1401
1395
|
const factory = provider.lib || (provider.loading ? await provider.loading : await provider.get?.());
|
|
1402
1396
|
const mod = typeof factory === "function" ? factory() : factory;
|
|
1403
1397
|
const resolved = await Promise.resolve(mod);
|
|
1404
|
-
__mfModuleCache.share[
|
|
1398
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1405
1399
|
}
|
|
1406
1400
|
return initRes
|
|
1407
1401
|
}
|
|
@@ -1435,7 +1429,8 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1435
1429
|
${normalizeRuntimeShareCode}
|
|
1436
1430
|
${shouldPreloadShares ? `
|
|
1437
1431
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1438
|
-
|
|
1432
|
+
const cacheKey = share.shareConfig?.singleton || !share.version ? pkg : \`\${pkg}@\${share.version}\`;
|
|
1433
|
+
if (__mfModuleCache.share[cacheKey] !== undefined) {
|
|
1439
1434
|
continue;
|
|
1440
1435
|
}
|
|
1441
1436
|
await runtime.loadShare(pkg, {
|
|
@@ -1443,7 +1438,7 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1443
1438
|
}).then((factory) => {
|
|
1444
1439
|
const mod = typeof factory === "function" ? factory() : factory;
|
|
1445
1440
|
return Promise.resolve(mod).then((resolved) => {
|
|
1446
|
-
__mfModuleCache.share[
|
|
1441
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1447
1442
|
});
|
|
1448
1443
|
});
|
|
1449
1444
|
}
|
|
@@ -1984,11 +1979,13 @@ const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClien
|
|
|
1984
1979
|
let rewritten = false;
|
|
1985
1980
|
htmlContent = htmlContent.replace(scriptRegex, (scriptTag, entrySrc) => {
|
|
1986
1981
|
rewritten = true;
|
|
1987
|
-
const
|
|
1982
|
+
const bootstrapSource = getSystemBootstrapSource(initPath, entrySrc);
|
|
1983
|
+
const bootstrapHash = (0, node_crypto.createHash)("sha256").update(bootstrapSource).digest("hex").slice(0, 8);
|
|
1984
|
+
const bootstrapFileName = `mf-entry-bootstrap-${bootstrapIndex++}-${bootstrapHash}.js`;
|
|
1988
1985
|
const bootstrapRef = this.emitFile({
|
|
1989
1986
|
type: "asset",
|
|
1990
1987
|
fileName: bootstrapFileName,
|
|
1991
|
-
source:
|
|
1988
|
+
source: bootstrapSource
|
|
1992
1989
|
});
|
|
1993
1990
|
const bootstrapPath = viteConfig.base + this.getFileName(bootstrapRef);
|
|
1994
1991
|
return scriptTag.replace(entrySrc, bootstrapPath);
|
|
@@ -2936,7 +2933,7 @@ function getCommonSharedSubpathFromNodeModulePath(source, sharedKey) {
|
|
|
2936
2933
|
*/
|
|
2937
2934
|
function resolvePublicPath(options, viteBase, originalBase) {
|
|
2938
2935
|
if (options.publicPath && options.publicPath !== "auto") return options.publicPath;
|
|
2939
|
-
if (originalBase
|
|
2936
|
+
if (!originalBase) return "auto";
|
|
2940
2937
|
if (viteBase) return ensureTrailingSlash(viteBase);
|
|
2941
2938
|
return "auto";
|
|
2942
2939
|
}
|
|
@@ -3438,7 +3435,8 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
3438
3435
|
if (id.includes(getHostAutoInitPath())) {
|
|
3439
3436
|
if (_command === "serve") {
|
|
3440
3437
|
const host = typeof viteConfig.server?.host === "string" && viteConfig.server.host !== "0.0.0.0" ? viteConfig.server.host : "localhost";
|
|
3441
|
-
const
|
|
3438
|
+
const resolvedPublicPath = resolvePublicPath(options, viteConfig.base);
|
|
3439
|
+
const publicPath = JSON.stringify((resolvedPublicPath === "auto" ? "/" : resolvedPublicPath) + options.filename);
|
|
3442
3440
|
const fallbackOrigin = `//${host}:${viteConfig.server?.port}`;
|
|
3443
3441
|
const ssrRemoteEntry = "data:text/javascript," + encodeURIComponent("export async function init(){return {loadRemote:async()=>({}),loadShare:async()=>({})}}");
|
|
3444
3442
|
return `
|
|
@@ -3602,6 +3600,7 @@ function isBuildConfigImporter(importer) {
|
|
|
3602
3600
|
}
|
|
3603
3601
|
function matchesSharedSource(source, key) {
|
|
3604
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;
|
|
3605
3604
|
if (key.endsWith("/")) return source === keyBase || source.startsWith(`${keyBase}/`);
|
|
3606
3605
|
if (getCommonSharedSubpaths(keyBase).includes(source)) return true;
|
|
3607
3606
|
return source === keyBase;
|
|
@@ -3716,6 +3715,12 @@ function proxySharedModule(options) {
|
|
|
3716
3715
|
name: "proxyPreBuildShared:resolve-shared-loadShare",
|
|
3717
3716
|
enforce: "pre",
|
|
3718
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
|
+
}
|
|
3719
3724
|
const key = findSharedKeyForSource(source, shared);
|
|
3720
3725
|
if (!key) return;
|
|
3721
3726
|
if (useDirectReactImport && key === "react") return;
|
|
@@ -3724,9 +3729,9 @@ function proxySharedModule(options) {
|
|
|
3724
3729
|
if (useDirectReactImport && source === "react") return;
|
|
3725
3730
|
if (importer && importer.includes("localSharedImportMap")) return;
|
|
3726
3731
|
if (importer && (importer.includes("hostAutoInit") || importer.includes("__H_A_I__"))) return;
|
|
3727
|
-
if (
|
|
3728
|
-
if (
|
|
3729
|
-
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;
|
|
3730
3735
|
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown);
|
|
3731
3736
|
writeLoadShareModule(shareSource, shared[key], _command, useRolldown);
|
|
3732
3737
|
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key]);
|
|
@@ -3813,19 +3818,8 @@ function applyRewrites(code, imports, id) {
|
|
|
3813
3818
|
const ms = new CodeRewriter(code);
|
|
3814
3819
|
let changed = false;
|
|
3815
3820
|
let counter = 0;
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
const src = JSON.stringify(imp.source);
|
|
3819
|
-
if (imp.namespaceLocal && !imp.defaultLocal && imp.named.length === 0) ms.overwrite(imp.start, imp.end, `import { __moduleExports as ${imp.namespaceLocal} } from ${src};`);
|
|
3820
|
-
else {
|
|
3821
|
-
const nsId = `__mf_ns_${counter++}`;
|
|
3822
|
-
const importParts = [];
|
|
3823
|
-
if (imp.defaultLocal) importParts.push(`default as ${imp.defaultLocal}`);
|
|
3824
|
-
importParts.push(`__moduleExports as ${nsId}`);
|
|
3825
|
-
let rewrite = `import { ${importParts.join(", ")} } from ${src};`;
|
|
3826
|
-
if (imp.named.length > 0) {
|
|
3827
|
-
const isProxyId = `__mf_is_proxy_${counter++}`;
|
|
3828
|
-
const namedProxyHelper = `function __mfCreateNamedRemoteProxy(ns, key) {
|
|
3821
|
+
let namedProxyHelperDeclared = false;
|
|
3822
|
+
const namedProxyHelper = `function __mfCreateNamedRemoteProxy(ns, key) {
|
|
3829
3823
|
const target = function (...args) {
|
|
3830
3824
|
const value = ns[key];
|
|
3831
3825
|
return typeof value === "function" ? value.apply(this, args) : value;
|
|
@@ -3843,13 +3837,29 @@ function applyRewrites(code, imports, id) {
|
|
|
3843
3837
|
}
|
|
3844
3838
|
});
|
|
3845
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++}`;
|
|
3846
3852
|
const tempNames = imp.named.map((_s) => `__mf_named_${counter++}`);
|
|
3847
3853
|
const destructParts = imp.named.map((s, index) => `${s.imported}: ${tempNames[index]}`);
|
|
3848
3854
|
const bindingLines = imp.named.map((s, index) => {
|
|
3849
3855
|
const temp = tempNames[index];
|
|
3850
3856
|
return `const ${s.local} = ${isProxyId} ? __mfCreateNamedRemoteProxy(${nsId}, ${JSON.stringify(s.imported)}) : ${temp};`;
|
|
3851
3857
|
});
|
|
3852
|
-
|
|
3858
|
+
if (!namedProxyHelperDeclared) {
|
|
3859
|
+
rewrite += `\n${namedProxyHelper}`;
|
|
3860
|
+
namedProxyHelperDeclared = true;
|
|
3861
|
+
}
|
|
3862
|
+
rewrite += `\nconst ${isProxyId} = ${nsId} && ${nsId}.__mf_is_remote_proxy;`;
|
|
3853
3863
|
rewrite += `\nconst { ${destructParts.join(", ")} } = ${isProxyId} ? {} : ${nsId};`;
|
|
3854
3864
|
rewrite += `\n${bindingLines.join("\n")}`;
|
|
3855
3865
|
}
|
|
@@ -4751,7 +4761,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4751
4761
|
const SSR_ONLY_PLUGINS = new Set(["@module-federation/vite/ssrEntryLoader"]);
|
|
4752
4762
|
function loadPluginDts(options) {
|
|
4753
4763
|
if (options.dts === false) return [];
|
|
4754
|
-
return [Promise.resolve().then(() => require("./pluginDts-
|
|
4764
|
+
return [Promise.resolve().then(() => require("./pluginDts-7EoFboCu.cjs")).then(({ default: pluginDts }) => pluginDts(options))];
|
|
4755
4765
|
}
|
|
4756
4766
|
function federation(mfUserOptions) {
|
|
4757
4767
|
if (isTestEnv()) return [];
|
package/lib/index.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
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";
|
|
5
5
|
import * as path$1 from "pathe";
|
|
6
6
|
import path, { basename } from "pathe";
|
|
7
7
|
import { version } from "vite";
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
8
9
|
import { fileURLToPath } from "url";
|
|
9
10
|
import { init, parse } from "es-module-lexer";
|
|
10
11
|
//#region src/utils/codeRewriter.ts
|
|
@@ -253,22 +254,8 @@ function getLitExportSubpathShares(sharedName) {
|
|
|
253
254
|
return Object.keys(exportsField).filter((key) => key.startsWith("./") && key !== "." && !key.includes("*")).map((key) => `${sharedName}/${key.slice(2)}`);
|
|
254
255
|
}
|
|
255
256
|
function normalizeShareItem(key, shareItem) {
|
|
256
|
-
let version;
|
|
257
257
|
const isImportFalse = typeof shareItem === "object" && shareItem.import === false;
|
|
258
|
-
|
|
259
|
-
try {
|
|
260
|
-
version = __require(path$1.join(getPackageName(key), "package.json")).version;
|
|
261
|
-
} catch (e1) {
|
|
262
|
-
try {
|
|
263
|
-
version = __require(path$1.join(process.cwd(), "node_modules", getPackageName(key), "package.json")).version;
|
|
264
|
-
} catch (e2) {
|
|
265
|
-
version = searchPackageVersion(key);
|
|
266
|
-
if (!version && !isImportFalse) mfError(e1);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
} catch (e) {
|
|
270
|
-
if (!isImportFalse) mfError(`Unexpected error resolving version for ${key}:`, e);
|
|
271
|
-
}
|
|
258
|
+
const version = (typeof shareItem === "object" ? shareItem.version || inferVersionFromRequiredVersion(shareItem.requiredVersion) : void 0) || searchPackageVersion(key);
|
|
272
259
|
if (typeof shareItem === "string") return {
|
|
273
260
|
name: shareItem,
|
|
274
261
|
version,
|
|
@@ -283,12 +270,12 @@ function normalizeShareItem(key, shareItem) {
|
|
|
283
270
|
return {
|
|
284
271
|
name: key,
|
|
285
272
|
from: "",
|
|
286
|
-
version
|
|
273
|
+
version,
|
|
287
274
|
scope: shareItem.shareScope || "default",
|
|
288
275
|
shareConfig: {
|
|
289
276
|
import: shareItem.import,
|
|
290
277
|
singleton: shareItem.singleton || false,
|
|
291
|
-
requiredVersion: shareItem.requiredVersion || (isImportFalse ? "*" : version ? `^${version}` : "*"),
|
|
278
|
+
requiredVersion: shareItem.requiredVersion || (isImportFalse || shareItem.version ? "*" : version ? `^${version}` : "*"),
|
|
292
279
|
strictVersion: !!shareItem.strictVersion
|
|
293
280
|
}
|
|
294
281
|
};
|
|
@@ -316,11 +303,13 @@ function normalizeShared(shared) {
|
|
|
316
303
|
const result = {};
|
|
317
304
|
const sourceEntries = [];
|
|
318
305
|
if (Array.isArray(shared)) shared.forEach((key) => {
|
|
306
|
+
if (isModuleFederationRuntimePackage(key)) return;
|
|
319
307
|
result[key] = normalizeShareItem(key, key);
|
|
320
308
|
explicitSharedKeys.add(key);
|
|
321
309
|
sourceEntries.push([key, key]);
|
|
322
310
|
});
|
|
323
311
|
else if (typeof shared === "object") Object.keys(shared).forEach((key) => {
|
|
312
|
+
if (isModuleFederationRuntimePackage(key)) return;
|
|
324
313
|
const value = shared[key];
|
|
325
314
|
result[key] = normalizeShareItem(key, value);
|
|
326
315
|
explicitSharedKeys.add(key);
|
|
@@ -334,11 +323,14 @@ function normalizeShared(shared) {
|
|
|
334
323
|
});
|
|
335
324
|
return result;
|
|
336
325
|
}
|
|
326
|
+
function isModuleFederationRuntimePackage(key) {
|
|
327
|
+
return key === "@module-federation/runtime" || key === "@module-federation/runtime-core";
|
|
328
|
+
}
|
|
337
329
|
function shouldAutoShareDependency(key) {
|
|
338
330
|
const pkg = getInstalledPackageJson(key)?.packageJson;
|
|
339
331
|
if (!pkg) return false;
|
|
340
332
|
if (!pkg.browser && !pkg.exports && typeof pkg.module !== "string") return false;
|
|
341
|
-
if (key === "@module-federation/vite") return false;
|
|
333
|
+
if (key === "@module-federation/vite" || isModuleFederationRuntimePackage(key)) return false;
|
|
342
334
|
const entry = [
|
|
343
335
|
pkg.module,
|
|
344
336
|
pkg.main,
|
|
@@ -977,7 +969,7 @@ function getLoadShareModulePath(pkg, isRolldown) {
|
|
|
977
969
|
if (!loadShareCacheMap[pkg]) getLoadShareImportId(pkg, isRolldown);
|
|
978
970
|
return loadShareCacheMap[pkg].getImportId();
|
|
979
971
|
}
|
|
980
|
-
function generateDeferredHostProvidedExports(namedExports, pkg) {
|
|
972
|
+
function generateDeferredHostProvidedExports(namedExports, pkg, cacheKey) {
|
|
981
973
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
982
974
|
const declarations = ["let __mf_default;", ...namedExportVars.map((name) => `let ${name};`)].join("\n ");
|
|
983
975
|
const assignments = [...namedExports.map((name, i) => `${namedExportVars[i]} = exportModule[${escapeGeneratedStringLiteral(name)}];`), "__mf_default = exportModule.default ?? exportModule;"].join("\n ");
|
|
@@ -986,10 +978,10 @@ function generateDeferredHostProvidedExports(namedExports, pkg) {
|
|
|
986
978
|
const __mfApplyHostProvidedExports = (exportModule) => {
|
|
987
979
|
${assignments}
|
|
988
980
|
};
|
|
989
|
-
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
981
|
+
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}];
|
|
990
982
|
if (exportModule === undefined) {
|
|
991
983
|
initPromise.then(() => {
|
|
992
|
-
exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
984
|
+
exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}];
|
|
993
985
|
if (exportModule === undefined) {
|
|
994
986
|
throw new Error("[Module Federation] Shared module ${pkg} was imported before federation bootstrap finished.");
|
|
995
987
|
}
|
|
@@ -1020,13 +1012,14 @@ const normalizeLocalShareModuleCode = `const __mfNormalizeShareModule = (mod) =>
|
|
|
1020
1012
|
function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
1021
1013
|
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = new VirtualModule(pkg, LOAD_SHARE_TAG, ".mjs");
|
|
1022
1014
|
const importLine = getRuntimeModuleCacheBootstrapCode();
|
|
1015
|
+
const cacheKey = getSharedCacheKey(pkg, shareItem);
|
|
1023
1016
|
if (shareItem.shareConfig.import === false) {
|
|
1024
1017
|
const namedExports = getPackageNamedExports(pkg);
|
|
1025
1018
|
let exportLine;
|
|
1026
|
-
if (namedExports.length > 0) exportLine = generateDeferredHostProvidedExports(namedExports, pkg);
|
|
1019
|
+
if (namedExports.length > 0) exportLine = generateDeferredHostProvidedExports(namedExports, pkg, cacheKey);
|
|
1027
1020
|
else {
|
|
1028
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.`);
|
|
1029
|
-
exportLine = generateDeferredHostProvidedExports([], pkg);
|
|
1022
|
+
exportLine = generateDeferredHostProvidedExports([], pkg, cacheKey);
|
|
1030
1023
|
}
|
|
1031
1024
|
loadShareCacheMap[pkg].writeSync(`
|
|
1032
1025
|
${getRuntimeInitPromiseBootstrapCode()}
|
|
@@ -1067,11 +1060,11 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
1067
1060
|
${devDynamicImportLine}
|
|
1068
1061
|
${importLine}
|
|
1069
1062
|
${normalizeLocalShareModuleCode}
|
|
1070
|
-
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1063
|
+
let exportModule = __mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}]
|
|
1071
1064
|
if (exportModule === undefined) {
|
|
1072
1065
|
${usesLazyLocalFallback ? `exportModule = __mfNormalizeShareModule(await import(${escapeGeneratedStringLiteral(lazyLocalFallbackSource)}));
|
|
1073
|
-
__mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1074
|
-
__mfModuleCache.share[${escapeGeneratedStringLiteral(
|
|
1066
|
+
__mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}] = exportModule;` : `exportModule = __mfNormalizeShareModule(__mfLocalShare);
|
|
1067
|
+
__mfModuleCache.share[${escapeGeneratedStringLiteral(cacheKey)}] = exportModule;`}
|
|
1075
1068
|
}
|
|
1076
1069
|
${exportLine}
|
|
1077
1070
|
`, true);
|
|
@@ -1225,8 +1218,9 @@ function getShareItemForPreload(pkg) {
|
|
|
1225
1218
|
if (isExplicitSharedKey(pkg)) return shared[pkg];
|
|
1226
1219
|
if (isExplicitSharedKey(wildcardKey)) return shared[wildcardKey];
|
|
1227
1220
|
}
|
|
1228
|
-
function generateSharedCacheSeedItem(pkg, importPath) {
|
|
1229
|
-
|
|
1221
|
+
function generateSharedCacheSeedItem(pkg, shareItem, importPath) {
|
|
1222
|
+
const cacheKey = getSharedCacheKey(pkg, shareItem);
|
|
1223
|
+
return `if (__mfModuleCache.share[${JSON.stringify(cacheKey)}] === undefined) {
|
|
1230
1224
|
const mod = await import(${JSON.stringify(importPath)});
|
|
1231
1225
|
${normalizeRuntimeShareCode}
|
|
1232
1226
|
const normalizedModule = __mfNormalizeRuntimeShare(mod);
|
|
@@ -1235,7 +1229,7 @@ function generateSharedCacheSeedItem(pkg, importPath) {
|
|
|
1235
1229
|
value: true,
|
|
1236
1230
|
enumerable: false
|
|
1237
1231
|
});
|
|
1238
|
-
__mfModuleCache.share[${JSON.stringify(
|
|
1232
|
+
__mfModuleCache.share[${JSON.stringify(cacheKey)}] = exportModule;
|
|
1239
1233
|
}`;
|
|
1240
1234
|
}
|
|
1241
1235
|
const normalizeRuntimeShareCode = `const __mfNormalizeRuntimeShare = (mod) => {
|
|
@@ -1253,7 +1247,7 @@ function generateDirectSharedCacheSeedCode(command = "build") {
|
|
|
1253
1247
|
return getOrderedUsedShares().map((pkg) => {
|
|
1254
1248
|
const shareItem = getShareItemForPreload(pkg);
|
|
1255
1249
|
if (!shareItem || shareItem.shareConfig.import === false) return null;
|
|
1256
|
-
return generateSharedCacheSeedItem(pkg, command === "serve" ? getLocalSharedPackagePath(pkg, shareItem) : getDirectSharedCacheSeedImportPath(pkg, shareItem));
|
|
1250
|
+
return generateSharedCacheSeedItem(pkg, shareItem, command === "serve" ? getLocalSharedPackagePath(pkg, shareItem) : getDirectSharedCacheSeedImportPath(pkg, shareItem));
|
|
1257
1251
|
}).filter((item) => item !== null).join("\n");
|
|
1258
1252
|
}
|
|
1259
1253
|
function getBrowserImportPath(importPath) {
|
|
@@ -1275,7 +1269,7 @@ function generateHostAutoInitSharedCacheSeedCode(command = "build") {
|
|
|
1275
1269
|
if (command === "build") return "";
|
|
1276
1270
|
return getHostAutoInitSharedSeedItems().map(({ pkg, shareItem }) => {
|
|
1277
1271
|
if (!shareItem) return null;
|
|
1278
|
-
return generateSharedCacheSeedItem(pkg, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
1272
|
+
return generateSharedCacheSeedItem(pkg, shareItem, getBrowserImportPath(getDirectSharedCacheSeedImportPath(pkg, shareItem)));
|
|
1279
1273
|
}).filter((item) => item !== null).join("\n");
|
|
1280
1274
|
}
|
|
1281
1275
|
const REMOTE_ENTRY_ID = "virtual:mf-REMOTE_ENTRY_ID";
|
|
@@ -1391,7 +1385,8 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1391
1385
|
console.error('[Module Federation]', e)
|
|
1392
1386
|
}
|
|
1393
1387
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1394
|
-
|
|
1388
|
+
const cacheKey = share.shareConfig?.singleton || !share.version ? pkg : \`\${pkg}@\${share.version}\`;
|
|
1389
|
+
if (share.shareConfig?.import !== false || __mfModuleCache.share[cacheKey] !== undefined) continue;
|
|
1395
1390
|
${normalizeRuntimeShareCode}
|
|
1396
1391
|
const versions = shared?.[pkg];
|
|
1397
1392
|
const provider = versions && versions[Object.keys(versions)[0]];
|
|
@@ -1399,7 +1394,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1399
1394
|
const factory = provider.lib || (provider.loading ? await provider.loading : await provider.get?.());
|
|
1400
1395
|
const mod = typeof factory === "function" ? factory() : factory;
|
|
1401
1396
|
const resolved = await Promise.resolve(mod);
|
|
1402
|
-
__mfModuleCache.share[
|
|
1397
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1403
1398
|
}
|
|
1404
1399
|
return initRes
|
|
1405
1400
|
}
|
|
@@ -1433,7 +1428,8 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1433
1428
|
${normalizeRuntimeShareCode}
|
|
1434
1429
|
${shouldPreloadShares ? `
|
|
1435
1430
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1436
|
-
|
|
1431
|
+
const cacheKey = share.shareConfig?.singleton || !share.version ? pkg : \`\${pkg}@\${share.version}\`;
|
|
1432
|
+
if (__mfModuleCache.share[cacheKey] !== undefined) {
|
|
1437
1433
|
continue;
|
|
1438
1434
|
}
|
|
1439
1435
|
await runtime.loadShare(pkg, {
|
|
@@ -1441,7 +1437,7 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1441
1437
|
}).then((factory) => {
|
|
1442
1438
|
const mod = typeof factory === "function" ? factory() : factory;
|
|
1443
1439
|
return Promise.resolve(mod).then((resolved) => {
|
|
1444
|
-
__mfModuleCache.share[
|
|
1440
|
+
__mfModuleCache.share[cacheKey] = __mfNormalizeRuntimeShare(resolved);
|
|
1445
1441
|
});
|
|
1446
1442
|
});
|
|
1447
1443
|
}
|
|
@@ -1982,11 +1978,13 @@ const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClien
|
|
|
1982
1978
|
let rewritten = false;
|
|
1983
1979
|
htmlContent = htmlContent.replace(scriptRegex, (scriptTag, entrySrc) => {
|
|
1984
1980
|
rewritten = true;
|
|
1985
|
-
const
|
|
1981
|
+
const bootstrapSource = getSystemBootstrapSource(initPath, entrySrc);
|
|
1982
|
+
const bootstrapHash = createHash("sha256").update(bootstrapSource).digest("hex").slice(0, 8);
|
|
1983
|
+
const bootstrapFileName = `mf-entry-bootstrap-${bootstrapIndex++}-${bootstrapHash}.js`;
|
|
1986
1984
|
const bootstrapRef = this.emitFile({
|
|
1987
1985
|
type: "asset",
|
|
1988
1986
|
fileName: bootstrapFileName,
|
|
1989
|
-
source:
|
|
1987
|
+
source: bootstrapSource
|
|
1990
1988
|
});
|
|
1991
1989
|
const bootstrapPath = viteConfig.base + this.getFileName(bootstrapRef);
|
|
1992
1990
|
return scriptTag.replace(entrySrc, bootstrapPath);
|
|
@@ -2934,7 +2932,7 @@ function getCommonSharedSubpathFromNodeModulePath(source, sharedKey) {
|
|
|
2934
2932
|
*/
|
|
2935
2933
|
function resolvePublicPath(options, viteBase, originalBase) {
|
|
2936
2934
|
if (options.publicPath && options.publicPath !== "auto") return options.publicPath;
|
|
2937
|
-
if (originalBase
|
|
2935
|
+
if (!originalBase) return "auto";
|
|
2938
2936
|
if (viteBase) return ensureTrailingSlash(viteBase);
|
|
2939
2937
|
return "auto";
|
|
2940
2938
|
}
|
|
@@ -3436,7 +3434,8 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
3436
3434
|
if (id.includes(getHostAutoInitPath())) {
|
|
3437
3435
|
if (_command === "serve") {
|
|
3438
3436
|
const host = typeof viteConfig.server?.host === "string" && viteConfig.server.host !== "0.0.0.0" ? viteConfig.server.host : "localhost";
|
|
3439
|
-
const
|
|
3437
|
+
const resolvedPublicPath = resolvePublicPath(options, viteConfig.base);
|
|
3438
|
+
const publicPath = JSON.stringify((resolvedPublicPath === "auto" ? "/" : resolvedPublicPath) + options.filename);
|
|
3440
3439
|
const fallbackOrigin = `//${host}:${viteConfig.server?.port}`;
|
|
3441
3440
|
const ssrRemoteEntry = "data:text/javascript," + encodeURIComponent("export async function init(){return {loadRemote:async()=>({}),loadShare:async()=>({})}}");
|
|
3442
3441
|
return `
|
|
@@ -3600,6 +3599,7 @@ function isBuildConfigImporter(importer) {
|
|
|
3600
3599
|
}
|
|
3601
3600
|
function matchesSharedSource(source, key) {
|
|
3602
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;
|
|
3603
3603
|
if (key.endsWith("/")) return source === keyBase || source.startsWith(`${keyBase}/`);
|
|
3604
3604
|
if (getCommonSharedSubpaths(keyBase).includes(source)) return true;
|
|
3605
3605
|
return source === keyBase;
|
|
@@ -3714,6 +3714,12 @@ function proxySharedModule(options) {
|
|
|
3714
3714
|
name: "proxyPreBuildShared:resolve-shared-loadShare",
|
|
3715
3715
|
enforce: "pre",
|
|
3716
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
|
+
}
|
|
3717
3723
|
const key = findSharedKeyForSource(source, shared);
|
|
3718
3724
|
if (!key) return;
|
|
3719
3725
|
if (useDirectReactImport && key === "react") return;
|
|
@@ -3722,9 +3728,9 @@ function proxySharedModule(options) {
|
|
|
3722
3728
|
if (useDirectReactImport && source === "react") return;
|
|
3723
3729
|
if (importer && importer.includes("localSharedImportMap")) return;
|
|
3724
3730
|
if (importer && (importer.includes("hostAutoInit") || importer.includes("__H_A_I__"))) return;
|
|
3725
|
-
if (
|
|
3726
|
-
if (
|
|
3727
|
-
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;
|
|
3728
3734
|
const loadSharePath = getLoadShareModulePath(shareSource, useRolldown);
|
|
3729
3735
|
writeLoadShareModule(shareSource, shared[key], _command, useRolldown);
|
|
3730
3736
|
if (shared[key].shareConfig.import !== false) writePreBuildLibPath(shareSource, shared[key]);
|
|
@@ -3811,19 +3817,8 @@ function applyRewrites(code, imports, id) {
|
|
|
3811
3817
|
const ms = new CodeRewriter(code);
|
|
3812
3818
|
let changed = false;
|
|
3813
3819
|
let counter = 0;
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
const src = JSON.stringify(imp.source);
|
|
3817
|
-
if (imp.namespaceLocal && !imp.defaultLocal && imp.named.length === 0) ms.overwrite(imp.start, imp.end, `import { __moduleExports as ${imp.namespaceLocal} } from ${src};`);
|
|
3818
|
-
else {
|
|
3819
|
-
const nsId = `__mf_ns_${counter++}`;
|
|
3820
|
-
const importParts = [];
|
|
3821
|
-
if (imp.defaultLocal) importParts.push(`default as ${imp.defaultLocal}`);
|
|
3822
|
-
importParts.push(`__moduleExports as ${nsId}`);
|
|
3823
|
-
let rewrite = `import { ${importParts.join(", ")} } from ${src};`;
|
|
3824
|
-
if (imp.named.length > 0) {
|
|
3825
|
-
const isProxyId = `__mf_is_proxy_${counter++}`;
|
|
3826
|
-
const namedProxyHelper = `function __mfCreateNamedRemoteProxy(ns, key) {
|
|
3820
|
+
let namedProxyHelperDeclared = false;
|
|
3821
|
+
const namedProxyHelper = `function __mfCreateNamedRemoteProxy(ns, key) {
|
|
3827
3822
|
const target = function (...args) {
|
|
3828
3823
|
const value = ns[key];
|
|
3829
3824
|
return typeof value === "function" ? value.apply(this, args) : value;
|
|
@@ -3841,13 +3836,29 @@ function applyRewrites(code, imports, id) {
|
|
|
3841
3836
|
}
|
|
3842
3837
|
});
|
|
3843
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++}`;
|
|
3844
3851
|
const tempNames = imp.named.map((_s) => `__mf_named_${counter++}`);
|
|
3845
3852
|
const destructParts = imp.named.map((s, index) => `${s.imported}: ${tempNames[index]}`);
|
|
3846
3853
|
const bindingLines = imp.named.map((s, index) => {
|
|
3847
3854
|
const temp = tempNames[index];
|
|
3848
3855
|
return `const ${s.local} = ${isProxyId} ? __mfCreateNamedRemoteProxy(${nsId}, ${JSON.stringify(s.imported)}) : ${temp};`;
|
|
3849
3856
|
});
|
|
3850
|
-
|
|
3857
|
+
if (!namedProxyHelperDeclared) {
|
|
3858
|
+
rewrite += `\n${namedProxyHelper}`;
|
|
3859
|
+
namedProxyHelperDeclared = true;
|
|
3860
|
+
}
|
|
3861
|
+
rewrite += `\nconst ${isProxyId} = ${nsId} && ${nsId}.__mf_is_remote_proxy;`;
|
|
3851
3862
|
rewrite += `\nconst { ${destructParts.join(", ")} } = ${isProxyId} ? {} : ${nsId};`;
|
|
3852
3863
|
rewrite += `\n${bindingLines.join("\n")}`;
|
|
3853
3864
|
}
|
|
@@ -4749,7 +4760,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
4749
4760
|
const SSR_ONLY_PLUGINS = new Set(["@module-federation/vite/ssrEntryLoader"]);
|
|
4750
4761
|
function loadPluginDts(options) {
|
|
4751
4762
|
if (options.dts === false) return [];
|
|
4752
|
-
return [import("./pluginDts-
|
|
4763
|
+
return [import("./pluginDts-DDx4TPN8.mjs").then(({ default: pluginDts }) => pluginDts(options))];
|
|
4753
4764
|
}
|
|
4754
4765
|
function federation(mfUserOptions) {
|
|
4755
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";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/vite",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.2",
|
|
4
4
|
"description": "Vite plugin for Module Federation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.cjs",
|
|
@@ -96,4 +96,4 @@
|
|
|
96
96
|
"vite": "^8.0.10",
|
|
97
97
|
"vitest": "^4.0.18"
|
|
98
98
|
}
|
|
99
|
-
}
|
|
99
|
+
}
|