-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
60 lines (57 loc) · 1.57 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// <reference no-default-lib="true"/>
/// <reference lib="deno.ns" />
/// <reference lib="dom" />
/// <reference lib="esnext" />
import { Configuration, create, TW } from "https://esm.sh/twind@0.16.13";
import { Config } from "./types.ts";
import {
getStyleTagProperties,
VirtualSheet,
virtualSheet,
} from "https://esm.sh/twind@0.16.13/shim/server";
export type TwInfo = {
tw: TW;
sheet: VirtualSheet;
};
export function init({
mode,
preflight,
theme,
plugins,
}: GenerateConfig): TwInfo {
const sheet = virtualSheet();
const { tw } = create({ sheet, mode, preflight, theme, plugins });
sheet.reset();
return {
tw,
sheet,
};
}
export type GenerateConfig = Config & Pick<Configuration, "mode">;
export function generate(docs: string[], { tw, sheet }: TwInfo): string {
for (const html of docs) {
const m = html.match(/[^<>\[\]\(\)|&"'`\.\s]*[^<>\[\]\(\)|&"'`\.\s:]/g);
if (m) {
for (const c of m) {
// See https://github.com/tw-in-js/twind/issues/189
if (
c === "__defineGetter__" || c === "__defineSetter__" ||
c === "__lookupGetter__" || c === "__lookupSetter__" ||
c === "hasOwnProperty" || c === "isPrototypeOf" ||
c === "propertyIsEnumerable" || c === "valueOf" ||
c === "toLocaleString"
) {
continue;
}
try {
tw(c);
} catch (e) {
console.log(`Error: Failed to handle the pattern '${c}'`);
throw e;
}
}
}
}
const { textContent } = getStyleTagProperties(sheet);
return textContent;
}