@esmx/core
Version:
A high-performance microfrontend framework supporting Vue, React, Preact, Solid, and Svelte with SSR and Module Federation capabilities.
184 lines (183 loc) • 5.11 kB
JavaScript
import path from "node:path";
export function parseModuleConfig(name, root, config = {}) {
return {
name,
root,
links: getLinks(name, root, config),
environments: {
client: getEnvironments(config, "client", name),
server: getEnvironments(config, "server", name)
}
};
}
export function getLinks(name, root, config) {
const result = {};
Object.entries({
[name]: path.resolve(root, "dist"),
...config.links
}).forEach(([name2, value]) => {
const serverRoot = path.isAbsolute(value) ? value : path.resolve(root, value);
result[name2] = {
name: name2,
root: value,
client: path.resolve(serverRoot, "client"),
clientManifestJson: path.resolve(
serverRoot,
"client/manifest.json"
),
server: path.resolve(serverRoot, "server"),
serverManifestJson: path.resolve(serverRoot, "server/manifest.json")
};
});
return result;
}
export function getEnvironmentImports(environment, imports = {}) {
const result = {};
for (const [key, value] of Object.entries(imports)) {
if (typeof value === "string") {
result[key] = value;
} else {
const environmentValue = value[environment];
if (environmentValue !== void 0) {
result[key] = environmentValue;
}
}
}
return result;
}
export function getEnvironmentScopes(environment, scopes = {}) {
const result = {};
for (const [scopeName, scopeImports] of Object.entries(scopes)) {
result[scopeName] = getEnvironmentImports(environment, scopeImports);
}
return result;
}
export function getEnvironments(config, env, moduleName) {
const imports = getEnvironmentImports(env, config.imports);
const exports = getEnvironmentExports(config, env);
const scopes = getEnvironmentScopes(env, config.scopes);
addPackageExportsToScopes(exports, scopes, moduleName);
return {
imports,
exports,
scopes
};
}
export function createDefaultExports(env) {
switch (env) {
case "client":
return {
"src/entry.client": {
name: "src/entry.client",
file: "./src/entry.client",
pkg: false
},
"src/entry.server": {
name: "src/entry.server",
file: "",
pkg: false
}
};
case "server":
return {
"src/entry.client": {
name: "src/entry.client",
file: "",
pkg: false
},
"src/entry.server": {
name: "src/entry.server",
file: "./src/entry.server",
pkg: false
}
};
}
}
export function processStringExport(exportString) {
const parsedValue = parsedExportValue(exportString);
return { [parsedValue.name]: parsedValue };
}
export function processObjectExport(exportObject, env) {
const exports = {};
Object.keys(exportObject).forEach((name) => {
const config = exportObject[name];
if (typeof config === "string") {
const parsedValue2 = parsedExportValue(config);
exports[name] = { ...parsedValue2, name };
return;
}
const filePath = resolveExportFile(config, env, name);
const parsedValue = parsedExportValue(filePath);
exports[name] = { ...parsedValue, name };
});
return exports;
}
export function resolveExportFile(config, env, name) {
if (typeof config === "string") {
return config;
}
const value = config[env];
if (typeof value === "boolean") {
return value === true ? name : "";
} else if (typeof value === "string") {
return value || name;
}
return name;
}
export function processExportArray(exportArray, env) {
const exports = {};
exportArray.forEach((item) => {
if (typeof item === "string") {
const itemExports = processStringExport(item);
Object.assign(exports, itemExports);
} else {
const itemExports = processObjectExport(item, env);
Object.assign(exports, itemExports);
}
});
return exports;
}
export function getEnvironmentExports(config, env) {
const exports = createDefaultExports(env);
if (config.exports) {
const userExports = processExportArray(config.exports, env);
Object.assign(exports, userExports);
}
return exports;
}
export function addPackageExportsToScopes(exports, scopes, moduleName) {
Object.entries(exports).forEach(([exportName, exportConfig]) => {
if (exportConfig.pkg) {
if (!scopes[""]) {
scopes[""] = {};
}
scopes[""][exportName] = moduleName + "/" + exportName;
}
});
return scopes;
}
export function parsedExportValue(value) {
const FILE_EXT_REGEX = /\.(js|mjs|cjs|jsx|mjsx|cjsx|ts|mts|cts|tsx|mtsx|ctsx)$/i;
if (value.startsWith("pkg:")) {
const item = value.substring("pkg:".length);
return {
name: item,
pkg: true,
file: item
};
} else if (value.startsWith("root:")) {
const item = value.substring("root:".length);
const name = item.replace(FILE_EXT_REGEX, "");
return {
name,
pkg: false,
file: "./" + item
};
} else {
return {
name: value,
pkg: false,
file: value
};
}
}