-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev-server.ts
More file actions
72 lines (63 loc) · 2.25 KB
/
Copy pathdev-server.ts
File metadata and controls
72 lines (63 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bun
// Local dev server for testing the static build on the LAN. It builds the site
// once, serves ./dist on 0.0.0.0 (so other devices on the network can reach it),
// and rebuilds whenever a source file changes. This is a dev convenience only,
// production is the static ./dist published to GitHub Pages, never this server.
import { watch } from 'node:fs'
const PORT = Number(Bun.env.PORT ?? 8080)
const DIST = `${import.meta.dir}/dist`
const WATCH_DIRS = ['src', 'assets']
const WATCH_FILES = ['index.html', 'build.ts', 'sync-fonts.ts']
const build = async (): Promise<void> => {
const proc = Bun.spawn(['bun', 'run', 'build.ts'], {
cwd: import.meta.dir,
stdout: 'inherit',
stderr: 'inherit'
})
await proc.exited
}
await build()
// Debounced rebuild so a burst of editor saves triggers a single build.
let pending: ReturnType<typeof setTimeout> | undefined
const scheduleBuild = (): void => {
if (pending) clearTimeout(pending)
pending = setTimeout(() => {
console.log('↻ change detected, rebuilding…')
void build()
}, 120)
}
for (const dir of WATCH_DIRS) {
watch(`${import.meta.dir}/${dir}`, { recursive: true }, scheduleBuild)
}
for (const file of WATCH_FILES) {
watch(`${import.meta.dir}/${file}`, scheduleBuild)
}
const MIME: Record<string, string> = {
html: 'text/html; charset=utf-8',
js: 'text/javascript; charset=utf-8',
css: 'text/css; charset=utf-8',
woff2: 'font/woff2',
svg: 'image/svg+xml'
}
const server = Bun.serve({
port: PORT,
hostname: '0.0.0.0',
async fetch(req) {
const url = new URL(req.url)
let path = decodeURIComponent(url.pathname)
if (path === '/' || path.endsWith('/')) path += 'index.html'
const file = Bun.file(DIST + path)
if (await file.exists()) {
const ext = path.split('.').pop() ?? ''
const headers = MIME[ext] ? { 'Content-Type': MIME[ext] } : undefined
return new Response(file, { headers })
}
return new Response('Not found', { status: 404 })
}
})
console.log(`\nWorld Clock dev server`)
console.log(` local: http://localhost:${server.port}/`)
console.log(` network: http://0.0.0.0:${server.port}/ (reachable on your LAN IP)`)
console.log(
`Watching ${[...WATCH_DIRS, ...WATCH_FILES].join(', ')}, edits rebuild automatically.\n`
)