fix(scripts/dev): only rebuild when necessary

This commit is contained in:
PalmDevs 2025-03-29 23:30:25 +07:00
parent fdb1f4d888
commit 60487ec890
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA

View file

@ -20,6 +20,8 @@ function debounce<F extends (...args: any[]) => 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()