murasaki 0.0.4 → 0.2.0
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 +13 -5
- package/src/components/Link.tsx +25 -0
- package/src/env.ts +14 -3
- package/src/index.ts +4 -0
- package/src/jsx/index.ts +21 -0
- package/src/jsx/jsx-dev-runtime.ts +6 -0
- package/src/jsx/jsx-runtime.ts +10 -0
- package/src/jsx/runtime.ts +298 -0
- package/src/jsx/types.ts +36 -0
- package/src/runtime/render.tsx +194 -24
- package/src/runtime/routes.ts +73 -0
- package/src/types.ts +3 -3
- package/src/runtime/load.ts +0 -39
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "murasaki",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The desktop framework for Next.js developers. Node-powered. WebView-thin. No Rust. No Chromium.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"desktop",
|
|
@@ -30,6 +30,18 @@
|
|
|
30
30
|
".": {
|
|
31
31
|
"types": "./src/index.ts",
|
|
32
32
|
"default": "./src/index.ts"
|
|
33
|
+
},
|
|
34
|
+
"./jsx": {
|
|
35
|
+
"types": "./src/jsx/index.ts",
|
|
36
|
+
"default": "./src/jsx/index.ts"
|
|
37
|
+
},
|
|
38
|
+
"./jsx-runtime": {
|
|
39
|
+
"types": "./src/jsx/jsx-runtime.ts",
|
|
40
|
+
"default": "./src/jsx/jsx-runtime.ts"
|
|
41
|
+
},
|
|
42
|
+
"./jsx-dev-runtime": {
|
|
43
|
+
"types": "./src/jsx/jsx-dev-runtime.ts",
|
|
44
|
+
"default": "./src/jsx/jsx-dev-runtime.ts"
|
|
33
45
|
}
|
|
34
46
|
},
|
|
35
47
|
"files": [
|
|
@@ -47,15 +59,11 @@
|
|
|
47
59
|
},
|
|
48
60
|
"dependencies": {
|
|
49
61
|
"@webviewjs/webview": "^0.3.2",
|
|
50
|
-
"react": "^19.2.7",
|
|
51
|
-
"react-dom": "^19.2.7",
|
|
52
62
|
"tsx": "^4.22.4"
|
|
53
63
|
},
|
|
54
64
|
"devDependencies": {
|
|
55
65
|
"@biomejs/biome": "^2.5.1",
|
|
56
66
|
"@types/node": "^26.0.1",
|
|
57
|
-
"@types/react": "^19.2.17",
|
|
58
|
-
"@types/react-dom": "^19.2.3",
|
|
59
67
|
"typescript": "^6.0.3"
|
|
60
68
|
}
|
|
61
69
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// <Link href="/about">About</Link>
|
|
2
|
+
//
|
|
3
|
+
// Emits a plain <a> tagged with data-murasaki-link. The dev runner injects
|
|
4
|
+
// a tiny script that intercepts clicks on these and switches the visible
|
|
5
|
+
// route block in place — no full reload, no flash.
|
|
6
|
+
|
|
7
|
+
import { jsx } from '../jsx/runtime.ts'
|
|
8
|
+
import type { Child } from '../jsx/types.ts'
|
|
9
|
+
|
|
10
|
+
export type LinkProps = {
|
|
11
|
+
href: string
|
|
12
|
+
children?: Child
|
|
13
|
+
className?: string
|
|
14
|
+
// Pass-through anchor props
|
|
15
|
+
[key: string]: unknown
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function Link({ href, children, ...rest }: LinkProps) {
|
|
19
|
+
return jsx('a', {
|
|
20
|
+
href: `#${href}`,
|
|
21
|
+
'data-murasaki-link': href,
|
|
22
|
+
...rest,
|
|
23
|
+
children,
|
|
24
|
+
})
|
|
25
|
+
}
|
package/src/env.ts
CHANGED
|
@@ -6,9 +6,20 @@ import { fileURLToPath } from 'node:url'
|
|
|
6
6
|
|
|
7
7
|
export const projectRoot = process.cwd()
|
|
8
8
|
export const SRC_DIR = join(projectRoot, 'src')
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
|
|
10
|
+
// App-router convention (new, preferred):
|
|
11
|
+
// src/app/page.tsx → "/"
|
|
12
|
+
// src/app/layout.tsx → root layout (html/head/body)
|
|
13
|
+
// src/app/<sub>/page.tsx → "/<sub>"
|
|
14
|
+
// src/app/globals.css → auto-injected
|
|
15
|
+
export const APP_DIR = join(SRC_DIR, 'app')
|
|
16
|
+
export const APP_GLOBALS_CSS = join(APP_DIR, 'globals.css')
|
|
17
|
+
|
|
18
|
+
// Legacy single-page convention (still supported):
|
|
19
|
+
// src/app.tsx + src/layout.tsx + src/globals.css
|
|
20
|
+
export const LEGACY_APP_PATH = join(SRC_DIR, 'app.tsx')
|
|
21
|
+
export const LEGACY_LAYOUT_PATH = join(SRC_DIR, 'layout.tsx')
|
|
22
|
+
export const LEGACY_GLOBALS_CSS = join(SRC_DIR, 'globals.css')
|
|
12
23
|
|
|
13
24
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
14
25
|
export const VERSION: string = (() => {
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Import like:
|
|
4
4
|
// import type { Metadata } from 'murasaki'
|
|
5
|
+
// import { Link } from 'murasaki'
|
|
6
|
+
|
|
7
|
+
export type { LinkProps } from './components/Link.tsx'
|
|
8
|
+
export { Link } from './components/Link.tsx'
|
|
5
9
|
|
|
6
10
|
export type Metadata = {
|
|
7
11
|
/** Default <title> for the app (overridden by <title> tag inside <head>) */
|
package/src/jsx/index.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Public surface for `import { ... } from 'murasaki/jsx'`.
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
createElement,
|
|
5
|
+
Fragment,
|
|
6
|
+
isJSXNode,
|
|
7
|
+
isValidElement,
|
|
8
|
+
JSXNode,
|
|
9
|
+
jsx,
|
|
10
|
+
raw,
|
|
11
|
+
renderToString,
|
|
12
|
+
} from './runtime.ts'
|
|
13
|
+
|
|
14
|
+
export type {
|
|
15
|
+
Child,
|
|
16
|
+
Component,
|
|
17
|
+
Element,
|
|
18
|
+
FC,
|
|
19
|
+
JSXNodeLike,
|
|
20
|
+
Props,
|
|
21
|
+
} from './types.ts'
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// JSX dev runtime entry — used when tsconfig has `jsx: "react-jsxdev"`.
|
|
2
|
+
// We don't (yet) track source locations or component stacks, so jsxDEV
|
|
3
|
+
// behaves identically to jsx.
|
|
4
|
+
|
|
5
|
+
export { Fragment, jsx as jsxDEV } from './runtime.ts'
|
|
6
|
+
export type { JSX } from './types.ts'
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// JSX automatic runtime entry — consumed by `tsx` / `esbuild` when the
|
|
2
|
+
// user's tsconfig has `jsxImportSource: "murasaki"`.
|
|
3
|
+
//
|
|
4
|
+
// The transform emits calls like:
|
|
5
|
+
// jsx(Component, props, key?)
|
|
6
|
+
// jsxs(Component, propsWithChildrenArray, key?)
|
|
7
|
+
// <></> → jsx(Fragment, { children: [...] })
|
|
8
|
+
|
|
9
|
+
export { Fragment, jsx, jsx as jsxs } from './runtime.ts'
|
|
10
|
+
export type { JSX } from './types.ts'
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// murasaki/jsx — SSR-only JSX runtime.
|
|
2
|
+
//
|
|
3
|
+
// Inspired by hono/jsx but trimmed for desktop-server use:
|
|
4
|
+
// - no hooks (the view is rendered once per HMR cycle, no client state)
|
|
5
|
+
// - no DOM renderer (we ship HTML to the OS WebView and that's it)
|
|
6
|
+
// - no streaming / Suspense (single render → loadHtml())
|
|
7
|
+
// - React-compatible enough to swap in for renderToStaticMarkup
|
|
8
|
+
//
|
|
9
|
+
// Two-step pipeline:
|
|
10
|
+
// 1. jsx(tag, props) → JSXNode (tree)
|
|
11
|
+
// 2. JSXNode.toString() → HTML string
|
|
12
|
+
//
|
|
13
|
+
// User code that runs is the *user's* JSX (transformed to jsx() calls
|
|
14
|
+
// by tsx/esbuild with `jsxImportSource: "murasaki"`).
|
|
15
|
+
|
|
16
|
+
import type { Child, Component, JSXNodeLike, Props } from './types.ts'
|
|
17
|
+
|
|
18
|
+
// ── HTML escape ──────────────────────────────────────────────────────
|
|
19
|
+
const AMP = /&/g
|
|
20
|
+
const LT = /</g
|
|
21
|
+
const GT = />/g
|
|
22
|
+
const QT = /"/g
|
|
23
|
+
|
|
24
|
+
function escapeHtml(s: string): string {
|
|
25
|
+
return s.replace(AMP, '&').replace(LT, '<').replace(GT, '>')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function escapeAttr(s: string): string {
|
|
29
|
+
return s.replace(AMP, '&').replace(QT, '"')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ── Void elements (no closing tag) ────────────────────────────────────
|
|
33
|
+
const VOID_ELEMENTS = new Set([
|
|
34
|
+
'area',
|
|
35
|
+
'base',
|
|
36
|
+
'br',
|
|
37
|
+
'col',
|
|
38
|
+
'embed',
|
|
39
|
+
'hr',
|
|
40
|
+
'img',
|
|
41
|
+
'input',
|
|
42
|
+
'link',
|
|
43
|
+
'meta',
|
|
44
|
+
'source',
|
|
45
|
+
'track',
|
|
46
|
+
'wbr',
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
// ── Attribute name normalization (React → HTML) ───────────────────────
|
|
50
|
+
const ATTR_ALIAS: Record<string, string> = {
|
|
51
|
+
className: 'class',
|
|
52
|
+
htmlFor: 'for',
|
|
53
|
+
charSet: 'charset',
|
|
54
|
+
crossOrigin: 'crossorigin',
|
|
55
|
+
httpEquiv: 'http-equiv',
|
|
56
|
+
itemProp: 'itemprop',
|
|
57
|
+
fetchPriority: 'fetchpriority',
|
|
58
|
+
noModule: 'nomodule',
|
|
59
|
+
formAction: 'formaction',
|
|
60
|
+
acceptCharset: 'accept-charset',
|
|
61
|
+
autoComplete: 'autocomplete',
|
|
62
|
+
autoFocus: 'autofocus',
|
|
63
|
+
autoPlay: 'autoplay',
|
|
64
|
+
contentEditable: 'contenteditable',
|
|
65
|
+
defaultValue: 'value',
|
|
66
|
+
defaultChecked: 'checked',
|
|
67
|
+
encType: 'enctype',
|
|
68
|
+
formMethod: 'formmethod',
|
|
69
|
+
formNoValidate: 'formnovalidate',
|
|
70
|
+
formTarget: 'formtarget',
|
|
71
|
+
maxLength: 'maxlength',
|
|
72
|
+
minLength: 'minlength',
|
|
73
|
+
noValidate: 'novalidate',
|
|
74
|
+
readOnly: 'readonly',
|
|
75
|
+
rowSpan: 'rowspan',
|
|
76
|
+
colSpan: 'colspan',
|
|
77
|
+
spellCheck: 'spellcheck',
|
|
78
|
+
tabIndex: 'tabindex',
|
|
79
|
+
useMap: 'usemap',
|
|
80
|
+
srcDoc: 'srcdoc',
|
|
81
|
+
srcSet: 'srcset',
|
|
82
|
+
hrefLang: 'hreflang',
|
|
83
|
+
dateTime: 'datetime',
|
|
84
|
+
enterKeyHint: 'enterkeyhint',
|
|
85
|
+
inputMode: 'inputmode',
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function normalizeAttrName(k: string): string {
|
|
89
|
+
return ATTR_ALIAS[k] || k
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ── Style object → CSS string ─────────────────────────────────────────
|
|
93
|
+
function camelToKebab(k: string): string {
|
|
94
|
+
// Leave already-kebab keys and CSS custom props (--foo) alone.
|
|
95
|
+
if (k[0] === '-' || !/[A-Z]/.test(k)) return k
|
|
96
|
+
return k.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// CSS properties that take unitless numbers (subset of React's list).
|
|
100
|
+
const UNITLESS = new Set([
|
|
101
|
+
'animationIterationCount',
|
|
102
|
+
'borderImageOutset',
|
|
103
|
+
'borderImageSlice',
|
|
104
|
+
'borderImageWidth',
|
|
105
|
+
'boxFlex',
|
|
106
|
+
'boxFlexGroup',
|
|
107
|
+
'boxOrdinalGroup',
|
|
108
|
+
'columnCount',
|
|
109
|
+
'columns',
|
|
110
|
+
'flex',
|
|
111
|
+
'flexGrow',
|
|
112
|
+
'flexShrink',
|
|
113
|
+
'fontWeight',
|
|
114
|
+
'gridArea',
|
|
115
|
+
'gridColumn',
|
|
116
|
+
'gridColumnEnd',
|
|
117
|
+
'gridColumnStart',
|
|
118
|
+
'gridRow',
|
|
119
|
+
'gridRowEnd',
|
|
120
|
+
'gridRowStart',
|
|
121
|
+
'lineClamp',
|
|
122
|
+
'lineHeight',
|
|
123
|
+
'opacity',
|
|
124
|
+
'order',
|
|
125
|
+
'orphans',
|
|
126
|
+
'tabSize',
|
|
127
|
+
'widows',
|
|
128
|
+
'zIndex',
|
|
129
|
+
'zoom',
|
|
130
|
+
])
|
|
131
|
+
|
|
132
|
+
function styleObjToString(obj: Record<string, unknown>): string {
|
|
133
|
+
let out = ''
|
|
134
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
135
|
+
if (v == null || v === false) continue
|
|
136
|
+
let value: string
|
|
137
|
+
if (typeof v === 'number') {
|
|
138
|
+
value = UNITLESS.has(k) ? String(v) : `${v}px`
|
|
139
|
+
} else if (typeof v === 'string') {
|
|
140
|
+
value = v
|
|
141
|
+
} else {
|
|
142
|
+
continue
|
|
143
|
+
}
|
|
144
|
+
out += `${out ? ';' : ''}${camelToKebab(k)}:${value}`
|
|
145
|
+
}
|
|
146
|
+
return out
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ── Attribute rendering ──────────────────────────────────────────────
|
|
150
|
+
function renderAttrs(props: Props): string {
|
|
151
|
+
let out = ''
|
|
152
|
+
for (const k in props) {
|
|
153
|
+
if (k === 'children' || k === 'key' || k === 'ref' || k === '__source' || k === '__self')
|
|
154
|
+
continue
|
|
155
|
+
const v = props[k]
|
|
156
|
+
if (v == null || v === false) continue
|
|
157
|
+
|
|
158
|
+
const name = normalizeAttrName(k)
|
|
159
|
+
|
|
160
|
+
// Boolean attribute
|
|
161
|
+
if (v === true) {
|
|
162
|
+
out += ` ${name}`
|
|
163
|
+
continue
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Inline style object
|
|
167
|
+
if (k === 'style' && typeof v === 'object') {
|
|
168
|
+
const css = styleObjToString(v as Record<string, unknown>)
|
|
169
|
+
if (css) out += ` style="${escapeAttr(css)}"`
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// dangerouslySetInnerHTML is handled in JSXNode.toString(), skip here
|
|
174
|
+
if (k === 'dangerouslySetInnerHTML') continue
|
|
175
|
+
|
|
176
|
+
out += ` ${name}="${escapeAttr(String(v))}"`
|
|
177
|
+
}
|
|
178
|
+
return out
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ── Raw HTML escape hatch (pre-rendered HTML as a child) ─────────────
|
|
182
|
+
class RawHtml {
|
|
183
|
+
readonly __isJSXNode = true as const
|
|
184
|
+
tag = '__raw__'
|
|
185
|
+
props: Props = {}
|
|
186
|
+
children: Child[] = []
|
|
187
|
+
html: string
|
|
188
|
+
constructor(html: string) {
|
|
189
|
+
this.html = html
|
|
190
|
+
}
|
|
191
|
+
toString(): string {
|
|
192
|
+
return this.html
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Wrap pre-rendered HTML so it's emitted verbatim as a JSX child. */
|
|
197
|
+
export function raw(html: string): JSXNodeLike {
|
|
198
|
+
return new RawHtml(html)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// ── Children rendering ───────────────────────────────────────────────
|
|
202
|
+
function renderChild(c: Child): string {
|
|
203
|
+
if (c == null || c === false || c === true) return ''
|
|
204
|
+
if (typeof c === 'string') return escapeHtml(c)
|
|
205
|
+
if (typeof c === 'number' || typeof c === 'bigint') return String(c)
|
|
206
|
+
if (Array.isArray(c)) {
|
|
207
|
+
let s = ''
|
|
208
|
+
for (const item of c) s += renderChild(item)
|
|
209
|
+
return s
|
|
210
|
+
}
|
|
211
|
+
if (isJSXNode(c)) return c.toString()
|
|
212
|
+
return ''
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ── JSXNode ──────────────────────────────────────────────────────────
|
|
216
|
+
export class JSXNode implements JSXNodeLike {
|
|
217
|
+
readonly __isJSXNode = true as const
|
|
218
|
+
tag: string | Component
|
|
219
|
+
props: Props
|
|
220
|
+
children: Child[]
|
|
221
|
+
|
|
222
|
+
constructor(tag: string | Component, props: Props, children: Child[]) {
|
|
223
|
+
this.tag = tag
|
|
224
|
+
this.props = props
|
|
225
|
+
this.children = children
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
toString(): string {
|
|
229
|
+
const { tag, props, children } = this
|
|
230
|
+
|
|
231
|
+
// Fragment / functional component
|
|
232
|
+
if (typeof tag === 'function') {
|
|
233
|
+
// Always pass children via props (React-compat)
|
|
234
|
+
const merged = { ...props, children: children.length === 1 ? children[0] : children }
|
|
235
|
+
const result = tag(merged)
|
|
236
|
+
return renderChild(result as Child)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Intrinsic element
|
|
240
|
+
const attrs = renderAttrs(props)
|
|
241
|
+
|
|
242
|
+
// dangerouslySetInnerHTML overrides children
|
|
243
|
+
const dsi = props.dangerouslySetInnerHTML as { __html?: string } | undefined
|
|
244
|
+
if (dsi && typeof dsi.__html === 'string') {
|
|
245
|
+
return `<${tag}${attrs}>${dsi.__html}</${tag}>`
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (VOID_ELEMENTS.has(tag)) {
|
|
249
|
+
// Self-closing for void elements
|
|
250
|
+
return `<${tag}${attrs}/>`
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
let childHtml = ''
|
|
254
|
+
for (const c of children) childHtml += renderChild(c)
|
|
255
|
+
|
|
256
|
+
return `<${tag}${attrs}>${childHtml}</${tag}>`
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function isJSXNode(v: unknown): v is JSXNode {
|
|
261
|
+
return typeof v === 'object' && v !== null && (v as JSXNodeLike).__isJSXNode === true
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ── Public factory (React-compat: createElement / jsx) ────────────────
|
|
265
|
+
export function jsx(
|
|
266
|
+
tag: string | Component,
|
|
267
|
+
props: Props | null,
|
|
268
|
+
..._restChildren: unknown[]
|
|
269
|
+
): JSXNode {
|
|
270
|
+
const p = props ?? {}
|
|
271
|
+
const rawChildren = (p as { children?: Child }).children
|
|
272
|
+
const children: Child[] = Array.isArray(rawChildren)
|
|
273
|
+
? rawChildren
|
|
274
|
+
: rawChildren != null
|
|
275
|
+
? [rawChildren]
|
|
276
|
+
: []
|
|
277
|
+
// Strip children from props (it's stored separately)
|
|
278
|
+
const { children: _drop, ...rest } = p as Props & { children?: Child }
|
|
279
|
+
return new JSXNode(tag, rest, children)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** React-compatible alias. */
|
|
283
|
+
export const createElement = jsx
|
|
284
|
+
|
|
285
|
+
/** Fragment — renders children without a wrapper tag. */
|
|
286
|
+
export function Fragment(props: { children?: Child }): Child {
|
|
287
|
+
return props.children ?? null
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Check if something is a JSX element (React.isValidElement compat). */
|
|
291
|
+
export function isValidElement(v: unknown): v is JSXNode {
|
|
292
|
+
return isJSXNode(v)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Convert any value (JSXNode, string, array, etc.) to an HTML string. */
|
|
296
|
+
export function renderToString(value: Child): string {
|
|
297
|
+
return renderChild(value)
|
|
298
|
+
}
|
package/src/jsx/types.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Public JSX types for murasaki/jsx.
|
|
2
|
+
|
|
3
|
+
export type Props = Record<string, unknown>
|
|
4
|
+
|
|
5
|
+
export type Child = string | number | bigint | boolean | null | undefined | JSXNodeLike | Child[]
|
|
6
|
+
|
|
7
|
+
export interface JSXNodeLike {
|
|
8
|
+
readonly __isJSXNode: true
|
|
9
|
+
tag: string | Component
|
|
10
|
+
props: Props
|
|
11
|
+
children: Child[]
|
|
12
|
+
toString(): string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type Component<P = Props> = (props: P & { children?: Child }) => Child
|
|
16
|
+
|
|
17
|
+
/** React-compatible alias used by most user code. */
|
|
18
|
+
export type FC<P = Props> = Component<P>
|
|
19
|
+
|
|
20
|
+
/** For component refs / cloning. */
|
|
21
|
+
export type Element = JSXNodeLike
|
|
22
|
+
|
|
23
|
+
declare global {
|
|
24
|
+
namespace JSX {
|
|
25
|
+
type Element = JSXNodeLike
|
|
26
|
+
interface ElementChildrenAttribute {
|
|
27
|
+
children: object
|
|
28
|
+
}
|
|
29
|
+
// Loose intrinsic catalog — every HTML/SVG tag accepts any prop.
|
|
30
|
+
// (Tightening this to a real catalog is a follow-up; keeps the
|
|
31
|
+
// runtime usable without bloating the type surface today.)
|
|
32
|
+
interface IntrinsicElements {
|
|
33
|
+
[tagName: string]: Record<string, unknown> & { children?: Child }
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/runtime/render.tsx
CHANGED
|
@@ -1,15 +1,35 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
1
|
+
// Multi-route renderer.
|
|
2
|
+
//
|
|
3
|
+
// Pipeline:
|
|
4
|
+
// 1. discover all src/app/<...>/page.tsx
|
|
5
|
+
// 2. render each page wrapped in its nested layouts (NOT the root layout)
|
|
6
|
+
// 3. render the root layout once with a switcher block as its children
|
|
7
|
+
// 4. inject metadata (<title>, <meta description>) + globals.css into <head>
|
|
8
|
+
// 5. inject a tiny navigation script that listens to hash changes and
|
|
9
|
+
// intercepts <Link> clicks
|
|
10
|
+
//
|
|
11
|
+
// Legacy single-page (src/app.tsx + src/layout.tsx) is still supported:
|
|
12
|
+
// if src/app/ doesn't exist, we render the legacy convention.
|
|
3
13
|
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
14
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
15
|
+
import { pathToFileURL } from 'node:url'
|
|
16
|
+
import {
|
|
17
|
+
APP_DIR,
|
|
18
|
+
APP_GLOBALS_CSS,
|
|
19
|
+
LEGACY_APP_PATH,
|
|
20
|
+
LEGACY_GLOBALS_CSS,
|
|
21
|
+
LEGACY_LAYOUT_PATH,
|
|
22
|
+
} from '../env.ts'
|
|
23
|
+
import type { Metadata } from '../index.ts'
|
|
24
|
+
import { jsx, raw, renderToString } from '../jsx/runtime.ts'
|
|
25
|
+
import type { Child, Component } from '../jsx/types.ts'
|
|
6
26
|
import type { RenderResult } from '../types.ts'
|
|
7
|
-
import {
|
|
27
|
+
import { discoverRoutes, type Route } from './routes.ts'
|
|
8
28
|
|
|
9
29
|
const NO_APP_HTML =
|
|
10
30
|
'<!doctype html><html><body style="font-family:system-ui;padding:40px;">' +
|
|
11
|
-
'<h1 style="color:#A855F7">
|
|
12
|
-
'<p>Create
|
|
31
|
+
'<h1 style="color:#A855F7">No app found</h1>' +
|
|
32
|
+
'<p>Create <code>src/app/page.tsx</code> and the window will reload.</p></body></html>'
|
|
13
33
|
|
|
14
34
|
function escapeHtml(s: string): string {
|
|
15
35
|
return s
|
|
@@ -19,16 +39,119 @@ function escapeHtml(s: string): string {
|
|
|
19
39
|
.replace(/"/g, '"')
|
|
20
40
|
}
|
|
21
41
|
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
42
|
+
async function dynImport(path: string) {
|
|
43
|
+
const url = pathToFileURL(path).href + `?v=${Date.now()}`
|
|
44
|
+
return import(url)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type Loaded = {
|
|
48
|
+
default: Component
|
|
49
|
+
metadata?: Metadata
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function loadModule(path: string): Promise<Loaded | null> {
|
|
53
|
+
if (!existsSync(path)) return null
|
|
54
|
+
const mod = await dynImport(path)
|
|
55
|
+
if (!mod.default) return null
|
|
56
|
+
return mod
|
|
57
|
+
}
|
|
25
58
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
let
|
|
59
|
+
// ── Page rendering ──────────────────────────────────────────────────
|
|
60
|
+
async function renderPageInner(route: Route, rootLayoutFile: string | null): Promise<string> {
|
|
61
|
+
const pageMod = await loadModule(route.pageFile)
|
|
62
|
+
if (!pageMod) return ''
|
|
63
|
+
let tree: Child = jsx(pageMod.default, null)
|
|
31
64
|
|
|
65
|
+
// Wrap in nested layouts, innermost first (i.e. skip root layout — it wraps everything later)
|
|
66
|
+
// route.layoutFiles: outermost first → so iterate from end backwards excluding root
|
|
67
|
+
const layoutsToApply = route.layoutFiles.filter((f) => f !== rootLayoutFile)
|
|
68
|
+
// Apply from innermost (last in array) outward (first in array) so the
|
|
69
|
+
// outer layout wraps the inner: <Outer><Inner><Page/></Inner></Outer>
|
|
70
|
+
for (let i = layoutsToApply.length - 1; i >= 0; i--) {
|
|
71
|
+
const layoutMod = await loadModule(layoutsToApply[i])
|
|
72
|
+
if (!layoutMod) continue
|
|
73
|
+
tree = jsx(layoutMod.default, { children: tree })
|
|
74
|
+
}
|
|
75
|
+
return renderToString(tree)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ── Root layout + metadata ──────────────────────────────────────────
|
|
79
|
+
async function renderRootLayout(rootLayout: Loaded | null, body: Child): Promise<string> {
|
|
80
|
+
if (!rootLayout) {
|
|
81
|
+
// Fallback root if user didn't write src/app/layout.tsx
|
|
82
|
+
const fallback = jsx('html', {
|
|
83
|
+
lang: 'en',
|
|
84
|
+
children: [
|
|
85
|
+
jsx('head', { children: jsx('meta', { charSet: 'utf-8' }) }),
|
|
86
|
+
jsx('body', { children: body }),
|
|
87
|
+
],
|
|
88
|
+
})
|
|
89
|
+
return renderToString(fallback)
|
|
90
|
+
}
|
|
91
|
+
const tree = jsx(rootLayout.default, { children: body })
|
|
92
|
+
return renderToString(tree)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ── Navigation script (injected once) ───────────────────────────────
|
|
96
|
+
const NAV_SCRIPT = `
|
|
97
|
+
<script>
|
|
98
|
+
(function(){
|
|
99
|
+
function showRoute(path){
|
|
100
|
+
var blocks=document.querySelectorAll('[data-murasaki-route]');
|
|
101
|
+
var matched=false;
|
|
102
|
+
for(var i=0;i<blocks.length;i++){
|
|
103
|
+
var b=blocks[i];
|
|
104
|
+
if(b.getAttribute('data-murasaki-route')===path){
|
|
105
|
+
b.removeAttribute('hidden');matched=true;
|
|
106
|
+
} else {
|
|
107
|
+
b.setAttribute('hidden','');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if(!matched){
|
|
111
|
+
// fallback to "/"
|
|
112
|
+
var root=document.querySelector('[data-murasaki-route="/"]');
|
|
113
|
+
if(root)root.removeAttribute('hidden');
|
|
114
|
+
}
|
|
115
|
+
document.dispatchEvent(new CustomEvent('murasaki:navigate',{detail:{path:path}}));
|
|
116
|
+
}
|
|
117
|
+
function currentPath(){
|
|
118
|
+
var h=location.hash||'';
|
|
119
|
+
return h.charAt(0)==='#'?h.slice(1)||'/':'/'
|
|
120
|
+
}
|
|
121
|
+
window.addEventListener('hashchange',function(){showRoute(currentPath())});
|
|
122
|
+
document.addEventListener('click',function(e){
|
|
123
|
+
var t=e.target;
|
|
124
|
+
while(t&&t.nodeType===1){
|
|
125
|
+
if(t.tagName==='A'&&t.hasAttribute('data-murasaki-link')){
|
|
126
|
+
e.preventDefault();
|
|
127
|
+
var href=t.getAttribute('data-murasaki-link');
|
|
128
|
+
if('#'+href!==location.hash){location.hash=href;}
|
|
129
|
+
else{showRoute(href);}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
t=t.parentNode;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
// Initial render
|
|
136
|
+
showRoute(currentPath());
|
|
137
|
+
})();
|
|
138
|
+
</script>
|
|
139
|
+
`.trim()
|
|
140
|
+
|
|
141
|
+
// ── Globals.css discovery (app/ takes precedence over src/) ─────────
|
|
142
|
+
function loadGlobalsCss(): string {
|
|
143
|
+
for (const p of [APP_GLOBALS_CSS, LEGACY_GLOBALS_CSS]) {
|
|
144
|
+
if (existsSync(p)) {
|
|
145
|
+
try {
|
|
146
|
+
return readFileSync(p, 'utf8')
|
|
147
|
+
} catch {}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return ''
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ── Head injection ──────────────────────────────────────────────────
|
|
154
|
+
function injectHead(html: string, metadata: Metadata | undefined, css: string): string {
|
|
32
155
|
const headInjects: string[] = []
|
|
33
156
|
if (metadata?.title && !/<title>.*?<\/title>/i.test(html)) {
|
|
34
157
|
headInjects.push(`<title>${escapeHtml(metadata.title)}</title>`)
|
|
@@ -36,20 +159,67 @@ export async function renderApp(): Promise<RenderResult> {
|
|
|
36
159
|
if (metadata?.description && !/<meta[^>]+name=["']description["']/i.test(html)) {
|
|
37
160
|
headInjects.push(`<meta name="description" content="${escapeHtml(metadata.description)}">`)
|
|
38
161
|
}
|
|
39
|
-
|
|
40
|
-
const css = loadGlobalsCss()
|
|
41
162
|
if (css) {
|
|
42
163
|
headInjects.push(`<style data-murasaki="globals.css">${css}</style>`)
|
|
43
164
|
}
|
|
165
|
+
if (!headInjects.length) return html
|
|
166
|
+
const blob = headInjects.join('')
|
|
167
|
+
if (html.includes('</head>')) return html.replace('</head>', blob + '</head>')
|
|
168
|
+
return html.replace('<body', blob + '<body')
|
|
169
|
+
}
|
|
44
170
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
171
|
+
// ── Main entry ──────────────────────────────────────────────────────
|
|
172
|
+
export async function renderApp(): Promise<RenderResult> {
|
|
173
|
+
// 1. Try app-router convention first
|
|
174
|
+
if (existsSync(APP_DIR)) {
|
|
175
|
+
return renderAppRouter()
|
|
176
|
+
}
|
|
177
|
+
// 2. Fall back to legacy single-page
|
|
178
|
+
if (existsSync(LEGACY_APP_PATH)) {
|
|
179
|
+
return renderLegacy()
|
|
180
|
+
}
|
|
181
|
+
return { html: NO_APP_HTML }
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function renderAppRouter(): Promise<RenderResult> {
|
|
185
|
+
const routes = discoverRoutes(APP_DIR)
|
|
186
|
+
if (routes.length === 0) return { html: NO_APP_HTML }
|
|
187
|
+
|
|
188
|
+
// Identify root layout (src/app/layout.tsx) if any
|
|
189
|
+
const rootLayoutPath = routes[0]?.layoutFiles[0]
|
|
190
|
+
const rootLayoutFile =
|
|
191
|
+
rootLayoutPath && rootLayoutPath.endsWith('/app/layout.tsx') ? rootLayoutPath : null
|
|
192
|
+
const rootLayoutMod = rootLayoutFile ? await loadModule(rootLayoutFile) : null
|
|
193
|
+
const metadata = rootLayoutMod?.metadata
|
|
194
|
+
|
|
195
|
+
// 2. Render each page (wrapped in its nested layouts, excluding root)
|
|
196
|
+
const blocks: string[] = []
|
|
197
|
+
for (const route of routes) {
|
|
198
|
+
const inner = await renderPageInner(route, rootLayoutFile)
|
|
199
|
+
blocks.push(`<div data-murasaki-route="${escapeHtml(route.path)}" hidden>${inner}</div>`)
|
|
52
200
|
}
|
|
201
|
+
const switcher = raw(blocks.join('') + NAV_SCRIPT)
|
|
202
|
+
|
|
203
|
+
// 3. Render root layout with switcher as children
|
|
204
|
+
const bodyContent = await renderRootLayout(rootLayoutMod, switcher)
|
|
205
|
+
let html = '<!doctype html>' + bodyContent
|
|
206
|
+
|
|
207
|
+
// 4. Inject metadata + globals.css
|
|
208
|
+
html = injectHead(html, metadata, loadGlobalsCss())
|
|
209
|
+
|
|
210
|
+
return { html, metadata }
|
|
211
|
+
}
|
|
53
212
|
|
|
213
|
+
// ── Legacy single-page render (unchanged shape) ─────────────────────
|
|
214
|
+
async function renderLegacy(): Promise<RenderResult> {
|
|
215
|
+
const pageMod = await loadModule(LEGACY_APP_PATH)
|
|
216
|
+
if (!pageMod) return { html: NO_APP_HTML }
|
|
217
|
+
const layoutMod = await loadModule(LEGACY_LAYOUT_PATH)
|
|
218
|
+
const metadata = layoutMod?.metadata
|
|
219
|
+
const body: Child = layoutMod
|
|
220
|
+
? jsx(layoutMod.default, { children: jsx(pageMod.default, null) })
|
|
221
|
+
: jsx(pageMod.default, null)
|
|
222
|
+
let html = '<!doctype html>' + renderToString(body)
|
|
223
|
+
html = injectHead(html, metadata, loadGlobalsCss())
|
|
54
224
|
return { html, metadata }
|
|
55
225
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// File-based route discovery for src/app/.
|
|
2
|
+
//
|
|
3
|
+
// Conventions (subset of Next.js app router):
|
|
4
|
+
// src/app/page.tsx → "/"
|
|
5
|
+
// src/app/layout.tsx → root layout (wraps everything)
|
|
6
|
+
// src/app/about/page.tsx → "/about"
|
|
7
|
+
// src/app/about/layout.tsx → nested layout for /about and its children
|
|
8
|
+
// src/app/_foo/ → ignored (leading underscore = private)
|
|
9
|
+
//
|
|
10
|
+
// A page.tsx is required to register a route. A layout.tsx without a
|
|
11
|
+
// page.tsx in the same dir just contributes to children's layouts.
|
|
12
|
+
|
|
13
|
+
import { existsSync, readdirSync, statSync } from 'node:fs'
|
|
14
|
+
import { join } from 'node:path'
|
|
15
|
+
|
|
16
|
+
export type Route = {
|
|
17
|
+
/** URL-style path: "/", "/about", "/settings/profile" */
|
|
18
|
+
path: string
|
|
19
|
+
/** Absolute path to the page.tsx file */
|
|
20
|
+
pageFile: string
|
|
21
|
+
/** Absolute paths to layout.tsx files, OUTERMOST first (root → innermost) */
|
|
22
|
+
layoutFiles: string[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function discoverRoutes(appDir: string): Route[] {
|
|
26
|
+
if (!existsSync(appDir)) return []
|
|
27
|
+
const out: Route[] = []
|
|
28
|
+
walk(appDir, '', [], out)
|
|
29
|
+
// Stable sort so "/" comes first, then alphabetical
|
|
30
|
+
out.sort((a, b) => {
|
|
31
|
+
if (a.path === '/') return -1
|
|
32
|
+
if (b.path === '/') return 1
|
|
33
|
+
return a.path.localeCompare(b.path)
|
|
34
|
+
})
|
|
35
|
+
return out
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function walk(dir: string, routePath: string, layouts: string[], out: Route[]): void {
|
|
39
|
+
let entries: string[]
|
|
40
|
+
try {
|
|
41
|
+
entries = readdirSync(dir)
|
|
42
|
+
} catch {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const hasLayout = entries.includes('layout.tsx')
|
|
47
|
+
const hasPage = entries.includes('page.tsx')
|
|
48
|
+
|
|
49
|
+
// The current directory contributes its layout to itself and descendants.
|
|
50
|
+
const ownLayouts = hasLayout ? [...layouts, join(dir, 'layout.tsx')] : layouts
|
|
51
|
+
|
|
52
|
+
if (hasPage) {
|
|
53
|
+
out.push({
|
|
54
|
+
path: routePath || '/',
|
|
55
|
+
pageFile: join(dir, 'page.tsx'),
|
|
56
|
+
layoutFiles: ownLayouts,
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Recurse into subdirectories (skip files, hidden, private "_*", node_modules).
|
|
61
|
+
for (const entry of entries) {
|
|
62
|
+
if (entry.startsWith('.') || entry.startsWith('_') || entry === 'node_modules') continue
|
|
63
|
+
const full = join(dir, entry)
|
|
64
|
+
let isDir = false
|
|
65
|
+
try {
|
|
66
|
+
isDir = statSync(full).isDirectory()
|
|
67
|
+
} catch {
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
if (!isDir) continue
|
|
71
|
+
walk(full, `${routePath}/${entry}`, ownLayouts, out)
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// Internal shared types (the public Metadata type is re-exported from index.ts).
|
|
2
2
|
|
|
3
|
-
import type { ComponentType, ReactNode } from 'react'
|
|
4
3
|
import type { Metadata } from './index.ts'
|
|
4
|
+
import type { Component } from './jsx/types.ts'
|
|
5
5
|
|
|
6
|
-
export type
|
|
6
|
+
export type AppComponent = Component
|
|
7
7
|
|
|
8
8
|
export type LayoutModule = {
|
|
9
|
-
component:
|
|
9
|
+
component: AppComponent
|
|
10
10
|
metadata?: Metadata
|
|
11
11
|
} | null
|
|
12
12
|
|
package/src/runtime/load.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
// Loads the user's src/app.tsx, src/layout.tsx, src/globals.css.
|
|
2
|
-
// Uses cache-busting dynamic import so file edits are picked up
|
|
3
|
-
// without restarting the Node process.
|
|
4
|
-
|
|
5
|
-
import { existsSync, readFileSync } from 'node:fs'
|
|
6
|
-
import { pathToFileURL } from 'node:url'
|
|
7
|
-
import { APP_PATH, GLOBALS_CSS, LAYOUT_PATH } from '../env.ts'
|
|
8
|
-
import type { Metadata } from '../index.ts'
|
|
9
|
-
import type { LayoutModule, ReactComponent } from '../types.ts'
|
|
10
|
-
|
|
11
|
-
async function dynImport(path: string) {
|
|
12
|
-
const url = pathToFileURL(path).href + `?v=${Date.now()}`
|
|
13
|
-
return import(url)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export async function loadApp(): Promise<ReactComponent | null> {
|
|
17
|
-
if (!existsSync(APP_PATH)) return null
|
|
18
|
-
const mod = await dynImport(APP_PATH)
|
|
19
|
-
return mod.default as ReactComponent
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export async function loadLayout(): Promise<LayoutModule> {
|
|
23
|
-
if (!existsSync(LAYOUT_PATH)) return null
|
|
24
|
-
const mod = await dynImport(LAYOUT_PATH)
|
|
25
|
-
if (!mod.default) return null
|
|
26
|
-
return {
|
|
27
|
-
component: mod.default as ReactComponent,
|
|
28
|
-
metadata: mod.metadata as Metadata | undefined,
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function loadGlobalsCss(): string {
|
|
33
|
-
if (!existsSync(GLOBALS_CSS)) return ''
|
|
34
|
-
try {
|
|
35
|
-
return readFileSync(GLOBALS_CSS, 'utf8')
|
|
36
|
-
} catch {
|
|
37
|
-
return ''
|
|
38
|
-
}
|
|
39
|
-
}
|