Files
emaui/node_modules/@module-federation/sdk/dist/generateSnapshotFromManifest.js
2026-05-29 15:23:46 +03:00

119 lines
4.5 KiB
JavaScript

import { MANIFEST_EXT, ManifestFileName, StatsFileName } from "./constant.js";
//#region src/generateSnapshotFromManifest.ts
const simpleJoinRemoteEntry = (rPath, rName) => {
if (!rPath) return rName;
const transformPath = (str) => {
if (str === ".") return "";
if (str.startsWith("./")) return str.replace("./", "");
if (str.startsWith("/")) {
const strWithoutSlash = str.slice(1);
if (strWithoutSlash.endsWith("/")) return strWithoutSlash.slice(0, -1);
return strWithoutSlash;
}
return str;
};
const transformedPath = transformPath(rPath);
if (!transformedPath) return rName;
if (transformedPath.endsWith("/")) return `${transformedPath}${rName}`;
return `${transformedPath}/${rName}`;
};
function inferAutoPublicPath(url) {
return url.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
}
function generateSnapshotFromManifest(manifest, options = {}) {
const { remotes = {}, overrides = {}, version } = options;
let remoteSnapshot;
const getPublicPath = () => {
if ("publicPath" in manifest.metaData) {
if ((manifest.metaData.publicPath === "auto" || manifest.metaData.publicPath === "") && version) return inferAutoPublicPath(version);
return manifest.metaData.publicPath;
} else return manifest.metaData.getPublicPath;
};
const overridesKeys = Object.keys(overrides);
let remotesInfo = {};
if (!Object.keys(remotes).length) remotesInfo = manifest.remotes?.reduce((res, next) => {
let matchedVersion;
const name = next.federationContainerName;
if (overridesKeys.includes(name)) matchedVersion = overrides[name];
else if ("version" in next) matchedVersion = next.version;
else matchedVersion = next.entry;
res[name] = { matchedVersion };
return res;
}, {}) || {};
Object.keys(remotes).forEach((key) => remotesInfo[key] = { matchedVersion: overridesKeys.includes(key) ? overrides[key] : remotes[key] });
const { remoteEntry: { path: remoteEntryPath, name: remoteEntryName, type: remoteEntryType }, types: remoteTypes = {
path: "",
name: "",
zip: "",
api: ""
}, buildInfo: { buildVersion }, globalName, ssrRemoteEntry } = manifest.metaData;
const { exposes } = manifest;
let basicRemoteSnapshot = {
version: version ? version : "",
buildVersion,
globalName,
remoteEntry: simpleJoinRemoteEntry(remoteEntryPath, remoteEntryName),
remoteEntryType,
remoteTypes: simpleJoinRemoteEntry(remoteTypes.path, remoteTypes.name),
remoteTypesZip: remoteTypes.zip || "",
remoteTypesAPI: remoteTypes.api || "",
remotesInfo,
shared: manifest?.shared.map((item) => ({
assets: item.assets,
sharedName: item.name,
version: item.version,
usedExports: item.referenceExports || []
})),
modules: exposes?.map((expose) => ({
moduleName: expose.name,
modulePath: expose.path,
assets: expose.assets
}))
};
if ("publicPath" in manifest.metaData) {
remoteSnapshot = {
...basicRemoteSnapshot,
publicPath: getPublicPath()
};
if (typeof manifest.metaData.ssrPublicPath === "string") remoteSnapshot.ssrPublicPath = manifest.metaData.ssrPublicPath;
} else remoteSnapshot = {
...basicRemoteSnapshot,
getPublicPath: getPublicPath()
};
if (ssrRemoteEntry) {
const fullSSRRemoteEntry = simpleJoinRemoteEntry(ssrRemoteEntry.path, ssrRemoteEntry.name);
remoteSnapshot.ssrRemoteEntry = fullSSRRemoteEntry;
remoteSnapshot.ssrRemoteEntryType = ssrRemoteEntry.type || "commonjs-module";
}
return remoteSnapshot;
}
function isManifestProvider(moduleInfo) {
if ("remoteEntry" in moduleInfo && moduleInfo.remoteEntry.includes(MANIFEST_EXT)) return true;
else return false;
}
function getManifestFileName(manifestOptions) {
if (!manifestOptions) return {
statsFileName: StatsFileName,
manifestFileName: ManifestFileName
};
let filePath = typeof manifestOptions === "boolean" ? "" : manifestOptions.filePath || "";
let fileName = typeof manifestOptions === "boolean" ? "" : manifestOptions.fileName || "";
const JSON_EXT = ".json";
const addExt = (name) => {
if (name.endsWith(JSON_EXT)) return name;
return `${name}${JSON_EXT}`;
};
const insertSuffix = (name, suffix) => {
return name.replace(JSON_EXT, `${suffix}${JSON_EXT}`);
};
const manifestFileName = fileName ? addExt(fileName) : ManifestFileName;
return {
statsFileName: simpleJoinRemoteEntry(filePath, fileName ? insertSuffix(manifestFileName, "-stats") : StatsFileName),
manifestFileName: simpleJoinRemoteEntry(filePath, manifestFileName)
};
}
//#endregion
export { generateSnapshotFromManifest, getManifestFileName, inferAutoPublicPath, isManifestProvider, simpleJoinRemoteEntry };
//# sourceMappingURL=generateSnapshotFromManifest.js.map