@tschk/crepus-moonshine 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/package.json +53 -0
- package/src/framework.ts +101 -0
- package/src/index.ts +31 -0
- package/src/render.ts +273 -0
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# `@tschk/crepus-moonshine`
|
|
2
|
+
|
|
3
|
+
`.crepus` source → Crepuscularity View IR → React. Parsing is done by the Rust
|
|
4
|
+
parser compiled to WASM (`@tschk/crepuscularity-wasm`); it is never reimplemented here.
|
|
5
|
+
Solid renders the same IR through its own adapter — no shared vnode.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { parseCrepus, renderCrepusIr } from "@tschk/crepus-moonshine";
|
|
9
|
+
import { renderCrepusIrSolid } from "@tschk/moonshine-solid";
|
|
10
|
+
|
|
11
|
+
const ir = parseCrepus(source, { name: "Ada" });
|
|
12
|
+
const element = renderCrepusIr(ir);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The package has a single export (`.`). IR types (`ViewIr`, `ViewNode`,
|
|
16
|
+
`ViewStyle`, `StackAxis`, `PickerOption`, `TabItem`) come from
|
|
17
|
+
`@tschk/crepuscularity-wasm` and are re-exported here.
|
|
18
|
+
|
|
19
|
+
Template class tokens travel on `style.classes` and are emitted verbatim as
|
|
20
|
+
`className` / `class`, so UnoCSS or Tailwind styles the output. The lowered
|
|
21
|
+
`style` hints exist for native targets and are deliberately not converted to
|
|
22
|
+
inline CSS.
|
|
23
|
+
|
|
24
|
+
Also re-exports signals from `@tschk/moonshine` and `createApp` / `useSignal`
|
|
25
|
+
from `@tschk/moonshine-react` so the CLI emit can depend on one package.
|
|
26
|
+
|
|
27
|
+
In this monorepo the dependency is `"@tschk/moonshine": "workspace:*"`.
|
|
28
|
+
Out-of-tree, point it at a published version or `file:../core`.
|
|
29
|
+
|
|
30
|
+
See [DESIGN.md](../../DESIGN.md).
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tschk/crepus-moonshine",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Bridge: .crepus (parsed by the Rust parser via WASM) to moonshine React elements",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./src/index.ts",
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./src/index.ts"
|
|
11
|
+
},
|
|
12
|
+
"peerDependenciesMeta": {
|
|
13
|
+
"react": {
|
|
14
|
+
"optional": true
|
|
15
|
+
},
|
|
16
|
+
"react-dom": {
|
|
17
|
+
"optional": true
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "bun test",
|
|
22
|
+
"typecheck": "bunx tsc --noEmit",
|
|
23
|
+
"format": "bunx prettier --check .",
|
|
24
|
+
"lint": "bunx oxlint ."
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@tschk/crepuscularity-wasm": "^0.1.0",
|
|
28
|
+
"@tschk/moonshine": "^0.3.1",
|
|
29
|
+
"@tschk/moonshine-framework": "^0.3.1"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=18",
|
|
33
|
+
"react-dom": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/bun": "1.3.14",
|
|
37
|
+
"@types/react": "^19.1.0",
|
|
38
|
+
"@types/react-dom": "^19.1.0",
|
|
39
|
+
"react": "19.1.0",
|
|
40
|
+
"react-dom": "19.1.0",
|
|
41
|
+
"typescript": "7.0.2"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"src",
|
|
45
|
+
"README.md"
|
|
46
|
+
],
|
|
47
|
+
"keywords": [
|
|
48
|
+
"crepuscularity",
|
|
49
|
+
"moonshine",
|
|
50
|
+
"react",
|
|
51
|
+
"view-ir"
|
|
52
|
+
]
|
|
53
|
+
}
|
package/src/framework.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createElement, type ReactElement } from "react";
|
|
2
|
+
import { renderToReadableStream, renderToStaticMarkup } from "react-dom/server";
|
|
3
|
+
import type { RenderContext, Renderer } from "@tschk/moonshine-framework";
|
|
4
|
+
import {
|
|
5
|
+
parseCrepus,
|
|
6
|
+
IR_VERSION,
|
|
7
|
+
type ViewIr,
|
|
8
|
+
} from "@tschk/crepuscularity-wasm";
|
|
9
|
+
import { renderCrepusIr } from "./render";
|
|
10
|
+
|
|
11
|
+
const EMPTY_IR: ViewIr = { version: IR_VERSION, root: [] };
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Route data may be `.crepus` source or already-parsed View IR. Source is
|
|
15
|
+
* parsed by the Rust parser via WASM.
|
|
16
|
+
*/
|
|
17
|
+
function getCrepusIr(context: RenderContext): ViewIr {
|
|
18
|
+
const data = context.data;
|
|
19
|
+
if (typeof data === "string") {
|
|
20
|
+
return data.trim().length > 0 ? parseCrepus(data) : EMPTY_IR;
|
|
21
|
+
}
|
|
22
|
+
if (
|
|
23
|
+
data &&
|
|
24
|
+
typeof data === "object" &&
|
|
25
|
+
"root" in data &&
|
|
26
|
+
Array.isArray((data as ViewIr).root)
|
|
27
|
+
) {
|
|
28
|
+
return data as ViewIr;
|
|
29
|
+
}
|
|
30
|
+
return EMPTY_IR;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function buildRoot(context: RenderContext, body: ReactElement): ReactElement {
|
|
34
|
+
const preloads = context.route.clientEntries.map((href) =>
|
|
35
|
+
createElement("link", { rel: "modulepreload", href, key: href }),
|
|
36
|
+
);
|
|
37
|
+
const scripts = context.route.clientEntries.map((href) =>
|
|
38
|
+
createElement("script", { type: "module", src: href, key: href }),
|
|
39
|
+
);
|
|
40
|
+
return createElement(
|
|
41
|
+
"html",
|
|
42
|
+
null,
|
|
43
|
+
createElement("head", null, ...preloads, ...scripts),
|
|
44
|
+
createElement(
|
|
45
|
+
"body",
|
|
46
|
+
{
|
|
47
|
+
"data-moonshine-mode": context.route.mode,
|
|
48
|
+
"data-moonshine-app": context.route.id,
|
|
49
|
+
},
|
|
50
|
+
body,
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function renderReactToStringAsync(
|
|
56
|
+
element: ReactElement,
|
|
57
|
+
signal?: AbortSignal,
|
|
58
|
+
): Promise<string> {
|
|
59
|
+
const stream = await renderToReadableStream(element, { signal });
|
|
60
|
+
await stream.allReady;
|
|
61
|
+
const res = new Response(stream);
|
|
62
|
+
return res.text();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const crepusRenderer: Renderer = {
|
|
66
|
+
name: "crepus",
|
|
67
|
+
async prerender(context) {
|
|
68
|
+
if (context.route.mode === "api") return JSON.stringify(context.data);
|
|
69
|
+
if (context.route.mode === "spa") {
|
|
70
|
+
const body = createElement("div", {
|
|
71
|
+
id: "moonshine-app",
|
|
72
|
+
"data-moonshine-app": context.route.id,
|
|
73
|
+
});
|
|
74
|
+
return `<!DOCTYPE html>${renderToStaticMarkup(buildRoot(context, body))}`;
|
|
75
|
+
}
|
|
76
|
+
const body = renderCrepusIr(getCrepusIr(context));
|
|
77
|
+
return `<!DOCTYPE html>${renderToStaticMarkup(buildRoot(context, body))}`;
|
|
78
|
+
},
|
|
79
|
+
async render(context) {
|
|
80
|
+
if (context.route.mode === "api") {
|
|
81
|
+
return new Response(JSON.stringify(context.data), {
|
|
82
|
+
headers: { "content-type": "application/json; charset=utf-8" },
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (context.route.mode === "spa") {
|
|
86
|
+
const body = createElement("div", {
|
|
87
|
+
id: "moonshine-app",
|
|
88
|
+
"data-moonshine-app": context.route.id,
|
|
89
|
+
});
|
|
90
|
+
const html = `<!DOCTYPE html>${renderToStaticMarkup(buildRoot(context, body))}`;
|
|
91
|
+
return new Response(html, {
|
|
92
|
+
headers: { "content-type": "text/html; charset=utf-8" },
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
const body = renderCrepusIr(getCrepusIr(context));
|
|
96
|
+
const html = `<!DOCTYPE html>${await renderReactToStringAsync(buildRoot(context, body), context.signal)}`;
|
|
97
|
+
return new Response(html, {
|
|
98
|
+
headers: { "content-type": "text/html; charset=utf-8" },
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** The `.crepus` parser is the Rust one, compiled to WASM — never reimplemented here. */
|
|
2
|
+
export { parseCrepus, IR_VERSION } from "@tschk/crepuscularity-wasm";
|
|
3
|
+
export type {
|
|
4
|
+
CrepusContext,
|
|
5
|
+
PickerOption,
|
|
6
|
+
StackAxis,
|
|
7
|
+
TabItem,
|
|
8
|
+
ViewIr,
|
|
9
|
+
ViewNode,
|
|
10
|
+
ViewNodeKind,
|
|
11
|
+
ViewStyle,
|
|
12
|
+
} from "@tschk/crepuscularity-wasm";
|
|
13
|
+
|
|
14
|
+
/** Document alias kept for consumers that predate the WASM parser. */
|
|
15
|
+
export type { ViewIr as CrepusIr } from "@tschk/crepuscularity-wasm";
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
renderCrepusIr,
|
|
19
|
+
renderCrepusNode,
|
|
20
|
+
type CrepusNode,
|
|
21
|
+
type RenderCrepusOptions,
|
|
22
|
+
} from "./render";
|
|
23
|
+
|
|
24
|
+
// Re-export so CLI emit can depend on one package.
|
|
25
|
+
export { createSignal, createMemo, createStore } from "@tschk/moonshine";
|
|
26
|
+
export {
|
|
27
|
+
createApp,
|
|
28
|
+
createMoonshineApp,
|
|
29
|
+
useSignal,
|
|
30
|
+
} from "@tschk/moonshine/react";
|
|
31
|
+
export { crepusRenderer } from "./framework";
|
package/src/render.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { createElement, type ReactElement, type ReactNode } from "react";
|
|
2
|
+
import type { TabItem, ViewIr, ViewNode } from "@tschk/crepuscularity-wasm";
|
|
3
|
+
|
|
4
|
+
export type CrepusNode = ViewNode;
|
|
5
|
+
|
|
6
|
+
export interface RenderCrepusOptions {
|
|
7
|
+
/** Wrapper element for the document root. */
|
|
8
|
+
rootTag?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Class tokens are carried verbatim from the template, so UnoCSS/Tailwind
|
|
13
|
+
* styles the output. The lowered `style` hints exist for native targets and are
|
|
14
|
+
* deliberately not converted to inline CSS here.
|
|
15
|
+
*/
|
|
16
|
+
function classNameOf(node: ViewNode): string | undefined {
|
|
17
|
+
const classes = (node as { style?: { classes?: string[] } }).style?.classes;
|
|
18
|
+
if (!classes || classes.length === 0) return undefined;
|
|
19
|
+
const joined = classes.filter(Boolean).join(" ");
|
|
20
|
+
return joined.length > 0 ? joined : undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function renderChildren(children: ViewNode[], keyPrefix: string): ReactNode[] {
|
|
24
|
+
return children.map((child, i) =>
|
|
25
|
+
renderCrepusNode(child, `${keyPrefix}.${i}`),
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function renderCrepusNode(node: ViewNode, key: string): ReactNode {
|
|
30
|
+
const className = classNameOf(node);
|
|
31
|
+
|
|
32
|
+
switch (node.kind) {
|
|
33
|
+
case "text":
|
|
34
|
+
return createElement("span", { key, className }, node.content);
|
|
35
|
+
|
|
36
|
+
case "link":
|
|
37
|
+
return createElement(
|
|
38
|
+
"a",
|
|
39
|
+
{
|
|
40
|
+
key,
|
|
41
|
+
className,
|
|
42
|
+
href: node.href,
|
|
43
|
+
target: node.target,
|
|
44
|
+
rel: node.rel,
|
|
45
|
+
},
|
|
46
|
+
...renderChildren(node.children, key),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
case "stack":
|
|
50
|
+
case "scroll":
|
|
51
|
+
return createElement(
|
|
52
|
+
"div",
|
|
53
|
+
{ key, className },
|
|
54
|
+
...renderChildren(node.children, key),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
case "dropzone":
|
|
58
|
+
return createElement(
|
|
59
|
+
"div",
|
|
60
|
+
{ key, className, "aria-label": node.label },
|
|
61
|
+
...renderChildren(node.children, key),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
case "list":
|
|
65
|
+
return createElement(
|
|
66
|
+
node.ordered ? "ol" : "ul",
|
|
67
|
+
{ key, className },
|
|
68
|
+
...renderChildren(node.children, key),
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
case "listItem":
|
|
72
|
+
return createElement(
|
|
73
|
+
"li",
|
|
74
|
+
{ key, className },
|
|
75
|
+
...renderChildren(node.children, key),
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
case "button":
|
|
79
|
+
return createElement(
|
|
80
|
+
"button",
|
|
81
|
+
{ key, className, type: "button" },
|
|
82
|
+
node.label,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
case "badge":
|
|
86
|
+
return createElement(
|
|
87
|
+
"span",
|
|
88
|
+
{ key, className, "data-tone": node.tone },
|
|
89
|
+
node.label,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
case "divider":
|
|
93
|
+
return createElement("hr", { key, className });
|
|
94
|
+
|
|
95
|
+
case "spacer":
|
|
96
|
+
return createElement("div", { key, className, "aria-hidden": true });
|
|
97
|
+
|
|
98
|
+
case "image":
|
|
99
|
+
return createElement("img", {
|
|
100
|
+
key,
|
|
101
|
+
className,
|
|
102
|
+
src: node.src,
|
|
103
|
+
alt: node.alt ?? "",
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
case "webView":
|
|
107
|
+
return createElement("iframe", { key, className, src: node.src });
|
|
108
|
+
|
|
109
|
+
case "toggle":
|
|
110
|
+
return createElement(
|
|
111
|
+
"button",
|
|
112
|
+
{
|
|
113
|
+
key,
|
|
114
|
+
className,
|
|
115
|
+
type: "button",
|
|
116
|
+
role: "switch",
|
|
117
|
+
"aria-checked": node.checked,
|
|
118
|
+
},
|
|
119
|
+
node.label,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
case "checkbox":
|
|
123
|
+
return createElement(
|
|
124
|
+
"label",
|
|
125
|
+
{ key, className },
|
|
126
|
+
createElement("input", {
|
|
127
|
+
key: `${key}.input`,
|
|
128
|
+
type: "checkbox",
|
|
129
|
+
defaultChecked: node.checked,
|
|
130
|
+
name: node.bind,
|
|
131
|
+
}),
|
|
132
|
+
node.label,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
case "slider":
|
|
136
|
+
return createElement("input", {
|
|
137
|
+
key,
|
|
138
|
+
className,
|
|
139
|
+
type: "range",
|
|
140
|
+
defaultValue: node.value,
|
|
141
|
+
min: node.min,
|
|
142
|
+
max: node.max,
|
|
143
|
+
step: node.step,
|
|
144
|
+
name: node.bind,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
case "progress":
|
|
148
|
+
return createElement("progress", {
|
|
149
|
+
key,
|
|
150
|
+
className,
|
|
151
|
+
value: node.value,
|
|
152
|
+
max: node.max,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
case "meter":
|
|
156
|
+
return createElement("meter", {
|
|
157
|
+
key,
|
|
158
|
+
className,
|
|
159
|
+
value: node.value,
|
|
160
|
+
min: node.min,
|
|
161
|
+
max: node.max,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
case "input":
|
|
165
|
+
return createElement(node.multiline ? "textarea" : "input", {
|
|
166
|
+
key,
|
|
167
|
+
className,
|
|
168
|
+
type: node.multiline ? undefined : node.secure ? "password" : "text",
|
|
169
|
+
placeholder: node.placeholder,
|
|
170
|
+
name: node.bind,
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
case "picker":
|
|
174
|
+
return createElement(
|
|
175
|
+
"select",
|
|
176
|
+
{ key, className, name: node.bind },
|
|
177
|
+
...node.options.map((option, i) =>
|
|
178
|
+
createElement(
|
|
179
|
+
"option",
|
|
180
|
+
{ key: `${key}.${i}`, value: option.value },
|
|
181
|
+
option.label,
|
|
182
|
+
),
|
|
183
|
+
),
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
case "filePicker":
|
|
187
|
+
return createElement(
|
|
188
|
+
"label",
|
|
189
|
+
{ key, className },
|
|
190
|
+
createElement("input", {
|
|
191
|
+
key: `${key}.input`,
|
|
192
|
+
type: "file",
|
|
193
|
+
accept: node.accept?.join(","),
|
|
194
|
+
multiple: node.multiple,
|
|
195
|
+
}),
|
|
196
|
+
node.label,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
case "slotRotate":
|
|
200
|
+
return createElement(
|
|
201
|
+
"span",
|
|
202
|
+
{
|
|
203
|
+
key,
|
|
204
|
+
className,
|
|
205
|
+
"data-crepus-slot-rotate": node.phrases.join("|"),
|
|
206
|
+
"data-interval-ms": node.intervalMs,
|
|
207
|
+
},
|
|
208
|
+
node.phrases[0] ?? "",
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
case "tabs":
|
|
212
|
+
return createElement(
|
|
213
|
+
"div",
|
|
214
|
+
{ key, className },
|
|
215
|
+
createElement(
|
|
216
|
+
"div",
|
|
217
|
+
{ key: `${key}.tablist`, role: "tablist" },
|
|
218
|
+
...node.tabs.map((tab: TabItem, i: number) =>
|
|
219
|
+
createElement(
|
|
220
|
+
"button",
|
|
221
|
+
{ key: `${key}.tab.${i}`, type: "button", role: "tab" },
|
|
222
|
+
tab.label,
|
|
223
|
+
),
|
|
224
|
+
),
|
|
225
|
+
),
|
|
226
|
+
...node.tabs.map((tab: TabItem, i: number) =>
|
|
227
|
+
createElement(
|
|
228
|
+
"div",
|
|
229
|
+
{ key: `${key}.panel.${i}`, role: "tabpanel" },
|
|
230
|
+
...renderChildren(tab.children, `${key}.panel.${i}`),
|
|
231
|
+
),
|
|
232
|
+
),
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
// Control flow the parser could not resolve statically. The chosen branch is
|
|
236
|
+
// rendered and the source expression kept as a data attribute.
|
|
237
|
+
case "if":
|
|
238
|
+
return createElement(
|
|
239
|
+
"div",
|
|
240
|
+
{ key, className, "data-crepus-if": node.condition },
|
|
241
|
+
...renderChildren(node.thenChildren, key),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
case "forEach":
|
|
245
|
+
return createElement(
|
|
246
|
+
"div",
|
|
247
|
+
{
|
|
248
|
+
key,
|
|
249
|
+
className,
|
|
250
|
+
"data-crepus-for-each": node.bind,
|
|
251
|
+
"data-crepus-item": node.itemName,
|
|
252
|
+
},
|
|
253
|
+
...renderChildren(node.itemBody, key),
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
default:
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function renderCrepusIr(
|
|
262
|
+
ir: ViewIr,
|
|
263
|
+
options: RenderCrepusOptions = {},
|
|
264
|
+
): ReactElement {
|
|
265
|
+
const children = (ir.root ?? []).map((node, i) =>
|
|
266
|
+
renderCrepusNode(node, `k${i}`),
|
|
267
|
+
);
|
|
268
|
+
return createElement(
|
|
269
|
+
options.rootTag ?? "div",
|
|
270
|
+
{ "data-crepus-root": "true", "data-crepus-ir-version": ir.version },
|
|
271
|
+
...children,
|
|
272
|
+
);
|
|
273
|
+
}
|