mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-27 10:52:54 +00:00
123 lines
4.7 KiB
JavaScript
123 lines
4.7 KiB
JavaScript
const require_constant = require('./constant.cjs');
|
|
|
|
//#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(require_constant.MANIFEST_EXT)) return true;
|
|
else return false;
|
|
}
|
|
function getManifestFileName(manifestOptions) {
|
|
if (!manifestOptions) return {
|
|
statsFileName: require_constant.StatsFileName,
|
|
manifestFileName: require_constant.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) : require_constant.ManifestFileName;
|
|
return {
|
|
statsFileName: simpleJoinRemoteEntry(filePath, fileName ? insertSuffix(manifestFileName, "-stats") : require_constant.StatsFileName),
|
|
manifestFileName: simpleJoinRemoteEntry(filePath, manifestFileName)
|
|
};
|
|
}
|
|
|
|
//#endregion
|
|
exports.generateSnapshotFromManifest = generateSnapshotFromManifest;
|
|
exports.getManifestFileName = getManifestFileName;
|
|
exports.inferAutoPublicPath = inferAutoPublicPath;
|
|
exports.isManifestProvider = isManifestProvider;
|
|
exports.simpleJoinRemoteEntry = simpleJoinRemoteEntry;
|
|
//# sourceMappingURL=generateSnapshotFromManifest.cjs.map
|