Compare commits

..

No commits in common. "9b8653ed97b988ea68bd8a7dfab02bc42b5f9b60" and "feb30369cffbbfd386345f99363e8175ce1d89cf" have entirely different histories.

3 changed files with 19 additions and 20 deletions

View File

@ -6,6 +6,6 @@ fe(function test() {
export default () => ( export default () => (
<section> <section>
<a href="#" onclick="test()">test</a> <a href="#" click="test()">test</a>
</section> </section>
) )

View File

@ -1,14 +1,14 @@
import { AsyncLocalStorage } from 'async_hooks' let funcs: string[] = []
export const fnStorage = new AsyncLocalStorage<{ fns: string[] }>()
// Designate a function in a .tsx file as frontend // Designate a function in a .tsx file as frontend
export function frontend(code: Function) { export function frontend(code: Function) {
const store = fnStorage.getStore() funcs.push(code.toString())
store?.fns.push(code.toString())
} }
export function feFunctions(): string[] { export function feFunctions(): string[] {
const store = fnStorage.getStore() return funcs
return store?.fns ?? [] }
export function clearFeFunctions() {
funcs.length = 0
} }

View File

@ -6,7 +6,7 @@ import color from 'kleur'
import { transpile } from './utils' import { transpile } from './utils'
import defaultLayout from './layout' import defaultLayout from './layout'
import { feFunctions, fnStorage } from './frontend' import { feFunctions, clearFeFunctions } from './frontend'
const SHOW_HTTP_LOG = true const SHOW_HTTP_LOG = true
const CSS_RESET = await Bun.file(join(import.meta.dir, '/reset.css')).text() const CSS_RESET = await Bun.file(join(import.meta.dir, '/reset.css')).text()
@ -90,7 +90,6 @@ export class Hype<
// serve frontend js // serve frontend js
this.use('*', async (c, next) => { this.use('*', async (c, next) => {
await fnStorage.run({ fns: [] }, async () => {
await next() await next()
const contentType = c.res.headers.get('content-type') const contentType = c.res.headers.get('content-type')
@ -102,10 +101,10 @@ export class Hype<
const res = c.res.clone() const res = c.res.clone()
const html = await res.text() const html = await res.text()
const newHtml = html.replace('</body>', `<script>${fns.join('\n')}</script></body>`) const newHtml = html.replace('</body>', `<script>${fns.join('\n')}</script></body>`)
clearFeFunctions()
c.res = new Response(newHtml, c.res) c.res = new Response(newHtml, c.res)
}) })
})
// css reset // css reset
this.get('/css/reset.css', async c => new Response(CSS_RESET, { headers: { 'Content-Type': 'text/css' } })) this.get('/css/reset.css', async c => new Response(CSS_RESET, { headers: { 'Content-Type': 'text/css' } }))