Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
refactor: simplify render module (#185)
Browse files Browse the repository at this point in the history
* refactor(render): fmt error message

* fix issues and complete update

* fix lint issue
  • Loading branch information
boywithkeyboard authored Aug 20, 2023
1 parent b6f347c commit dd0aadb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 46 deletions.
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type { Extension } from './extensions.ts'
export { default as jwt } from './jwt.ts'
export { LocationData } from './location_data.ts'
export { otp } from './otp.ts'
export { h, Renderer } from './render.ts'
export { sendMail } from './send_mail.ts'
export { Store } from './store.ts'

Expand Down
58 changes: 58 additions & 0 deletions render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license.
import { brightYellow, gray } from 'https://deno.land/std@0.198.0/fmt/colors.ts'
import {
defineConfig,
extract,
install,
} from 'https://esm.sh/@twind/core@1.1.3'
import presetAutoPrefix from 'https://esm.sh/@twind/preset-autoprefix@1.0.7'
import presetTailwind from 'https://esm.sh/@twind/preset-tailwind@1.1.4'
import { default as renderToString } from 'https://esm.sh/preact-render-to-string@6.1.0?deps=preact@10.17.1&target=es2022'
import { VNode } from 'https://esm.sh/preact@10.17.1?target=es2022'
import { Context } from './mod.ts'

export function render(c: Context, Component: VNode) {
const htmlString = renderToString(Component)

try {
const { html, css } = extract(htmlString) // twind throws error when trying to extract if it is not installed

c.res.body = `<style>${css}</style><body>${html}</body>`
} catch (_err) {
if (c.dev) {
console.warn(
gray(
`${
brightYellow('warning')
} - twind is not installed, thus styles might not be applied`,
),
)
}

c.res.body = htmlString
}

c.res.header('content-type', 'text/html; charset=utf-8')
}

export class Renderer {
render

constructor(options?: Parameters<typeof defineConfig>[0]) {
if (options) {
options.presets = options.presets
? [presetAutoPrefix(), presetTailwind(), ...options.presets]
: [presetAutoPrefix(), presetTailwind()]
}

install(defineConfig(
options ?? {
presets: [presetAutoPrefix(), presetTailwind()],
},
))

this.render = render
}
}

export { h } from 'https://esm.sh/preact@10.17.1?target=es2022'
23 changes: 0 additions & 23 deletions render.tsx

This file was deleted.

7 changes: 1 addition & 6 deletions test/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@ export {
assertEquals,
assertInstanceOf,
} from 'https://deno.land/std@0.198.0/assert/mod.ts'
export { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts'
export { defineConfig } from 'https://esm.sh/@twind/core@1.1.3'
import presetAutoPrefix from 'https://esm.sh/@twind/preset-autoprefix@1.0.7'
import presetTailwind from 'https://esm.sh/@twind/preset-tailwind@1.1.4'
export { presetAutoPrefix, presetTailwind }
export { DOMParser } from 'https://deno.land/x/deno_dom@v0.1.38/deno-dom-wasm.ts'
export { install } from 'https://esm.sh/@twind/core@1.1.3'
export { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts'
25 changes: 8 additions & 17 deletions test/render.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license.
/** @jsx h */

import { h, render } from '../render.tsx'
import cheetah from '../mod.ts'
import {
assert,
assertEquals,
defineConfig,
DOMParser,
install,
presetAutoPrefix,
presetTailwind,
} from './deps.ts'
import cheetah, { h, Renderer } from '../mod.ts'
import { assert, assertEquals, DOMParser } from './deps.ts'

Deno.test('render', async () => {
const app = new cheetah()
install(defineConfig({
presets: [presetAutoPrefix(), presetTailwind()],
}))

const { render } = new Renderer()

function Styled() {
return (
Expand All @@ -27,12 +16,14 @@ Deno.test('render', async () => {
)
}

app.get('/a', (c) => render(c, Styled))
app.get('/a', (c) => render(c, <Styled />))

const a = await app.fetch(new Request('http://localhost/a'))

const tx = await a.text()

const document = new DOMParser().parseFromString(
await a.text(),
tx,
'text/html',
)

Expand Down

0 comments on commit dd0aadb

Please sign in to comment.