From fe1481be14cecaa2d7ad006f5331254e36c66180 Mon Sep 17 00:00:00 2001 From: Samuel Kopp <62482066+boywithkeyboard@users.noreply.github.com> Date: Sun, 20 Aug 2023 01:27:39 +0200 Subject: [PATCH] refactor(render): fmt error message --- mod.ts | 1 + render.tsx | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/mod.ts b/mod.ts index 93c6db3..f119d8e 100644 --- a/mod.ts +++ b/mod.ts @@ -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 { render } from './render.tsx' export { sendMail } from './send_mail.ts' export { Store } from './store.ts' diff --git a/render.tsx b/render.tsx index 741a6c7..29ab5b8 100644 --- a/render.tsx +++ b/render.tsx @@ -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, ) + 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 = `${html}` } 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'