forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwind.ts
46 lines (41 loc) · 1.35 KB
/
twind.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
import { virtualSheet } from "twind/sheets";
import { Plugin } from "../server.ts";
import { Options, setup, STYLE_ELEMENT_ID } from "./twind/shared.ts";
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": main },
async renderAsync(ctx) {
sheet.reset(undefined);
await ctx.renderAsync();
const cssTexts = [...sheet.target];
const snapshot = sheet.reset();
const precedences = snapshot[1] as number[];
const cssText = cssTexts.map((cssText, i) =>
`${cssText}/*${precedences[i].toString(36)}*/`
).join("\n");
const mappings: (string | [string, string])[] = [];
for (
const [key, value] of (snapshot[3] as Map<string, string>).entries()
) {
if (key === value) {
mappings.push(key);
} else {
mappings.push([key, value]);
}
}
return {
scripts: [{ entrypoint: "main", state: mappings }],
styles: [{ cssText, id: STYLE_ELEMENT_ID }],
};
},
};
}