beesbuild
Version:
构建工具链
131 lines (130 loc) • 4.18 kB
JavaScript
import path from "path";
import { rollup, watch } from "rollup";
import chalk from "chalk-unified";
import { consola, pathExtra } from "@beesbuild/utils";
import { existsSync, rmSync } from "fs-extra";
import { defu } from "defu";
import chokidar from "chokidar";
import { handleBuildFiles } from "./utils.mjs";
const createRollupWatcher = (ctx, options, bundle) => {
const script = [
pathExtra.resolve(ctx.options.rootDir, "src/**/*"),
pathExtra.resolve(ctx.options.rootDir, "index.*")
];
const includes = script.concat(options.input);
const output = options.output.find((output2) => output2._module === "esm");
if (!output) return {};
const watchOptions = defu(
{
output: [output],
cache: true,
logLevel: "silent",
watch: {
buildDelay: 1e3,
// chokidar: {
// cwd: ctx.options.rootDir,
// },
clearScreen: true,
// skipWrite: false,
exclude: "node_modules/**",
include: script
}
},
options
);
const { output: outputs, ...opts } = watchOptions;
const writeBundle = async (files) => {
var _a;
const dir = (_a = output.dir) != null ? _a : "";
existsSync(dir) && rmSync(dir, { recursive: true });
const rollupBundle = await rollup(
defu(
{
input: files
},
opts
)
);
await rollupBundle.write(output);
(bundle == null ? void 0 : bundle.copy) && await bundle.copy("esm", files);
};
const watcher = watch(watchOptions);
let first = true;
let changeFiles = [];
watcher.on("event", async (event) => {
const code = first ? `FIRST_${event.code}` : event.code;
if (event.code == "BUNDLE_END" && code !== "FIRST_BUNDLE_END") {
consola.info(`\u5B8C\u6210\u6784\u5EFA\u65F6\u95F4\uFF1A${event.duration}`);
const { watchFiles } = event.result;
event.result.watchFiles = handleBuildFiles(ctx, ctx == null ? void 0 : ctx.argv).map((file) => typeof file === "string" ? file : file == null ? void 0 : file.input).filter((file) => typeof file === "string");
for (const file of event.result.watchFiles.filter((value) => !watchFiles.includes(value))) {
consola.info(`Add listening file\uFF1A${pathExtra.relative(ctx.options.rootDir, file)}`);
}
}
if (event.code === "END") {
consola.success(chalk.green("\u5B8C\u6210\u6240\u6709\u4EA7\u7269\u7684\u6784\u5EFA"));
code === "FIRST_END" && (first = false);
if (bundle.copy) {
consola.info(chalk.cyan("Start for copy file!"));
return bundle.copy("esm", [...changeFiles]).then((r) => {
changeFiles = [];
});
}
}
switch (code) {
case "START":
consola.info(chalk.yellow("\u76D1\u89C6\u5668\u6B63\u5728\uFF08\u91CD\u65B0\uFF09\u542F\u52A8"));
break;
}
consola.warn(chalk.yellow(`\u6267\u884C code:${code}`));
return Promise.resolve();
});
watcher.on("change", async (id, { event }) => {
consola.info(id, "change", event);
consola.info(
chalk.gray(`File ${path.relative(ctx.options.rootDir, id)} break out changed:${event}`)
);
if (event !== "delete") {
changeFiles.push(id);
} else if (event === "delete") {
await bundle.delete("esm", [id]);
}
});
watcher.on("restart", () => {
consola.info("restart");
});
watcher.on("close", () => {
consola.info("close");
watcher.close().then((r) => r);
});
return {
script,
includes,
watchOptions,
watcher,
writeBundle
// chokidar,
};
};
const createChokidarWatcher = (options) => {
const watcher = chokidar.watch(options.script, {
persistent: true
});
watcher.on("change", async (path2) => {
consola.info(chalk.gray(`File ${path2} has been changed`));
});
watcher.on("unlink", async (path2) => {
consola.log(`File ${path2} changed unlink`);
});
return watcher;
};
const rollupWatch = async (ctx, options, bundle) => {
const { script, watcher: rollupWatcher } = createRollupWatcher(ctx, options, bundle);
if (!script) return;
return rollupWatcher;
};
export {
createChokidarWatcher,
createRollupWatcher,
rollupWatch
};