Files

70 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2026-02-27 12:13:52 +02:00
const path = require("path");
const chokidar = require("chokidar");
const browserSync = require("browser-sync").create();
const { build } = require("./build");
const ROOT_DIR = path.resolve(__dirname, "..");
const SOURCE_GLOBS = [
path.join(ROOT_DIR, "css/**/*.css"),
path.join(ROOT_DIR, "js/**/*.js")
];
const EXAMPLE_GLOBS = [
path.join(ROOT_DIR, "examples/**/*.html")
];
let isBuilding = false;
let hasPendingBuild = false;
async function queueBuild(trigger) {
if (isBuilding) {
hasPendingBuild = true;
return;
}
isBuilding = true;
try {
await build();
console.log(`[watch] Built after ${trigger}`);
browserSync.reload();
} catch (error) {
console.error("[watch] Build failed");
console.error(error);
} finally {
isBuilding = false;
}
if (hasPendingBuild) {
hasPendingBuild = false;
await queueBuild("queued change");
}
}
async function start() {
await build();
browserSync.init({
server: ROOT_DIR,
startPath: "examples/generic.html",
notify: false,
open: false
});
chokidar.watch(SOURCE_GLOBS, { ignoreInitial: true }).on("all", (event, filePath) => {
const relativePath = path.relative(ROOT_DIR, filePath);
queueBuild(`${event}: ${relativePath}`);
});
chokidar.watch(EXAMPLE_GLOBS, { ignoreInitial: true }).on("all", () => {
browserSync.reload();
});
console.log("[watch] Watching css/, js/, and examples/.");
}
start().catch((error) => {
console.error("[watch] Failed to start");
console.error(error);
process.exit(1);
});