@kayjs/kay 2.0.0-beta.0 → 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 +10 -10
- package/src/build/files.ts +136 -0
- package/src/build/graph.ts +158 -0
- package/src/build/index.ts +204 -1460
- 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 +297 -94
- package/src/cli/new.ts +53 -32
- package/src/compiler/analyze.ts +666 -91
- package/src/compiler/dom.ts +808 -743
- 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 +329 -342
- package/src/context.ts +120 -0
- package/src/dev/index.ts +271 -75
- package/src/dom/action.ts +23 -13
- package/src/dom/extras.ts +121 -83
- package/src/dom/index.ts +114 -77
- package/src/dom/island.ts +71 -36
- package/src/dom/loop.ts +28 -20
- 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 +114 -18
- package/src/reactivity/index.ts +237 -213
- package/src/reactivity/selector.ts +11 -9
- package/src/reactivity/store.ts +36 -11
- package/src/server/action.ts +147 -76
- package/src/server/compress.ts +116 -96
- package/src/server/cookies.ts +46 -16
- package/src/server/env.ts +18 -13
- package/src/server/http.ts +107 -54
- package/src/server/index.ts +887 -870
- package/src/server/loader.ts +120 -0
- package/src/server/router.ts +22 -8
- package/src/server/security.ts +48 -58
- package/src/server/standard-schema.ts +42 -11
- package/src/server/static-dir.ts +144 -0
- package/src/ssr/context.ts +95 -10
- package/src/ssr/index.ts +167 -66
- package/src/style.ts +33 -0
- package/src/test/index.ts +2 -1
- package/src/unrenderable.ts +35 -0
package/src/compiler/ssr.ts
CHANGED
|
@@ -1,48 +1,40 @@
|
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const URL_ATTRS = new Set(['href', 'src', 'action', 'formaction', 'xlink:href'])
|
|
31
|
-
const UNSAFE_URL_RE = /^(?:javascript|vbscript):/i
|
|
32
|
-
const STRING_BOOL_ATTRS = new Set(['draggable', 'spellcheck', 'contenteditable'])
|
|
33
|
-
|
|
34
|
-
function isStringBoolAttr(name: string): boolean {
|
|
35
|
-
const lower = name.toLowerCase()
|
|
36
|
-
return lower.startsWith('aria-') || STRING_BOOL_ATTRS.has(lower)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function isUnsafeUrlAttr(name: string, value: unknown): boolean {
|
|
40
|
-
if (!URL_ATTRS.has(name.toLowerCase()))
|
|
41
|
-
return false
|
|
42
|
-
return UNSAFE_URL_RE.test(String(value).replace(/[\u0000-\u0020]+/g, ''))
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function containsDirectAwait(root: AstNode): boolean {
|
|
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 {
|
|
46
38
|
let found = false
|
|
47
39
|
walk(root, (child) => {
|
|
48
40
|
if (found)
|
|
@@ -115,73 +107,14 @@ class StringParts {
|
|
|
115
107
|
}
|
|
116
108
|
}
|
|
117
109
|
|
|
118
|
-
|
|
119
|
-
if (!raw.includes('\n'))
|
|
120
|
-
return raw
|
|
121
|
-
const lines = raw.split(/\r\n|\n|\r/)
|
|
122
|
-
let out = ''
|
|
123
|
-
for (let i = 0; i < lines.length; i++) {
|
|
124
|
-
let line = lines[i]!.replace(/\t/g, ' ')
|
|
125
|
-
if (i > 0)
|
|
126
|
-
line = line.replace(/^ +/, '')
|
|
127
|
-
if (i < lines.length - 1)
|
|
128
|
-
line = line.replace(/ +$/, '')
|
|
129
|
-
if (line !== '') {
|
|
130
|
-
if (out !== '')
|
|
131
|
-
out += ' '
|
|
132
|
-
out += line
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return out
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function escapeStaticAttr(str: string): string {
|
|
139
|
-
return str
|
|
140
|
-
.replaceAll('&', '&')
|
|
141
|
-
.replaceAll('"', '"')
|
|
142
|
-
.replaceAll('<', '<')
|
|
143
|
-
.replaceAll('>', '>')
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function jsxTagName(node: AstNode): string {
|
|
147
|
-
if (node.type === 'JSXIdentifier')
|
|
148
|
-
return node.name as string
|
|
149
|
-
if (node.type === 'JSXMemberExpression')
|
|
150
|
-
return `${jsxTagName(node.object)}.${node.property.name}`
|
|
151
|
-
if (node.type === 'JSXNamespacedName')
|
|
152
|
-
return `${node.namespace.name}:${node.name.name}`
|
|
153
|
-
return ''
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function isComponentTag(tag: string): boolean {
|
|
157
|
-
const first = tag.charCodeAt(0)
|
|
158
|
-
return (first >= 65 && first <= 90) || tag.includes('.')
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function unwrapParens(node: AstNode): AstNode {
|
|
162
|
-
let cur = node
|
|
163
|
-
while (cur.type === 'ParenthesizedExpression')
|
|
164
|
-
cur = cur.expression as AstNode
|
|
165
|
-
return cur
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function isSimpleLiteral(node: AstNode): { ok: true, value: string | number | boolean | null } | { ok: false } {
|
|
169
|
-
if (node.type === 'Literal') {
|
|
170
|
-
const v = node.value
|
|
171
|
-
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || v === null)
|
|
172
|
-
return { ok: true, value: v }
|
|
173
|
-
}
|
|
174
|
-
if (node.type === 'UnaryExpression' && node.operator === '-' && node.argument?.type === 'Literal' && typeof node.argument.value === 'number')
|
|
175
|
-
return { ok: true, value: -node.argument.value }
|
|
176
|
-
return { ok: false }
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function isStaticNullish(node: AstNode): boolean {
|
|
110
|
+
function isStaticNullish(node: Node): boolean {
|
|
180
111
|
return (node.type === 'Identifier' && node.name === 'undefined')
|
|
181
112
|
|| (node.type === 'Literal' && node.value === null)
|
|
182
113
|
}
|
|
183
114
|
|
|
184
|
-
function formatStaticAttrBranch(attrName: string, node:
|
|
115
|
+
function formatStaticAttrBranch(attrName: string, node: Node): string | undefined {
|
|
116
|
+
if (isBlockedAttr(attrName))
|
|
117
|
+
return '""'
|
|
185
118
|
if (isStaticNullish(node))
|
|
186
119
|
return '""'
|
|
187
120
|
if (node.type === 'Literal') {
|
|
@@ -218,22 +151,14 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
218
151
|
const islands = options.islands
|
|
219
152
|
const emitMarkers = options.emitMarkers ?? options.isIsland ?? analysis.isInteractive
|
|
220
153
|
|
|
221
|
-
function compileSubExpression(node:
|
|
222
|
-
|
|
223
|
-
walk(node, (child) => {
|
|
224
|
-
if (child.type === 'JSXElement' || child.type === 'JSXFragment') {
|
|
225
|
-
hasNestedJsx = true
|
|
226
|
-
return false
|
|
227
|
-
}
|
|
228
|
-
return undefined
|
|
229
|
-
})
|
|
230
|
-
if (!hasNestedJsx)
|
|
154
|
+
function compileSubExpression(node: Node): string {
|
|
155
|
+
if (!containsJsx(node))
|
|
231
156
|
return code.slice(node.start, node.end)
|
|
232
157
|
|
|
233
158
|
const sub = new SourceEditor(code.slice(node.start, node.end))
|
|
234
|
-
const offset = node.start
|
|
159
|
+
const offset = node.start
|
|
235
160
|
walk(node, (child) => {
|
|
236
|
-
if (child
|
|
161
|
+
if (isJsxNode(child)) {
|
|
237
162
|
sub.replace(child.start - offset, child.end - offset, compileRootJsx(child))
|
|
238
163
|
return false
|
|
239
164
|
}
|
|
@@ -242,10 +167,86 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
242
167
|
return sub.toString()
|
|
243
168
|
}
|
|
244
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
|
+
|
|
245
246
|
// Emits JSX directly into a string accumulator variable `_out` (zero closures on nested `.map()` loops!)
|
|
246
|
-
function emitIntoBuffer(node:
|
|
247
|
+
function emitIntoBuffer(node: JSXElement | JSXFragment, bufVar: string, parts: StringParts, flushParts: () => void, pushStmt: (stmt: string) => void): void {
|
|
247
248
|
if (node.type === 'JSXFragment') {
|
|
248
|
-
emitChildrenIntoBuffer(node.children
|
|
249
|
+
emitChildrenIntoBuffer(node.children, bufVar, parts, flushParts, pushStmt)
|
|
249
250
|
return
|
|
250
251
|
}
|
|
251
252
|
|
|
@@ -258,7 +259,7 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
258
259
|
parts.pushStatic(`<${tag}`)
|
|
259
260
|
let innerHtmlExpr: string | undefined
|
|
260
261
|
|
|
261
|
-
for (const attr of node.openingElement.attributes
|
|
262
|
+
for (const attr of node.openingElement.attributes) {
|
|
262
263
|
if (attr.type === 'JSXSpreadAttribute') {
|
|
263
264
|
parts.pushExpr(`${use('ssrSpread')}(${compileSubExpression(attr.argument)})`)
|
|
264
265
|
continue
|
|
@@ -269,12 +270,15 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
269
270
|
if (rawName === 'innerHTML') {
|
|
270
271
|
if (attr.value?.type === 'Literal')
|
|
271
272
|
innerHtmlExpr = JSON.stringify(String(attr.value.value))
|
|
272
|
-
else if (attr.value?.type === 'JSXExpressionContainer')
|
|
273
|
+
else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
|
|
273
274
|
innerHtmlExpr = `${use('ssrRaw')}(${compileSubExpression(attr.value.expression)})`
|
|
274
275
|
continue
|
|
275
276
|
}
|
|
276
277
|
|
|
277
278
|
const name = rawName === 'className' ? 'class' : rawName === 'htmlFor' ? 'for' : rawName
|
|
279
|
+
if (isBlockedAttr(name))
|
|
280
|
+
continue
|
|
281
|
+
|
|
278
282
|
if (attr.value === null) {
|
|
279
283
|
parts.pushStatic(isStringBoolAttr(name) ? ` ${name}="true"` : ` ${name}`)
|
|
280
284
|
}
|
|
@@ -282,8 +286,8 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
282
286
|
if (!isUnsafeUrlAttr(name, attr.value.value))
|
|
283
287
|
parts.pushStatic(` ${name}="${escapeStaticAttr(String(attr.value.value))}"`)
|
|
284
288
|
}
|
|
285
|
-
else if (attr.value.type === 'JSXExpressionContainer') {
|
|
286
|
-
const expr = attr.value.expression
|
|
289
|
+
else if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
290
|
+
const expr = attr.value.expression
|
|
287
291
|
|
|
288
292
|
// Fold static ternary attribute branches (`class={cond ? 'stock out' : 'stock in'}`) with 0 runtime escaping!
|
|
289
293
|
if (name !== 'style' && expr.type === 'ConditionalExpression') {
|
|
@@ -310,259 +314,215 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
310
314
|
if (innerHtmlExpr !== undefined)
|
|
311
315
|
parts.pushExpr(innerHtmlExpr)
|
|
312
316
|
else
|
|
313
|
-
emitChildrenIntoBuffer(node.children
|
|
317
|
+
emitChildrenIntoBuffer(node.children, bufVar, parts, flushParts, pushStmt, RAW_TEXT_ELEMENTS.has(tag) ? tag as 'style' | 'script' : undefined)
|
|
314
318
|
parts.pushStatic(`</${tag}>`)
|
|
315
319
|
}
|
|
316
320
|
|
|
317
321
|
function emitChildrenIntoBuffer(
|
|
318
|
-
children:
|
|
322
|
+
children: readonly JSXChild[],
|
|
319
323
|
bufVar: string,
|
|
320
324
|
parts: StringParts,
|
|
321
325
|
flushParts: () => void,
|
|
322
326
|
pushStmt: (stmt: string) => void,
|
|
323
327
|
rawTextTag?: 'style' | 'script',
|
|
324
328
|
): void {
|
|
325
|
-
const meaningful = children
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
else if (child.type === 'JSXElement' || child.type === 'JSXFragment') {
|
|
342
|
-
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
|
|
343
344
|
}
|
|
344
|
-
else if (child.type === 'JSXExpressionContainer') {
|
|
345
|
-
const expr = child.expression as AstNode
|
|
346
345
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
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)})`)
|
|
350
358
|
continue
|
|
351
359
|
}
|
|
360
|
+
}
|
|
352
361
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
&& (expr.expressions as AstNode[]).length === 0
|
|
358
|
-
&& (expr.quasis as AstNode[]).length === 1
|
|
359
|
-
) {
|
|
360
|
-
const cooked = expr.quasis[0]?.value?.cooked
|
|
361
|
-
if (typeof cooked === 'string') {
|
|
362
|
-
parts.pushStatic(escapeRawText(cooked, rawTextTag))
|
|
363
|
-
continue
|
|
364
|
-
}
|
|
365
|
-
}
|
|
362
|
+
if (slot.kind === 'element') {
|
|
363
|
+
emitIntoBuffer(slot.node, bufVar, parts, flushParts, pushStmt)
|
|
364
|
+
continue
|
|
365
|
+
}
|
|
366
366
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
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)
|
|
370
376
|
}
|
|
377
|
+
hadVariableRange = true
|
|
378
|
+
continue
|
|
379
|
+
}
|
|
371
380
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const arrVar = `_a${n}`
|
|
393
|
-
const idxVar = idxParam ?? `_i${n}`
|
|
394
|
-
const innerStmts: string[] = []
|
|
395
|
-
const innerParts = new StringParts()
|
|
396
|
-
const flushInner = () => {
|
|
397
|
-
if (!innerParts.isEmpty()) {
|
|
398
|
-
innerStmts.push(`${bufVar} += ${innerParts.toExpr()};`)
|
|
399
|
-
innerParts.clear()
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
emitIntoBuffer(body, bufVar, innerParts, flushInner, s => innerStmts.push(s))
|
|
403
|
-
flushInner()
|
|
404
|
-
|
|
405
|
-
pushStmt(
|
|
406
|
-
`const ${arrVar} = ${target}; if (${arrVar}) for (let ${idxVar} = 0; ${idxVar} < ${arrVar}.length; ${idxVar}++) { const ${itemParam} = ${arrVar}[${idxVar}]; ${innerStmts.join(' ')} }`,
|
|
407
|
-
)
|
|
408
|
-
if (emitMarkers && !isLast)
|
|
409
|
-
parts.pushStatic('<!---->')
|
|
410
|
-
continue
|
|
411
|
-
}
|
|
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()
|
|
412
401
|
}
|
|
413
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)})`)
|
|
414
418
|
}
|
|
415
419
|
|
|
416
|
-
|
|
420
|
+
if (needsBracket)
|
|
421
|
+
parts.pushStatic('<!--k]-->')
|
|
422
|
+
hadVariableRange = true
|
|
423
|
+
continue
|
|
417
424
|
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
425
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
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
|
+
}
|
|
424
440
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
if ((cb.type === 'ArrowFunctionExpression' || cb.type === 'FunctionExpression') && !cb.async && cb.body !== null) {
|
|
434
|
-
const body = (cb.body.type === 'ParenthesizedExpression' ? cb.body.expression : cb.body) as AstNode
|
|
435
|
-
if (body.type === 'JSXElement' || body.type === 'JSXFragment') {
|
|
436
|
-
const target = compileSubExpression(expr.callee.object)
|
|
437
|
-
const params = (cb.params as AstNode[]).map(p => code.slice(p.start, p.end)).join(', ')
|
|
438
|
-
const rendered = `${use('ssrLoop')}(${target}, (${params}) => ${compileJsxToStringExpr(body)})`
|
|
439
|
-
return emitMarkers && !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]-->')
|
|
440
449
|
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
walk(body, (c) => {
|
|
444
|
-
if (c.type === 'JSXElement' || c.type === 'JSXFragment') {
|
|
445
|
-
blockHasJsx = true
|
|
446
|
-
return false
|
|
447
|
-
}
|
|
448
|
-
return undefined
|
|
449
|
-
})
|
|
450
|
-
if (blockHasJsx) {
|
|
451
|
-
const rendered = `${use('esc')}(${compileSubExpression(expr)})`
|
|
452
|
-
return emitMarkers && !isLast ? `(${rendered}) + "<!---->"` : rendered
|
|
453
|
-
}
|
|
450
|
+
else {
|
|
451
|
+
parts.pushExpr(`((${test}) ? (${cons}) : (${alt}))`)
|
|
454
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)
|
|
455
480
|
}
|
|
456
481
|
}
|
|
482
|
+
}
|
|
457
483
|
|
|
484
|
+
function compileExpressionAsString(expr: Node): string {
|
|
485
|
+
if (isJsxNode(expr))
|
|
486
|
+
return compileJsxToStringExpr(expr)
|
|
458
487
|
if (expr.type === 'LogicalExpression' && expr.operator === '&&') {
|
|
459
488
|
const right = unwrapParens(expr.right)
|
|
460
|
-
if (right
|
|
461
|
-
|
|
462
|
-
const emptyVal = emitMarkers && !isLast ? '"<!---->"' : '""'
|
|
463
|
-
return `((${left}) ? (${compileJsxToStringExpr(right)}) : ${emptyVal})`
|
|
464
|
-
}
|
|
489
|
+
if (isJsxNode(right))
|
|
490
|
+
return `((${compileSubExpression(expr.left)}) ? (${compileJsxToStringExpr(right)}) : "")`
|
|
465
491
|
}
|
|
466
|
-
|
|
467
492
|
if (expr.type === 'ConditionalExpression') {
|
|
468
493
|
const consNode = unwrapParens(expr.consequent)
|
|
469
494
|
const altNode = unwrapParens(expr.alternate)
|
|
470
|
-
if (consNode
|
|
471
|
-
|
|
472
|
-
const cons = compileExpressionAsString(consNode, false, true)
|
|
473
|
-
const alt = compileExpressionAsString(altNode, false, true)
|
|
474
|
-
const base = `((${test}) ? (${cons}) : (${alt}))`
|
|
475
|
-
return emitMarkers && !isLast ? `(${base} || "<!---->")` : base
|
|
476
|
-
}
|
|
495
|
+
if (isJsxNode(consNode) || isJsxNode(altNode))
|
|
496
|
+
return `((${compileSubExpression(expr.test)}) ? (${compileExpressionAsString(consNode)}) : (${compileExpressionAsString(altNode)}))`
|
|
477
497
|
}
|
|
478
|
-
|
|
479
|
-
const exprCode = compileSubExpression(expr)
|
|
480
|
-
const rendered = `${use('esc')}(${exprCode})`
|
|
481
|
-
if (emitMarkers && hasSiblings)
|
|
482
|
-
return isLast ? `"<!---->" + ${rendered}` : `"<!---->" + ${rendered} + "<!---->"`
|
|
483
|
-
return rendered
|
|
498
|
+
return `${use('esc')}(${compileSubExpression(expr)})`
|
|
484
499
|
}
|
|
485
500
|
|
|
486
|
-
function emitComponent(node:
|
|
487
|
-
const
|
|
488
|
-
const
|
|
489
|
-
|
|
490
|
-
const staticPropsObj: Record<string, unknown> = {}
|
|
491
|
-
let hydrateExpr: string | undefined
|
|
492
|
-
let staticHydrate: string | undefined
|
|
493
|
-
|
|
494
|
-
for (const attr of attrs) {
|
|
495
|
-
if (attr.type === 'JSXSpreadAttribute') {
|
|
496
|
-
allStaticProps = false
|
|
497
|
-
propEntries.push(`...(${compileSubExpression(attr.argument)})`)
|
|
498
|
-
continue
|
|
499
|
-
}
|
|
500
|
-
const name = jsxTagName(attr.name)
|
|
501
|
-
if (name === 'key')
|
|
502
|
-
continue
|
|
503
|
-
if (name === '$hydrate') {
|
|
504
|
-
if (attr.value?.type === 'Literal' && typeof attr.value.value === 'string') {
|
|
505
|
-
staticHydrate = attr.value.value
|
|
506
|
-
hydrateExpr = JSON.stringify(attr.value.value)
|
|
507
|
-
}
|
|
508
|
-
else if (attr.value?.type === 'JSXExpressionContainer') {
|
|
509
|
-
hydrateExpr = compileSubExpression(attr.value.expression)
|
|
510
|
-
}
|
|
511
|
-
continue
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
const propKey = /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name)
|
|
515
|
-
if (attr.value === null) {
|
|
516
|
-
propEntries.push(`${propKey}: true`)
|
|
517
|
-
staticPropsObj[name] = true
|
|
518
|
-
}
|
|
519
|
-
else if (attr.value.type === 'Literal') {
|
|
520
|
-
propEntries.push(`${propKey}: ${JSON.stringify(attr.value.value)}`)
|
|
521
|
-
staticPropsObj[name] = attr.value.value
|
|
522
|
-
}
|
|
523
|
-
else if (attr.value.type === 'JSXExpressionContainer') {
|
|
524
|
-
const expr = attr.value.expression as AstNode
|
|
525
|
-
const lit = isSimpleLiteral(expr)
|
|
526
|
-
if (lit.ok)
|
|
527
|
-
staticPropsObj[name] = lit.value
|
|
528
|
-
else
|
|
529
|
-
allStaticProps = false
|
|
530
|
-
propEntries.push(`${propKey}: ${compileSubExpression(expr)}`)
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
const childNodes = node.children as AstNode[]
|
|
535
|
-
const meaningfulChildren = childNodes.filter(c =>
|
|
536
|
-
!(c.type === 'JSXText' && cleanJsxText(c.value as string) === '')
|
|
537
|
-
&& !(c.type === 'JSXExpressionContainer' && c.expression.type === 'JSXEmptyExpression'),
|
|
538
|
-
)
|
|
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)
|
|
539
505
|
const hasChildren = meaningfulChildren.length > 0
|
|
540
506
|
|
|
541
|
-
// Special case: <Suspense fallback={...}><AsyncComp
|
|
507
|
+
// Special case: <Suspense fallback={...}><AsyncComp .../></Suspense> passes the raw component call/Promise!
|
|
542
508
|
if (tag === 'Suspense') {
|
|
543
509
|
if (meaningfulChildren.length === 1) {
|
|
544
510
|
const only = meaningfulChildren[0]!
|
|
545
511
|
if (only.type === 'JSXElement' && isComponentTag(jsxTagName(only.openingElement.name))) {
|
|
546
512
|
const innerTag = jsxTagName(only.openingElement.name)
|
|
547
|
-
const
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
if (attr.value === null)
|
|
553
|
-
innerProps.push(`${k}: true`)
|
|
554
|
-
else if (attr.value.type === 'Literal')
|
|
555
|
-
innerProps.push(`${k}: ${JSON.stringify(attr.value.value)}`)
|
|
556
|
-
else if (attr.value.type === 'JSXExpressionContainer')
|
|
557
|
-
innerProps.push(`${k}: ${compileSubExpression(attr.value.expression)}`)
|
|
558
|
-
}
|
|
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})`)
|
|
559
518
|
}
|
|
560
|
-
propEntries.
|
|
519
|
+
const innerObj = inner.propEntries.length > 0 ? `{ ${inner.propEntries.join(', ')} }` : '{}'
|
|
520
|
+
propEntries.push(`children: ${innerTag}(${innerObj})`)
|
|
561
521
|
}
|
|
562
|
-
else if (only.type === 'JSXExpressionContainer') {
|
|
522
|
+
else if (only.type === 'JSXExpressionContainer' && only.expression.type !== 'JSXEmptyExpression') {
|
|
563
523
|
propEntries.push(`children: ${compileSubExpression(only.expression)}`)
|
|
564
524
|
}
|
|
565
|
-
else {
|
|
525
|
+
else if (isJsxNode(only)) {
|
|
566
526
|
propEntries.push(`children: ${use('raw')}(${compileJsxToStringExpr(only)})`)
|
|
567
527
|
}
|
|
568
528
|
}
|
|
@@ -571,9 +531,13 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
571
531
|
return
|
|
572
532
|
}
|
|
573
533
|
|
|
574
|
-
const
|
|
534
|
+
const childrenHaveAwait = hasChildren && childNodes.some(c => containsDirectAwait(c))
|
|
535
|
+
const childExpr = hasChildren ? compileChildrenListToStringExpr(childNodes) : '""'
|
|
575
536
|
|
|
576
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`)
|
|
540
|
+
|
|
577
541
|
if (islandInfo !== undefined) {
|
|
578
542
|
const propsCode = `{ ${propEntries.join(', ')} }`
|
|
579
543
|
const callPropsCode = hasChildren
|
|
@@ -581,9 +545,9 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
581
545
|
: propsCode
|
|
582
546
|
const resolvedHydrate = staticHydrate ?? (hydrateExpr === undefined ? islandInfo.defaultHydrate : undefined)
|
|
583
547
|
|
|
584
|
-
// Compile-time static island props fast path (only when not inside an island-reachable component):
|
|
585
|
-
// 0 JSON.stringify / Object.keys / escAttr at runtime, while still
|
|
586
|
-
if (!emitMarkers && allStaticProps && (hydrateExpr === undefined || staticHydrate !== undefined)) {
|
|
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)) {
|
|
587
551
|
const pAttr = Object.keys(staticPropsObj).length > 0
|
|
588
552
|
? ` p="${escapeStaticAttr(JSON.stringify(staticPropsObj))}"`
|
|
589
553
|
: ''
|
|
@@ -591,29 +555,30 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
591
555
|
? ` h="${escapeStaticAttr(resolvedHydrate)}"`
|
|
592
556
|
: ''
|
|
593
557
|
const openTag = `<kay-island c="${escapeStaticAttr(islandInfo.chunk)}"${pAttr}${hAttr}>`
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
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})`)
|
|
598
563
|
return
|
|
599
564
|
}
|
|
600
565
|
|
|
601
566
|
const hydrateArg = hydrateExpr ?? (islandInfo.defaultHydrate !== undefined ? JSON.stringify(islandInfo.defaultHydrate) : 'undefined')
|
|
602
|
-
if (allStaticProps && (hydrateExpr === undefined || staticHydrate !== undefined)) {
|
|
567
|
+
if (!childrenHaveAwait && allStaticProps && (hydrateExpr === undefined || staticHydrate !== undefined)) {
|
|
603
568
|
parts.pushExpr(
|
|
604
|
-
`${use('ssrIsland')}(${JSON.stringify(islandInfo.
|
|
569
|
+
`${use('ssrIsland')}(${JSON.stringify(islandInfo.chunk)}, () => ${use('ssrChild')}(${tag}(${callPropsCode})), ${propsCode}, ${hydrateArg})`,
|
|
605
570
|
)
|
|
606
571
|
return
|
|
607
572
|
}
|
|
608
573
|
|
|
609
|
-
// Hoist dynamic island props into a single binding so expressions
|
|
574
|
+
// Hoist dynamic island props / awaited children into a single binding so expressions evaluate once!
|
|
610
575
|
const pVar = `_p${loopCounter++}`
|
|
611
576
|
const argNames = [pVar]
|
|
612
577
|
const argExprs = [propsCode]
|
|
613
578
|
let callArg = pVar
|
|
614
579
|
if (hasChildren) {
|
|
615
580
|
let childSlotVal = childExpr
|
|
616
|
-
if (
|
|
581
|
+
if (childrenHaveAwait) {
|
|
617
582
|
const cVar = `_c${loopCounter++}`
|
|
618
583
|
argNames.push(cVar)
|
|
619
584
|
argExprs.push(childExpr)
|
|
@@ -629,11 +594,33 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
629
594
|
hArg = hVar
|
|
630
595
|
}
|
|
631
596
|
parts.pushExpr(
|
|
632
|
-
`((${argNames.join(', ')}) => ${use('ssrIsland')}(${JSON.stringify(islandInfo.
|
|
597
|
+
`((${argNames.join(', ')}) => ${use('ssrIsland')}(${JSON.stringify(islandInfo.chunk)}, () => ${use('ssrChild')}(${tag}(${callArg})), ${pVar}, ${hArg}))(${argExprs.join(', ')})`,
|
|
633
598
|
)
|
|
634
599
|
return
|
|
635
600
|
}
|
|
636
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
|
+
|
|
637
624
|
if (hasChildren)
|
|
638
625
|
propEntries.push(`children: ${use('raw')}(${childExpr})`)
|
|
639
626
|
|
|
@@ -641,7 +628,7 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
641
628
|
parts.pushExpr(`${use('ssrChild')}(${tag}(${propsObj}))`)
|
|
642
629
|
}
|
|
643
630
|
|
|
644
|
-
function compileJsxToStringExpr(node:
|
|
631
|
+
function compileJsxToStringExpr(node: JSXElement | JSXFragment): string {
|
|
645
632
|
const bufVar = `_o${loopCounter++}`
|
|
646
633
|
const stmts: string[] = []
|
|
647
634
|
const parts = new StringParts()
|
|
@@ -665,12 +652,12 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
|
|
|
665
652
|
return `(() => { let ${bufVar} = ""; ${stmts.join(' ')} return ${bufVar}; })()`
|
|
666
653
|
}
|
|
667
654
|
|
|
668
|
-
function compileRootJsx(node:
|
|
655
|
+
function compileRootJsx(node: JSXElement | JSXFragment): string {
|
|
669
656
|
return `${use('raw')}(${compileJsxToStringExpr(node)})`
|
|
670
657
|
}
|
|
671
658
|
|
|
672
659
|
walk(program, (node) => {
|
|
673
|
-
if (node
|
|
660
|
+
if (isJsxNode(node)) {
|
|
674
661
|
editor.replace(node.start, node.end, compileRootJsx(node))
|
|
675
662
|
return false
|
|
676
663
|
}
|