Merge branch 'fix-ts-import'

This commit is contained in:
Chris Wanstrath 2026-02-20 16:10:32 -08:00
commit 1333deb5e9

View File

@ -1,4 +1,4 @@
import { join } from 'path' import { join, resolve } from 'path'
import { render as formatHTML } from './lib/html-formatter' import { render as formatHTML } from './lib/html-formatter'
import { type Context, Hono, type Schema, type Env } from 'hono' import { type Context, Hono, type Schema, type Env } from 'hono'
import { serveStatic } from 'hono/bun' import { serveStatic } from 'hono/bun'
@ -174,23 +174,25 @@ export class Hype<
// serve transpiled js // serve transpiled js
this.on('GET', ['/client/:path{.+}', '/shared/:path{.+}'], async c => { this.on('GET', ['/client/:path{.+}', '/shared/:path{.+}'], async c => {
let path = './src/' + c.req.path.replace('..', '.') const reqPath = resolve('./src/', c.req.path.slice(1))
if (!reqPath.startsWith(resolve('./src/'))) return render404(c)
// path must end in .js or .ts // strip known extension to get base path
if (!path.endsWith('.js') && !path.endsWith('.ts')) path += '.ts' const base = reqPath.replace(/\.(js|jsx|ts|tsx)$/, '')
const ts = path.replace('.js', '.ts') // try TS extensions first (needs transpilation)
if (await Bun.file(ts).exists()) for (const ext of ['.ts', '.tsx', '.jsx']) {
return new Response(await transpile(ts), { headers: { 'Content-Type': 'text/javascript' } }) const file = base + ext
if (await Bun.file(file).exists())
return new Response(await transpile(file), { headers: { 'Content-Type': 'text/javascript' } })
}
else if (await Bun.file(ts + 'x').exists()) // try plain .js (serve raw)
return new Response(await transpile(ts + 'x'), { headers: { 'Content-Type': 'text/javascript' } }) const jsFile = base + '.js'
if (await Bun.file(jsFile).exists())
return new Response(Bun.file(jsFile), { headers: { 'Content-Type': 'text/javascript' } })
else if (await Bun.file(path).exists()) return render404(c)
return new Response(Bun.file(path), { headers: { 'Content-Type': 'text/javascript' } })
else
return render404(c)
}) })
// file based routing // file based routing