@module-federation/vite 1.16.13 → 1.16.15
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.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _ as createModuleFederationError, a as getIsRolldown, b as rebaseImport, c as getPackageNameFromNodeModulePath, d as isNuxtProjectRoot, f as packageNameDecode, g as sharedCacheHelperCode, h as setPackageDetectionCwd, i as getInstalledPackageJson, l as getSharedCacheDescriptor, m as resolveImportPath, o as getPackageDetectionCwd, p as packageNameEncode, r as getInstalledPackageEntry, s as getPackageName, u as hasPackageDependency, v as mfWarn, y as normalizePathForImport } from "./pluginDts-
|
|
1
|
+
import { _ as createModuleFederationError, a as getIsRolldown, b as rebaseImport, c as getPackageNameFromNodeModulePath, d as isNuxtProjectRoot, f as packageNameDecode, g as sharedCacheHelperCode, h as setPackageDetectionCwd, i as getInstalledPackageJson, l as getSharedCacheDescriptor, m as resolveImportPath, o as getPackageDetectionCwd, p as packageNameEncode, r as getInstalledPackageEntry, s as getPackageName, u as hasPackageDependency, v as mfWarn, y as normalizePathForImport } from "./pluginDts-Bo95ELmX.js";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import * as fs$2 from "fs";
|
|
4
4
|
import { existsSync, readFileSync, realpathSync, statSync, writeFileSync } from "fs";
|
|
@@ -192,6 +192,51 @@ const COMMON_SHARED_SUBPATHS = {
|
|
|
192
192
|
],
|
|
193
193
|
zustand: ["zustand/vanilla", "zustand/react"]
|
|
194
194
|
};
|
|
195
|
+
const VITE_DEFAULT_ASSET_TYPES = [
|
|
196
|
+
"apng",
|
|
197
|
+
"bmp",
|
|
198
|
+
"png",
|
|
199
|
+
"jpe?g",
|
|
200
|
+
"jfif",
|
|
201
|
+
"pjpeg",
|
|
202
|
+
"pjp",
|
|
203
|
+
"gif",
|
|
204
|
+
"svg",
|
|
205
|
+
"ico",
|
|
206
|
+
"webp",
|
|
207
|
+
"avif",
|
|
208
|
+
"cur",
|
|
209
|
+
"jxl",
|
|
210
|
+
"mp4",
|
|
211
|
+
"webm",
|
|
212
|
+
"ogg",
|
|
213
|
+
"mp3",
|
|
214
|
+
"wav",
|
|
215
|
+
"flac",
|
|
216
|
+
"aac",
|
|
217
|
+
"opus",
|
|
218
|
+
"mov",
|
|
219
|
+
"m4a",
|
|
220
|
+
"vtt",
|
|
221
|
+
"woff2?",
|
|
222
|
+
"eot",
|
|
223
|
+
"ttf",
|
|
224
|
+
"otf",
|
|
225
|
+
"webmanifest",
|
|
226
|
+
"pdf",
|
|
227
|
+
"txt"
|
|
228
|
+
];
|
|
229
|
+
const ASSET_LIKE_IMPORT_RE = new RegExp(`\\.(${[...[
|
|
230
|
+
"css",
|
|
231
|
+
"scss",
|
|
232
|
+
"sass",
|
|
233
|
+
"less",
|
|
234
|
+
"styl",
|
|
235
|
+
"stylus"
|
|
236
|
+
], ...VITE_DEFAULT_ASSET_TYPES].join("|")})(?:[?#].*)?$`, "i");
|
|
237
|
+
function isAssetLikeImport(source) {
|
|
238
|
+
return ASSET_LIKE_IMPORT_RE.test(source);
|
|
239
|
+
}
|
|
195
240
|
function removeTrailingSlash(value) {
|
|
196
241
|
return value.endsWith("/") ? value.slice(0, -1) : value;
|
|
197
242
|
}
|
|
@@ -233,7 +278,10 @@ function getCommonSharedSubpathFromNodeModulePath(source, sharedKey) {
|
|
|
233
278
|
function resolvePublicPath(options, viteBase, originalBase) {
|
|
234
279
|
if (options.publicPath && options.publicPath !== "auto") return options.publicPath;
|
|
235
280
|
if (!originalBase) return "auto";
|
|
236
|
-
if (viteBase)
|
|
281
|
+
if (viteBase) {
|
|
282
|
+
if (viteBase === "./") return "auto";
|
|
283
|
+
return ensureTrailingSlash(viteBase);
|
|
284
|
+
}
|
|
237
285
|
return "auto";
|
|
238
286
|
}
|
|
239
287
|
//#endregion
|
|
@@ -380,7 +428,9 @@ function normalizeShared(shared) {
|
|
|
380
428
|
if (Array.isArray(shared)) shared.forEach((key) => {
|
|
381
429
|
if (isModuleFederationRuntimePackage(key)) return;
|
|
382
430
|
const normalizedKey = normalizeSharedKey(key);
|
|
431
|
+
const hadConfiguredPackageSubpath = (result[normalizedKey]?.shareConfig)?.__mfConfiguredPackageSubpath === true;
|
|
383
432
|
result[normalizedKey] = normalizeShareItem(normalizedKey, normalizedKey);
|
|
433
|
+
if (key.endsWith("/") || hadConfiguredPackageSubpath) result[normalizedKey].shareConfig.__mfConfiguredPackageSubpath = true;
|
|
384
434
|
explicitSharedKeys.add(normalizedKey);
|
|
385
435
|
sourceEntries.push([normalizedKey, normalizedKey]);
|
|
386
436
|
});
|
|
@@ -388,7 +438,9 @@ function normalizeShared(shared) {
|
|
|
388
438
|
if (isModuleFederationRuntimePackage(key)) return;
|
|
389
439
|
const normalizedKey = normalizeSharedKey(key);
|
|
390
440
|
const value = shared[key];
|
|
441
|
+
const hadConfiguredPackageSubpath = (result[normalizedKey]?.shareConfig)?.__mfConfiguredPackageSubpath === true;
|
|
391
442
|
result[normalizedKey] = normalizeShareItem(normalizedKey, value);
|
|
443
|
+
if (key.endsWith("/") || hadConfiguredPackageSubpath) result[normalizedKey].shareConfig.__mfConfiguredPackageSubpath = true;
|
|
392
444
|
explicitSharedKeys.add(normalizedKey);
|
|
393
445
|
sourceEntries.push([normalizedKey, value]);
|
|
394
446
|
});
|
|
@@ -997,6 +1049,25 @@ function resolveRelativeModule(filePath, specifier) {
|
|
|
997
1049
|
if (existsSync(candidate)) return candidate;
|
|
998
1050
|
}
|
|
999
1051
|
}
|
|
1052
|
+
function resolveReExportModule(filePath, specifier) {
|
|
1053
|
+
if (specifier.startsWith(".")) return resolveRelativeModule(filePath, specifier);
|
|
1054
|
+
const esmEntry = getInstalledPackageEntry(specifier, {
|
|
1055
|
+
cwd: path$1.dirname(filePath),
|
|
1056
|
+
conditions: [
|
|
1057
|
+
"browser",
|
|
1058
|
+
"import",
|
|
1059
|
+
"module",
|
|
1060
|
+
"default"
|
|
1061
|
+
],
|
|
1062
|
+
resolveSubpathWithRequire: false
|
|
1063
|
+
});
|
|
1064
|
+
if (esmEntry) return esmEntry;
|
|
1065
|
+
try {
|
|
1066
|
+
return resolveFileLikeModule(createRequire$1(pathToFileURL(filePath)).resolve(specifier));
|
|
1067
|
+
} catch {
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1000
1071
|
function getNamedExportsViaRegex(source, filePath, visited) {
|
|
1001
1072
|
const names = /* @__PURE__ */ new Set();
|
|
1002
1073
|
visited = visited || /* @__PURE__ */ new Set();
|
|
@@ -1040,8 +1111,7 @@ function getNamedExportsViaRegex(source, filePath, visited) {
|
|
|
1040
1111
|
const starExportRegex = /export\s+\*\s+from\s+['"]([^'"]+)['"]/g;
|
|
1041
1112
|
while ((match = starExportRegex.exec(source)) !== null) {
|
|
1042
1113
|
const specifier = match[1];
|
|
1043
|
-
|
|
1044
|
-
const resolvedPath = resolveRelativeModule(filePath, specifier);
|
|
1114
|
+
const resolvedPath = resolveReExportModule(filePath, specifier);
|
|
1045
1115
|
if (!resolvedPath || visited.has(resolvedPath)) continue;
|
|
1046
1116
|
try {
|
|
1047
1117
|
const reExportNames = getNamedExportsViaRegex(readFileSync(resolvedPath, "utf-8"), resolvedPath, visited);
|
|
@@ -1142,6 +1212,15 @@ function getWorkspacePackageJson(pkg) {
|
|
|
1142
1212
|
fromResolvedEntry: resolved
|
|
1143
1213
|
})?.packageJson;
|
|
1144
1214
|
}
|
|
1215
|
+
function getSharedDependencyGraphPackageJson(pkg) {
|
|
1216
|
+
const installedPackageJson = getInstalledPackageJson(pkg, { packageName: getPackageName(pkg) })?.packageJson;
|
|
1217
|
+
if (installedPackageJson) return installedPackageJson;
|
|
1218
|
+
try {
|
|
1219
|
+
const packageJsonPath = createRequire$1(pathToFileURL(path$1.join(getPackageDetectionCwd(), "package.json"))).resolve(`${getPackageName(pkg)}/package.json`);
|
|
1220
|
+
return JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
1221
|
+
} catch {}
|
|
1222
|
+
return getWorkspacePackageJson(pkg);
|
|
1223
|
+
}
|
|
1145
1224
|
function getDependencyNames(packageJson) {
|
|
1146
1225
|
if (!packageJson) return [];
|
|
1147
1226
|
const names = /* @__PURE__ */ new Set();
|
|
@@ -1156,7 +1235,7 @@ function getDependencyNames(packageJson) {
|
|
|
1156
1235
|
}
|
|
1157
1236
|
return Array.from(names);
|
|
1158
1237
|
}
|
|
1159
|
-
function
|
|
1238
|
+
function isSharedSingletonConsumedByPeer(pkg) {
|
|
1160
1239
|
const shared = getNormalizeModuleFederationOptions()?.shared || {};
|
|
1161
1240
|
const sharedKeyByPackageName = /* @__PURE__ */ new Map();
|
|
1162
1241
|
Object.entries(shared).filter(([, item]) => item.shareConfig.singleton === true).forEach(([key]) => {
|
|
@@ -1164,7 +1243,7 @@ function isWorkspaceSingletonConsumedByPeer(pkg) {
|
|
|
1164
1243
|
if (!sharedKeyByPackageName.get(packageName) || key === packageName) sharedKeyByPackageName.set(packageName, key);
|
|
1165
1244
|
});
|
|
1166
1245
|
const reachesPkg = (current, seen) => {
|
|
1167
|
-
const packageJson =
|
|
1246
|
+
const packageJson = getSharedDependencyGraphPackageJson(current);
|
|
1168
1247
|
for (const dependency of getDependencyNames(packageJson)) {
|
|
1169
1248
|
const sharedDependency = sharedKeyByPackageName.get(dependency);
|
|
1170
1249
|
if (!sharedDependency) continue;
|
|
@@ -1177,6 +1256,10 @@ function isWorkspaceSingletonConsumedByPeer(pkg) {
|
|
|
1177
1256
|
};
|
|
1178
1257
|
return Array.from(sharedKeyByPackageName.values()).some((sharedPkg) => sharedPkg !== pkg && reachesPkg(sharedPkg, new Set([sharedPkg])));
|
|
1179
1258
|
}
|
|
1259
|
+
function isRemoteOnlyContainer() {
|
|
1260
|
+
const options = getNormalizeModuleFederationOptions();
|
|
1261
|
+
return Object.keys(options.exposes || {}).length > 0 && Object.keys(options.remotes || {}).length === 0;
|
|
1262
|
+
}
|
|
1180
1263
|
function tryResolveImportFromPackageRoot(pkg, root) {
|
|
1181
1264
|
try {
|
|
1182
1265
|
return resolveWorkspaceEsmEntry(pkg, createRequire$1(pathToFileURL(path$1.join(root, "package.json"))).resolve(pkg), root);
|
|
@@ -1330,44 +1413,46 @@ function generateEagerWorkspaceSingletonExports(namedExports, importSource, cach
|
|
|
1330
1413
|
const __mf_default = exportModule.default ?? exportModule;${namedExportAssignments}
|
|
1331
1414
|
export { __mf_default as default };${namedExportLine}`;
|
|
1332
1415
|
}
|
|
1333
|
-
function generateLazyWorkspaceSingletonExports(namedExports, importSource, cacheDescriptor
|
|
1416
|
+
function generateLazyWorkspaceSingletonExports(namedExports, importSource, cacheDescriptor) {
|
|
1334
1417
|
const namedExportVars = namedExports.map((_name, i) => `__mf_${i}`);
|
|
1335
1418
|
const declarations = namedExports.length > 0 ? ["let __mf_default;", ...namedExportVars.map((name) => `let ${name};`)].join("\n ") : "let __mf_default;";
|
|
1336
1419
|
const assignments = namedExports.length > 0 ? [...namedExports.map((name, i) => `${namedExportVars[i]} = mod[${escapeGeneratedStringLiteral(name)}];`), "__mf_default = mod.default ?? mod;"].join("\n ") : "__mf_default = mod.default ?? mod;";
|
|
1337
1420
|
const namedExportLine = namedExports.length > 0 ? `\n export { ${namedExports.map((name, i) => `${namedExportVars[i]} as ${name}`).join(", ")} };` : "";
|
|
1338
|
-
|
|
1339
|
-
__mfWriteSharedCache(__mfModuleCache.share, ${cacheDescriptor}, exportModule);
|
|
1340
|
-
__mfApplyLazyShareExports(exportModule);`;
|
|
1341
|
-
const body = `${declarations}
|
|
1421
|
+
return `${declarations}
|
|
1342
1422
|
const __mfApplyLazyShareExports = (mod) => {
|
|
1343
1423
|
${assignments}
|
|
1344
1424
|
};
|
|
1345
1425
|
let exportModule = __mfReadSharedCache(__mfModuleCache.share, ${cacheDescriptor});
|
|
1346
1426
|
if (exportModule === undefined) {
|
|
1347
|
-
|
|
1348
|
-
${
|
|
1427
|
+
if (import.meta.env.SSR) {
|
|
1428
|
+
${`exportModule = __mfNormalizeShareModule(__mfLocalShare);
|
|
1429
|
+
__mfWriteSharedCache(__mfModuleCache.share, ${cacheDescriptor}, exportModule);
|
|
1430
|
+
__mfApplyLazyShareExports(exportModule);`}
|
|
1349
1431
|
} else {
|
|
1350
|
-
(__mfModuleCache.pendingShareLoads ||= []).push(initPromise.then(() =>
|
|
1351
|
-
|
|
1432
|
+
(__mfModuleCache.pendingShareLoads ||= []).push(initPromise.then(() => {
|
|
1433
|
+
exportModule = __mfReadSharedCache(__mfModuleCache.share, ${cacheDescriptor});
|
|
1434
|
+
if (exportModule !== undefined) {
|
|
1435
|
+
__mfApplyLazyShareExports(exportModule);
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
return import(${escapeGeneratedStringLiteral(importSource)}).then((mod) => {
|
|
1352
1439
|
exportModule = __mfNormalizeShareModule(mod);
|
|
1353
1440
|
__mfWriteSharedCache(__mfModuleCache.share, ${cacheDescriptor}, exportModule);
|
|
1354
1441
|
__mfApplyLazyShareExports(exportModule);
|
|
1355
|
-
})
|
|
1356
|
-
));
|
|
1357
|
-
}
|
|
1442
|
+
});
|
|
1443
|
+
}));
|
|
1444
|
+
}
|
|
1358
1445
|
} else {
|
|
1359
1446
|
__mfApplyLazyShareExports(exportModule);
|
|
1360
1447
|
}
|
|
1361
1448
|
export { __mf_default as default };${namedExportLine}`;
|
|
1362
|
-
return eagerLocalFallback ? `import * as __mfLocalShare from ${escapeGeneratedStringLiteral(importSource)};
|
|
1363
|
-
${body}` : body;
|
|
1364
1449
|
}
|
|
1365
1450
|
const WORKSPACE_SINGLETON_SSR_LOCAL_SHARE = "__mfNormalizeShareModule(__mfLocalShare)";
|
|
1366
1451
|
function prependWorkspaceSingletonSsrImport(code) {
|
|
1367
1452
|
if (!code.includes("if (import.meta.env.SSR)")) return code;
|
|
1368
1453
|
if (!code.includes(WORKSPACE_SINGLETON_SSR_LOCAL_SHARE)) return code;
|
|
1369
1454
|
if (code.includes("import * as __mfLocalShare")) return code;
|
|
1370
|
-
const importMatch = code.match(/initPromise\.then\(\(\)\s*=>\s*\n\s*import\((["'])(.+?)\1\)\.then\(\(mod\)\s*=>\s*\{[\s\S]*?__mfApplyLazyShareExports/);
|
|
1455
|
+
const importMatch = code.match(/initPromise\.then\(\(\)\s*=>\s*\{[\s\S]*?\breturn import\((["'])(.+?)\1\)\.then\(\(mod\)\s*=>\s*\{[\s\S]*?__mfApplyLazyShareExports/) ?? code.match(/initPromise\.then\(\(\)\s*=>\s*\n\s*import\((["'])(.+?)\1\)\.then\(\(mod\)\s*=>\s*\{[\s\S]*?__mfApplyLazyShareExports/);
|
|
1371
1456
|
if (!importMatch) return code;
|
|
1372
1457
|
const quote = importMatch[1];
|
|
1373
1458
|
return `import * as __mfLocalShare from ${quote}${importMatch[2]}${quote};\n${code}`;
|
|
@@ -1434,17 +1519,21 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
1434
1519
|
const devImportSource = concreteSharedImportSource || pkg;
|
|
1435
1520
|
const localProviderPath = getLocalProviderImportPath(pkg);
|
|
1436
1521
|
const isWorkspacePackage = isWorkspacePackageEntry(pkg, localProviderPath) || isWorkspacePackageEntry(pkg, concreteSharedImportSource);
|
|
1437
|
-
const lazyLocalFallbackSource = concreteSharedImportSource || localProviderPath || sharedImportSource;
|
|
1522
|
+
const lazyLocalFallbackSource = command !== "build" ? concreteSharedImportSource || localProviderPath || devImportSource : concreteSharedImportSource || localProviderPath || sharedImportSource;
|
|
1438
1523
|
const skipServePrebuildWarmup = command !== "build" && (pkg === "lit" || pkg.startsWith("lit/"));
|
|
1439
1524
|
const isWorkspaceSingleton = isWorkspacePackage && shareItem.shareConfig.singleton === true;
|
|
1440
|
-
const
|
|
1525
|
+
const isDefaultShareScope = shareItem.scope === void 0 || shareItem.scope === "default" || Array.isArray(shareItem.scope) && shareItem.scope[0] === "default";
|
|
1526
|
+
const usesDeferredSingletonFallback = isWorkspaceSingleton || command !== "build" && isRemoteOnlyContainer() && shareItem.shareConfig.singleton === true || command === "build" && isRemoteOnlyContainer() && shareItem.shareConfig.singleton === true && !isDefaultShareScope;
|
|
1527
|
+
const isConsumedByPeerSingleton = isSharedSingletonConsumedByPeer(pkg);
|
|
1528
|
+
const usesEntryInjectedRemoteFallback = command !== "build" && !isWorkspaceSingleton && isRemoteOnlyContainer() && shareItem.shareConfig.singleton === true && getNormalizeModuleFederationOptions().hostInitInjectLocation === "entry" && isConsumedByPeerSingleton;
|
|
1529
|
+
const usesEagerWorkspaceFallback = isWorkspaceSingleton && isConsumedByPeerSingleton;
|
|
1441
1530
|
const namedExports = getSharedNamedExports(pkg, shareItem);
|
|
1442
1531
|
let exportLine;
|
|
1443
1532
|
let initBlock = "";
|
|
1444
|
-
if (usesEagerWorkspaceFallback) exportLine = generateEagerWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor);
|
|
1445
|
-
else if (
|
|
1533
|
+
if (usesEagerWorkspaceFallback || usesEntryInjectedRemoteFallback) exportLine = generateEagerWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor);
|
|
1534
|
+
else if (usesDeferredSingletonFallback) {
|
|
1446
1535
|
importLine = `${getRuntimeInitPromiseBootstrapCode()}\n ${importLine}`;
|
|
1447
|
-
exportLine = generateLazyWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor
|
|
1536
|
+
exportLine = generateLazyWorkspaceSingletonExports(namedExports, lazyLocalFallbackSource, cacheDescriptor);
|
|
1448
1537
|
} else if (namedExports.length > 0) {
|
|
1449
1538
|
const destructure = `const { ${namedExports.map((name, i) => `${name}: __mf_${i}`).join(", ")} } = exportModule;`;
|
|
1450
1539
|
const namedExportLine = `export { ${namedExports.map((name, i) => `__mf_${i} as ${name}`).join(", ")} };`;
|
|
@@ -1465,9 +1554,9 @@ function writeLoadShareModule(pkg, shareItem, command, _isRolldown) {
|
|
|
1465
1554
|
initBlock = `exportModule = __mfNormalizeShareModule(__mfLocalShare);
|
|
1466
1555
|
__mfWriteSharedCache(__mfModuleCache.share, ${cacheDescriptor}, exportModule);`;
|
|
1467
1556
|
}
|
|
1468
|
-
const prebuildImportLine =
|
|
1469
|
-
const devDynamicImportLine = isWorkspacePackage ? "" : command !== "build" && !skipServePrebuildWarmup ? `;() => import(${escapeGeneratedStringLiteral(devImportSource)}).catch(() => {});` : "";
|
|
1470
|
-
const moduleBody =
|
|
1557
|
+
const prebuildImportLine = usesDeferredSingletonFallback || isWorkspacePackage && command !== "build" ? "" : `import * as __mfLocalShare from ${escapeGeneratedStringLiteral(skipServePrebuildWarmup ? devImportSource : sharedImportSource)};`;
|
|
1558
|
+
const devDynamicImportLine = isWorkspacePackage ? "" : usesDeferredSingletonFallback ? "" : command !== "build" && !skipServePrebuildWarmup ? `;() => import(${escapeGeneratedStringLiteral(devImportSource)}).catch(() => {});` : "";
|
|
1559
|
+
const moduleBody = usesDeferredSingletonFallback ? `
|
|
1471
1560
|
${prebuildImportLine}
|
|
1472
1561
|
${devDynamicImportLine}
|
|
1473
1562
|
${importLine}
|
|
@@ -1600,29 +1689,14 @@ function generateLocalSharedImportMap() {
|
|
|
1600
1689
|
}
|
|
1601
1690
|
`;
|
|
1602
1691
|
}
|
|
1603
|
-
function generateUsedSharedPreloadConfig() {
|
|
1604
|
-
return `{
|
|
1605
|
-
${getOrderedUsedShares().map((pkg) => {
|
|
1606
|
-
const shareItem = getShareItemForPreload(pkg);
|
|
1607
|
-
if (!shareItem) return null;
|
|
1608
|
-
return `${JSON.stringify(pkg)}: {
|
|
1609
|
-
version: ${JSON.stringify(shareItem.version)},
|
|
1610
|
-
scope: ${JSON.stringify(shareItem.scope)},
|
|
1611
|
-
shareConfig: {
|
|
1612
|
-
singleton: ${shareItem.shareConfig.singleton},
|
|
1613
|
-
requiredVersion: ${JSON.stringify(shareItem.shareConfig.requiredVersion)},
|
|
1614
|
-
strictVersion: ${shareItem.shareConfig.strictVersion},
|
|
1615
|
-
${shareItem.shareConfig.import === false ? "import: false," : ""}
|
|
1616
|
-
}
|
|
1617
|
-
}`;
|
|
1618
|
-
}).filter((item) => item !== null).join(",\n")}
|
|
1619
|
-
}`;
|
|
1620
|
-
}
|
|
1621
1692
|
function getOrderedUsedShares() {
|
|
1622
1693
|
const shares = new Set(getUsedShares());
|
|
1623
1694
|
try {
|
|
1624
1695
|
Object.keys(getNormalizeModuleFederationOptions().shared).forEach((pkg) => {
|
|
1625
|
-
if (!pkg.endsWith("/"))
|
|
1696
|
+
if (!pkg.endsWith("/")) {
|
|
1697
|
+
shares.add(pkg);
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1626
1700
|
});
|
|
1627
1701
|
} catch {}
|
|
1628
1702
|
return orderSharedDependenciesFirst(Array.from(shares).sort((a, b) => {
|
|
@@ -1725,12 +1799,31 @@ const sharedProviderSelectionHelperCode = `const __mfOriginalProviderKey = Symbo
|
|
|
1725
1799
|
function hasImportFalseShared$1(options) {
|
|
1726
1800
|
return Object.values(options.shared ?? {}).some((share) => share?.shareConfig?.import === false);
|
|
1727
1801
|
}
|
|
1728
|
-
function
|
|
1729
|
-
return
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1802
|
+
function generateRuntimeSharedCacheSeedCode() {
|
|
1803
|
+
return `
|
|
1804
|
+
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
1805
|
+
const cacheDescriptor = __mfGetSharedCacheDescriptor(pkg, share.shareConfig?.singleton, share.version, share.scope);
|
|
1806
|
+
if (share.shareConfig?.import === false || __mfReadSharedCache(__mfModuleCache.share, cacheDescriptor) !== undefined) {
|
|
1807
|
+
continue;
|
|
1808
|
+
}
|
|
1809
|
+
const singletonCacheDescriptor = __mfGetSharedCacheDescriptor(pkg, true, share.version, share.scope);
|
|
1810
|
+
const singletonModule = __mfReadSharedCache(__mfModuleCache.share, singletonCacheDescriptor);
|
|
1811
|
+
if (singletonModule !== undefined) {
|
|
1812
|
+
__mfWriteSharedCache(__mfModuleCache.share, cacheDescriptor, singletonModule);
|
|
1813
|
+
continue;
|
|
1814
|
+
}
|
|
1815
|
+
const factory = await share.get();
|
|
1816
|
+
const mod = typeof factory === "function" ? factory() : factory;
|
|
1817
|
+
const resolved = await Promise.resolve(mod);
|
|
1818
|
+
${normalizeRuntimeShareCode}
|
|
1819
|
+
const normalizedModule = __mfNormalizeRuntimeShare(resolved);
|
|
1820
|
+
const exportModule = normalizedModule === resolved ? {...resolved} : normalizedModule;
|
|
1821
|
+
Object.defineProperty(exportModule, "__esModule", {
|
|
1822
|
+
value: true,
|
|
1823
|
+
enumerable: false
|
|
1824
|
+
});
|
|
1825
|
+
__mfWriteSharedCache(__mfModuleCache.share, cacheDescriptor, exportModule);
|
|
1826
|
+
}`;
|
|
1734
1827
|
}
|
|
1735
1828
|
function getBrowserImportPath(importPath) {
|
|
1736
1829
|
if (/^(?:[a-zA-Z]:[\\/]|\/)/.test(importPath) && !importPath.startsWith("/@")) return `/@fs/${importPath}`;
|
|
@@ -1880,7 +1973,7 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1880
1973
|
__mfWriteSharedCache(__mfModuleCache.share, cacheDescriptor, singletonModule);
|
|
1881
1974
|
}
|
|
1882
1975
|
}
|
|
1883
|
-
${
|
|
1976
|
+
${generateRuntimeSharedCacheSeedCode()}
|
|
1884
1977
|
const __browserPlugins = [${pluginImportNames.filter((item) => !isSsrOnlyPlugin(item[1])).map((item) => `${item[0]}(${item[2]})`).join(", ")}];
|
|
1885
1978
|
const __ssrPlugins = typeof globalThis.window === 'undefined'
|
|
1886
1979
|
? await Promise.all([${pluginImportNames.filter((item) => isSsrOnlyPlugin(item[1])).map((item) => {
|
|
@@ -1933,6 +2026,9 @@ function generateRemoteEntry(options, virtualExposesId = getVirtualExposesId(opt
|
|
|
1933
2026
|
async function getExposes(moduleName) {
|
|
1934
2027
|
const exposesMap = await getExposesMap()
|
|
1935
2028
|
if (!(moduleName in exposesMap)) throw new Error(\`[Module Federation] Module \${moduleName} does not exist in container.\`)
|
|
2029
|
+
if (__mfModuleCache.pendingShareLoads) {
|
|
2030
|
+
await Promise.all(__mfModuleCache.pendingShareLoads)
|
|
2031
|
+
}
|
|
1936
2032
|
return (exposesMap[moduleName])().then(res => () => res)
|
|
1937
2033
|
}
|
|
1938
2034
|
export {
|
|
@@ -1956,7 +2052,7 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
|
|
|
1956
2052
|
${generateHostAutoInitSharedCacheSeedCode(_command)}
|
|
1957
2053
|
const remoteEntry = await import(${remoteEntryImport});
|
|
1958
2054
|
const runtime = await remoteEntry.init();
|
|
1959
|
-
const usedShared = ${
|
|
2055
|
+
const {usedShared} = await import("${getLocalSharedImportMapPath()}");
|
|
1960
2056
|
${normalizeRuntimeShareCode}
|
|
1961
2057
|
${shouldPreloadShares ? `
|
|
1962
2058
|
for (const [pkg, share] of Object.entries(usedShared)) {
|
|
@@ -4116,11 +4212,37 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
4116
4212
|
function isRemoteImport(source) {
|
|
4117
4213
|
return Object.keys(options.remotes).some((name) => source === name || source.startsWith(name + "/"));
|
|
4118
4214
|
}
|
|
4119
|
-
function
|
|
4120
|
-
const
|
|
4215
|
+
function collectImportSources(code) {
|
|
4216
|
+
const sources = /* @__PURE__ */ new Set();
|
|
4121
4217
|
for (const match of code.matchAll(/(?:^|[;\n\r])\s*import\s+(?:[\s\S]*?\s+from\s+)?["']([^"']+)["']|import\(\s*["']([^"']+)["']\s*\)/g)) {
|
|
4122
4218
|
const source = match[1] || match[2];
|
|
4123
|
-
if (source
|
|
4219
|
+
if (source) sources.add(source);
|
|
4220
|
+
}
|
|
4221
|
+
return Array.from(sources).sort();
|
|
4222
|
+
}
|
|
4223
|
+
function shouldScanResolvedImport(id) {
|
|
4224
|
+
if (!id || id.includes("\0")) return false;
|
|
4225
|
+
if (id.includes("/node_modules/") || id.includes("\\node_modules\\")) return false;
|
|
4226
|
+
return /\.(?:[cm]?[jt]sx?|vue|svelte)(?:\?|$)/.test(id);
|
|
4227
|
+
}
|
|
4228
|
+
async function collectRemoteDependencies(ctx, id, seen = /* @__PURE__ */ new Set()) {
|
|
4229
|
+
if (seen.has(id) || !shouldScanResolvedImport(id)) return [];
|
|
4230
|
+
seen.add(id);
|
|
4231
|
+
let code;
|
|
4232
|
+
try {
|
|
4233
|
+
code = readFileSync$1(id, "utf8");
|
|
4234
|
+
} catch {
|
|
4235
|
+
return [];
|
|
4236
|
+
}
|
|
4237
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
4238
|
+
for (const source of collectImportSources(code)) {
|
|
4239
|
+
if (isRemoteImport(source)) {
|
|
4240
|
+
dependencies.add(source);
|
|
4241
|
+
continue;
|
|
4242
|
+
}
|
|
4243
|
+
const resolved = await ctx.resolve(source, id);
|
|
4244
|
+
if (!resolved?.id || !shouldScanResolvedImport(resolved.id)) continue;
|
|
4245
|
+
for (const dependency of await collectRemoteDependencies(ctx, resolved.id, seen)) dependencies.add(dependency);
|
|
4124
4246
|
}
|
|
4125
4247
|
return Array.from(dependencies).sort();
|
|
4126
4248
|
}
|
|
@@ -4128,12 +4250,7 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
|
|
|
4128
4250
|
const next = {};
|
|
4129
4251
|
for (const [exposeKey, expose] of Object.entries(options.exposes)) {
|
|
4130
4252
|
const resolved = await ctx.resolve(expose.import);
|
|
4131
|
-
|
|
4132
|
-
try {
|
|
4133
|
-
next[exposeKey] = collectRemoteDependencies(readFileSync$1(resolved.id, "utf8"));
|
|
4134
|
-
} catch {
|
|
4135
|
-
next[exposeKey] = [];
|
|
4136
|
-
}
|
|
4253
|
+
next[exposeKey] = resolved?.id ? await collectRemoteDependencies(ctx, resolved.id) : [];
|
|
4137
4254
|
}
|
|
4138
4255
|
exposeRemoteDependencies = next;
|
|
4139
4256
|
}
|
|
@@ -4533,7 +4650,7 @@ function proxySharedModule(options) {
|
|
|
4533
4650
|
const key = findSharedKeyForSource(source, shared);
|
|
4534
4651
|
if (!key) return;
|
|
4535
4652
|
if (useDirectReactImport && key === "react") return;
|
|
4536
|
-
if (
|
|
4653
|
+
if (isAssetLikeImport(source)) return;
|
|
4537
4654
|
if (isBuildConfigImporter(importer)) return;
|
|
4538
4655
|
if (useDirectReactImport && source === "react") return;
|
|
4539
4656
|
if (importer && importer.includes("localSharedImportMap")) return;
|
|
@@ -5579,7 +5696,7 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
5579
5696
|
if (isSharedResolverInternalImporter(importer)) return;
|
|
5580
5697
|
const key = findSharedKey(source, shared);
|
|
5581
5698
|
if (!key) return;
|
|
5582
|
-
if (source
|
|
5699
|
+
if (isAssetLikeImport(source)) return;
|
|
5583
5700
|
const shareItem = shared[key];
|
|
5584
5701
|
const isReactSingleton = source === "react" && key === "react" && shareItem.shareConfig?.singleton === true;
|
|
5585
5702
|
const isReactRequire = options?.kind?.startsWith("require") && isReactSingleton;
|
|
@@ -5615,7 +5732,7 @@ function createEarlyVirtualModulesPlugin(options) {
|
|
|
5615
5732
|
if (args.kind === "entry-point") return;
|
|
5616
5733
|
if (!args.importer || args.namespace === "mf-shared") return;
|
|
5617
5734
|
if (isSharedResolverInternalImporter(args.importer)) return;
|
|
5618
|
-
if (!findSharedKey(args.path, shared) || args.path
|
|
5735
|
+
if (!findSharedKey(args.path, shared) || isAssetLikeImport(args.path)) return;
|
|
5619
5736
|
return {
|
|
5620
5737
|
path: args.path,
|
|
5621
5738
|
namespace: "mf-shared"
|
|
@@ -5726,7 +5843,7 @@ export default __mfShared.default ?? __mfShared;`
|
|
|
5726
5843
|
const SSR_ONLY_PLUGINS = new Set(["@module-federation/vite/ssrEntryLoader"]);
|
|
5727
5844
|
function loadPluginDts(options) {
|
|
5728
5845
|
if (options.dts === false) return [];
|
|
5729
|
-
return [import("./pluginDts-
|
|
5846
|
+
return [import("./pluginDts-Bo95ELmX.js").then((n) => n.n).then(({ default: pluginDts }) => pluginDts(options))];
|
|
5730
5847
|
}
|
|
5731
5848
|
function federation(mfUserOptions) {
|
|
5732
5849
|
if (isTestEnv()) return [];
|
|
@@ -141,12 +141,47 @@ function resolveExportsEntry(exportsField, conditions = DEFAULT_EXPORT_CONDITION
|
|
|
141
141
|
if (resolved) return resolved;
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
|
+
function substituteExportsWildcard(target, patternMatch) {
|
|
145
|
+
if (typeof target === "string") return target.split("*").join(patternMatch);
|
|
146
|
+
if (Array.isArray(target)) return target.map((entry) => substituteExportsWildcard(entry, patternMatch));
|
|
147
|
+
if (target && typeof target === "object") {
|
|
148
|
+
const source = target;
|
|
149
|
+
const out = {};
|
|
150
|
+
for (const key of Object.keys(source)) out[key] = substituteExportsWildcard(source[key], patternMatch);
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
return target;
|
|
154
|
+
}
|
|
155
|
+
function matchExportsSubpath(record, subpath) {
|
|
156
|
+
if (subpath in record) return record[subpath];
|
|
157
|
+
let bestKey;
|
|
158
|
+
let bestBaseLength = -1;
|
|
159
|
+
let bestKeyLength = -1;
|
|
160
|
+
for (const key of Object.keys(record)) {
|
|
161
|
+
const wildcardIndex = key.indexOf("*");
|
|
162
|
+
if (wildcardIndex === -1) continue;
|
|
163
|
+
const patternBase = key.slice(0, wildcardIndex);
|
|
164
|
+
const patternTrailer = key.slice(wildcardIndex + 1);
|
|
165
|
+
if (patternTrailer.includes("*")) continue;
|
|
166
|
+
if (!subpath.startsWith(patternBase) || !subpath.endsWith(patternTrailer)) continue;
|
|
167
|
+
if (subpath.length <= patternBase.length + patternTrailer.length) continue;
|
|
168
|
+
if (patternBase.length > bestBaseLength || patternBase.length === bestBaseLength && key.length > bestKeyLength) {
|
|
169
|
+
bestKey = key;
|
|
170
|
+
bestBaseLength = patternBase.length;
|
|
171
|
+
bestKeyLength = key.length;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (bestKey === void 0) return void 0;
|
|
175
|
+
const patternTrailer = bestKey.slice(bestKey.indexOf("*") + 1);
|
|
176
|
+
const patternMatch = subpath.slice(bestBaseLength, subpath.length - patternTrailer.length);
|
|
177
|
+
return substituteExportsWildcard(record[bestKey], patternMatch);
|
|
178
|
+
}
|
|
144
179
|
function getPackageExportsTarget(pkg, packageName, exportsField) {
|
|
145
180
|
if (typeof exportsField === "string") return pkg === packageName ? exportsField : void 0;
|
|
146
181
|
if (!exportsField || typeof exportsField !== "object") return void 0;
|
|
147
182
|
const record = exportsField;
|
|
148
183
|
const subpath = pkg === packageName ? "." : `.${pkg.slice(packageName.length)}`;
|
|
149
|
-
if (subpath !== ".") return record
|
|
184
|
+
if (subpath !== ".") return matchExportsSubpath(record, subpath);
|
|
150
185
|
return record["."] ?? (!Object.keys(record).some((key) => key.startsWith(".")) ? record : void 0);
|
|
151
186
|
}
|
|
152
187
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/vite",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.15",
|
|
4
4
|
"description": "Vite plugin for Module Federation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -70,9 +70,9 @@
|
|
|
70
70
|
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@module-federation/dts-plugin": "2.
|
|
74
|
-
"@module-federation/runtime": "2.
|
|
75
|
-
"@module-federation/sdk": "2.
|
|
73
|
+
"@module-federation/dts-plugin": "2.7.0",
|
|
74
|
+
"@module-federation/runtime": "2.7.0",
|
|
75
|
+
"@module-federation/sdk": "2.7.0"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"@playwright/test": "1.58.2",
|