This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6f347c
commit fe1481b
Showing
2 changed files
with
22 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,35 @@ | ||
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. | ||
/** @jsx h */ | ||
import { default as renderToString } from 'https://esm.sh/preact-render-to-string@6.1.0?deps=preact@10.16.0' | ||
import { h, VNode } from 'https://esm.sh/preact@10.17.0' | ||
import { extract } from 'https://esm.sh/@twind/core@1.1.3' | ||
|
||
import { brightYellow, gray } from 'https://deno.land/std@0.198.0/fmt/colors.ts' | ||
import { extract } from 'https://esm.sh/@twind/core@1.1.3?target=es2022' | ||
import { default as renderToString } from 'https://esm.sh/preact-render-to-string@6.1.0?deps=preact@10.17.0&target=es2022' | ||
import { h, VNode } from 'https://esm.sh/preact@10.17.0?target=es2022' | ||
import { Context } from './mod.ts' | ||
|
||
export function render(c: Context, Component: (() => h.JSX.Element) | VNode) { | ||
const html$ = renderToString( | ||
const htmlString = renderToString( | ||
Component instanceof Function ? <Component /> : Component, | ||
) | ||
|
||
try { | ||
const { html, css } = extract(html$) // twind throws error when trying to extract if it is not installed | ||
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 (e) { | ||
console.warn('twind is not installed, styles might not be applied') | ||
c.res.body = html$ | ||
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 { h } from 'https://esm.sh/preact@10.17.0' | ||
export { h } from 'https://esm.sh/preact@10.17.0?target=es2022' |