@esmx/rspack
Version:
A high-performance Rspack integration for Esmx microfrontend framework, providing Module Linking and SSR capabilities.
68 lines (67 loc) • 1.86 kB
JavaScript
import { styleText } from "node:util";
import { rspack } from "@rspack/core";
function showError(message) {
console.error(styleText("red", message));
}
export function createRsBuild(options) {
const multiCompiler = rspack(options);
return {
get compilers() {
return multiCompiler.compilers;
},
build() {
return new Promise((resolve) => {
multiCompiler.run((err, stats) => {
if (err) {
showError(err.message);
return resolve(false);
}
if (stats?.hasErrors()) {
stats.toJson({ errors: true })?.errors?.forEach((err2) => {
showError(err2.message);
});
return resolve(false);
}
multiCompiler.close((err2) => {
if (err2) {
showError(err2.message);
return resolve(false);
}
process.nextTick(() => {
resolve(true);
});
});
});
});
},
watch() {
const watching = multiCompiler.watch({}, (err, stats) => {
if (err) {
console.log(styleText("red", err.message));
return;
}
if (stats?.hasErrors()) {
stats.toJson({ errors: true })?.errors?.forEach((err2) => {
console.log(styleText("red", err2.message));
});
}
});
const signals = ["SIGINT", "SIGTERM", "SIGHUP"];
signals.forEach((signal) => {
process.on(signal, () => {
watching.close(() => {
process.exit();
});
});
});
process.on("uncaughtException", handleExit);
process.on("unhandledRejection", handleExit);
function handleExit(err) {
console.error(err);
watching.close(() => {
process.exit(1);
});
}
}
};
}