From 5bb67aff1f16e666614f214031a941161096310e Mon Sep 17 00:00:00 2001 From: Thomas Crescenzi Date: Fri, 24 Feb 2023 12:32:29 -0500 Subject: [PATCH] feat: add twindv1 plugin (#946) --- plugins/twindv1.ts | 31 +++++++++++++++++++++++++ plugins/twindv1/main.ts | 5 ++++ plugins/twindv1/shared.ts | 48 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 plugins/twindv1.ts create mode 100644 plugins/twindv1/main.ts create mode 100644 plugins/twindv1/shared.ts diff --git a/plugins/twindv1.ts b/plugins/twindv1.ts new file mode 100644 index 00000000..848e9773 --- /dev/null +++ b/plugins/twindv1.ts @@ -0,0 +1,31 @@ +import { stringify, virtual } from "https://esm.sh/@twind/core@1.1.3"; +import { Plugin } from "../server.ts"; + +import { Options, setup, STYLE_ELEMENT_ID } from "./twindv1/shared.ts"; +export type { Options }; + +export default function twindv1(options: Options): Plugin { + const sheet = virtual(true); + setup(options, sheet); + const main = `data:application/javascript,import hydrate from "${ + new URL("./twindv1/main.ts", import.meta.url).href + }"; +import options from "${options.selfURL}"; +export default function(state) { hydrate(options, state); }`; + return { + name: "twind", + entrypoints: { "main": main }, + render(ctx) { + const res = ctx.render(); + const cssText = stringify(sheet.target); + const scripts = []; + if (res.requiresHydration) { + scripts.push({ entrypoint: "main", state: [] }); + } + return { + scripts, + styles: [{ cssText, id: STYLE_ELEMENT_ID }], + }; + }, + }; +} diff --git a/plugins/twindv1/main.ts b/plugins/twindv1/main.ts new file mode 100644 index 00000000..9b6a1902 --- /dev/null +++ b/plugins/twindv1/main.ts @@ -0,0 +1,5 @@ +import { getSheet, setup, TwindConfig } from "https://esm.sh/@twind/core@1.1.3"; + +export default function hydrate(options: TwindConfig) { + setup(options, getSheet()); +} diff --git a/plugins/twindv1/shared.ts b/plugins/twindv1/shared.ts new file mode 100644 index 00000000..5974b34c --- /dev/null +++ b/plugins/twindv1/shared.ts @@ -0,0 +1,48 @@ +import { JSX, options as preactOptions, VNode } from "preact"; +import { + setup as twSetup, + Sheet, + tw, + TwindConfig, +} from "https://esm.sh/@twind/core@1.1.3"; + +export const STYLE_ELEMENT_ID = "__FRSH_TWIND"; + +export interface Options extends TwindConfig { + /** The import.meta.url of the module defining these options. */ + selfURL: string; +} + +declare module "preact" { + namespace JSX { + interface DOMAttributes { + class?: string; + className?: string; + } + } +} + +export function setup({ selfURL: _selfURL, ...config }: Options, sheet: Sheet) { + twSetup(config, sheet); + + const originalHook = preactOptions.vnode; + // deno-lint-ignore no-explicit-any + preactOptions.vnode = (vnode: VNode>) => { + if (typeof vnode.type === "string" && typeof vnode.props === "object") { + const { props } = vnode; + const classes: string[] = []; + if (props.class) { + classes.push(tw(props.class)); + props.class = undefined; + } + if (props.className) { + classes.push(tw(props.className)); + } + if (classes.length) { + props.class = classes.join(" "); + } + } + + originalHook?.(vnode); + }; +}