@kayjs/kay 2.0.0-beta.1 → 2.0.0-beta.3
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/build/bundle.ts +209 -34
- package/src/build/compile.ts +22 -46
- package/src/build/files.ts +38 -0
- package/src/build/graph.ts +67 -2
- package/src/build/index.ts +75 -13
- package/src/build/manifest.ts +5 -0
- package/src/build/plugin-host.ts +2 -0
- package/src/build/prerender.ts +16 -7
- package/src/build/resolve.ts +11 -18
- package/src/build/routes.ts +33 -5
- package/src/build/server-entry.ts +38 -0
- package/src/cli/check.ts +102 -45
- package/src/cli/new.ts +6 -7
- package/src/compiler/analyze.ts +65 -10
- package/src/compiler/dom.ts +143 -122
- package/src/compiler/server-only.ts +14 -8
- package/src/compiler/shared.ts +57 -14
- package/src/compiler/ssr.ts +14 -12
- package/src/dev/index.ts +96 -29
- package/src/dom/action.ts +91 -1
- package/src/dom/index.ts +8 -3
- package/src/index.ts +3 -0
- package/src/jsx-runtime.ts +421 -93
- package/src/plugins/config.ts +49 -0
- package/src/plugins/context.ts +4 -0
- package/src/plugins/index.ts +7 -216
- package/src/plugins/load.ts +38 -0
- package/src/plugins/order.ts +107 -0
- package/src/plugins/services.ts +23 -0
- package/src/plugins/types.ts +197 -0
- package/src/server/action-registry.ts +72 -0
- package/src/server/action.ts +15 -8
- package/src/server/app-entry.ts +23 -0
- package/src/server/assets.ts +182 -0
- package/src/server/compress.ts +87 -14
- package/src/server/document.ts +192 -0
- package/src/server/errors.ts +123 -0
- package/src/server/http.ts +103 -30
- package/src/server/index.ts +131 -701
- package/src/server/loader.ts +62 -69
- package/src/server/page.ts +107 -0
- package/src/server/plugin-host.ts +167 -0
- package/src/server/router.ts +49 -20
- package/src/server/security.ts +44 -3
- package/src/server/static-dir.ts +22 -3
- package/src/server/stream.ts +94 -0
- package/src/test/index.ts +24 -19
package/src/compiler/dom.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
Program,
|
|
14
14
|
} from 'oxc-parser'
|
|
15
15
|
import type { ModuleAnalysis } from './analyze'
|
|
16
|
+
import { visitorKeys } from 'oxc-parser'
|
|
16
17
|
import { analyzeModule, loadDeclarations, parseTsx, walk } from './analyze'
|
|
17
18
|
import { SourceEditor } from './patch'
|
|
18
19
|
import {
|
|
@@ -116,94 +117,20 @@ function literalArrayConsts(program: Program): Set<string> {
|
|
|
116
117
|
return literal
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (found || node === null || node === undefined)
|
|
126
|
-
return
|
|
127
|
-
switch (node.type) {
|
|
128
|
-
case 'Identifier':
|
|
129
|
-
if (!shadowed.has(node.name) && names.has(node.name))
|
|
130
|
-
found = true
|
|
131
|
-
return
|
|
132
|
-
case 'MemberExpression':
|
|
133
|
-
visit(node.object, shadowed)
|
|
134
|
-
if (node.computed)
|
|
135
|
-
visit(node.property, shadowed)
|
|
136
|
-
return
|
|
137
|
-
case 'Property':
|
|
138
|
-
if (node.computed)
|
|
139
|
-
visit(node.key, shadowed)
|
|
140
|
-
visit(node.value, shadowed)
|
|
141
|
-
return
|
|
142
|
-
case 'ArrowFunctionExpression':
|
|
143
|
-
case 'FunctionExpression':
|
|
144
|
-
case 'FunctionDeclaration': {
|
|
145
|
-
const next = new Set(shadowed)
|
|
146
|
-
if (node.type !== 'ArrowFunctionExpression' && node.id !== null)
|
|
147
|
-
next.add(node.id.name)
|
|
148
|
-
for (const p of node.params)
|
|
149
|
-
collectPatternNames(p, next)
|
|
150
|
-
visit(node.body, next)
|
|
151
|
-
return
|
|
152
|
-
}
|
|
153
|
-
case 'BlockStatement': {
|
|
154
|
-
const next = new Set(shadowed)
|
|
155
|
-
for (const stmt of node.body) {
|
|
156
|
-
if (stmt.type === 'VariableDeclaration') {
|
|
157
|
-
for (const d of stmt.declarations) {
|
|
158
|
-
visit(d.init, next)
|
|
159
|
-
collectPatternNames(d.id, next)
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
visit(stmt, next)
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return
|
|
167
|
-
}
|
|
168
|
-
default: {
|
|
169
|
-
if (node.type.startsWith('TS'))
|
|
170
|
-
return
|
|
171
|
-
for (const [k, v] of Object.entries(node)) {
|
|
172
|
-
if (k === 'start' || k === 'end' || k === 'type')
|
|
173
|
-
continue
|
|
174
|
-
if (Array.isArray(v)) {
|
|
175
|
-
for (const item of v) {
|
|
176
|
-
if (item !== null && typeof item === 'object' && 'type' in item)
|
|
177
|
-
visit(item as Node, shadowed)
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
else if (v !== null && typeof v === 'object' && 'type' in v) {
|
|
181
|
-
visit(v as Node, shadowed)
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
visit(rootNode, new Set())
|
|
188
|
-
return found
|
|
120
|
+
interface ScopedWalkCallbacks {
|
|
121
|
+
shouldStop?: (() => boolean) | undefined
|
|
122
|
+
onIdentifier?: ((node: Extract<Node, { type: 'Identifier' }>, shadowed: ReadonlySet<string>) => void) | undefined
|
|
123
|
+
onShorthandProperty?: ((node: Extract<Node, { type: 'Property' }>, shadowed: ReadonlySet<string>) => boolean) | undefined
|
|
124
|
+
onJsx?: ((node: JSXElement | JSXFragment, shadowed: ReadonlySet<string>) => void) | undefined
|
|
125
|
+
onFunction?: ((node: ArrowFunctionExpression | OxcFunction, shadowed: ReadonlySet<string>) => boolean) | undefined
|
|
189
126
|
}
|
|
190
127
|
|
|
191
|
-
/**
|
|
192
|
-
function
|
|
193
|
-
|
|
194
|
-
rootNode: Node,
|
|
195
|
-
replacements: ReadonlyMap<string, string>,
|
|
196
|
-
onJsx?: (node: JSXElement | JSXFragment, rewrites: ReadonlyMap<string, string>) => string,
|
|
197
|
-
onFunction?: (fn: ArrowFunctionExpression | OxcFunction, rewrites: ReadonlyMap<string, string>) => string | undefined,
|
|
198
|
-
): string {
|
|
199
|
-
if (replacements.size === 0 && (onJsx === undefined || !containsJsx(rootNode)))
|
|
200
|
-
return source.slice(rootNode.start, rootNode.end)
|
|
201
|
-
|
|
202
|
-
const offset = rootNode.start
|
|
203
|
-
const editor = new SourceEditor(source.slice(rootNode.start, rootNode.end))
|
|
128
|
+
/** Single lexical-scope AST visitor shared by `referencesAny` and `rewriteFreeIdentifiers` (respects `for...of`, `catch (e)`, class members, labels, and TS nodes). */
|
|
129
|
+
function walkScoped(rootNode: Node, callbacks: ScopedWalkCallbacks): void {
|
|
130
|
+
const { shouldStop, onIdentifier, onShorthandProperty, onJsx, onFunction } = callbacks
|
|
204
131
|
|
|
205
132
|
function visitPatternDefaults(pattern: Node | null | undefined, shadowed: Set<string>): void {
|
|
206
|
-
if (pattern === null || pattern === undefined)
|
|
133
|
+
if (pattern === null || pattern === undefined || shouldStop?.())
|
|
207
134
|
return
|
|
208
135
|
switch (pattern.type) {
|
|
209
136
|
case 'AssignmentPattern':
|
|
@@ -234,27 +161,19 @@ function rewriteFreeIdentifiers(
|
|
|
234
161
|
}
|
|
235
162
|
}
|
|
236
163
|
|
|
237
|
-
function activeMap(shadowed: Set<string>): ReadonlyMap<string, string> {
|
|
238
|
-
return shadowed.size === 0 ? replacements : new Map([...replacements].filter(([k]) => !shadowed.has(k)))
|
|
239
|
-
}
|
|
240
|
-
|
|
241
164
|
function visit(node: Node | null | undefined, shadowed: Set<string>): void {
|
|
242
|
-
if (node === null || node === undefined)
|
|
165
|
+
if (node === null || node === undefined || shouldStop?.())
|
|
243
166
|
return
|
|
244
167
|
|
|
245
168
|
if (onJsx !== undefined && isJsxNode(node)) {
|
|
246
|
-
|
|
169
|
+
onJsx(node, shadowed)
|
|
247
170
|
return
|
|
248
171
|
}
|
|
249
172
|
|
|
250
173
|
switch (node.type) {
|
|
251
174
|
case 'Identifier': {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const rep = replacements.get(name)
|
|
255
|
-
if (rep !== undefined)
|
|
256
|
-
editor.replace(node.start - offset, node.end - offset, rep)
|
|
257
|
-
}
|
|
175
|
+
if (!shadowed.has(node.name))
|
|
176
|
+
onIdentifier?.(node, shadowed)
|
|
258
177
|
return
|
|
259
178
|
}
|
|
260
179
|
case 'MemberExpression': {
|
|
@@ -274,13 +193,8 @@ function rewriteFreeIdentifiers(
|
|
|
274
193
|
case 'Property': {
|
|
275
194
|
if (node.computed)
|
|
276
195
|
visit(node.key, shadowed)
|
|
277
|
-
if (node.shorthand && node.value.type === 'Identifier')
|
|
278
|
-
|
|
279
|
-
if (!shadowed.has(name) && replacements.has(name)) {
|
|
280
|
-
editor.replace(node.start - offset, node.end - offset, `${name}: ${replacements.get(name)}`)
|
|
281
|
-
return
|
|
282
|
-
}
|
|
283
|
-
}
|
|
196
|
+
if (node.shorthand && node.value.type === 'Identifier' && onShorthandProperty?.(node, shadowed) === true)
|
|
197
|
+
return
|
|
284
198
|
visit(node.value, shadowed)
|
|
285
199
|
return
|
|
286
200
|
}
|
|
@@ -345,13 +259,8 @@ function rewriteFreeIdentifiers(
|
|
|
345
259
|
case 'ArrowFunctionExpression':
|
|
346
260
|
case 'FunctionExpression':
|
|
347
261
|
case 'FunctionDeclaration': {
|
|
348
|
-
if (node !== rootNode && onFunction
|
|
349
|
-
|
|
350
|
-
if (compiled !== undefined) {
|
|
351
|
-
editor.replace(node.start - offset, node.end - offset, compiled)
|
|
352
|
-
return
|
|
353
|
-
}
|
|
354
|
-
}
|
|
262
|
+
if (node !== rootNode && onFunction?.(node, shadowed) === true)
|
|
263
|
+
return
|
|
355
264
|
const nextShadow = new Set(shadowed)
|
|
356
265
|
if (node.type !== 'ArrowFunctionExpression' && node.id !== null)
|
|
357
266
|
nextShadow.add(node.id.name)
|
|
@@ -400,6 +309,24 @@ function rewriteFreeIdentifiers(
|
|
|
400
309
|
default: {
|
|
401
310
|
if (node.type.startsWith('TS'))
|
|
402
311
|
return
|
|
312
|
+
const keys = visitorKeys[node.type]
|
|
313
|
+
if (keys !== undefined) {
|
|
314
|
+
const rec = node as unknown as Record<string, unknown>
|
|
315
|
+
for (let i = 0; i < keys.length; i++) {
|
|
316
|
+
const val = rec[keys[i]!]
|
|
317
|
+
if (Array.isArray(val)) {
|
|
318
|
+
for (let j = 0; j < val.length; j++) {
|
|
319
|
+
const item = val[j]
|
|
320
|
+
if (item !== null && typeof item === 'object' && 'type' in item)
|
|
321
|
+
visit(item as Node, shadowed)
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else if (val !== null && typeof val === 'object' && 'type' in val) {
|
|
325
|
+
visit(val as Node, shadowed)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return
|
|
329
|
+
}
|
|
403
330
|
for (const [key, val] of Object.entries(node)) {
|
|
404
331
|
if (key === 'start' || key === 'end' || key === 'type')
|
|
405
332
|
continue
|
|
@@ -419,9 +346,105 @@ function rewriteFreeIdentifiers(
|
|
|
419
346
|
}
|
|
420
347
|
|
|
421
348
|
visit(rootNode, new Set())
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Returns true if `rootNode` contains any free identifier reference to a name in `names`. */
|
|
352
|
+
function referencesAny(rootNode: Node, names: ReadonlySet<string>): boolean {
|
|
353
|
+
if (names.size === 0)
|
|
354
|
+
return false
|
|
355
|
+
let found = false
|
|
356
|
+
walkScoped(rootNode, {
|
|
357
|
+
shouldStop: () => found,
|
|
358
|
+
onIdentifier(node) {
|
|
359
|
+
if (names.has(node.name))
|
|
360
|
+
found = true
|
|
361
|
+
},
|
|
362
|
+
})
|
|
363
|
+
return found
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Rewrites free identifier reads inside an AST node while respecting lexical scopes, class/object keys, labels, and nested JSX/components. */
|
|
367
|
+
function rewriteFreeIdentifiers(
|
|
368
|
+
source: string,
|
|
369
|
+
rootNode: Node,
|
|
370
|
+
replacements: ReadonlyMap<string, string>,
|
|
371
|
+
jsxNodes: WeakSet<Node>,
|
|
372
|
+
onJsx?: (node: JSXElement | JSXFragment, rewrites: ReadonlyMap<string, string>) => string,
|
|
373
|
+
onFunction?: (fn: ArrowFunctionExpression | OxcFunction, rewrites: ReadonlyMap<string, string>) => string | undefined,
|
|
374
|
+
): string {
|
|
375
|
+
if (replacements.size === 0 && (onJsx === undefined || !containsJsx(rootNode, jsxNodes)))
|
|
376
|
+
return source.slice(rootNode.start, rootNode.end)
|
|
377
|
+
|
|
378
|
+
const offset = rootNode.start
|
|
379
|
+
const editor = new SourceEditor(source.slice(rootNode.start, rootNode.end))
|
|
380
|
+
const activeMap = (shadowed: ReadonlySet<string>): ReadonlyMap<string, string> =>
|
|
381
|
+
shadowed.size === 0 ? replacements : new Map([...replacements].filter(([k]) => !shadowed.has(k)))
|
|
382
|
+
|
|
383
|
+
walkScoped(rootNode, {
|
|
384
|
+
onJsx: onJsx === undefined
|
|
385
|
+
? undefined
|
|
386
|
+
: (node, shadowed) => {
|
|
387
|
+
editor.replace(node.start - offset, node.end - offset, onJsx(node, activeMap(shadowed)))
|
|
388
|
+
},
|
|
389
|
+
onIdentifier(node) {
|
|
390
|
+
const rep = replacements.get(node.name)
|
|
391
|
+
if (rep !== undefined)
|
|
392
|
+
editor.replace(node.start - offset, node.end - offset, rep)
|
|
393
|
+
},
|
|
394
|
+
onShorthandProperty(node, shadowed) {
|
|
395
|
+
if (node.value.type !== 'Identifier')
|
|
396
|
+
return false
|
|
397
|
+
const name = node.value.name
|
|
398
|
+
if (!shadowed.has(name) && replacements.has(name)) {
|
|
399
|
+
editor.replace(node.start - offset, node.end - offset, `${name}: ${replacements.get(name)}`)
|
|
400
|
+
return true
|
|
401
|
+
}
|
|
402
|
+
return false
|
|
403
|
+
},
|
|
404
|
+
onFunction: onFunction === undefined
|
|
405
|
+
? undefined
|
|
406
|
+
: (node, shadowed) => {
|
|
407
|
+
const compiled = onFunction(node, activeMap(shadowed))
|
|
408
|
+
if (compiled !== undefined) {
|
|
409
|
+
editor.replace(node.start - offset, node.end - offset, compiled)
|
|
410
|
+
return true
|
|
411
|
+
}
|
|
412
|
+
return false
|
|
413
|
+
},
|
|
414
|
+
})
|
|
422
415
|
return editor.toString()
|
|
423
416
|
}
|
|
424
417
|
|
|
418
|
+
/** True when a JSX prop expression is statically stable (non-rewritten identifier, literal, function, or constant array/object) and needs no reactive getter. */
|
|
419
|
+
function isStaticPropExpr(rawNode: Node, scopeRewrites: ReadonlyMap<string, string>): boolean {
|
|
420
|
+
const node = unwrapTsExpr(rawNode)
|
|
421
|
+
switch (node.type) {
|
|
422
|
+
case 'Literal':
|
|
423
|
+
case 'ArrowFunctionExpression':
|
|
424
|
+
case 'FunctionExpression':
|
|
425
|
+
return true
|
|
426
|
+
case 'Identifier':
|
|
427
|
+
return !scopeRewrites.has(node.name)
|
|
428
|
+
case 'UnaryExpression':
|
|
429
|
+
return (node.operator === '-' || node.operator === '+' || node.operator === '!' || node.operator === '~' || node.operator === 'void')
|
|
430
|
+
&& isStaticPropExpr(node.argument, scopeRewrites)
|
|
431
|
+
case 'TemplateLiteral':
|
|
432
|
+
return node.expressions.every(e => isStaticPropExpr(e, scopeRewrites))
|
|
433
|
+
case 'ArrayExpression':
|
|
434
|
+
return node.elements.every(el => el === null || (el.type !== 'SpreadElement' && isStaticPropExpr(el, scopeRewrites)))
|
|
435
|
+
case 'ObjectExpression':
|
|
436
|
+
return node.properties.every((prop) => {
|
|
437
|
+
if (prop.type !== 'Property' || prop.kind !== 'init')
|
|
438
|
+
return false
|
|
439
|
+
if (prop.computed && !isStaticPropExpr(prop.key, scopeRewrites))
|
|
440
|
+
return false
|
|
441
|
+
return isStaticPropExpr(prop.value, scopeRewrites)
|
|
442
|
+
})
|
|
443
|
+
default:
|
|
444
|
+
return false
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
425
448
|
export function compileDom(filename: string, code: string, options: CompileDomOptions = {}): CompileDomResult {
|
|
426
449
|
const program = options.analysis?.program ?? parseTsx(filename, code)
|
|
427
450
|
const analysis = options.analysis ?? analyzeModule(filename, code, program)
|
|
@@ -431,6 +454,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
431
454
|
|
|
432
455
|
const staticArrays = literalArrayConsts(program)
|
|
433
456
|
const loadNodes = new Set<Node>(loadDeclarations(program))
|
|
457
|
+
const jsxNodes = analysis.jsxNodes
|
|
434
458
|
|
|
435
459
|
const editor = new SourceEditor(code)
|
|
436
460
|
const used = new Set<string>()
|
|
@@ -532,7 +556,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
532
556
|
}
|
|
533
557
|
|
|
534
558
|
function compileNestedComponent(fn: ArrowFunctionExpression | OxcFunction, outerRewrites: ReadonlyMap<string, string>): string | undefined {
|
|
535
|
-
if (!containsJsx(fn.body))
|
|
559
|
+
if (!containsJsx(fn.body, jsxNodes))
|
|
536
560
|
return undefined
|
|
537
561
|
const firstParam = fn.params[0]
|
|
538
562
|
const destructured = extractDestructuredParam(firstParam)
|
|
@@ -542,7 +566,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
542
566
|
}
|
|
543
567
|
|
|
544
568
|
function compileExpression(node: Node, scopeRewrites: ReadonlyMap<string, string>): string {
|
|
545
|
-
return rewriteFreeIdentifiers(code, node, scopeRewrites, compileRootJsx, compileNestedComponent)
|
|
569
|
+
return rewriteFreeIdentifiers(code, node, scopeRewrites, jsxNodes, compileRootJsx, compileNestedComponent)
|
|
546
570
|
}
|
|
547
571
|
|
|
548
572
|
function compileComponentCall(node: JSXElement, scopeRewrites: ReadonlyMap<string, string>): string {
|
|
@@ -579,11 +603,8 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
579
603
|
else if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
580
604
|
const expr = attr.value.expression
|
|
581
605
|
const exprCode = compileExpression(expr, scopeRewrites)
|
|
582
|
-
// Reactive getter for dynamic expressions; plain property for literals and inline functions.
|
|
583
|
-
|
|
584
|
-
|| expr.type === 'ArrowFunctionExpression'
|
|
585
|
-
|| expr.type === 'FunctionExpression'
|
|
586
|
-
if (isStatic)
|
|
606
|
+
// Reactive getter for dynamic expressions; plain property for identifiers, constant literals/collections, and inline functions.
|
|
607
|
+
if (isStaticPropExpr(expr, scopeRewrites))
|
|
587
608
|
currentObj.push(`${propKey}: ${exprCode}`)
|
|
588
609
|
else
|
|
589
610
|
currentObj.push(`get ${propKey}() { return ${exprCode} }`)
|
|
@@ -751,7 +772,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
751
772
|
return html
|
|
752
773
|
|
|
753
774
|
const meaningful = filterMeaningfulChildren(el.children)
|
|
754
|
-
const slots = meaningful.map(c => classifyJsxChild(c)).filter(s => s !== undefined)
|
|
775
|
+
const slots = meaningful.map(c => classifyJsxChild(c, jsxNodes)).filter(s => s !== undefined)
|
|
755
776
|
const hasSiblings = slots.length > 1
|
|
756
777
|
let domIndex = 0
|
|
757
778
|
let prevWasStaticText = false
|
|
@@ -944,7 +965,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
944
965
|
|
|
945
966
|
// Generic expression slot
|
|
946
967
|
const exprCode = compileExpression(slot.expr, scopeRewrites)
|
|
947
|
-
if (isNodeCapableExpr(slot.expr)) {
|
|
968
|
+
if (isNodeCapableExpr(slot.expr, jsxNodes)) {
|
|
948
969
|
if (!hasSiblings) {
|
|
949
970
|
ops.push(`${use('insert')}(${elVar}, () => ${exprCode}, null, null);`)
|
|
950
971
|
}
|
|
@@ -1094,7 +1115,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
1094
1115
|
return `document.createTextNode(${JSON.stringify(decodeHtmlEntities(cleanJsxText(child.value)))})`
|
|
1095
1116
|
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression') {
|
|
1096
1117
|
const exprCode = compileExpression(child.expression, scopeRewrites)
|
|
1097
|
-
if (isNodeCapableExpr(child.expression))
|
|
1118
|
+
if (isNodeCapableExpr(child.expression, jsxNodes))
|
|
1098
1119
|
return `(() => { const _f = document.createDocumentFragment(); ${use('insert')}(_f, () => (${exprCode}), null, null); return _f; })()`
|
|
1099
1120
|
return `(() => { const _t = document.createTextNode(""); ${use('effect')}(() => ${use('setText')}(_t, (${exprCode}))); return _t; })()`
|
|
1100
1121
|
}
|
|
@@ -1180,7 +1201,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
1180
1201
|
|
|
1181
1202
|
// Handle top-level component functions with destructured props + JSX compilation
|
|
1182
1203
|
function processFunction(fn: ArrowFunctionExpression | OxcFunction): void {
|
|
1183
|
-
if (fn.body === null || !containsJsx(fn.body))
|
|
1204
|
+
if (fn.body === null || !containsJsx(fn.body, jsxNodes))
|
|
1184
1205
|
return
|
|
1185
1206
|
|
|
1186
1207
|
const destructured = extractDestructuredParam(fn.params[0])
|
|
@@ -1209,7 +1230,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
|
|
|
1209
1230
|
if (!shadowed.has(k))
|
|
1210
1231
|
active.set(k, v)
|
|
1211
1232
|
}
|
|
1212
|
-
if (active.size > 0 || containsJsx(stmt))
|
|
1233
|
+
if (active.size > 0 || containsJsx(stmt, jsxNodes))
|
|
1213
1234
|
editor.replace(stmt.start, stmt.end, compileExpression(stmt, active))
|
|
1214
1235
|
if (stmt.type === 'VariableDeclaration') {
|
|
1215
1236
|
for (const d of stmt.declarations)
|
|
@@ -90,13 +90,13 @@ function isEnvRoot(node: Node, shadowed: ReadonlySet<string>): 'process' | 'bun'
|
|
|
90
90
|
return undefined
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
/**
|
|
94
|
-
export function
|
|
95
|
-
let
|
|
93
|
+
/** Byte offset of the first `process.env` or `Bun.env` read outside `load()` for a key the client build does not inline, or `undefined`. */
|
|
94
|
+
export function findPrivateEnvOffset(program: Program, isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key)): number | undefined {
|
|
95
|
+
let foundOffset: number | undefined
|
|
96
96
|
const loadCode = new Set<Node>(loadDeclarations(program))
|
|
97
97
|
|
|
98
98
|
function visit(node: unknown, shadowed: Set<string>): void {
|
|
99
|
-
if (
|
|
99
|
+
if (foundOffset !== undefined || node === null || typeof node !== 'object')
|
|
100
100
|
return
|
|
101
101
|
if (Array.isArray(node)) {
|
|
102
102
|
for (let i = 0; i < node.length; i++)
|
|
@@ -112,15 +112,16 @@ export function readsPrivateEnv(program: Program, isInlined: (key: string) => bo
|
|
|
112
112
|
if (rootKind !== undefined) {
|
|
113
113
|
// Bun.env is never statically inlined in browser bundles.
|
|
114
114
|
if (rootKind === 'bun') {
|
|
115
|
-
|
|
115
|
+
foundOffset = n.start
|
|
116
116
|
return
|
|
117
117
|
}
|
|
118
118
|
const key = propKeyName(n)
|
|
119
|
-
|
|
119
|
+
if (key === undefined || !isInlined(key))
|
|
120
|
+
foundOffset = n.start
|
|
120
121
|
return
|
|
121
122
|
}
|
|
122
123
|
if (isEnvRoot(n, shadowed) !== undefined) {
|
|
123
|
-
|
|
124
|
+
foundOffset = n.start
|
|
124
125
|
return
|
|
125
126
|
}
|
|
126
127
|
}
|
|
@@ -201,5 +202,10 @@ export function readsPrivateEnv(program: Program, isInlined: (key: string) => bo
|
|
|
201
202
|
}
|
|
202
203
|
|
|
203
204
|
visit(program, new Set())
|
|
204
|
-
return
|
|
205
|
+
return foundOffset
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** True when the module reads `process.env` or `Bun.env` outside its `load()` (stripped from client code) for a key the client build does not inline. */
|
|
209
|
+
export function readsPrivateEnv(program: Program, isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key)): boolean {
|
|
210
|
+
return findPrivateEnvOffset(program, isInlined) !== undefined
|
|
205
211
|
}
|
package/src/compiler/shared.ts
CHANGED
|
@@ -115,12 +115,10 @@ export function decodeHtmlEntities(str: string): string {
|
|
|
115
115
|
})
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
export function
|
|
119
|
-
if (offset === undefined || offset < 0)
|
|
120
|
-
return filename
|
|
118
|
+
export function offsetToLineCol(code: string, offset: number): { line: number, col: number } {
|
|
121
119
|
let line = 1
|
|
122
120
|
let col = 1
|
|
123
|
-
const end = Math.min(offset, code.length)
|
|
121
|
+
const end = Math.min(Math.max(0, offset), code.length)
|
|
124
122
|
for (let i = 0; i < end; i++) {
|
|
125
123
|
if (code.charCodeAt(i) === 10) {
|
|
126
124
|
line++
|
|
@@ -130,9 +128,48 @@ export function formatLoc(filename: string, code: string, offset?: number): stri
|
|
|
130
128
|
col++
|
|
131
129
|
}
|
|
132
130
|
}
|
|
131
|
+
return { line, col }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function formatLoc(filename: string, code: string, offset?: number): string {
|
|
135
|
+
if (offset === undefined || offset < 0)
|
|
136
|
+
return filename
|
|
137
|
+
const { line, col } = offsetToLineCol(code, offset)
|
|
133
138
|
return `${filename}:${line}:${col}`
|
|
134
139
|
}
|
|
135
140
|
|
|
141
|
+
/** Renders a short source code frame around `offset` with 1 context line before/after and a caret underline. */
|
|
142
|
+
export function formatCodeFrame(code: string, offset: number, endOffset?: number): string {
|
|
143
|
+
if (code.length === 0)
|
|
144
|
+
return ''
|
|
145
|
+
const clamped = Math.min(Math.max(0, offset), code.length)
|
|
146
|
+
const { line, col } = offsetToLineCol(code, clamped)
|
|
147
|
+
const lines = code.split(/\r?\n/)
|
|
148
|
+
const lineIdx = Math.min(line - 1, lines.length - 1)
|
|
149
|
+
const startIdx = Math.max(0, lineIdx - 1)
|
|
150
|
+
const endIdx = Math.min(lines.length - 1, lineIdx + 1)
|
|
151
|
+
const gutterWidth = String(endIdx + 1).length
|
|
152
|
+
const pad = (n: number | '') => String(n).padStart(gutterWidth, ' ')
|
|
153
|
+
const targetLine = lines[lineIdx] ?? ''
|
|
154
|
+
const maxSpan = Math.max(1, targetLine.length - (col - 1))
|
|
155
|
+
const rawSpan = endOffset !== undefined && endOffset > clamped ? endOffset - clamped : 1
|
|
156
|
+
const caretLen = Math.max(1, Math.min(rawSpan, maxSpan))
|
|
157
|
+
const out: string[] = []
|
|
158
|
+
for (let i = startIdx; i <= endIdx; i++) {
|
|
159
|
+
const text = lines[i] ?? ''
|
|
160
|
+
out.push(` ${pad(i + 1)} | ${text}`)
|
|
161
|
+
if (i === lineIdx)
|
|
162
|
+
out.push(` ${pad('')} | ${' '.repeat(Math.max(0, col - 1))}${'^'.repeat(caretLen)}`)
|
|
163
|
+
}
|
|
164
|
+
return out.join('\n')
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export const DIAGNOSTIC_PREFIX_RE = /^.+:\d+:\d+:\s+(?:error|warning):/
|
|
168
|
+
|
|
169
|
+
export function formatDiagnostic(loc: string, message: string, severity: 'error' | 'warning' = 'error'): string {
|
|
170
|
+
return `${loc}: ${severity}: ${message}`
|
|
171
|
+
}
|
|
172
|
+
|
|
136
173
|
export function cleanJsxText(raw: string): string {
|
|
137
174
|
if (!LINE_BREAK_RE.test(raw))
|
|
138
175
|
return raw
|
|
@@ -205,8 +242,12 @@ export function isJsxNode(node: Node | null | undefined): node is JSXElement | J
|
|
|
205
242
|
return node?.type === 'JSXElement' || node?.type === 'JSXFragment'
|
|
206
243
|
}
|
|
207
244
|
|
|
208
|
-
/** Whether JSX appears anywhere inside `node`, itself included. */
|
|
209
|
-
export function containsJsx(node: Node | null | undefined): boolean {
|
|
245
|
+
/** Whether JSX appears anywhere inside `node`, itself included. Uses O(1) lookup when precomputed `jsxNodes` is passed. */
|
|
246
|
+
export function containsJsx(node: Node | null | undefined, jsxNodes?: WeakSet<Node>): boolean {
|
|
247
|
+
if (node === null || node === undefined)
|
|
248
|
+
return false
|
|
249
|
+
if (jsxNodes !== undefined)
|
|
250
|
+
return jsxNodes.has(node)
|
|
210
251
|
let found = false
|
|
211
252
|
walk(node, (child) => {
|
|
212
253
|
if (found)
|
|
@@ -219,7 +260,9 @@ export function containsJsx(node: Node | null | undefined): boolean {
|
|
|
219
260
|
}
|
|
220
261
|
|
|
221
262
|
/** Whether a BlockStatement directly returns JSX (ignoring nested functions). */
|
|
222
|
-
export function blockReturnsJsx(block: BlockStatement): boolean {
|
|
263
|
+
export function blockReturnsJsx(block: BlockStatement, jsxNodes?: WeakSet<Node>): boolean {
|
|
264
|
+
if (jsxNodes !== undefined && !jsxNodes.has(block))
|
|
265
|
+
return false
|
|
223
266
|
let found = false
|
|
224
267
|
walk(block, (child) => {
|
|
225
268
|
if (found)
|
|
@@ -231,7 +274,7 @@ export function blockReturnsJsx(block: BlockStatement): boolean {
|
|
|
231
274
|
)) {
|
|
232
275
|
return false
|
|
233
276
|
}
|
|
234
|
-
if (child.type === 'ReturnStatement' && child.argument !== null && containsJsx(child.argument)) {
|
|
277
|
+
if (child.type === 'ReturnStatement' && child.argument !== null && containsJsx(child.argument, jsxNodes)) {
|
|
235
278
|
found = true
|
|
236
279
|
return false
|
|
237
280
|
}
|
|
@@ -263,7 +306,7 @@ export interface MapCallMatch {
|
|
|
263
306
|
}
|
|
264
307
|
|
|
265
308
|
/** Recognizes `arr.map((item, idx?) => <jsx> | { ... return <jsx> })` identically in SSR and DOM compilers. */
|
|
266
|
-
export function matchJsxMapCall(expr: Node): MapCallMatch | undefined {
|
|
309
|
+
export function matchJsxMapCall(expr: Node, jsxNodes?: WeakSet<Node>): MapCallMatch | undefined {
|
|
267
310
|
if (
|
|
268
311
|
expr.type !== 'CallExpression'
|
|
269
312
|
|| expr.callee.type !== 'MemberExpression'
|
|
@@ -285,7 +328,7 @@ export function matchJsxMapCall(expr: Node): MapCallMatch | undefined {
|
|
|
285
328
|
const body = unwrapParens(arg.body)
|
|
286
329
|
if (isJsxNode(body))
|
|
287
330
|
return { call: expr, target: expr.callee.object, cb: arg, body, isJsxBody: true, params: arg.params }
|
|
288
|
-
if (body.type === 'BlockStatement' && blockReturnsJsx(body))
|
|
331
|
+
if (body.type === 'BlockStatement' && blockReturnsJsx(body, jsxNodes))
|
|
289
332
|
return { call: expr, target: expr.callee.object, cb: arg, body, isJsxBody: false, params: arg.params }
|
|
290
333
|
return undefined
|
|
291
334
|
}
|
|
@@ -313,7 +356,7 @@ export type ChildSlotKind =
|
|
|
313
356
|
* Single shared child classifier for SSR & DOM codegen.
|
|
314
357
|
* Both emitters derive exact hydration marker placement (`<!--k[-->...<!--k]-->` vs `<!---->`) from this classification.
|
|
315
358
|
*/
|
|
316
|
-
export function classifyJsxChild(child: JSXChild): ChildSlotKind | undefined {
|
|
359
|
+
export function classifyJsxChild(child: JSXChild, jsxNodes?: WeakSet<Node>): ChildSlotKind | undefined {
|
|
317
360
|
if (child.type === 'JSXText') {
|
|
318
361
|
const text = cleanJsxText(child.value)
|
|
319
362
|
return text === '' ? undefined : { kind: 'static-text', text, fromLiteral: false }
|
|
@@ -335,7 +378,7 @@ export function classifyJsxChild(child: JSXChild): ChildSlotKind | undefined {
|
|
|
335
378
|
? undefined
|
|
336
379
|
: { kind: 'static-text', text: expr.value, fromLiteral: true }
|
|
337
380
|
}
|
|
338
|
-
const mapMatch = matchJsxMapCall(expr)
|
|
381
|
+
const mapMatch = matchJsxMapCall(expr, jsxNodes)
|
|
339
382
|
if (mapMatch !== undefined)
|
|
340
383
|
return { kind: 'loop', match: mapMatch }
|
|
341
384
|
if (expr.type === 'LogicalExpression' && expr.operator === '&&') {
|
|
@@ -433,10 +476,10 @@ export function mayReturnNode(node: Node): boolean {
|
|
|
433
476
|
}
|
|
434
477
|
|
|
435
478
|
/** Shared SSR/DOM check for whether a dynamic expression slot may produce DOM nodes/fragments (requiring balanced `<!--k[-->...<!--k]-->` markers). */
|
|
436
|
-
export function isNodeCapableExpr(expr: Expression): boolean {
|
|
479
|
+
export function isNodeCapableExpr(expr: Expression, jsxNodes?: WeakSet<Node>): boolean {
|
|
437
480
|
const isChildrenExpr = (expr.type === 'Identifier' && expr.name === 'children')
|
|
438
481
|
|| (expr.type === 'MemberExpression' && !expr.computed && expr.property.type === 'Identifier' && expr.property.name === 'children')
|
|
439
|
-
return isChildrenExpr || containsJsx(expr) || mayReturnNode(expr)
|
|
482
|
+
return isChildrenExpr || containsJsx(expr, jsxNodes) || mayReturnNode(expr)
|
|
440
483
|
}
|
|
441
484
|
|
|
442
485
|
export type { Program }
|