Compare commits

..

No commits in common. "68406a0a800111e0a3c8f65f37803c625e43fdcb" and "ea2109179242af35609570e8aea547a0e4493d29" have entirely different histories.

3 changed files with 8 additions and 13 deletions

View File

@ -1,5 +1,5 @@
import { join } from 'path' import { join } from 'path'
import { render as formatHTML } from './lib/html-formatter' import { render as formatHTML } from './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'
import color from 'kleur' import color from 'kleur'
@ -139,6 +139,7 @@ export class Hype<
const pageName = (c.req.param('page') ?? 'index').replace('.', '') const pageName = (c.req.param('page') ?? 'index').replace('.', '')
if (pageName.startsWith('_')) return render404(c) if (pageName.startsWith('_')) return render404(c)
console.log(process.env.PWD)
const path = join(process.env.PWD ?? '.', `./src/pages/${pageName}.tsx`) const path = join(process.env.PWD ?? '.', `./src/pages/${pageName}.tsx`)
if (!(await Bun.file(path).exists())) if (!(await Bun.file(path).exists()))

View File

@ -8,8 +8,6 @@ export function css(strings: TemplateStringsArray, ...values: any[]) {
}} /> }} />
} }
const transpiler = new Bun.Transpiler({ loader: 'tsx' })
// template literal tag for inline JS. transpiles and returns a <script> tag // template literal tag for inline JS. transpiles and returns a <script> tag
export function js(strings: TemplateStringsArray, ...values: any[]) { export function js(strings: TemplateStringsArray, ...values: any[]) {
return <script dangerouslySetInnerHTML={{ return <script dangerouslySetInnerHTML={{
@ -139,25 +137,21 @@ export function weightedRand(): number {
return numbers[numbers.length - 1]! return numbers[numbers.length - 1]!
} }
const transpiler = new Bun.Transpiler({ loader: 'tsx' })
const transpileCache: Record<string, string> = {} const transpileCache: Record<string, string> = {}
// transpile frontend ts to js // Transpile the frontend *.ts file at `path` to JavaScript.
export async function transpile(path: string): Promise<string> { export async function transpile(path: string): Promise<string> {
const { mtime } = await stat(path) const { mtime } = await stat(path)
const key = `${path}?${mtime}` const key = `${path}?${mtime}`
let cached = transpileCache[key] let cached = transpileCache[key]
if (!cached) { if (!cached) {
const result = await Bun.build({ const code = await Bun.file(path).text()
entrypoints: [path], cached = transpiler.transformSync(code)
format: 'esm', cached = cached.replaceAll(/\bjsxDEV_?\w*\(/g, "jsx(")
minify: false, cached = cached.replaceAll(/\bFragment_?\w*,/g, "Fragment,")
sourcemap: 'none',
})
if (!result.outputs[0]) throw new Error(`Failed to build ${path}`)
cached = await result.outputs[0].text()
transpileCache[key] = cached transpileCache[key] = cached
} }