fix(plugin/twind): load configuration as module (#705)

This commit loads the Twind configuration as an actual module rather
than evaluating it on the server and then serializing. This allows usage
of unserializable plugin functions.

Co-authored-by: Elias Sjögreen <eliassjogreen1@gmail.com>
This commit is contained in:
Luca Casonato
2022-09-07 00:44:46 +02:00
committed by GitHub
co-authored by Elias Sjögreen
parent 8e9658cae5
commit 1b3c9f2569
7 changed files with 30 additions and 54 deletions
+7 -2
View File
@@ -7,9 +7,14 @@ export type { Options };
export default function twind(options: Options): Plugin {
const sheet = virtualSheet();
setup(options, sheet);
const main = `data:application/javascript,import hydrate from "${
new URL("./twind/main.ts", import.meta.url).href
}";
import options from "${options.selfURL}";
export default function(state) { hydrate(options, state); }`;
return {
name: "twind",
entrypoints: { "main": new URL("./twind/main.ts", import.meta.url).href },
entrypoints: { "main": main },
render(ctx) {
sheet.reset(undefined);
const res = ctx.render();
@@ -28,7 +33,7 @@ export default function twind(options: Options): Plugin {
mappings.push([key, value]);
}
}
const state = [options, precedences, mappings];
const state = [precedences, mappings];
scripts.push({ entrypoint: "main", state });
}
return {
+5 -5
View File
@@ -1,13 +1,13 @@
import { Sheet } from "twind";
import { Options, setup, STYLE_ELEMENT_ID } from "./shared.ts";
type State = [Options, string[], [string, string][]];
type State = [string[], [string, string][]];
export default function hydrate(state: State) {
export default function hydrate(options: Options, state: State) {
const el = document.getElementById(STYLE_ELEMENT_ID) as HTMLStyleElement;
const rules = new Set(el.innerText.split("\n"));
const precedences = state[1];
const mappings = new Map(state[2]
const precedences = state[0];
const mappings = new Map(state[1]
.map((v) => typeof v === "string" ? [v, v] : v));
// deno-lint-ignore no-explicit-any
const sheetState: any[] = [precedences, rules, mappings, true];
@@ -17,5 +17,5 @@ export default function hydrate(state: State) {
insert: (rule, index) => target.insertRule(rule, index),
init: (cb) => cb(sheetState.shift()),
};
setup(state[0], sheet);
setup(options, sheet);
}
+5 -37
View File
@@ -1,40 +1,11 @@
import { JSX, options as preactOptions, VNode } from "preact";
import {
Configuration,
DarkMode,
setup as twSetup,
Sheet,
ThemeConfiguration,
tw,
} from "twind";
import { Configuration, setup as twSetup, Sheet, tw } from "twind";
export const STYLE_ELEMENT_ID = "__FRSH_TWIND";
export interface Options {
/**
* Determines the dark mode strategy (default: `"media"`).
*/
darkMode?: DarkMode;
/**
* The twind theme to use.
*/
theme?: ThemeConfiguration;
/**
* ```js
* {
* ':new-variant': '& .selector',
* }
* ```
*/
variants?: Record<string, string>;
/**
* Configure wether the class names should be emitted "as is", or if they
* should be hashed.
*/
hash?: boolean;
export interface Options extends Omit<Configuration, "mode" | "sheet"> {
/** The import.meta.url of the module defining these options. */
selfURL: string;
}
declare module "preact" {
@@ -48,12 +19,9 @@ declare module "preact" {
export function setup(options: Options, sheet: Sheet) {
const config: Configuration = {
darkMode: options.darkMode,
hash: options.hash,
...options,
mode: "silent",
sheet,
variants: options.variants,
theme: options.theme,
};
twSetup(config);