@kayjs/kay 2.0.0-alpha.8 → 2.0.0-beta.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/package.json +1 -1
- package/src/attrs.ts +35 -0
- package/src/build/bundle.ts +442 -0
- package/src/build/compile.ts +20 -11
- package/src/build/files.ts +136 -0
- package/src/build/graph.ts +158 -0
- package/src/build/index.ts +231 -1296
- package/src/build/islands.ts +177 -0
- package/src/build/manifest.ts +224 -0
- package/src/build/plugin-host.ts +184 -0
- package/src/build/prerender.ts +93 -0
- package/src/build/report.ts +157 -0
- package/src/build/resolve.ts +58 -0
- package/src/build/routes.ts +135 -0
- package/src/build/static.ts +73 -0
- package/src/cli/check.ts +228 -114
- package/src/cli/index.ts +298 -79
- package/src/cli/new.ts +67 -35
- package/src/compiler/analyze.ts +666 -91
- package/src/compiler/dom.ts +822 -629
- package/src/compiler/mistakes.ts +354 -0
- package/src/compiler/patch.ts +1 -1
- package/src/compiler/server-only.ts +164 -21
- package/src/compiler/shared.ts +442 -0
- package/src/compiler/ssr.ts +390 -324
- package/src/context.ts +120 -0
- package/src/dev/index.ts +291 -76
- package/src/dom/action.ts +23 -13
- package/src/dom/extras.ts +157 -84
- package/src/dom/index.ts +154 -33
- package/src/dom/island.ts +117 -39
- package/src/dom/loop.ts +34 -21
- package/src/index.ts +2 -1
- package/src/jsx-runtime.ts +11 -0
- package/src/plugins/context.ts +57 -0
- package/src/plugins/index.ts +120 -17
- package/src/reactivity/index.ts +250 -157
- package/src/reactivity/selector.ts +11 -9
- package/src/reactivity/store.ts +37 -12
- package/src/server/action.ts +217 -74
- package/src/server/compress.ts +155 -56
- package/src/server/cookies.ts +59 -22
- package/src/server/env.ts +18 -13
- package/src/server/http.ts +201 -33
- package/src/server/index.ts +964 -737
- package/src/server/loader.ts +120 -0
- package/src/server/router.ts +27 -8
- package/src/server/security.ts +87 -39
- package/src/server/standard-schema.ts +42 -11
- package/src/server/static-dir.ts +144 -0
- package/src/ssr/context.ts +96 -10
- package/src/ssr/index.ts +225 -56
- package/src/style.ts +33 -0
- package/src/test/index.ts +5 -0
- package/src/unrenderable.ts +35 -0
package/src/compiler/ssr.ts
CHANGED
|
@@ -1,29 +1,60 @@
|
|
|
1
1
|
// Kay v2 SSR compiler: compiles TSX directly to flat string concatenation, inline for-loops,
|
|
2
|
-
// static attribute literal folding,
|
|
3
|
-
|
|
4
|
-
import type {
|
|
2
|
+
// static attribute literal folding, compile-time static island props, and shared balanced hydration markers.
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
JSXAttributeItem,
|
|
6
|
+
JSXChild,
|
|
7
|
+
JSXElement,
|
|
8
|
+
JSXFragment,
|
|
9
|
+
Node,
|
|
10
|
+
} from 'oxc-parser'
|
|
11
|
+
import type { ModuleAnalysis } from './analyze'
|
|
5
12
|
import { analyzeModule, parseTsx, walk } from './analyze'
|
|
13
|
+
import { mistake } from './mistakes'
|
|
6
14
|
import { SourceEditor } from './patch'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
import {
|
|
16
|
+
classifyJsxChild,
|
|
17
|
+
containsJsx,
|
|
18
|
+
decodeHtmlEntities,
|
|
19
|
+
escapeRawText,
|
|
20
|
+
escapeStaticAttr,
|
|
21
|
+
escapeStaticText,
|
|
22
|
+
EVENT_ATTR,
|
|
23
|
+
filterMeaningfulChildren,
|
|
24
|
+
isBlockedAttr,
|
|
25
|
+
isComponentTag,
|
|
26
|
+
isJsxNode,
|
|
27
|
+
isNodeCapableExpr,
|
|
28
|
+
isStringBoolAttr,
|
|
29
|
+
isUnsafeUrlAttr,
|
|
30
|
+
jsxTagName,
|
|
31
|
+
RAW_TEXT_ELEMENTS,
|
|
32
|
+
staticLiteral,
|
|
33
|
+
unwrapParens,
|
|
34
|
+
VOID_ELEMENTS,
|
|
35
|
+
} from './shared'
|
|
36
|
+
|
|
37
|
+
function containsDirectAwait(root: Node): boolean {
|
|
38
|
+
let found = false
|
|
39
|
+
walk(root, (child) => {
|
|
40
|
+
if (found)
|
|
41
|
+
return false
|
|
42
|
+
if (child !== root && (
|
|
43
|
+
child.type === 'FunctionDeclaration'
|
|
44
|
+
|| child.type === 'FunctionExpression'
|
|
45
|
+
|| child.type === 'ArrowFunctionExpression'
|
|
46
|
+
)) {
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
if (child.type === 'AwaitExpression') {
|
|
50
|
+
found = true
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
return undefined
|
|
54
|
+
})
|
|
55
|
+
return found
|
|
18
56
|
}
|
|
19
57
|
|
|
20
|
-
const VOID_ELEMENTS = new Set([
|
|
21
|
-
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
|
22
|
-
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
|
23
|
-
])
|
|
24
|
-
|
|
25
|
-
const EVENT_ATTR = /^on[A-Z]/
|
|
26
|
-
|
|
27
58
|
export interface IslandBinding {
|
|
28
59
|
id: string
|
|
29
60
|
chunk: string
|
|
@@ -33,6 +64,7 @@ export interface IslandBinding {
|
|
|
33
64
|
export interface CompileSsrOptions {
|
|
34
65
|
islands?: ReadonlyMap<string, IslandBinding> | undefined
|
|
35
66
|
isIsland?: boolean | undefined
|
|
67
|
+
emitMarkers?: boolean | undefined
|
|
36
68
|
analysis?: ModuleAnalysis | undefined
|
|
37
69
|
}
|
|
38
70
|
|
|
@@ -75,80 +107,27 @@ class StringParts {
|
|
|
75
107
|
}
|
|
76
108
|
}
|
|
77
109
|
|
|
78
|
-
|
|
79
|
-
if (!raw.includes('\n'))
|
|
80
|
-
return raw
|
|
81
|
-
const lines = raw.split(/\r\n|\n|\r/)
|
|
82
|
-
let out = ''
|
|
83
|
-
for (let i = 0; i < lines.length; i++) {
|
|
84
|
-
let line = lines[i]!.replace(/\t/g, ' ')
|
|
85
|
-
if (i > 0)
|
|
86
|
-
line = line.replace(/^ +/, '')
|
|
87
|
-
if (i < lines.length - 1)
|
|
88
|
-
line = line.replace(/ +$/, '')
|
|
89
|
-
if (line !== '') {
|
|
90
|
-
if (out !== '')
|
|
91
|
-
out += ' '
|
|
92
|
-
out += line
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return out
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function escapeStaticAttr(str: string): string {
|
|
99
|
-
return str
|
|
100
|
-
.replaceAll('&', '&')
|
|
101
|
-
.replaceAll('"', '"')
|
|
102
|
-
.replaceAll('<', '<')
|
|
103
|
-
.replaceAll('>', '>')
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function jsxTagName(node: AstNode): string {
|
|
107
|
-
if (node.type === 'JSXIdentifier')
|
|
108
|
-
return node.name as string
|
|
109
|
-
if (node.type === 'JSXMemberExpression')
|
|
110
|
-
return `${jsxTagName(node.object)}.${node.property.name}`
|
|
111
|
-
if (node.type === 'JSXNamespacedName')
|
|
112
|
-
return `${node.namespace.name}:${node.name.name}`
|
|
113
|
-
return ''
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function isComponentTag(tag: string): boolean {
|
|
117
|
-
const first = tag.charCodeAt(0)
|
|
118
|
-
return (first >= 65 && first <= 90) || tag.includes('.')
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function unwrapParens(node: AstNode): AstNode {
|
|
122
|
-
let cur = node
|
|
123
|
-
while (cur.type === 'ParenthesizedExpression')
|
|
124
|
-
cur = cur.expression as AstNode
|
|
125
|
-
return cur
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function isSimpleLiteral(node: AstNode): { ok: true, value: string | number | boolean | null } | { ok: false } {
|
|
129
|
-
if (node.type === 'Literal') {
|
|
130
|
-
const v = node.value
|
|
131
|
-
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || v === null)
|
|
132
|
-
return { ok: true, value: v }
|
|
133
|
-
}
|
|
134
|
-
if (node.type === 'UnaryExpression' && node.operator === '-' && node.argument?.type === 'Literal' && typeof node.argument.value === 'number')
|
|
135
|
-
return { ok: true, value: -node.argument.value }
|
|
136
|
-
return { ok: false }
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function isStaticNullish(node: AstNode): boolean {
|
|
110
|
+
function isStaticNullish(node: Node): boolean {
|
|
140
111
|
return (node.type === 'Identifier' && node.name === 'undefined')
|
|
141
|
-
|| (node.type === 'Literal' &&
|
|
112
|
+
|| (node.type === 'Literal' && node.value === null)
|
|
142
113
|
}
|
|
143
114
|
|
|
144
|
-
function formatStaticAttrBranch(attrName: string, node:
|
|
115
|
+
function formatStaticAttrBranch(attrName: string, node: Node): string | undefined {
|
|
116
|
+
if (isBlockedAttr(attrName))
|
|
117
|
+
return '""'
|
|
145
118
|
if (isStaticNullish(node))
|
|
146
119
|
return '""'
|
|
147
120
|
if (node.type === 'Literal') {
|
|
148
|
-
if (node.value ===
|
|
149
|
-
|
|
150
|
-
|
|
121
|
+
if (typeof node.value === 'boolean') {
|
|
122
|
+
if (isStringBoolAttr(attrName))
|
|
123
|
+
return JSON.stringify(` ${attrName}="${node.value ? 'true' : 'false'}"`)
|
|
124
|
+
return node.value ? JSON.stringify(` ${attrName}`) : '""'
|
|
125
|
+
}
|
|
126
|
+
if (typeof node.value === 'string' || typeof node.value === 'number') {
|
|
127
|
+
if (isUnsafeUrlAttr(attrName, node.value))
|
|
128
|
+
return '""'
|
|
151
129
|
return JSON.stringify(` ${attrName}="${escapeStaticAttr(String(node.value))}"`)
|
|
130
|
+
}
|
|
152
131
|
}
|
|
153
132
|
return undefined
|
|
154
133
|
}
|
|
@@ -170,24 +149,16 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
170
149
|
}
|
|
171
150
|
|
|
172
151
|
const islands = options.islands
|
|
173
|
-
const
|
|
152
|
+
const emitMarkers = options.emitMarkers ?? options.isIsland ?? analysis.isInteractive
|
|
174
153
|
|
|
175
|
-
function compileSubExpression(node:
|
|
176
|
-
|
|
177
|
-
walk(node, (child) => {
|
|
178
|
-
if (child.type === 'JSXElement' || child.type === 'JSXFragment') {
|
|
179
|
-
hasNestedJsx = true
|
|
180
|
-
return false
|
|
181
|
-
}
|
|
182
|
-
return undefined
|
|
183
|
-
})
|
|
184
|
-
if (!hasNestedJsx)
|
|
154
|
+
function compileSubExpression(node: Node): string {
|
|
155
|
+
if (!containsJsx(node))
|
|
185
156
|
return code.slice(node.start, node.end)
|
|
186
157
|
|
|
187
158
|
const sub = new SourceEditor(code.slice(node.start, node.end))
|
|
188
|
-
const offset = node.start
|
|
159
|
+
const offset = node.start
|
|
189
160
|
walk(node, (child) => {
|
|
190
|
-
if (child
|
|
161
|
+
if (isJsxNode(child)) {
|
|
191
162
|
sub.replace(child.start - offset, child.end - offset, compileRootJsx(child))
|
|
192
163
|
return false
|
|
193
164
|
}
|
|
@@ -196,10 +167,86 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
196
167
|
return sub.toString()
|
|
197
168
|
}
|
|
198
169
|
|
|
170
|
+
function serializeComponentProps(attrs: readonly JSXAttributeItem[]): {
|
|
171
|
+
propEntries: string[]
|
|
172
|
+
allStaticProps: boolean
|
|
173
|
+
staticPropsObj: Record<string, unknown>
|
|
174
|
+
hydrateExpr: string | undefined
|
|
175
|
+
staticHydrate: string | undefined
|
|
176
|
+
} {
|
|
177
|
+
const propEntries: string[] = []
|
|
178
|
+
let allStaticProps = true
|
|
179
|
+
const staticPropsObj: Record<string, unknown> = {}
|
|
180
|
+
let hydrateExpr: string | undefined
|
|
181
|
+
let staticHydrate: string | undefined
|
|
182
|
+
|
|
183
|
+
for (const attr of attrs) {
|
|
184
|
+
if (attr.type === 'JSXSpreadAttribute') {
|
|
185
|
+
allStaticProps = false
|
|
186
|
+
propEntries.push(`...(${compileSubExpression(attr.argument)})`)
|
|
187
|
+
continue
|
|
188
|
+
}
|
|
189
|
+
const name = jsxTagName(attr.name)
|
|
190
|
+
if (name === 'key')
|
|
191
|
+
continue
|
|
192
|
+
if (name === '$hydrate') {
|
|
193
|
+
if (attr.value?.type === 'Literal' && typeof attr.value.value === 'string') {
|
|
194
|
+
staticHydrate = attr.value.value
|
|
195
|
+
hydrateExpr = JSON.stringify(attr.value.value)
|
|
196
|
+
}
|
|
197
|
+
else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
198
|
+
hydrateExpr = compileSubExpression(attr.value.expression)
|
|
199
|
+
}
|
|
200
|
+
continue
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const propKey = /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name)
|
|
204
|
+
if (attr.value === null) {
|
|
205
|
+
propEntries.push(`${propKey}: true`)
|
|
206
|
+
staticPropsObj[name] = true
|
|
207
|
+
}
|
|
208
|
+
else if (attr.value.type === 'Literal') {
|
|
209
|
+
propEntries.push(`${propKey}: ${JSON.stringify(attr.value.value)}`)
|
|
210
|
+
staticPropsObj[name] = attr.value.value
|
|
211
|
+
}
|
|
212
|
+
else if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
213
|
+
const expr = attr.value.expression
|
|
214
|
+
const lit = staticLiteral(expr)
|
|
215
|
+
if (lit.ok)
|
|
216
|
+
staticPropsObj[name] = lit.value
|
|
217
|
+
else
|
|
218
|
+
allStaticProps = false
|
|
219
|
+
propEntries.push(`${propKey}: ${compileSubExpression(expr)}`)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return { propEntries, allStaticProps, staticPropsObj, hydrateExpr, staticHydrate }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function compileChildrenListToStringExpr(children: readonly JSXChild[]): string {
|
|
227
|
+
const bufVar = `_o${loopCounter++}`
|
|
228
|
+
const stmts: string[] = []
|
|
229
|
+
const parts = new StringParts()
|
|
230
|
+
const flush = () => {
|
|
231
|
+
if (!parts.isEmpty()) {
|
|
232
|
+
stmts.push(`${bufVar} += ${parts.toExpr()};`)
|
|
233
|
+
parts.clear()
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
emitChildrenIntoBuffer(children, bufVar, parts, flush, s => stmts.push(s))
|
|
237
|
+
if (stmts.length === 0)
|
|
238
|
+
return parts.toExpr()
|
|
239
|
+
flush()
|
|
240
|
+
const hasAwait = children.some(c => containsDirectAwait(c))
|
|
241
|
+
if (hasAwait)
|
|
242
|
+
return `(await (async () => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })())`
|
|
243
|
+
return `(() => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })()`
|
|
244
|
+
}
|
|
245
|
+
|
|
199
246
|
// Emits JSX directly into a string accumulator variable `_out` (zero closures on nested `.map()` loops!)
|
|
200
|
-
function emitIntoBuffer(node:
|
|
247
|
+
function emitIntoBuffer(node: JSXElement | JSXFragment, bufVar: string, parts: StringParts, flushParts: () => void, pushStmt: (stmt: string) => void): void {
|
|
201
248
|
if (node.type === 'JSXFragment') {
|
|
202
|
-
emitChildrenIntoBuffer(node.children
|
|
249
|
+
emitChildrenIntoBuffer(node.children, bufVar, parts, flushParts, pushStmt)
|
|
203
250
|
return
|
|
204
251
|
}
|
|
205
252
|
|
|
@@ -212,7 +259,7 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
212
259
|
parts.pushStatic(`<${tag}`)
|
|
213
260
|
let innerHtmlExpr: string | undefined
|
|
214
261
|
|
|
215
|
-
for (const attr of node.openingElement.attributes
|
|
262
|
+
for (const attr of node.openingElement.attributes) {
|
|
216
263
|
if (attr.type === 'JSXSpreadAttribute') {
|
|
217
264
|
parts.pushExpr(`${use('ssrSpread')}(${compileSubExpression(attr.argument)})`)
|
|
218
265
|
continue
|
|
@@ -223,20 +270,24 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
223
270
|
if (rawName === 'innerHTML') {
|
|
224
271
|
if (attr.value?.type === 'Literal')
|
|
225
272
|
innerHtmlExpr = JSON.stringify(String(attr.value.value))
|
|
226
|
-
else if (attr.value?.type === 'JSXExpressionContainer')
|
|
273
|
+
else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
|
|
227
274
|
innerHtmlExpr = `${use('ssrRaw')}(${compileSubExpression(attr.value.expression)})`
|
|
228
275
|
continue
|
|
229
276
|
}
|
|
230
277
|
|
|
231
278
|
const name = rawName === 'className' ? 'class' : rawName === 'htmlFor' ? 'for' : rawName
|
|
279
|
+
if (isBlockedAttr(name))
|
|
280
|
+
continue
|
|
281
|
+
|
|
232
282
|
if (attr.value === null) {
|
|
233
|
-
parts.pushStatic(` ${name}`)
|
|
283
|
+
parts.pushStatic(isStringBoolAttr(name) ? ` ${name}="true"` : ` ${name}`)
|
|
234
284
|
}
|
|
235
285
|
else if (attr.value.type === 'Literal') {
|
|
236
|
-
|
|
286
|
+
if (!isUnsafeUrlAttr(name, attr.value.value))
|
|
287
|
+
parts.pushStatic(` ${name}="${escapeStaticAttr(String(attr.value.value))}"`)
|
|
237
288
|
}
|
|
238
|
-
else if (attr.value.type === 'JSXExpressionContainer') {
|
|
239
|
-
const expr = attr.value.expression
|
|
289
|
+
else if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
290
|
+
const expr = attr.value.expression
|
|
240
291
|
|
|
241
292
|
// Fold static ternary attribute branches (`class={cond ? 'stock out' : 'stock in'}`) with 0 runtime escaping!
|
|
242
293
|
if (name !== 'style' && expr.type === 'ConditionalExpression') {
|
|
@@ -263,259 +314,215 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
263
314
|
if (innerHtmlExpr !== undefined)
|
|
264
315
|
parts.pushExpr(innerHtmlExpr)
|
|
265
316
|
else
|
|
266
|
-
emitChildrenIntoBuffer(node.children
|
|
317
|
+
emitChildrenIntoBuffer(node.children, bufVar, parts, flushParts, pushStmt, RAW_TEXT_ELEMENTS.has(tag) ? tag as 'style' | 'script' : undefined)
|
|
267
318
|
parts.pushStatic(`</${tag}>`)
|
|
268
319
|
}
|
|
269
320
|
|
|
270
321
|
function emitChildrenIntoBuffer(
|
|
271
|
-
children:
|
|
322
|
+
children: readonly JSXChild[],
|
|
272
323
|
bufVar: string,
|
|
273
324
|
parts: StringParts,
|
|
274
325
|
flushParts: () => void,
|
|
275
326
|
pushStmt: (stmt: string) => void,
|
|
276
327
|
rawTextTag?: 'style' | 'script',
|
|
277
328
|
): void {
|
|
278
|
-
const meaningful = children
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
else if (child.type === 'JSXElement' || child.type === 'JSXFragment') {
|
|
295
|
-
emitIntoBuffer(child, bufVar, parts, flushParts, pushStmt)
|
|
329
|
+
const meaningful = filterMeaningfulChildren(children)
|
|
330
|
+
const slots = meaningful.map(c => ({ child: c, slot: classifyJsxChild(c) })).filter(s => s.slot !== undefined)
|
|
331
|
+
const hasSiblings = slots.length > 1
|
|
332
|
+
let hadVariableRange = false
|
|
333
|
+
|
|
334
|
+
for (let i = 0; i < slots.length; i++) {
|
|
335
|
+
const slot = slots[i]!.slot!
|
|
336
|
+
const isLast = i === slots.length - 1
|
|
337
|
+
|
|
338
|
+
if (slot.kind === 'static-text') {
|
|
339
|
+
if (rawTextTag !== undefined)
|
|
340
|
+
parts.pushStatic(escapeRawText(slot.text, rawTextTag))
|
|
341
|
+
else
|
|
342
|
+
parts.pushStatic(escapeStaticText(slot.fromLiteral ? slot.text : decodeHtmlEntities(slot.text)))
|
|
343
|
+
continue
|
|
296
344
|
}
|
|
297
|
-
else if (child.type === 'JSXExpressionContainer') {
|
|
298
|
-
const expr = child.expression as AstNode
|
|
299
345
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
346
|
+
if (rawTextTag !== undefined) {
|
|
347
|
+
const rawChild = slots[i]!.child
|
|
348
|
+
if (rawChild.type === 'JSXExpressionContainer' && rawChild.expression.type !== 'JSXEmptyExpression') {
|
|
349
|
+
const expr = rawChild.expression
|
|
350
|
+
if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0 && expr.quasis.length === 1) {
|
|
351
|
+
const cooked = expr.quasis[0]?.value.cooked
|
|
352
|
+
if (typeof cooked === 'string') {
|
|
353
|
+
parts.pushStatic(escapeRawText(cooked, rawTextTag))
|
|
354
|
+
continue
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
parts.pushExpr(`${use('ssrRawText')}(${JSON.stringify(rawTextTag)}, ${compileSubExpression(expr)})`)
|
|
303
358
|
continue
|
|
304
359
|
}
|
|
360
|
+
}
|
|
305
361
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
&& (expr.expressions as AstNode[]).length === 0
|
|
311
|
-
&& (expr.quasis as AstNode[]).length === 1
|
|
312
|
-
) {
|
|
313
|
-
const cooked = expr.quasis[0]?.value?.cooked
|
|
314
|
-
if (typeof cooked === 'string') {
|
|
315
|
-
parts.pushStatic(escapeRawText(cooked, rawTextTag))
|
|
316
|
-
continue
|
|
317
|
-
}
|
|
318
|
-
}
|
|
362
|
+
if (slot.kind === 'element') {
|
|
363
|
+
emitIntoBuffer(slot.node, bufVar, parts, flushParts, pushStmt)
|
|
364
|
+
continue
|
|
365
|
+
}
|
|
319
366
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
367
|
+
if (slot.kind === 'component' || slot.kind === 'fragment') {
|
|
368
|
+
if (emitMarkers && hasSiblings) {
|
|
369
|
+
parts.pushStatic('<!--k[-->')
|
|
370
|
+
emitIntoBuffer(slot.node, bufVar, parts, flushParts, pushStmt)
|
|
371
|
+
if (!isLast)
|
|
372
|
+
parts.pushStatic('<!--k]-->')
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
emitIntoBuffer(slot.node, bufVar, parts, flushParts, pushStmt)
|
|
323
376
|
}
|
|
377
|
+
hadVariableRange = true
|
|
378
|
+
continue
|
|
379
|
+
}
|
|
324
380
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
const arrVar = `_a${n}`
|
|
346
|
-
const idxVar = idxParam ?? `_i${n}`
|
|
347
|
-
const innerStmts: string[] = []
|
|
348
|
-
const innerParts = new StringParts()
|
|
349
|
-
const flushInner = () => {
|
|
350
|
-
if (!innerParts.isEmpty()) {
|
|
351
|
-
innerStmts.push(`${bufVar} += ${innerParts.toExpr()};`)
|
|
352
|
-
innerParts.clear()
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
emitIntoBuffer(body, bufVar, innerParts, flushInner, s => innerStmts.push(s))
|
|
356
|
-
flushInner()
|
|
357
|
-
|
|
358
|
-
pushStmt(
|
|
359
|
-
`const ${arrVar} = ${target}; if (${arrVar}) for (let ${idxVar} = 0; ${idxVar} < ${arrVar}.length; ${idxVar}++) { const ${itemParam} = ${arrVar}[${idxVar}]; ${innerStmts.join(' ')} }`,
|
|
360
|
-
)
|
|
361
|
-
if (isIsland && !isLast)
|
|
362
|
-
parts.pushStatic('<!---->')
|
|
363
|
-
continue
|
|
364
|
-
}
|
|
381
|
+
if (slot.kind === 'loop') {
|
|
382
|
+
const { match } = slot
|
|
383
|
+
const needsBracket = emitMarkers && (!isLast || hadVariableRange)
|
|
384
|
+
if (needsBracket)
|
|
385
|
+
parts.pushStatic('<!--k[-->')
|
|
386
|
+
|
|
387
|
+
const itemParam = match.params[0]?.type === 'Identifier' ? match.params[0].name : undefined
|
|
388
|
+
const idxParam = match.params[1]?.type === 'Identifier' ? match.params[1].name : undefined
|
|
389
|
+
|
|
390
|
+
if (match.isJsxBody && itemParam !== undefined && match.params.length <= 2 && (match.params.length < 2 || idxParam !== undefined)) {
|
|
391
|
+
flushParts()
|
|
392
|
+
const n = loopCounter++
|
|
393
|
+
const arrVar = `_a${n}`
|
|
394
|
+
const idxVar = idxParam ?? `_i${n}`
|
|
395
|
+
const innerStmts: string[] = []
|
|
396
|
+
const innerParts = new StringParts()
|
|
397
|
+
const flushInner = () => {
|
|
398
|
+
if (!innerParts.isEmpty()) {
|
|
399
|
+
innerStmts.push(`${bufVar} += ${innerParts.toExpr()};`)
|
|
400
|
+
innerParts.clear()
|
|
365
401
|
}
|
|
366
402
|
}
|
|
403
|
+
emitIntoBuffer(match.body as JSXElement | JSXFragment, bufVar, innerParts, flushInner, s => innerStmts.push(s))
|
|
404
|
+
flushInner()
|
|
405
|
+
|
|
406
|
+
const target = compileSubExpression(match.target)
|
|
407
|
+
pushStmt(
|
|
408
|
+
`const ${arrVar} = ${target}; if (${arrVar}) for (let ${idxVar} = 0; ${idxVar} < ${arrVar}.length; ${idxVar}++) { const ${itemParam} = ${arrVar}[${idxVar}]; ${innerStmts.join(' ')} }`,
|
|
409
|
+
)
|
|
410
|
+
}
|
|
411
|
+
else if (match.isJsxBody) {
|
|
412
|
+
const target = compileSubExpression(match.target)
|
|
413
|
+
const params = match.params.map(p => code.slice(p.start, p.end)).join(', ')
|
|
414
|
+
parts.pushExpr(`${use('ssrLoop')}(${target}, (${params}) => ${compileJsxToStringExpr(match.body as JSXElement | JSXFragment)})`)
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
parts.pushExpr(`${use('esc')}(${compileSubExpression(match.call)})`)
|
|
367
418
|
}
|
|
368
419
|
|
|
369
|
-
|
|
420
|
+
if (needsBracket)
|
|
421
|
+
parts.pushStatic('<!--k]-->')
|
|
422
|
+
hadVariableRange = true
|
|
423
|
+
continue
|
|
370
424
|
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
425
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
426
|
+
if (slot.kind === 'and-show') {
|
|
427
|
+
const left = compileSubExpression(slot.expr.left)
|
|
428
|
+
const right = compileJsxToStringExpr(slot.right)
|
|
429
|
+
if (emitMarkers && hasSiblings) {
|
|
430
|
+
parts.pushStatic('<!--k[-->')
|
|
431
|
+
parts.pushExpr(`((${left}) ? (${right}) : "")`)
|
|
432
|
+
parts.pushStatic('<!--k]-->')
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
parts.pushExpr(`((${left}) ? (${right}) : "")`)
|
|
436
|
+
}
|
|
437
|
+
hadVariableRange = true
|
|
438
|
+
continue
|
|
439
|
+
}
|
|
377
440
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
if ((cb.type === 'ArrowFunctionExpression' || cb.type === 'FunctionExpression') && cb.body !== null) {
|
|
387
|
-
const body = (cb.body.type === 'ParenthesizedExpression' ? cb.body.expression : cb.body) as AstNode
|
|
388
|
-
if (body.type === 'JSXElement' || body.type === 'JSXFragment') {
|
|
389
|
-
const target = compileSubExpression(expr.callee.object)
|
|
390
|
-
const params = (cb.params as AstNode[]).map(p => code.slice(p.start, p.end)).join(', ')
|
|
391
|
-
const rendered = `${use('ssrLoop')}(${target}, (${params}) => ${compileJsxToStringExpr(body)})`
|
|
392
|
-
return isIsland && !isLast ? `(${rendered}) + "<!---->"` : rendered
|
|
441
|
+
if (slot.kind === 'ternary-show') {
|
|
442
|
+
const test = compileSubExpression(slot.expr.test)
|
|
443
|
+
const cons = compileExpressionAsString(slot.cons)
|
|
444
|
+
const alt = compileExpressionAsString(slot.alt)
|
|
445
|
+
if (emitMarkers && hasSiblings) {
|
|
446
|
+
parts.pushStatic('<!--k[-->')
|
|
447
|
+
parts.pushExpr(`((${test}) ? (${cons}) : (${alt}))`)
|
|
448
|
+
parts.pushStatic('<!--k]-->')
|
|
393
449
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
walk(body, (c) => {
|
|
397
|
-
if (c.type === 'JSXElement' || c.type === 'JSXFragment') {
|
|
398
|
-
blockHasJsx = true
|
|
399
|
-
return false
|
|
400
|
-
}
|
|
401
|
-
return undefined
|
|
402
|
-
})
|
|
403
|
-
if (blockHasJsx) {
|
|
404
|
-
const rendered = `${use('esc')}(${compileSubExpression(expr)})`
|
|
405
|
-
return isIsland && !isLast ? `(${rendered}) + "<!---->"` : rendered
|
|
406
|
-
}
|
|
450
|
+
else {
|
|
451
|
+
parts.pushExpr(`((${test}) ? (${cons}) : (${alt}))`)
|
|
407
452
|
}
|
|
453
|
+
hadVariableRange = true
|
|
454
|
+
continue
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// Generic expression slot
|
|
458
|
+
const exprCode = compileSubExpression(slot.expr)
|
|
459
|
+
const rendered = `${use('esc')}(${exprCode})`
|
|
460
|
+
if (isNodeCapableExpr(slot.expr)) {
|
|
461
|
+
if (emitMarkers && hasSiblings) {
|
|
462
|
+
parts.pushStatic('<!--k[-->')
|
|
463
|
+
parts.pushExpr(rendered)
|
|
464
|
+
if (!isLast)
|
|
465
|
+
parts.pushStatic('<!--k]-->')
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
parts.pushExpr(rendered)
|
|
469
|
+
}
|
|
470
|
+
hadVariableRange = true
|
|
471
|
+
}
|
|
472
|
+
else if (emitMarkers && hasSiblings) {
|
|
473
|
+
parts.pushStatic('<!---->')
|
|
474
|
+
parts.pushExpr(rendered)
|
|
475
|
+
if (!isLast)
|
|
476
|
+
parts.pushStatic('<!---->')
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
parts.pushExpr(rendered)
|
|
408
480
|
}
|
|
409
481
|
}
|
|
482
|
+
}
|
|
410
483
|
|
|
484
|
+
function compileExpressionAsString(expr: Node): string {
|
|
485
|
+
if (isJsxNode(expr))
|
|
486
|
+
return compileJsxToStringExpr(expr)
|
|
411
487
|
if (expr.type === 'LogicalExpression' && expr.operator === '&&') {
|
|
412
488
|
const right = unwrapParens(expr.right)
|
|
413
|
-
if (right
|
|
414
|
-
|
|
415
|
-
const emptyVal = isIsland && !isLast ? '"<!---->"' : '""'
|
|
416
|
-
return `((${left}) ? (${compileJsxToStringExpr(right)}) : ${emptyVal})`
|
|
417
|
-
}
|
|
489
|
+
if (isJsxNode(right))
|
|
490
|
+
return `((${compileSubExpression(expr.left)}) ? (${compileJsxToStringExpr(right)}) : "")`
|
|
418
491
|
}
|
|
419
|
-
|
|
420
492
|
if (expr.type === 'ConditionalExpression') {
|
|
421
493
|
const consNode = unwrapParens(expr.consequent)
|
|
422
494
|
const altNode = unwrapParens(expr.alternate)
|
|
423
|
-
if (consNode
|
|
424
|
-
|
|
425
|
-
const cons = compileExpressionAsString(consNode, false, true)
|
|
426
|
-
const alt = compileExpressionAsString(altNode, false, true)
|
|
427
|
-
const base = `((${test}) ? (${cons}) : (${alt}))`
|
|
428
|
-
return isIsland && !isLast ? `(${base} || "<!---->")` : base
|
|
429
|
-
}
|
|
495
|
+
if (isJsxNode(consNode) || isJsxNode(altNode))
|
|
496
|
+
return `((${compileSubExpression(expr.test)}) ? (${compileExpressionAsString(consNode)}) : (${compileExpressionAsString(altNode)}))`
|
|
430
497
|
}
|
|
431
|
-
|
|
432
|
-
const exprCode = compileSubExpression(expr)
|
|
433
|
-
const rendered = `${use('esc')}(${exprCode})`
|
|
434
|
-
if (isIsland && hasSiblings)
|
|
435
|
-
return isLast ? `"<!---->" + ${rendered}` : `"<!---->" + ${rendered} + "<!---->"`
|
|
436
|
-
return rendered
|
|
498
|
+
return `${use('esc')}(${compileSubExpression(expr)})`
|
|
437
499
|
}
|
|
438
500
|
|
|
439
|
-
function emitComponent(node:
|
|
440
|
-
const
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
const staticPropsObj: Record<string, unknown> = {}
|
|
444
|
-
let hydrateExpr: string | undefined
|
|
445
|
-
let staticHydrate: string | undefined
|
|
446
|
-
|
|
447
|
-
for (const attr of attrs) {
|
|
448
|
-
if (attr.type === 'JSXSpreadAttribute') {
|
|
449
|
-
allStaticProps = false
|
|
450
|
-
propEntries.push(`...(${compileSubExpression(attr.argument)})`)
|
|
451
|
-
continue
|
|
452
|
-
}
|
|
453
|
-
const name = jsxTagName(attr.name)
|
|
454
|
-
if (name === 'key')
|
|
455
|
-
continue
|
|
456
|
-
if (name === '$hydrate') {
|
|
457
|
-
if (attr.value?.type === 'Literal' && typeof attr.value.value === 'string') {
|
|
458
|
-
staticHydrate = attr.value.value
|
|
459
|
-
hydrateExpr = JSON.stringify(attr.value.value)
|
|
460
|
-
}
|
|
461
|
-
else if (attr.value?.type === 'JSXExpressionContainer') {
|
|
462
|
-
hydrateExpr = compileSubExpression(attr.value.expression)
|
|
463
|
-
}
|
|
464
|
-
continue
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
const propKey = /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name)
|
|
468
|
-
if (attr.value === null) {
|
|
469
|
-
propEntries.push(`${propKey}: true`)
|
|
470
|
-
staticPropsObj[name] = true
|
|
471
|
-
}
|
|
472
|
-
else if (attr.value.type === 'Literal') {
|
|
473
|
-
propEntries.push(`${propKey}: ${JSON.stringify(attr.value.value)}`)
|
|
474
|
-
staticPropsObj[name] = attr.value.value
|
|
475
|
-
}
|
|
476
|
-
else if (attr.value.type === 'JSXExpressionContainer') {
|
|
477
|
-
const expr = attr.value.expression as AstNode
|
|
478
|
-
const lit = isSimpleLiteral(expr)
|
|
479
|
-
if (lit.ok)
|
|
480
|
-
staticPropsObj[name] = lit.value
|
|
481
|
-
else
|
|
482
|
-
allStaticProps = false
|
|
483
|
-
propEntries.push(`${propKey}: ${compileSubExpression(expr)}`)
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
const childNodes = node.children as AstNode[]
|
|
488
|
-
const meaningfulChildren = childNodes.filter(c =>
|
|
489
|
-
!(c.type === 'JSXText' && cleanJsxText(c.value as string) === '')
|
|
490
|
-
&& !(c.type === 'JSXExpressionContainer' && c.expression.type === 'JSXEmptyExpression'),
|
|
491
|
-
)
|
|
501
|
+
function emitComponent(node: JSXElement, tag: string, parts: StringParts): void {
|
|
502
|
+
const { propEntries, allStaticProps, staticPropsObj, hydrateExpr, staticHydrate } = serializeComponentProps(node.openingElement.attributes)
|
|
503
|
+
const childNodes = node.children
|
|
504
|
+
const meaningfulChildren = filterMeaningfulChildren(childNodes)
|
|
492
505
|
const hasChildren = meaningfulChildren.length > 0
|
|
493
506
|
|
|
494
|
-
// Special case: <Suspense fallback={...}><AsyncComp
|
|
507
|
+
// Special case: <Suspense fallback={...}><AsyncComp .../></Suspense> passes the raw component call/Promise!
|
|
495
508
|
if (tag === 'Suspense') {
|
|
496
509
|
if (meaningfulChildren.length === 1) {
|
|
497
510
|
const only = meaningfulChildren[0]!
|
|
498
511
|
if (only.type === 'JSXElement' && isComponentTag(jsxTagName(only.openingElement.name))) {
|
|
499
512
|
const innerTag = jsxTagName(only.openingElement.name)
|
|
500
|
-
const
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
if (attr.value === null)
|
|
506
|
-
innerProps.push(`${k}: true`)
|
|
507
|
-
else if (attr.value.type === 'Literal')
|
|
508
|
-
innerProps.push(`${k}: ${JSON.stringify(attr.value.value)}`)
|
|
509
|
-
else if (attr.value.type === 'JSXExpressionContainer')
|
|
510
|
-
innerProps.push(`${k}: ${compileSubExpression(attr.value.expression)}`)
|
|
511
|
-
}
|
|
513
|
+
const inner = serializeComponentProps(only.openingElement.attributes)
|
|
514
|
+
const innerMeaningful = filterMeaningfulChildren(only.children)
|
|
515
|
+
if (innerMeaningful.length > 0) {
|
|
516
|
+
const innerChildExpr = compileChildrenListToStringExpr(only.children)
|
|
517
|
+
inner.propEntries.push(`children: ${use('raw')}(${innerChildExpr})`)
|
|
512
518
|
}
|
|
513
|
-
propEntries.
|
|
519
|
+
const innerObj = inner.propEntries.length > 0 ? `{ ${inner.propEntries.join(', ')} }` : '{}'
|
|
520
|
+
propEntries.push(`children: ${innerTag}(${innerObj})`)
|
|
514
521
|
}
|
|
515
|
-
else if (only.type === 'JSXExpressionContainer') {
|
|
522
|
+
else if (only.type === 'JSXExpressionContainer' && only.expression.type !== 'JSXEmptyExpression') {
|
|
516
523
|
propEntries.push(`children: ${compileSubExpression(only.expression)}`)
|
|
517
524
|
}
|
|
518
|
-
else {
|
|
525
|
+
else if (isJsxNode(only)) {
|
|
519
526
|
propEntries.push(`children: ${use('raw')}(${compileJsxToStringExpr(only)})`)
|
|
520
527
|
}
|
|
521
528
|
}
|
|
@@ -524,9 +531,13 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
524
531
|
return
|
|
525
532
|
}
|
|
526
533
|
|
|
527
|
-
const
|
|
534
|
+
const childrenHaveAwait = hasChildren && childNodes.some(c => containsDirectAwait(c))
|
|
535
|
+
const childExpr = hasChildren ? compileChildrenListToStringExpr(childNodes) : '""'
|
|
536
|
+
|
|
537
|
+
const islandInfo = islands?.get(tag)
|
|
538
|
+
if (islandInfo === undefined && islands !== undefined && hydrateExpr !== undefined)
|
|
539
|
+
throw mistake(`$hydrate on <${tag}>`, filename, `${tag} is not an island, so it never hydrates; drop $hydrate, or give ${tag} state or handlers`)
|
|
528
540
|
|
|
529
|
-
const islandInfo = !isIsland ? islands?.get(tag) : undefined
|
|
530
541
|
if (islandInfo !== undefined) {
|
|
531
542
|
const propsCode = `{ ${propEntries.join(', ')} }`
|
|
532
543
|
const callPropsCode = hasChildren
|
|
@@ -534,8 +545,9 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
534
545
|
: propsCode
|
|
535
546
|
const resolvedHydrate = staticHydrate ?? (hydrateExpr === undefined ? islandInfo.defaultHydrate : undefined)
|
|
536
547
|
|
|
537
|
-
// Compile-time static island props fast path
|
|
538
|
-
|
|
548
|
+
// Compile-time static island props fast path (only when not inside an island-reachable component and children don't contain direct `await`):
|
|
549
|
+
// 0 JSON.stringify / Object.keys / escAttr at runtime, while still recording rendered islands & island ID scope!
|
|
550
|
+
if (!emitMarkers && !childrenHaveAwait && allStaticProps && (hydrateExpr === undefined || staticHydrate !== undefined)) {
|
|
539
551
|
const pAttr = Object.keys(staticPropsObj).length > 0
|
|
540
552
|
? ` p="${escapeStaticAttr(JSON.stringify(staticPropsObj))}"`
|
|
541
553
|
: ''
|
|
@@ -543,20 +555,72 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
543
555
|
? ` h="${escapeStaticAttr(resolvedHydrate)}"`
|
|
544
556
|
: ''
|
|
545
557
|
const openTag = `<kay-island c="${escapeStaticAttr(islandInfo.chunk)}"${pAttr}${hAttr}>`
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
558
|
+
const hydrateArg = resolvedHydrate !== undefined ? JSON.stringify(resolvedHydrate) : 'undefined'
|
|
559
|
+
const renderArg = resolvedHydrate === 'client'
|
|
560
|
+
? 'undefined'
|
|
561
|
+
: `() => ${use('ssrChild')}(${tag}(${callPropsCode}))`
|
|
562
|
+
parts.pushExpr(`${use('ssrIslandStatic')}(${JSON.stringify(islandInfo.chunk)}, ${JSON.stringify(openTag)}, ${renderArg}, ${hydrateArg})`)
|
|
550
563
|
return
|
|
551
564
|
}
|
|
552
565
|
|
|
553
566
|
const hydrateArg = hydrateExpr ?? (islandInfo.defaultHydrate !== undefined ? JSON.stringify(islandInfo.defaultHydrate) : 'undefined')
|
|
567
|
+
if (!childrenHaveAwait && allStaticProps && (hydrateExpr === undefined || staticHydrate !== undefined)) {
|
|
568
|
+
parts.pushExpr(
|
|
569
|
+
`${use('ssrIsland')}(${JSON.stringify(islandInfo.chunk)}, () => ${use('ssrChild')}(${tag}(${callPropsCode})), ${propsCode}, ${hydrateArg})`,
|
|
570
|
+
)
|
|
571
|
+
return
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// Hoist dynamic island props / awaited children into a single binding so expressions evaluate once!
|
|
575
|
+
const pVar = `_p${loopCounter++}`
|
|
576
|
+
const argNames = [pVar]
|
|
577
|
+
const argExprs = [propsCode]
|
|
578
|
+
let callArg = pVar
|
|
579
|
+
if (hasChildren) {
|
|
580
|
+
let childSlotVal = childExpr
|
|
581
|
+
if (childrenHaveAwait) {
|
|
582
|
+
const cVar = `_c${loopCounter++}`
|
|
583
|
+
argNames.push(cVar)
|
|
584
|
+
argExprs.push(childExpr)
|
|
585
|
+
childSlotVal = cVar
|
|
586
|
+
}
|
|
587
|
+
callArg = `{ ...${pVar}, children: ${use('raw')}("<kay-slot>" + (${childSlotVal}) + "</kay-slot>") }`
|
|
588
|
+
}
|
|
589
|
+
let hArg = hydrateArg
|
|
590
|
+
if (hydrateExpr !== undefined && staticHydrate === undefined) {
|
|
591
|
+
const hVar = `_h${loopCounter++}`
|
|
592
|
+
argNames.push(hVar)
|
|
593
|
+
argExprs.push(hydrateExpr)
|
|
594
|
+
hArg = hVar
|
|
595
|
+
}
|
|
554
596
|
parts.pushExpr(
|
|
555
|
-
|
|
597
|
+
`((${argNames.join(', ')}) => ${use('ssrIsland')}(${JSON.stringify(islandInfo.chunk)}, () => ${use('ssrChild')}(${tag}(${callArg})), ${pVar}, ${hArg}))(${argExprs.join(', ')})`,
|
|
556
598
|
)
|
|
557
599
|
return
|
|
558
600
|
}
|
|
559
601
|
|
|
602
|
+
const childrenHaveDynamic = hasChildren && childNodes.some((c) => {
|
|
603
|
+
let dynamic = false
|
|
604
|
+
walk(c, (n) => {
|
|
605
|
+
if (dynamic)
|
|
606
|
+
return false
|
|
607
|
+
if (n.type === 'JSXExpressionContainer' && n.expression.type !== 'JSXEmptyExpression')
|
|
608
|
+
dynamic = true
|
|
609
|
+
else if (n.type === 'JSXElement' && isComponentTag(jsxTagName(n.openingElement.name)))
|
|
610
|
+
dynamic = true
|
|
611
|
+
return undefined
|
|
612
|
+
})
|
|
613
|
+
return dynamic
|
|
614
|
+
})
|
|
615
|
+
|
|
616
|
+
// Lazy memoized getter for non-async dynamic/component children so Providers & context components run before their children!
|
|
617
|
+
if (childrenHaveDynamic && !childrenHaveAwait) {
|
|
618
|
+
const cVar = `_c${loopCounter++}`
|
|
619
|
+
const propsWithGetter = [...propEntries, `get children() { return ${cVar} === undefined ? (${cVar} = ${use('raw')}(${childExpr})) : ${cVar} }`].join(', ')
|
|
620
|
+
parts.pushExpr(`${use('ssrChild')}(${tag}((() => { let ${cVar}; return { ${propsWithGetter} }; })()))`)
|
|
621
|
+
return
|
|
622
|
+
}
|
|
623
|
+
|
|
560
624
|
if (hasChildren)
|
|
561
625
|
propEntries.push(`children: ${use('raw')}(${childExpr})`)
|
|
562
626
|
|
|
@@ -564,7 +628,7 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
564
628
|
parts.pushExpr(`${use('ssrChild')}(${tag}(${propsObj}))`)
|
|
565
629
|
}
|
|
566
630
|
|
|
567
|
-
function compileJsxToStringExpr(node:
|
|
631
|
+
function compileJsxToStringExpr(node: JSXElement | JSXFragment): string {
|
|
568
632
|
const bufVar = `_o${loopCounter++}`
|
|
569
633
|
const stmts: string[] = []
|
|
570
634
|
const parts = new StringParts()
|
|
@@ -583,15 +647,17 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
583
647
|
return parts.toExpr()
|
|
584
648
|
|
|
585
649
|
flush()
|
|
650
|
+
if (containsDirectAwait(node))
|
|
651
|
+
return `(await (async () => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })())`
|
|
586
652
|
return `(() => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })()`
|
|
587
653
|
}
|
|
588
654
|
|
|
589
|
-
function compileRootJsx(node:
|
|
655
|
+
function compileRootJsx(node: JSXElement | JSXFragment): string {
|
|
590
656
|
return `${use('raw')}(${compileJsxToStringExpr(node)})`
|
|
591
657
|
}
|
|
592
658
|
|
|
593
659
|
walk(program, (node) => {
|
|
594
|
-
if (node
|
|
660
|
+
if (isJsxNode(node)) {
|
|
595
661
|
editor.replace(node.start, node.end, compileRootJsx(node))
|
|
596
662
|
return false
|
|
597
663
|
}
|