From 60487ec890bb66e9dedf8cd8f83027d7b0a36f0c Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Sat, 29 Mar 2025 23:30:25 +0700 Subject: [PATCH] fix(scripts/dev): only rebuild when necessary --- scripts/dev.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/dev.ts b/scripts/dev.ts index d60df25..537def2 100644 --- a/scripts/dev.ts +++ b/scripts/dev.ts @@ -20,6 +20,8 @@ function debounce any>(func: F, timeout: number) { } const buildDevWithDebounceAndCatchErrors = debounce(async () => { + needRebuild = false + try { return await build(true) } catch (e) { @@ -27,6 +29,8 @@ const buildDevWithDebounceAndCatchErrors = debounce(async () => { } }, 250) +let needRebuild = true + watcher.subscribe(process.cwd(), async (err, events) => { if (err) return console.error(err) @@ -35,11 +39,12 @@ watcher.subscribe(process.cwd(), async (err, events) => { process.exit() } - if (events.some(it => Sources.some(src => it.path.startsWith(src)))) await buildDevWithDebounceAndCatchErrors() + needRebuild ||= events.some(it => Sources.some(src => it.path.startsWith(src))) }) const server = Bun.serve({ - fetch(req, srv) { + async fetch(req, srv) { + if (needRebuild) await buildDevWithDebounceAndCatchErrors() console.debug(chalk.gray(`\u{1F79B} Receiving request from ${srv.requestIP(req)!.address}`)) return new Response(Bun.file('./dist/revenge.bundle')) }, @@ -47,5 +52,3 @@ const server = Bun.serve({ }) console.info(chalk.cyanBright(`\u24D8 Serving on port ${server.port}`)) - -await buildDevWithDebounceAndCatchErrors()