-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
jsx_preact.ts
109 lines (86 loc) 路 3.05 KB
/
jsx_preact.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
importSource,
isValidElement,
renderToString,
} from "../deps/preact.ts";
import loader from "../core/loaders/module.ts";
import { merge, parseJSX } from "../core/utils.ts";
import { dirname, join, toFileUrl } from "../deps/path.ts";
import type { Data, Engine, Helper, Site } from "../core.ts";
import type { ComponentChildren } from "../deps/preact.ts";
export interface Options {
/** The list of extensions this plugin applies to */
extensions: string[] | {
pages: string[];
components: string[];
};
}
// Default options
export const defaults: Options = {
extensions: [".jsx", ".tsx"],
};
// JSX children type
export type Children = ComponentChildren;
/** Template engine to render JSX files using Preact */
export class PreactJsxEngine implements Engine {
helpers: Record<string, Helper> = {};
basePath: string;
constructor(basePath: string) {
this.basePath = basePath;
}
deleteCache() {}
// deno-lint-ignore no-explicit-any
parseJSX(content: string, data: Data = {}, filename?: string): Promise<any> {
const baseUrl = filename
? toFileUrl(join(this.basePath, dirname(filename)))
: toFileUrl(this.basePath);
const jsxSource = `/** @jsxImportSource ${importSource} */`;
return parseJSX(baseUrl, content, data, jsxSource);
}
async render(content: unknown, data: Data = {}, filename?: string) {
// The content is a string, so we have to convert to a Preact element
if (typeof content === "string") {
content = await this.parseJSX(content, data, filename);
}
// Create the children property
let children = data.content;
// If the children is a string, convert it to a Preact element
if (typeof children === "string") {
const fn = await this.parseJSX(children, data, filename);
children = await fn({ ...data }, this.helpers);
}
const element = typeof content === "object" && isValidElement(content)
? content
: (typeof content === "function"
? await content({ ...data, children }, this.helpers)
: content) as preact.VNode;
if (element && typeof element === "object") {
element.toString = () => renderToString(element);
}
return element;
}
renderSync(content: unknown, data: Data = {}): string {
const element = typeof content === "function"
? content(data, this.helpers)
: content;
if (element && typeof element === "object") {
element.toString = () => renderToString(element);
}
return element;
}
addHelper(name: string, fn: Helper) {
this.helpers[name] = fn;
}
}
/** Register the plugin to support JSX and TSX files */
export default function (userOptions?: Partial<Options>) {
const options = merge(defaults, userOptions);
const extensions = Array.isArray(options.extensions)
? { pages: options.extensions, components: options.extensions }
: options.extensions;
return (site: Site) => {
const engine = new PreactJsxEngine(site.src());
site.loadPages(extensions.pages, loader, engine);
site.loadComponents(extensions.components, loader, engine);
};
}