prunelle 0.0.0-stage → 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/llms.txt +148 -0
- package/package.json +53 -4
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +35 -0
- package/src/build/bundle.ts +613 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +169 -0
- package/src/build/client-prune.ts +118 -0
- package/src/build/compile.ts +128 -0
- package/src/build/files.ts +156 -0
- package/src/build/first-load.ts +164 -0
- package/src/build/graph.ts +247 -0
- package/src/build/index.ts +614 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/islands.ts +641 -0
- package/src/build/manifest.ts +281 -0
- package/src/build/output.ts +84 -0
- package/src/build/plugin-host.ts +220 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +150 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +247 -0
- package/src/build/resolve.ts +50 -0
- package/src/build/routes.ts +338 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static.ts +163 -0
- package/src/build/status-pages.ts +127 -0
- package/src/cli/check-forms.ts +654 -0
- package/src/cli/check-links.ts +619 -0
- package/src/cli/check.ts +367 -0
- package/src/cli/commands.ts +265 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +335 -0
- package/src/cli/new.ts +41 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +354 -0
- package/src/compiler/analyze.ts +618 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +127 -0
- package/src/compiler/compliance-rgaa.ts +502 -0
- package/src/compiler/compliance.ts +497 -0
- package/src/compiler/data-keys.ts +295 -0
- package/src/compiler/dom-attrs.ts +379 -0
- package/src/compiler/dom.ts +566 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +408 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +499 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props.ts +460 -0
- package/src/compiler/scope.ts +568 -0
- package/src/compiler/server-only.ts +326 -0
- package/src/compiler/shared.ts +369 -0
- package/src/compiler/signal-reads.ts +242 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +56 -0
- package/src/compiler/ssr.ts +491 -0
- package/src/context.ts +165 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/index.ts +208 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/protocol.ts +21 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +157 -0
- package/src/dev/supervisor.ts +187 -0
- package/src/dev/watchers.ts +215 -0
- package/src/dom/action.ts +164 -0
- package/src/dom/attrs.ts +72 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +562 -0
- package/src/dom/index.ts +289 -0
- package/src/dom/island.ts +284 -0
- package/src/dom/loop.ts +450 -0
- package/src/dom/revive.ts +36 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +86 -0
- package/src/plugins/context.ts +66 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +70 -0
- package/src/plugins/order.ts +111 -0
- package/src/plugins/public.ts +12 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +35 -0
- package/src/plugins/types.ts +242 -0
- package/src/reactivity/graph.ts +249 -0
- package/src/reactivity/index.ts +533 -0
- package/src/reactivity/selector.ts +51 -0
- package/src/reactivity/store.ts +208 -0
- package/src/server/action-protect.ts +186 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +498 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/assets.ts +136 -0
- package/src/server/compress.ts +560 -0
- package/src/server/consent.ts +253 -0
- package/src/server/cookies.ts +139 -0
- package/src/server/document.ts +271 -0
- package/src/server/env.ts +33 -0
- package/src/server/errors.ts +382 -0
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +508 -0
- package/src/server/index.ts +687 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +118 -0
- package/src/server/page.ts +107 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +256 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +19 -0
- package/src/server/rate-limit.ts +133 -0
- package/src/server/region.ts +96 -0
- package/src/server/request-body.ts +27 -0
- package/src/server/request-path.ts +73 -0
- package/src/server/request-scope.ts +22 -0
- package/src/server/route-table.ts +82 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +377 -0
- package/src/server/security.ts +472 -0
- package/src/server/serve-handlers.ts +83 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +87 -0
- package/src/server/start.ts +99 -0
- package/src/server/static-dir.ts +167 -0
- package/src/server/stream.ts +192 -0
- package/src/ssr/context.ts +350 -0
- package/src/ssr/index.ts +596 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +171 -0
- package/src/style.ts +33 -0
- package/src/test/browser.ts +192 -0
- package/src/test/index.ts +191 -0
- package/src/unrenderable.ts +35 -0
- package/templates/_shared/AGENTS.md +10 -0
- package/templates/_shared/CLAUDE.md +1 -0
- package/templates/_shared/gitignore +3 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +13 -0
- package/templates/_shared/tsconfig.json +20 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +21 -0
- package/templates/app/prunelle.config.ts +11 -0
- package/templates/app/src/auth.ts +14 -0
- package/templates/app/src/routes/_layout.tsx +32 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +13 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +34 -0
- package/templates/app/src/styles/app.css +7 -0
- package/templates/app/tests/app.test.ts +32 -0
- package/templates/blog/content/posts/welcome.md +9 -0
- package/templates/blog/package.json +22 -0
- package/templates/blog/prunelle.config.ts +23 -0
- package/templates/blog/src/posts.ts +26 -0
- package/templates/blog/src/routes/_layout.tsx +20 -0
- package/templates/blog/src/routes/blog/[slug].tsx +31 -0
- package/templates/blog/src/routes/index.tsx +33 -0
- package/templates/blog/src/styles/app.css +10 -0
- package/templates/fullstack/package.json +19 -0
- package/templates/fullstack/prunelle.config.ts +6 -0
- package/templates/fullstack/src/actions/greet.ts +19 -0
- package/templates/fullstack/src/components/Counter.tsx +10 -0
- package/templates/fullstack/src/components/GreetForm.tsx +27 -0
- package/templates/fullstack/src/routes/_layout.tsx +14 -0
- package/templates/fullstack/src/routes/api/health.ts +5 -0
- package/templates/fullstack/src/routes/index.tsx +13 -0
- package/templates/fullstack/src/styles/app.css +29 -0
- package/templates/fullstack/tests/app.test.ts +37 -0
- package/templates/minimal/package.json +19 -0
- package/templates/minimal/prunelle.config.ts +6 -0
- package/templates/minimal/src/components/Counter.tsx +10 -0
- package/templates/minimal/src/routes/_layout.tsx +14 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +22 -0
- package/README.md +0 -3
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// The SSR compiler's emission of `<Suspense>` and `<Catch>`, whose children must reach the component unrendered.
|
|
2
|
+
|
|
3
|
+
import type { JSXChild, JSXElement, JSXFragment, Node } from 'oxc-parser'
|
|
4
|
+
import type { StringParts } from './ssr-helpers'
|
|
5
|
+
import { cleanJsxText, decodeHtmlEntities, filterMeaningfulChildren, isComponentTag, isJsxNode, jsxTagName } from './shared'
|
|
6
|
+
import { serializeComponentProps } from './ssr-helpers'
|
|
7
|
+
|
|
8
|
+
/** What the two emitters borrow from the running `compileSsr()`. */
|
|
9
|
+
export interface PrunelleTagEmitter {
|
|
10
|
+
use: (name: string) => string
|
|
11
|
+
compileSubExpression: (node: Node) => string
|
|
12
|
+
compileChildren: (children: readonly JSXChild[], slotMarkers?: boolean, inFragment?: boolean) => string
|
|
13
|
+
compileJsx: (node: JSXElement | JSXFragment) => string
|
|
14
|
+
markersAt: (offset: number) => boolean
|
|
15
|
+
directAwaitNodes: WeakSet<Node>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// `<Suspense fallback={...}><AsyncComp .../></Suspense>` passes the raw component call or Promise.
|
|
19
|
+
export function emitSuspense(em: PrunelleTagEmitter, node: JSXElement, tag: string, propEntries: string[], parts: StringParts): void {
|
|
20
|
+
const meaningful = filterMeaningfulChildren(node.children)
|
|
21
|
+
const only = meaningful.length === 1 ? meaningful[0]! : undefined
|
|
22
|
+
if (only?.type === 'JSXElement' && isComponentTag(jsxTagName(only.openingElement.name))) {
|
|
23
|
+
const inner = serializeComponentProps(only.openingElement.attributes, em.compileSubExpression)
|
|
24
|
+
if (filterMeaningfulChildren(only.children).length > 0)
|
|
25
|
+
inner.propEntries.push(`children: ${em.use('raw')}(${em.compileChildren(only.children)})`)
|
|
26
|
+
const innerObj = inner.propEntries.length > 0 ? `{ ${inner.propEntries.join(', ')} }` : '{}'
|
|
27
|
+
propEntries.push(`children: ${jsxTagName(only.openingElement.name)}(${innerObj})`)
|
|
28
|
+
}
|
|
29
|
+
else if (only?.type === 'JSXExpressionContainer' && only.expression.type !== 'JSXEmptyExpression') {
|
|
30
|
+
propEntries.push(`children: ${em.compileSubExpression(only.expression)}`)
|
|
31
|
+
}
|
|
32
|
+
else if (only?.type === 'JSXText') {
|
|
33
|
+
propEntries.push(`children: ${JSON.stringify(decodeHtmlEntities(cleanJsxText(only.value)))}`)
|
|
34
|
+
}
|
|
35
|
+
else if (only !== undefined && isJsxNode(only)) {
|
|
36
|
+
propEntries.push(`children: ${em.use('raw')}(${em.compileJsx(only)})`)
|
|
37
|
+
}
|
|
38
|
+
const propsObj = propEntries.length > 0 ? `{ ${propEntries.join(', ')} }` : '{}'
|
|
39
|
+
parts.pushExpr(`${em.use('ssrChild')}(${tag}(${propsObj}))`)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// `<Catch>` reads its children inside a try, so they reach it as a getter even when static; in an island it renders in a range of its own, which the browser swaps.
|
|
43
|
+
export function emitCatch(em: PrunelleTagEmitter, node: JSXElement, tag: string, propEntries: string[], parts: StringParts): void {
|
|
44
|
+
const meaningful = filterMeaningfulChildren(node.children)
|
|
45
|
+
const awaits = node.children.some(c => em.directAwaitNodes.has(c))
|
|
46
|
+
const markers = em.markersAt(node.start)
|
|
47
|
+
if (meaningful.length > 0) {
|
|
48
|
+
const childExpr = `${em.use('raw')}(${em.compileChildren(node.children, markers, markers && meaningful.length > 1)})`
|
|
49
|
+
propEntries.push(`get children() { return ${awaits ? `(async () => ${childExpr})()` : childExpr} }`)
|
|
50
|
+
}
|
|
51
|
+
if (markers)
|
|
52
|
+
parts.pushStatic('<!--k[-->')
|
|
53
|
+
parts.pushExpr(`${em.use('ssrChild')}(${awaits ? 'await ' : ''}${tag}({ ${propEntries.join(', ')} }))`)
|
|
54
|
+
if (markers)
|
|
55
|
+
parts.pushStatic('<!--k]-->')
|
|
56
|
+
}
|
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
// Prunelle SSR compiler: compiles TSX directly to flat string concatenation, inline for-loops,
|
|
2
|
+
// static attribute literal folding, compile-time static island props, and shared balanced hydration markers.
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
JSXChild,
|
|
6
|
+
JSXElement,
|
|
7
|
+
JSXFragment,
|
|
8
|
+
Node,
|
|
9
|
+
} from 'oxc-parser'
|
|
10
|
+
import type { ModuleAnalysis } from './analyze'
|
|
11
|
+
import type { SelectValueBinding } from './ssr-helpers'
|
|
12
|
+
import type { PrunelleTagEmitter } from './ssr-prunelle-tags'
|
|
13
|
+
import { analyzeModule, parseTsx } from './analyze'
|
|
14
|
+
import { walk } from './ast'
|
|
15
|
+
import { mistake } from './mistakes'
|
|
16
|
+
import { alignLines, lineMarker, SourceEditor } from './patch'
|
|
17
|
+
import {
|
|
18
|
+
containsJsx,
|
|
19
|
+
createExprContext,
|
|
20
|
+
decodeHtmlEntities,
|
|
21
|
+
escapeRawText,
|
|
22
|
+
escapeStaticText,
|
|
23
|
+
filterMeaningfulChildren,
|
|
24
|
+
isComponentTag,
|
|
25
|
+
isJsxNode,
|
|
26
|
+
jsxTagName,
|
|
27
|
+
planChildSlots,
|
|
28
|
+
RAW_TEXT_ELEMENTS,
|
|
29
|
+
resolvePrunelleTag,
|
|
30
|
+
unwrapParens,
|
|
31
|
+
VOID_ELEMENTS,
|
|
32
|
+
} from './shared'
|
|
33
|
+
import {
|
|
34
|
+
emitSsrElementAttrs,
|
|
35
|
+
precomputeSsrSubtreeFlags,
|
|
36
|
+
serializeComponentProps,
|
|
37
|
+
staticLiteralOrTemplate,
|
|
38
|
+
StringParts,
|
|
39
|
+
} from './ssr-helpers'
|
|
40
|
+
import { emitIsland } from './ssr-island'
|
|
41
|
+
import { emitCatch, emitSuspense } from './ssr-prunelle-tags'
|
|
42
|
+
|
|
43
|
+
const FORM_CONTROL_TAGS = new Set(['input', 'select', 'textarea'])
|
|
44
|
+
const FORM_ATTR_BITS: Record<string, number> = {
|
|
45
|
+
type: 1,
|
|
46
|
+
required: 2,
|
|
47
|
+
minlength: 4,
|
|
48
|
+
maxlength: 8,
|
|
49
|
+
min: 16,
|
|
50
|
+
max: 32,
|
|
51
|
+
step: 64,
|
|
52
|
+
pattern: 128,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function findDynamicFormAction(node: JSXElement): Node | undefined {
|
|
56
|
+
for (const attr of node.openingElement.attributes) {
|
|
57
|
+
if (attr.type !== 'JSXAttribute' || jsxTagName(attr.name) !== 'action')
|
|
58
|
+
continue
|
|
59
|
+
if (attr.value?.type !== 'JSXExpressionContainer' || attr.value.expression.type === 'JSXEmptyExpression')
|
|
60
|
+
return undefined
|
|
61
|
+
const expr = attr.value.expression
|
|
62
|
+
if (staticLiteralOrTemplate(expr).ok)
|
|
63
|
+
return undefined
|
|
64
|
+
if (expr.type === 'ConditionalExpression' && staticLiteralOrTemplate(expr.consequent).ok && staticLiteralOrTemplate(expr.alternate).ok)
|
|
65
|
+
return undefined
|
|
66
|
+
return expr
|
|
67
|
+
}
|
|
68
|
+
return undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function emitFormControlAttrs(
|
|
72
|
+
node: JSXElement,
|
|
73
|
+
tag: 'input' | 'select' | 'textarea',
|
|
74
|
+
parts: StringParts,
|
|
75
|
+
use: (name: string) => string,
|
|
76
|
+
compileSubExpression: (node: Node) => string,
|
|
77
|
+
): void {
|
|
78
|
+
const attrs = node.openingElement.attributes
|
|
79
|
+
if (attrs.some(a => a.type === 'JSXSpreadAttribute'))
|
|
80
|
+
return
|
|
81
|
+
let nameExpr: string | undefined
|
|
82
|
+
let typeExpr: string | undefined
|
|
83
|
+
let present = 0
|
|
84
|
+
for (const attr of attrs) {
|
|
85
|
+
if (attr.type !== 'JSXAttribute')
|
|
86
|
+
continue
|
|
87
|
+
const attrName = jsxTagName(attr.name).toLowerCase()
|
|
88
|
+
const bit = FORM_ATTR_BITS[attrName]
|
|
89
|
+
if (bit !== undefined)
|
|
90
|
+
present |= bit
|
|
91
|
+
if (attrName === 'name') {
|
|
92
|
+
if (attr.value?.type === 'Literal' && typeof attr.value.value === 'string') {
|
|
93
|
+
nameExpr = JSON.stringify(attr.value.value)
|
|
94
|
+
}
|
|
95
|
+
else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
96
|
+
const lit = staticLiteralOrTemplate(attr.value.expression)
|
|
97
|
+
nameExpr = lit.ok && typeof lit.value === 'string' ? JSON.stringify(lit.value) : compileSubExpression(attr.value.expression)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else if (tag === 'input' && attrName === 'type') {
|
|
101
|
+
if (attr.value?.type === 'Literal' && typeof attr.value.value === 'string') {
|
|
102
|
+
typeExpr = JSON.stringify(attr.value.value.toLowerCase())
|
|
103
|
+
}
|
|
104
|
+
else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
105
|
+
const lit = staticLiteralOrTemplate(attr.value.expression)
|
|
106
|
+
if (lit.ok && typeof lit.value === 'string')
|
|
107
|
+
typeExpr = JSON.stringify(lit.value.toLowerCase())
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (nameExpr === undefined)
|
|
112
|
+
return
|
|
113
|
+
const typeArg = typeExpr !== undefined ? `, ${typeExpr}` : ''
|
|
114
|
+
parts.pushExpr(`${use('ssrFormAttrs')}(${JSON.stringify(tag)}, ${nameExpr}, ${present}${typeArg})`)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface IslandBinding {
|
|
118
|
+
id: string
|
|
119
|
+
chunk: string
|
|
120
|
+
defaultHydrate?: string | undefined
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface CompileSsrOptions {
|
|
124
|
+
islands?: ReadonlyMap<string, IslandBinding> | undefined
|
|
125
|
+
isIsland?: boolean | undefined
|
|
126
|
+
/** Hydration markers everywhere, nowhere, or only inside these source spans. */
|
|
127
|
+
emitMarkers?: boolean | readonly (readonly [number, number])[] | undefined
|
|
128
|
+
analysis?: ModuleAnalysis | undefined
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface CompileResult {
|
|
132
|
+
code: string
|
|
133
|
+
analysis: ModuleAnalysis
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function emitActionFormExpr(
|
|
137
|
+
formAction: Node,
|
|
138
|
+
isAsyncForm: boolean,
|
|
139
|
+
nextId: () => number,
|
|
140
|
+
use: (name: string) => string,
|
|
141
|
+
compileSubExpression: (node: Node) => string,
|
|
142
|
+
emitBody: (bufVar: string, parts: StringParts, flush: () => void, pushStmt: (stmt: string) => void, override: { node: Node, ident: string }) => void,
|
|
143
|
+
): string {
|
|
144
|
+
const faVar = `_fa${nextId()}`
|
|
145
|
+
const innerBuf = `_o${nextId()}`
|
|
146
|
+
const innerStmts: string[] = []
|
|
147
|
+
const innerParts = new StringParts()
|
|
148
|
+
const flushInner = () => {
|
|
149
|
+
if (!innerParts.isEmpty()) {
|
|
150
|
+
innerStmts.push(`${innerBuf} += ${innerParts.toExpr()};`)
|
|
151
|
+
innerParts.clear()
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
emitBody(innerBuf, innerParts, flushInner, s => innerStmts.push(s), { node: formAction, ident: faVar })
|
|
155
|
+
if (innerStmts.length > 0)
|
|
156
|
+
flushInner()
|
|
157
|
+
const body = innerStmts.length === 0 ? innerParts.toExpr() : `{ let ${innerBuf} = ""; ${innerStmts.join(' ')} return ${innerBuf}; }`
|
|
158
|
+
return isAsyncForm
|
|
159
|
+
? `await ${use('ssrFormAsync')}(${compileSubExpression(formAction)}, async (${faVar}) => ${body})`
|
|
160
|
+
: `${use('ssrForm')}(${compileSubExpression(formAction)}, (${faVar}) => ${body})`
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function markerTest(markers: NonNullable<CompileSsrOptions['emitMarkers']>): (offset: number) => boolean {
|
|
164
|
+
return typeof markers === 'boolean' ? () => markers : offset => markers.some(([start, end]) => offset >= start && offset < end)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function compileSsr(filename: string, code: string, options: CompileSsrOptions = {}): CompileResult {
|
|
168
|
+
const program = options.analysis?.program ?? parseTsx(filename, code)
|
|
169
|
+
const analysis = options.analysis ?? analyzeModule(filename, code, program)
|
|
170
|
+
|
|
171
|
+
if (!analysis.hasJsx)
|
|
172
|
+
return { code, analysis }
|
|
173
|
+
|
|
174
|
+
const editor = new SourceEditor(code)
|
|
175
|
+
// Generated code keeps each expression on its source line: Bun maps a stack trace by the code this compiler hands it, not by a source map of its own.
|
|
176
|
+
const mark = lineMarker(code)
|
|
177
|
+
const used = new Set<string>()
|
|
178
|
+
let loopCounter = 0
|
|
179
|
+
let activeSelectValue: SelectValueBinding | undefined
|
|
180
|
+
|
|
181
|
+
const use = (name: string): string => {
|
|
182
|
+
used.add(name)
|
|
183
|
+
return `__${name}`
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const islands = options.islands
|
|
187
|
+
const markersAt = markerTest(options.emitMarkers ?? options.isIsland ?? analysis.isInteractive)
|
|
188
|
+
const jsxNodes = analysis.jsxNodes
|
|
189
|
+
const exprContext = createExprContext(program, jsxNodes)
|
|
190
|
+
const { directAwaitNodes, dynamicChildNodes } = precomputeSsrSubtreeFlags(program)
|
|
191
|
+
const prunelleTags: PrunelleTagEmitter = { use, compileSubExpression, compileChildren: compileChildrenListToStringExpr, compileJsx: compileJsxToStringExpr, markersAt, directAwaitNodes }
|
|
192
|
+
|
|
193
|
+
function compileSubExpression(node: Node): string {
|
|
194
|
+
if (!containsJsx(node, jsxNodes))
|
|
195
|
+
return mark(node.start) + code.slice(node.start, node.end)
|
|
196
|
+
|
|
197
|
+
const sub = new SourceEditor(code.slice(node.start, node.end))
|
|
198
|
+
const offset = node.start
|
|
199
|
+
walk(node, (child) => {
|
|
200
|
+
if (isJsxNode(child)) {
|
|
201
|
+
sub.replace(child.start - offset, child.end - offset, compileRootJsx(child) + mark(child.end))
|
|
202
|
+
return false
|
|
203
|
+
}
|
|
204
|
+
return undefined
|
|
205
|
+
})
|
|
206
|
+
return mark(node.start) + sub.toString()
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function compileChildrenListToStringExpr(children: readonly JSXChild[], slotMarkers = markersAt(children[0]?.start ?? 0), inFragment = false, rawTextTag?: 'style' | 'script'): string {
|
|
210
|
+
const bufVar = `_o${loopCounter++}`
|
|
211
|
+
const stmts: string[] = []
|
|
212
|
+
const parts = new StringParts()
|
|
213
|
+
const flush = () => {
|
|
214
|
+
if (!parts.isEmpty()) {
|
|
215
|
+
stmts.push(`${bufVar} += ${parts.toExpr()};`)
|
|
216
|
+
parts.clear()
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
emitChildrenIntoBuffer(children, bufVar, parts, flush, s => stmts.push(s), rawTextTag, slotMarkers, inFragment)
|
|
220
|
+
if (stmts.length === 0)
|
|
221
|
+
return parts.toExpr()
|
|
222
|
+
flush()
|
|
223
|
+
const hasAwait = children.some(c => directAwaitNodes.has(c))
|
|
224
|
+
if (hasAwait)
|
|
225
|
+
return `(await (async () => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })())`
|
|
226
|
+
return `(() => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })()`
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Emits JSX directly into a string accumulator variable `_out` (zero closures on nested `.map()` loops!)
|
|
230
|
+
function emitIntoBuffer(node: JSXElement | JSXFragment, bufVar: string, parts: StringParts, flushParts: () => void, pushStmt: (stmt: string) => void, actionOverride?: { node: Node, ident: string }): void {
|
|
231
|
+
if (node.type === 'JSXFragment') {
|
|
232
|
+
emitChildrenIntoBuffer(node.children, bufVar, parts, flushParts, pushStmt, undefined, markersAt(node.start), true)
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const tag = jsxTagName(node.openingElement.name)
|
|
237
|
+
if (isComponentTag(tag)) {
|
|
238
|
+
emitComponent(node, tag, parts)
|
|
239
|
+
return
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (tag === 'form' && actionOverride === undefined) {
|
|
243
|
+
const formAction = findDynamicFormAction(node)
|
|
244
|
+
if (formAction !== undefined) {
|
|
245
|
+
parts.pushExpr(emitActionFormExpr(formAction, directAwaitNodes.has(node), () => loopCounter++, use, compileSubExpression, (b, p, f, ps, ov) => emitIntoBuffer(node, b, p, f, ps, ov)))
|
|
246
|
+
return
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
parts.pushStatic(`<${tag}`)
|
|
251
|
+
const isTextarea = tag === 'textarea'
|
|
252
|
+
const isSelect = tag === 'select'
|
|
253
|
+
const compileAttrExpr = actionOverride !== undefined ? (n: Node) => (n === actionOverride.node ? actionOverride.ident : compileSubExpression(n)) : compileSubExpression
|
|
254
|
+
const { innerHtmlExpr, textareaValue, nextSelectValue } = emitSsrElementAttrs({ node, tag, parts, activeSelectValue, flushParts, pushStmt, nextId: () => loopCounter++, use, compileSubExpression: compileAttrExpr })
|
|
255
|
+
if (FORM_CONTROL_TAGS.has(tag))
|
|
256
|
+
emitFormControlAttrs(node, tag as 'input' | 'select' | 'textarea', parts, use, compileSubExpression)
|
|
257
|
+
parts.pushStatic('>')
|
|
258
|
+
if (VOID_ELEMENTS.has(tag))
|
|
259
|
+
return
|
|
260
|
+
|
|
261
|
+
const prevSelectValue = activeSelectValue
|
|
262
|
+
if (isSelect)
|
|
263
|
+
activeSelectValue = nextSelectValue
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
if (innerHtmlExpr !== undefined)
|
|
267
|
+
parts.pushExpr(innerHtmlExpr)
|
|
268
|
+
else if (isTextarea && textareaValue !== undefined && filterMeaningfulChildren(node.children).length === 0)
|
|
269
|
+
textareaValue.staticText !== undefined ? parts.pushStatic(escapeStaticText(textareaValue.staticText)) : textareaValue.expr !== undefined && parts.pushExpr(`${use('esc')}(${textareaValue.expr})`)
|
|
270
|
+
else if (tag === 'style') {
|
|
271
|
+
const styleExpr = compileChildrenListToStringExpr(node.children, false, false, 'style')
|
|
272
|
+
if (styleExpr !== '""')
|
|
273
|
+
parts.pushExpr(`${use('ssrStyleBlock')}(${styleExpr})`)
|
|
274
|
+
}
|
|
275
|
+
else
|
|
276
|
+
emitChildrenIntoBuffer(node.children, bufVar, parts, flushParts, pushStmt, RAW_TEXT_ELEMENTS.has(tag) ? tag as 'style' | 'script' : undefined)
|
|
277
|
+
}
|
|
278
|
+
finally {
|
|
279
|
+
activeSelectValue = prevSelectValue
|
|
280
|
+
}
|
|
281
|
+
if (actionOverride === undefined)
|
|
282
|
+
parts.pushStatic(`</${tag}>`)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function emitChildrenIntoBuffer(
|
|
286
|
+
children: readonly JSXChild[],
|
|
287
|
+
bufVar: string,
|
|
288
|
+
parts: StringParts,
|
|
289
|
+
flushParts: () => void,
|
|
290
|
+
pushStmt: (stmt: string) => void,
|
|
291
|
+
rawTextTag?: 'style' | 'script',
|
|
292
|
+
slotMarkers = markersAt(children[0]?.start ?? 0),
|
|
293
|
+
inFragment = false,
|
|
294
|
+
): void {
|
|
295
|
+
const planned = planChildSlots(children, { jsxNodes, exprContext, emitMarkers: slotMarkers, inFragment })
|
|
296
|
+
|
|
297
|
+
for (const { child, slot, open, close } of planned) {
|
|
298
|
+
if (slot.kind === 'static-text') {
|
|
299
|
+
if (rawTextTag !== undefined)
|
|
300
|
+
parts.pushStatic(escapeRawText(slot.text, rawTextTag))
|
|
301
|
+
else
|
|
302
|
+
parts.pushStatic(escapeStaticText(slot.fromLiteral ? slot.text : decodeHtmlEntities(slot.text)))
|
|
303
|
+
continue
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (rawTextTag !== undefined) {
|
|
307
|
+
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression') {
|
|
308
|
+
const expr = child.expression
|
|
309
|
+
if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0 && expr.quasis.length === 1) {
|
|
310
|
+
const cooked = expr.quasis[0]?.value.cooked
|
|
311
|
+
if (typeof cooked === 'string') {
|
|
312
|
+
parts.pushStatic(escapeRawText(cooked, rawTextTag))
|
|
313
|
+
continue
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
parts.pushExpr(`${use('ssrRawText')}(${JSON.stringify(rawTextTag)}, ${compileSubExpression(expr)})`)
|
|
317
|
+
continue
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (slot.kind === 'element') {
|
|
322
|
+
emitIntoBuffer(slot.node, bufVar, parts, flushParts, pushStmt)
|
|
323
|
+
continue
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (open !== '')
|
|
327
|
+
parts.pushStatic(open)
|
|
328
|
+
|
|
329
|
+
if (slot.kind === 'component' || slot.kind === 'fragment') {
|
|
330
|
+
emitIntoBuffer(slot.node, bufVar, parts, flushParts, pushStmt)
|
|
331
|
+
}
|
|
332
|
+
else if (slot.kind === 'loop') {
|
|
333
|
+
const { match } = slot
|
|
334
|
+
const itemParam = match.params[0]?.type === 'Identifier' ? match.params[0].name : undefined
|
|
335
|
+
const idxParam = match.params[1]?.type === 'Identifier' ? match.params[1].name : undefined
|
|
336
|
+
|
|
337
|
+
if (match.isJsxBody && itemParam !== undefined && match.params.length <= 2 && (match.params.length < 2 || idxParam !== undefined)) {
|
|
338
|
+
flushParts()
|
|
339
|
+
const n = loopCounter++
|
|
340
|
+
const arrVar = `_a${n}`
|
|
341
|
+
const idxVar = idxParam ?? `_i${n}`
|
|
342
|
+
const innerStmts: string[] = []
|
|
343
|
+
const innerParts = new StringParts()
|
|
344
|
+
const flushInner = () => {
|
|
345
|
+
if (!innerParts.isEmpty()) {
|
|
346
|
+
innerStmts.push(`${bufVar} += ${innerParts.toExpr()};`)
|
|
347
|
+
innerParts.clear()
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
emitIntoBuffer(match.body as JSXElement | JSXFragment, bufVar, innerParts, flushInner, s => innerStmts.push(s))
|
|
351
|
+
flushInner()
|
|
352
|
+
|
|
353
|
+
const target = compileSubExpression(match.target)
|
|
354
|
+
pushStmt(
|
|
355
|
+
`const ${arrVar} = ${target}; if (${arrVar}) for (let ${idxVar} = 0; ${idxVar} < ${arrVar}.length; ${idxVar}++) { const ${itemParam} = ${arrVar}[${idxVar}]; ${innerStmts.join(' ')} }`,
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
else if (match.isJsxBody) {
|
|
359
|
+
const target = compileSubExpression(match.target)
|
|
360
|
+
const params = match.params.map(p => code.slice(p.start, p.end)).join(', ')
|
|
361
|
+
parts.pushExpr(`${use('ssrLoop')}(${target}, (${params}) => ${compileJsxToStringExpr(match.body as JSXElement | JSXFragment)})`)
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
parts.pushExpr(`${use('esc')}(${compileSubExpression(match.call)})`)
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
else if (slot.kind === 'and-show') {
|
|
368
|
+
const left = compileSubExpression(slot.expr.left)
|
|
369
|
+
const right = compileJsxToStringExpr(slot.right)
|
|
370
|
+
parts.pushExpr(`((${left}) ? (${right}) : "")`)
|
|
371
|
+
}
|
|
372
|
+
else if (slot.kind === 'ternary-show') {
|
|
373
|
+
const test = compileSubExpression(slot.expr.test)
|
|
374
|
+
const cons = compileExpressionAsString(slot.cons)
|
|
375
|
+
const alt = compileExpressionAsString(slot.alt)
|
|
376
|
+
parts.pushExpr(`((${test}) ? (${cons}) : (${alt}))`)
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
parts.pushExpr(`${use('esc')}(${compileSubExpression(slot.expr)})`)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (close !== '')
|
|
383
|
+
parts.pushStatic(close)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function compileExpressionAsString(expr: Node): string {
|
|
388
|
+
if (isJsxNode(expr))
|
|
389
|
+
return compileJsxToStringExpr(expr)
|
|
390
|
+
if (expr.type === 'LogicalExpression' && expr.operator === '&&') {
|
|
391
|
+
const right = unwrapParens(expr.right)
|
|
392
|
+
if (isJsxNode(right))
|
|
393
|
+
return `((${compileSubExpression(expr.left)}) ? (${compileJsxToStringExpr(right)}) : "")`
|
|
394
|
+
}
|
|
395
|
+
if (expr.type === 'ConditionalExpression') {
|
|
396
|
+
const consNode = unwrapParens(expr.consequent)
|
|
397
|
+
const altNode = unwrapParens(expr.alternate)
|
|
398
|
+
if (isJsxNode(consNode) || isJsxNode(altNode))
|
|
399
|
+
return `((${compileSubExpression(expr.test)}) ? (${compileExpressionAsString(consNode)}) : (${compileExpressionAsString(altNode)}))`
|
|
400
|
+
}
|
|
401
|
+
return `${use('esc')}(${compileSubExpression(expr)})`
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function emitComponent(node: JSXElement, tag: string, parts: StringParts): void {
|
|
405
|
+
const { propEntries, allStaticProps, staticPropsObj, hydrateExpr, staticHydrate } = serializeComponentProps(node.openingElement.attributes, compileSubExpression)
|
|
406
|
+
const childNodes = node.children
|
|
407
|
+
const meaningfulChildren = filterMeaningfulChildren(childNodes)
|
|
408
|
+
const hasChildren = meaningfulChildren.length > 0
|
|
409
|
+
// The call sits on the line of its tag, so a component that throws names that line in the caller's frame.
|
|
410
|
+
const call = `${mark(node.start)}${tag}`
|
|
411
|
+
|
|
412
|
+
const prunelleTag = resolvePrunelleTag(tag, analysis.imports)
|
|
413
|
+
if (prunelleTag === 'Suspense' || prunelleTag === 'Catch') {
|
|
414
|
+
;(prunelleTag === 'Suspense' ? emitSuspense : emitCatch)(prunelleTags, node, tag, propEntries, parts)
|
|
415
|
+
return
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const islandInfo = islands?.get(tag)
|
|
419
|
+
if (islandInfo === undefined && islands !== undefined && hydrateExpr !== undefined)
|
|
420
|
+
throw mistake(`$hydrate on <${tag}>`, filename, `${tag} is not an island, so it never hydrates; drop $hydrate, or give ${tag} state or handlers`)
|
|
421
|
+
|
|
422
|
+
const childrenHaveAwait = hasChildren && childNodes.some(c => directAwaitNodes.has(c))
|
|
423
|
+
const childMarkers = islandInfo === undefined && markersAt(node.start)
|
|
424
|
+
const childExpr = hasChildren ? compileChildrenListToStringExpr(childNodes, childMarkers, childMarkers && meaningfulChildren.length > 1) : '""'
|
|
425
|
+
|
|
426
|
+
if (islandInfo !== undefined) {
|
|
427
|
+
emitIsland({ use, nextId: () => loopCounter++, markersAt }, { island: islandInfo, tag, call, start: node.start, propEntries, allStaticProps, staticPropsObj, hydrateExpr, staticHydrate, hasChildren, childExpr, childrenHaveAwait }, parts)
|
|
428
|
+
return
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const childrenHaveDynamic = hasChildren && childNodes.some(c => dynamicChildNodes.has(c))
|
|
432
|
+
|
|
433
|
+
// Lazy memoized getter for non-async dynamic/component children so Providers & context components run before their children!
|
|
434
|
+
if (childrenHaveDynamic && !childrenHaveAwait) {
|
|
435
|
+
const cVar = `_c${loopCounter++}`
|
|
436
|
+
const propsWithGetter = [...propEntries, `get children() { return ${cVar} === undefined ? (${cVar} = ${use('raw')}(${childExpr})) : ${cVar} }`].join(', ')
|
|
437
|
+
parts.pushExpr(`${use('ssrChild')}(${call}((() => { let ${cVar}; return { ${propsWithGetter} }; })()))`)
|
|
438
|
+
return
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (hasChildren)
|
|
442
|
+
propEntries.push(`children: ${use('raw')}(${childExpr})`)
|
|
443
|
+
|
|
444
|
+
const propsObj = propEntries.length > 0 ? `{ ${propEntries.join(', ')} }` : '{}'
|
|
445
|
+
parts.pushExpr(`${use('ssrChild')}(${call}(${propsObj}))`)
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function compileJsxToStringExpr(node: JSXElement | JSXFragment): string {
|
|
449
|
+
const bufVar = `_o${loopCounter++}`
|
|
450
|
+
const stmts: string[] = []
|
|
451
|
+
const parts = new StringParts()
|
|
452
|
+
|
|
453
|
+
const flush = () => {
|
|
454
|
+
if (!parts.isEmpty()) {
|
|
455
|
+
stmts.push(`${bufVar} += ${parts.toExpr()};`)
|
|
456
|
+
parts.clear()
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
emitIntoBuffer(node, bufVar, parts, flush, s => stmts.push(s))
|
|
461
|
+
|
|
462
|
+
// If no inline `for` statements were emitted, stay a pure flat concatenation expression!
|
|
463
|
+
if (stmts.length === 0)
|
|
464
|
+
return parts.toExpr()
|
|
465
|
+
|
|
466
|
+
flush()
|
|
467
|
+
if (directAwaitNodes.has(node))
|
|
468
|
+
return `(await (async () => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })())`
|
|
469
|
+
return `(() => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })()`
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function compileRootJsx(node: JSXElement | JSXFragment): string {
|
|
473
|
+
return `${use('raw')}(${compileJsxToStringExpr(node)})`
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
walk(program, (node) => {
|
|
477
|
+
if (isJsxNode(node)) {
|
|
478
|
+
editor.replace(node.start, node.end, compileRootJsx(node) + mark(node.end))
|
|
479
|
+
return false
|
|
480
|
+
}
|
|
481
|
+
return undefined
|
|
482
|
+
})
|
|
483
|
+
|
|
484
|
+
if (used.size > 0) {
|
|
485
|
+
const specifiers = [...used].sort().map(name => `${name} as __${name}`).join(', ')
|
|
486
|
+
// Last, where it moves no line: an import is hoisted wherever it is written.
|
|
487
|
+
editor.append(`\nimport { ${specifiers} } from 'prunelle/internal/ssr';\n`)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return { code: alignLines(editor.toString()), analysis }
|
|
491
|
+
}
|