-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.ts
31 lines (30 loc) · 929 Bytes
/
plugin.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
import { Plugin } from "$fresh/server.ts";
import { BaseTheme, stringify, TwindConfig } from "@twind/core";
import { setup, STYLE_ELEMENT_ID } from "./shared.ts";
export default function freshwind<Theme extends BaseTheme>(
config: TwindConfig<Theme>,
configURL: string,
): Plugin {
const instance = setup(config);
const main = `data:application/javascript,import hydrate from "${
new URL("./main.ts", import.meta.url).href
}";
import config from "${configURL}";
export default function() { hydrate(config); }`;
return {
name: "freshwind",
entrypoints: { "main": main },
render(ctx) {
const res = ctx.render();
const cssText = stringify(instance.target);
const scripts = [];
if (res.requiresHydration) {
scripts.push({ entrypoint: "main", state: [] });
}
return {
scripts,
styles: [{ cssText, id: STYLE_ELEMENT_ID }],
};
},
};
}