@tschk/crepus-moonshine 0.4.4 → 0.4.80
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/package.json +3 -3
- package/src/framework.ts +31 -6
- package/src/render.ts +6 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tschk/crepus-moonshine",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.80",
|
|
4
4
|
"description": "Bridge: .crepus (parsed by the Rust parser via WASM) to moonshine React elements",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"type": "module",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@tschk/crepuscularity-wasm": "^0.1.3",
|
|
28
|
-
"@tschk/moonshine": "^0.4.
|
|
29
|
-
"@tschk/moonshine-framework": "^0.4.
|
|
28
|
+
"@tschk/moonshine": "^0.4.80",
|
|
29
|
+
"@tschk/moonshine-framework": "^0.4.80"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"react": ">=18",
|
package/src/framework.ts
CHANGED
|
@@ -30,6 +30,10 @@ function getCrepusIr(context: RenderContext): ViewIr {
|
|
|
30
30
|
return EMPTY_IR;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/** Document shell matching crepuscularity CLI `assets/web/index.html`. */
|
|
34
|
+
const DOCUMENT_RESET_CSS =
|
|
35
|
+
"html{scroll-behavior:smooth}html,body{margin:0}body{min-height:100vh}";
|
|
36
|
+
|
|
33
37
|
function buildRoot(context: RenderContext, body: ReactElement): ReactElement {
|
|
34
38
|
const preloads = context.route.clientEntries.map((href) =>
|
|
35
39
|
createElement("link", { rel: "modulepreload", href, key: href }),
|
|
@@ -39,8 +43,23 @@ function buildRoot(context: RenderContext, body: ReactElement): ReactElement {
|
|
|
39
43
|
);
|
|
40
44
|
return createElement(
|
|
41
45
|
"html",
|
|
42
|
-
|
|
43
|
-
createElement(
|
|
46
|
+
{ lang: "en" },
|
|
47
|
+
createElement(
|
|
48
|
+
"head",
|
|
49
|
+
null,
|
|
50
|
+
createElement("meta", { charSet: "utf-8", key: "charset" }),
|
|
51
|
+
createElement("meta", {
|
|
52
|
+
name: "viewport",
|
|
53
|
+
content: "width=device-width, initial-scale=1",
|
|
54
|
+
key: "viewport",
|
|
55
|
+
}),
|
|
56
|
+
createElement("style", {
|
|
57
|
+
key: "document-reset",
|
|
58
|
+
dangerouslySetInnerHTML: { __html: DOCUMENT_RESET_CSS },
|
|
59
|
+
}),
|
|
60
|
+
...preloads,
|
|
61
|
+
...scripts,
|
|
62
|
+
),
|
|
44
63
|
createElement(
|
|
45
64
|
"body",
|
|
46
65
|
{
|
|
@@ -62,6 +81,10 @@ async function renderReactToStringAsync(
|
|
|
62
81
|
return res.text();
|
|
63
82
|
}
|
|
64
83
|
|
|
84
|
+
function documentHtml(markup: string): string {
|
|
85
|
+
return `<!DOCTYPE html>${markup.replaceAll("charSet=", "charset=")}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
65
88
|
export const crepusRenderer: Renderer = {
|
|
66
89
|
name: "crepus",
|
|
67
90
|
async prerender(context) {
|
|
@@ -71,10 +94,10 @@ export const crepusRenderer: Renderer = {
|
|
|
71
94
|
id: "moonshine-app",
|
|
72
95
|
"data-moonshine-app": context.route.id,
|
|
73
96
|
});
|
|
74
|
-
return
|
|
97
|
+
return documentHtml(renderToStaticMarkup(buildRoot(context, body)));
|
|
75
98
|
}
|
|
76
99
|
const body = renderCrepusIr(getCrepusIr(context));
|
|
77
|
-
return
|
|
100
|
+
return documentHtml(renderToStaticMarkup(buildRoot(context, body)));
|
|
78
101
|
},
|
|
79
102
|
async render(context) {
|
|
80
103
|
if (context.route.mode === "api") {
|
|
@@ -87,13 +110,15 @@ export const crepusRenderer: Renderer = {
|
|
|
87
110
|
id: "moonshine-app",
|
|
88
111
|
"data-moonshine-app": context.route.id,
|
|
89
112
|
});
|
|
90
|
-
const html =
|
|
113
|
+
const html = documentHtml(renderToStaticMarkup(buildRoot(context, body)));
|
|
91
114
|
return new Response(html, {
|
|
92
115
|
headers: { "content-type": "text/html; charset=utf-8" },
|
|
93
116
|
});
|
|
94
117
|
}
|
|
95
118
|
const body = renderCrepusIr(getCrepusIr(context));
|
|
96
|
-
const html =
|
|
119
|
+
const html = documentHtml(
|
|
120
|
+
await renderReactToStringAsync(buildRoot(context, body), context.signal),
|
|
121
|
+
);
|
|
97
122
|
return new Response(html, {
|
|
98
123
|
headers: { "content-type": "text/html; charset=utf-8" },
|
|
99
124
|
});
|
package/src/render.ts
CHANGED
|
@@ -282,16 +282,16 @@ type AnyNodeRenderer = (
|
|
|
282
282
|
) => ReactNode;
|
|
283
283
|
|
|
284
284
|
export function renderCrepusNode(node: ViewNode, key: string): ReactNode {
|
|
285
|
+
if (!Object.hasOwn(nodeRenderers, node.kind)) {
|
|
286
|
+
throw new TypeError(`Unsupported View IR node kind: ${node.kind}`);
|
|
287
|
+
}
|
|
285
288
|
const className = classNameOf(node);
|
|
286
289
|
const id = idOf(node);
|
|
287
290
|
|
|
288
291
|
const renderer = nodeRenderers[node.kind] as AnyNodeRenderer;
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
return renderer(node, key, id, className);
|
|
293
|
-
}
|
|
294
|
-
return null;
|
|
292
|
+
// Cast is necessary because TypeScript cannot correlate the discriminated
|
|
293
|
+
// union `node` with the generic function retrieved from `nodeRenderers`.
|
|
294
|
+
return renderer(node, key, id, className);
|
|
295
295
|
}
|
|
296
296
|
|
|
297
297
|
export function renderCrepusIr(
|