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.
refactor: simplify
render
module (#185)
* refactor(render): fmt error message * fix issues and complete update * fix lint issue
- Loading branch information
1 parent
b6f347c
commit dd0aadb
Showing
5 changed files
with
68 additions
and
46 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 |
---|---|---|
@@ -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' |
This file was deleted.
Oops, something went wrong.
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