need that cache for HMR

This commit is contained in:
Chris Wanstrath 2026-01-27 22:16:02 -08:00
parent b9b4e205c9
commit a8d3a8203e

View File

@ -1,4 +1,5 @@
import { type Context } from 'hono'
import { stat } from 'fs/promises'
// template literal tag for inline CSS. returns a <style> tag
export function css(strings: TemplateStringsArray, ...values: any[]) {
@ -142,8 +143,10 @@ const transpileCache: Record<string, string> = {}
// transpile frontend ts to js
export async function transpile(path: string): Promise<string> {
// cache by path; --watch restarts server on file changes, clearing the cache
let cached = transpileCache[path]
const { mtime } = await stat(path)
const key = `${path}?${mtime}`
let cached = process.env.NODE_ENV === 'production' ? transpileCache[key] : undefined
if (!cached) {
const result = await Bun.build({
@ -156,7 +159,7 @@ export async function transpile(path: string): Promise<string> {
if (!result.outputs[0]) throw new Error(`Failed to build ${path}`)
cached = await result.outputs[0].text()
transpileCache[path] = cached
transpileCache[key] = cached
}
return cached