diff --git a/example/src/css/main.css b/example/src/css/main.css index 7548b35..62bcd77 100644 --- a/example/src/css/main.css +++ b/example/src/css/main.css @@ -33,4 +33,8 @@ h1 { 100% { background-position: 100% 50%; } +} + +ul { + list-style-type: none; } \ No newline at end of file diff --git a/example/src/pages/index.tsx b/example/src/pages/index.tsx index 44a7cf7..8fd45e3 100644 --- a/example/src/pages/index.tsx +++ b/example/src/pages/index.tsx @@ -4,6 +4,10 @@ export default () => (

Hello, world!

Welcome to hype!

If you're seeing this, something definitely went right.

- About This Website + + ) diff --git a/example/src/pages/js-test.tsx b/example/src/pages/js-test.tsx new file mode 100644 index 0000000..d9e06c1 --- /dev/null +++ b/example/src/pages/js-test.tsx @@ -0,0 +1,11 @@ +import { frontend as fe } from 'hype' + +fe(function test() { + alert('ding dong') +}) + +export default () => ( +
+ test +
+) \ No newline at end of file diff --git a/src/frontend.ts b/src/frontend.ts new file mode 100644 index 0000000..3820659 --- /dev/null +++ b/src/frontend.ts @@ -0,0 +1,10 @@ +let funcs: string[] = [] + +// Designate a function in a .tsx file as frontend +export function frontend(code: Function) { + funcs.push(code.toString()) +} + +export function feFunctions(): string[] { + return funcs +} \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index ed6280b..2ac73f6 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,12 +6,14 @@ import color from 'kleur' import { transpile } from './utils' import defaultLayout from './layout' +import { feFunctions } from './frontend' const SHOW_HTTP_LOG = true const CSS_RESET = await Bun.file(join(import.meta.dir, '/reset.css')).text() const PICO_CSS = await Bun.file(join(import.meta.dir, '/pico.css')).text() export * from './utils' +export { frontend } from './frontend' export type HypeProps = { pico?: boolean @@ -76,14 +78,33 @@ export class Hype< this.use('*', async (c, next) => { await next() - if (c.res.headers.get('content-type')?.includes('text/html')) { - const html = await c.res.text() - const formatted = formatHTML(html) - c.res = new Response(formatted, c.res) - } + const contentType = c.res.headers.get('content-type') + if (!contentType?.includes('text/html')) return + + const res = c.res.clone() + const html = await res.text() + const formatted = formatHTML(html) + c.res = new Response(formatted, c.res) }) } + // serve frontend js + this.use('*', async (c, next) => { + await next() + + const contentType = c.res.headers.get('content-type') + if (!contentType?.includes('text/html')) return + + const fns = feFunctions() + if (!fns.length) return + + const res = c.res.clone() + const html = await res.text() + const newHtml = html.replace('', ``) + + c.res = new Response(newHtml, c.res) + }) + // css reset this.get('/css/reset.css', async c => new Response(CSS_RESET, { headers: { 'Content-Type': 'text/css' } }))