@esmx/core
Version:
A high-performance microfrontend framework supporting Vue, React, Preact, Solid, and Svelte with SSR and Module Federation capabilities.
90 lines (89 loc) • 2.6 kB
JavaScript
import module from "node:module";
import { styleText } from "node:util";
import pkg from "../../package.json" with { type: "json" };
import { COMMAND, Esmx } from "../core.mjs";
import { resolveImportPath } from "../utils/resolve-path.mjs";
async function getSrcOptions() {
return import(resolveImportPath(process.cwd(), "./src/entry.node.ts")).then(
(m) => m.default
);
}
async function getDistOptions() {
try {
return import(resolveImportPath(process.cwd(), "./dist/index.mjs")).then((m) => m.default);
} catch (e) {
throw new Error(
`Failed to load configuration from dist/index.mjs. Please make sure you have run 'esmx build' first.
${e}`
);
}
}
export async function cli(command) {
console.log(`\u{1F525} ${styleText("yellow", "Esmx")} v${pkg.version}
`);
if (command !== COMMAND.dev && typeof process.env.NODE_ENV === "undefined") {
process.env.NODE_ENV = "production";
}
let esmx;
let opts = null;
switch (command) {
case COMMAND.dev:
opts = await getSrcOptions();
esmx = new Esmx(opts);
exit(await esmx.init(COMMAND.dev));
esmx = null;
opts = null;
break;
case COMMAND.start:
opts = await getDistOptions();
esmx = new Esmx(opts);
exit(await esmx.init(COMMAND.start));
esmx = null;
opts = null;
break;
case COMMAND.build:
opts = await getSrcOptions();
esmx = new Esmx(opts);
exit(await esmx.init(COMMAND.build));
exit(await esmx.destroy());
if (typeof opts.postBuild === "function") {
esmx = new Esmx({
...opts,
server: void 0
});
exit(await esmx.init(COMMAND.start));
exit(await esmx.postBuild());
}
esmx = null;
opts = null;
break;
case COMMAND.preview:
opts = await getSrcOptions();
esmx = new Esmx(opts);
exit(await esmx.init(COMMAND.build));
exit(await esmx.destroy());
esmx = new Esmx(opts);
exit(await esmx.init(COMMAND.start));
exit(await esmx.postBuild());
esmx = null;
opts = null;
break;
default:
await import(resolveImportPath(process.cwd(), command));
break;
}
}
function exit(ok) {
if (!ok) {
process.exit(17);
}
}
module.register(import.meta.url, {
parentURL: import.meta.url
});
export function resolve(specifier, context, nextResolve) {
if (context?.parentURL.endsWith(".ts") && specifier.startsWith(".") && !specifier.endsWith(".ts")) {
return nextResolve(specifier + ".ts", context);
}
return nextResolve(specifier, context);
}