Files
sweet/plugins/twind/main.ts
T
Luca CasonatoandElias Sjögreen 1b3c9f2569 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>
2022-09-07 00:44:46 +02:00

22 lines
761 B
TypeScript

import { Sheet } from "twind";
import { Options, setup, STYLE_ELEMENT_ID } from "./shared.ts";
type State = [string[], [string, string][]];
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[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];
const target = el.sheet!;
const sheet: Sheet = {
target,
insert: (rule, index) => target.insertRule(rule, index),
init: (cb) => cb(sheetState.shift()),
};
setup(options, sheet);
}