@lynx-js/rspeedy
Version:
A webpack/rspack-based frontend toolchain for Lynx
39 lines (38 loc) • 1.99 kB
JavaScript
import { __webpack_require__ } from "./src_cli_main_ts-node_child_process-node_events-node_fs-node_path-node_process.js";
import node_path from "node:path";
import { logger } from "@rsbuild/core";
import { exit as exit_exit } from "./src_cli_main_ts-node_child_process-node_events-node_fs-node_path-node_process.js";
import { createRspeedy, init } from "./1~src_cli_init_ts-src_create-rspeedy_ts.js";
import { isCI } from "./1~is-ci.js";
import { watchFiles, getWatchedFiles } from "./1~watch.js";
const picocolors = __webpack_require__("../../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js");
var picocolors_default = /*#__PURE__*/ __webpack_require__.n(picocolors);
async function build(cwd, buildOptions) {
const shouldExit = 'true' !== process.env['RSDOCTOR'] || isCI();
const isWatch = buildOptions.watch ?? false;
let onBeforeRestart = [];
try {
const { rspeedyConfig, configPath, createRspeedyOptions } = await init(cwd, buildOptions);
if (isWatch) {
const watchedFiles = getWatchedFiles(configPath, rspeedyConfig);
await watchFiles(watchedFiles.map((filePath)=>node_path.isAbsolute(filePath) ? filePath : node_path.join(cwd, filePath)), async (filename)=>{
logger.info(`Restart because ${picocolors_default().yellow(filename)} is changed.\n`);
const cleanup = onBeforeRestart.map((f)=>f());
onBeforeRestart = [];
await Promise.all(cleanup);
await build.call(this, cwd, buildOptions);
});
}
const rspeedy = await createRspeedy(createRspeedyOptions);
const { close } = await rspeedy.build({
watch: isWatch
});
if (isWatch) onBeforeRestart.push(close);
else await close();
} catch (error) {
logger.error(error);
if (shouldExit) return void exit_exit(1);
}
if (shouldExit && !isWatch) exit_exit();
}
export { build };