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