@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/cli/check.ts
CHANGED
|
@@ -5,11 +5,11 @@ import { existsSync, readFileSync } from 'node:fs'
|
|
|
5
5
|
import { basename, join, relative, resolve } from 'node:path'
|
|
6
6
|
import { isTsFile, relPosix, walkFiles, walkRoutes } from '../build/files'
|
|
7
7
|
import { ModuleGraph } from '../build/graph'
|
|
8
|
-
import { fileToRoutePath, hasRouteHandlerExport, normalizeRoutePattern,
|
|
8
|
+
import { fileToRoutePath, hasRouteHandlerExport, normalizeRoutePattern, routeExportProblemLocation, SPECIAL_ROUTE_FILES } from '../build/routes'
|
|
9
9
|
import { analyzeModule, walk } from '../compiler/analyze'
|
|
10
|
-
import {
|
|
11
|
-
import { jsxTagName } from '../compiler/shared'
|
|
12
|
-
import { createAppContext, loadConfig, runWithAppContext } from '../plugins/index'
|
|
10
|
+
import { findPrivateEnvOffset, SERVER_ONLY_MODULES } from '../compiler/server-only'
|
|
11
|
+
import { DIAGNOSTIC_PREFIX_RE, formatDiagnostic, formatLoc, jsxTagName } from '../compiler/shared'
|
|
12
|
+
import { createAppContext, loadConfig, resolvePlugins, runWithAppContext } from '../plugins/index'
|
|
13
13
|
|
|
14
14
|
export interface CheckResult {
|
|
15
15
|
ok: boolean
|
|
@@ -28,10 +28,19 @@ function walkTsFiles(dir: string): string[] {
|
|
|
28
28
|
})
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
function
|
|
31
|
+
function sourceOf(file: string, sources: Map<string, string>): string {
|
|
32
|
+
let code = sources.get(file)
|
|
33
|
+
if (code === undefined) {
|
|
34
|
+
code = readFileSync(file, 'utf8')
|
|
35
|
+
sources.set(file, code)
|
|
36
|
+
}
|
|
37
|
+
return code
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function analysisOf(file: string, analyses: Map<string, ModuleAnalysis>, sources: Map<string, string>, unparsable: Set<string>): ModuleAnalysis | undefined {
|
|
32
41
|
if (!analyses.has(file) && !unparsable.has(file) && isTsFile(file)) {
|
|
33
42
|
try {
|
|
34
|
-
analyses.set(file, analyzeModule(file,
|
|
43
|
+
analyses.set(file, analyzeModule(file, sourceOf(file, sources)))
|
|
35
44
|
}
|
|
36
45
|
catch {
|
|
37
46
|
unparsable.add(file)
|
|
@@ -40,8 +49,44 @@ function analysisOf(file: string, analyses: Map<string, ModuleAnalysis>, unparsa
|
|
|
40
49
|
return analyses.get(file)
|
|
41
50
|
}
|
|
42
51
|
|
|
52
|
+
function locOf(root: string, file: string, sources: Map<string, string>, offset = 0): string {
|
|
53
|
+
const rel = relPosix(root, file)
|
|
54
|
+
const code = sources.get(file)
|
|
55
|
+
return code !== undefined ? formatLoc(rel, code, offset) : `${rel}:1:1`
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function findExportedConstOffset(analysis: ModuleAnalysis, name: string): number {
|
|
59
|
+
for (const stmt of analysis.program.body) {
|
|
60
|
+
if (stmt.type === 'ExportNamedDeclaration' && stmt.declaration?.type === 'VariableDeclaration') {
|
|
61
|
+
for (const d of stmt.declaration.declarations) {
|
|
62
|
+
if (d.id.type === 'Identifier' && d.id.name === name)
|
|
63
|
+
return d.start
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return 0
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function normalizeDiagnosticPath(root: string, file: string, message: string): string {
|
|
71
|
+
const rel = relPosix(root, file)
|
|
72
|
+
if (DIAGNOSTIC_PREFIX_RE.test(message)) {
|
|
73
|
+
if (message.startsWith(`${file}:`))
|
|
74
|
+
return `${rel}${message.slice(file.length)}`
|
|
75
|
+
return message
|
|
76
|
+
}
|
|
77
|
+
return formatDiagnostic(`${rel}:1:1`, message)
|
|
78
|
+
}
|
|
79
|
+
|
|
43
80
|
// Walks the local imports of an island the way the client build follows them: a module that only declares actions is stubbed there, so its imports stop the walk.
|
|
44
|
-
function checkIslandReach(
|
|
81
|
+
function checkIslandReach(
|
|
82
|
+
island: string,
|
|
83
|
+
root: string,
|
|
84
|
+
graph: ModuleGraph,
|
|
85
|
+
sources: Map<string, string>,
|
|
86
|
+
unparsable: Set<string>,
|
|
87
|
+
reported: Set<string>,
|
|
88
|
+
errors: string[],
|
|
89
|
+
): void {
|
|
45
90
|
const islandRel = relPosix(root, island)
|
|
46
91
|
const seen = new Set<string>()
|
|
47
92
|
const stack = [island]
|
|
@@ -52,30 +97,29 @@ function checkIslandReach(island: string, root: string, graph: ModuleGraph, unpa
|
|
|
52
97
|
seen.add(file)
|
|
53
98
|
if (NODE_MODULES_RE.test(file))
|
|
54
99
|
continue
|
|
55
|
-
const analysis = analysisOf(file, graph.analyses, unparsable)
|
|
100
|
+
const analysis = analysisOf(file, graph.analyses, sources, unparsable)
|
|
56
101
|
if (analysis === undefined || (file !== island && !analysis.isInteractive && analysis.exportedActions.length > 0))
|
|
57
102
|
continue
|
|
58
|
-
const
|
|
59
|
-
const report = (key: string, message: string): void => {
|
|
103
|
+
const report = (key: string, offset: number, message: string): void => {
|
|
60
104
|
if (reported.has(`${file}\0${key}`))
|
|
61
105
|
return
|
|
62
106
|
reported.add(`${file}\0${key}`)
|
|
63
|
-
errors.push(message)
|
|
107
|
+
errors.push(formatDiagnostic(locOf(root, file, sources, offset), message))
|
|
64
108
|
}
|
|
65
109
|
for (const imp of analysis.imports) {
|
|
66
110
|
if (SERVER_ONLY_MODULES.has(imp.source)) {
|
|
67
|
-
report(imp.source, file === island
|
|
68
|
-
? `kay check:
|
|
69
|
-
: `kay check:
|
|
111
|
+
report(imp.source, imp.start, file === island
|
|
112
|
+
? `kay check: interactive island imports server-only module "${imp.source}". Move server logic to a server action or async server component.`
|
|
113
|
+
: `kay check: imports server-only module "${imp.source}" and is reachable from the island in ${islandRel}. Move server logic to a server action or async server component.`)
|
|
70
114
|
continue
|
|
71
115
|
}
|
|
72
116
|
const target = graph.resolve(file, imp.source)
|
|
73
117
|
if (target !== undefined) {
|
|
74
|
-
const targetAnalysis = analysisOf(target, graph.analyses, unparsable)
|
|
118
|
+
const targetAnalysis = analysisOf(target, graph.analyses, sources, unparsable)
|
|
75
119
|
if (targetAnalysis !== undefined && !targetAnalysis.isInteractive && targetAnalysis.exportedActions.length > 0) {
|
|
76
120
|
const missing = imp.specifiers.find(s => s.imported !== '*' && !targetAnalysis.exportedActions.includes(s.imported))
|
|
77
121
|
if (missing !== undefined)
|
|
78
|
-
report(`action-export:${target}:${missing.imported}`, `kay check:
|
|
122
|
+
report(`action-export:${target}:${missing.imported}`, imp.start, `kay check: imports non-action "${missing.imported}" from server action file ${relPosix(root, target)}. Move ${missing.imported} into a module without actions.`)
|
|
79
123
|
continue
|
|
80
124
|
}
|
|
81
125
|
stack.push(target)
|
|
@@ -83,15 +127,16 @@ function checkIslandReach(island: string, root: string, graph: ModuleGraph, unpa
|
|
|
83
127
|
}
|
|
84
128
|
for (const re of analysis.reexports) {
|
|
85
129
|
if (SERVER_ONLY_MODULES.has(re.source)) {
|
|
86
|
-
report(re.source, `kay check:
|
|
130
|
+
report(re.source, re.start, `kay check: re-exports from server-only module "${re.source}" and is reachable from the island in ${islandRel}.`)
|
|
87
131
|
continue
|
|
88
132
|
}
|
|
89
133
|
const target = graph.resolve(file, re.source)
|
|
90
134
|
if (target !== undefined)
|
|
91
135
|
stack.push(target)
|
|
92
136
|
}
|
|
93
|
-
|
|
94
|
-
|
|
137
|
+
const envOffset = findPrivateEnvOffset(analysis.program)
|
|
138
|
+
if (envOffset !== undefined)
|
|
139
|
+
report('process.env', envOffset, `kay check: reads process.env and is reachable from the island in ${islandRel}. Only process.env.PUBLIC_* reaches the browser.`)
|
|
95
140
|
}
|
|
96
141
|
}
|
|
97
142
|
|
|
@@ -106,7 +151,7 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
106
151
|
let plugins: Awaited<ReturnType<typeof loadConfig>>['plugins'] = []
|
|
107
152
|
try {
|
|
108
153
|
const cfg = await loadConfig(root)
|
|
109
|
-
plugins = cfg.plugins
|
|
154
|
+
plugins = resolvePlugins(cfg.plugins)
|
|
110
155
|
if (cfg.routesDir !== undefined)
|
|
111
156
|
routesDir = resolve(root, cfg.routesDir)
|
|
112
157
|
if (cfg.outDir !== undefined)
|
|
@@ -115,12 +160,14 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
115
160
|
assetPrefix = cfg.assetPrefix.replace(/\/$/, '')
|
|
116
161
|
}
|
|
117
162
|
catch (err) {
|
|
118
|
-
errors.push(
|
|
163
|
+
errors.push(formatDiagnostic('kay.config.ts:1:1', err instanceof Error ? err.message : String(err)))
|
|
119
164
|
}
|
|
120
165
|
|
|
121
166
|
// 2. Validate routesDir exists
|
|
122
|
-
if (!existsSync(routesDir))
|
|
123
|
-
|
|
167
|
+
if (!existsSync(routesDir)) {
|
|
168
|
+
const relRoutes = relative(root, routesDir) || routesDir
|
|
169
|
+
errors.push(formatDiagnostic(`${relRoutes.replace(/\\/g, '/')}:1:1`, `kay check: missing routes directory: ${relRoutes}`))
|
|
170
|
+
}
|
|
124
171
|
|
|
125
172
|
// 3. Parse & run Kay static compiler analysis across src/ AND custom routesDir
|
|
126
173
|
const srcDir = resolve(root, 'src')
|
|
@@ -136,18 +183,21 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
136
183
|
const files = [...fileSet].sort()
|
|
137
184
|
const graph = new ModuleGraph()
|
|
138
185
|
const { analyses } = graph
|
|
186
|
+
const sources = new Map<string, string>()
|
|
139
187
|
const unparsable = new Set<string>()
|
|
140
188
|
|
|
141
189
|
const warnings: string[] = []
|
|
142
190
|
for (const file of files) {
|
|
143
191
|
try {
|
|
144
|
-
const
|
|
192
|
+
const code = sourceOf(file, sources)
|
|
193
|
+
const analysis = analyzeModule(file, code)
|
|
145
194
|
analyses.set(file, analysis)
|
|
146
195
|
warnings.push(...analysis.warnings)
|
|
147
196
|
}
|
|
148
197
|
catch (err) {
|
|
149
198
|
unparsable.add(file)
|
|
150
|
-
|
|
199
|
+
const msg = err instanceof Error ? err.message : String(err)
|
|
200
|
+
errors.push(normalizeDiagnosticPath(root, file, msg))
|
|
151
201
|
}
|
|
152
202
|
}
|
|
153
203
|
|
|
@@ -164,9 +214,9 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
164
214
|
return false
|
|
165
215
|
try {
|
|
166
216
|
if (spec.imported === '*')
|
|
167
|
-
return (analysisOf(resolved, analyses, unparsable)?.stateExports.length ?? 0) > 0
|
|
217
|
+
return (analysisOf(resolved, analyses, sources, unparsable)?.stateExports.length ?? 0) > 0
|
|
168
218
|
const target = graph.exportTarget(resolved, spec.imported)
|
|
169
|
-
return analysisOf(target.file, analyses, unparsable)?.stateExports.includes(target.exportName) === true
|
|
219
|
+
return analysisOf(target.file, analyses, sources, unparsable)?.stateExports.includes(target.exportName) === true
|
|
170
220
|
}
|
|
171
221
|
catch {
|
|
172
222
|
return false
|
|
@@ -175,9 +225,9 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
175
225
|
if (state !== undefined) {
|
|
176
226
|
const reason = `reads ${state.local} from '${imp.source}'`
|
|
177
227
|
if (analysis.hasAsyncDefault)
|
|
178
|
-
errors.push(`kay check: async route component
|
|
228
|
+
errors.push(formatDiagnostic(locOf(root, file, sources, imp.start), `kay check: async route component cannot use browser interactivity (${reason}).`))
|
|
179
229
|
else
|
|
180
|
-
analyses.set(file, { ...analysis, isInteractive: true, interactiveReason: reason })
|
|
230
|
+
analyses.set(file, { ...analysis, isInteractive: true, interactiveReason: reason, interactiveOffset: imp.start })
|
|
181
231
|
break
|
|
182
232
|
}
|
|
183
233
|
}
|
|
@@ -196,12 +246,15 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
196
246
|
|
|
197
247
|
// Special route files (`_layout.tsx`, `_error.tsx`, `_404.tsx`) cannot be interactive
|
|
198
248
|
if (routeFileSet.has(file) && SPECIAL_ROUTE_FILES.has(basename(file)) && analysis.isInteractive) {
|
|
199
|
-
errors.push(
|
|
249
|
+
errors.push(formatDiagnostic(
|
|
250
|
+
locOf(root, file, sources, analysis.interactiveOffset ?? 0),
|
|
251
|
+
`kay check: ${rel} uses browser interactivity (${analysis.interactiveReason}), but it renders on the server only. Move the interactive part into a component and render that component from ${rel}.`,
|
|
252
|
+
))
|
|
200
253
|
}
|
|
201
254
|
|
|
202
255
|
// Guardrail 1: server-only imports and private env reads in an island or any module it imports
|
|
203
256
|
if (analysis.isInteractive)
|
|
204
|
-
checkIslandReach(file, root, graph, unparsable, reported, errors)
|
|
257
|
+
checkIslandReach(file, root, graph, sources, unparsable, reported, errors)
|
|
205
258
|
|
|
206
259
|
// Guardrail 2: non-serializable props passed from static server code into an island (including barrels & namespaces)
|
|
207
260
|
if (!analysis.isInteractive && file.endsWith('.tsx')) {
|
|
@@ -218,7 +271,7 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
218
271
|
}
|
|
219
272
|
try {
|
|
220
273
|
const target = graph.exportTarget(resolved, spec.imported)
|
|
221
|
-
if (analysisOf(target.file, analyses, unparsable)?.isInteractive)
|
|
274
|
+
if (analysisOf(target.file, analyses, sources, unparsable)?.isInteractive)
|
|
222
275
|
islandTags.add(spec.local)
|
|
223
276
|
}
|
|
224
277
|
catch {}
|
|
@@ -234,7 +287,7 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
234
287
|
if (nsFile !== undefined && member !== undefined) {
|
|
235
288
|
try {
|
|
236
289
|
const target = graph.exportTarget(nsFile, member)
|
|
237
|
-
return analysisOf(target.file, analyses, unparsable)?.isInteractive === true
|
|
290
|
+
return analysisOf(target.file, analyses, sources, unparsable)?.isInteractive === true
|
|
238
291
|
}
|
|
239
292
|
catch {}
|
|
240
293
|
}
|
|
@@ -259,14 +312,16 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
259
312
|
continue
|
|
260
313
|
const expr = attr.value.expression
|
|
261
314
|
if (expr.type === 'ArrowFunctionExpression' || expr.type === 'FunctionExpression') {
|
|
262
|
-
errors.push(
|
|
263
|
-
|
|
264
|
-
|
|
315
|
+
errors.push(formatDiagnostic(
|
|
316
|
+
locOf(root, file, sources, attr.start),
|
|
317
|
+
`kay check: cannot pass function prop "${attrName}" from a server component into island <${tag}>. Define the handler inside the island or pass serializable data.`,
|
|
318
|
+
))
|
|
265
319
|
}
|
|
266
320
|
else if (expr.type === 'NewExpression' && expr.callee.type === 'Identifier' && !SERIALIZABLE_NEW_CTORS.has(expr.callee.name)) {
|
|
267
|
-
errors.push(
|
|
268
|
-
|
|
269
|
-
|
|
321
|
+
errors.push(formatDiagnostic(
|
|
322
|
+
locOf(root, file, sources, expr.start),
|
|
323
|
+
`kay check: cannot pass non-serializable \`new ${expr.callee.name}()\` prop "${attrName}" into island <${tag}>.`,
|
|
324
|
+
))
|
|
270
325
|
}
|
|
271
326
|
}
|
|
272
327
|
})
|
|
@@ -275,15 +330,15 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
275
330
|
|
|
276
331
|
// Route-specific guardrails (only real route files matching `kay build`)
|
|
277
332
|
if (routeFileSet.has(file) && !basename(file).startsWith('_')) {
|
|
278
|
-
const problem =
|
|
333
|
+
const problem = routeExportProblemLocation(analysis, rel)
|
|
279
334
|
if (problem !== undefined)
|
|
280
|
-
errors.push(`kay check: ${problem}`)
|
|
335
|
+
errors.push(formatDiagnostic(locOf(root, file, sources, problem.offset), `kay check: ${problem.message}`))
|
|
281
336
|
|
|
282
337
|
if (hasRouteHandlerExport(analysis)) {
|
|
283
338
|
const conflictKey = normalizeRoutePattern(fileToRoutePath(routesDir, file))
|
|
284
339
|
const prev = seenRoutes.get(conflictKey)
|
|
285
340
|
if (prev !== undefined) {
|
|
286
|
-
errors.push(`kay check: conflicting routes "${prev}" and "${rel}" both match "${conflictKey}".`)
|
|
341
|
+
errors.push(formatDiagnostic(`${rel}:1:1`, `kay check: conflicting routes "${prev}" and "${rel}" both match "${conflictKey}".`))
|
|
287
342
|
}
|
|
288
343
|
else {
|
|
289
344
|
seenRoutes.set(conflictKey, rel)
|
|
@@ -291,7 +346,8 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
291
346
|
|
|
292
347
|
// Guardrail 4: dynamic prerender = true route missing paths()
|
|
293
348
|
if (analysis.hasPrerender && (conflictKey.includes(':') || conflictKey.includes('*')) && !analysis.hasPaths) {
|
|
294
|
-
|
|
349
|
+
const offset = findExportedConstOffset(analysis, 'prerender')
|
|
350
|
+
errors.push(formatDiagnostic(locOf(root, file, sources, offset), `kay check: dynamic route exports \`prerender = true\` without exporting \`paths()\`.`))
|
|
295
351
|
}
|
|
296
352
|
}
|
|
297
353
|
}
|
|
@@ -310,7 +366,7 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
310
366
|
root,
|
|
311
367
|
routesDir,
|
|
312
368
|
error(message: string) {
|
|
313
|
-
errors.push(message)
|
|
369
|
+
errors.push(DIAGNOSTIC_PREFIX_RE.test(message) ? message : formatDiagnostic('kay.config.ts:1:1', message))
|
|
314
370
|
},
|
|
315
371
|
}
|
|
316
372
|
for (const plugin of plugins) {
|
|
@@ -319,7 +375,8 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
319
375
|
await runWithAppContext(appCtx, () => plugin.check!(pluginCtx))
|
|
320
376
|
}
|
|
321
377
|
catch (err) {
|
|
322
|
-
|
|
378
|
+
const msg = `${plugin.name}: ${err instanceof Error ? err.message : String(err)}`
|
|
379
|
+
errors.push(DIAGNOSTIC_PREFIX_RE.test(msg) ? msg : formatDiagnostic('kay.config.ts:1:1', msg))
|
|
323
380
|
}
|
|
324
381
|
}
|
|
325
382
|
}
|
package/src/cli/new.ts
CHANGED
|
@@ -15,7 +15,7 @@ export function scaffoldProject(targetDir: string, options: ScaffoldOptions = {}
|
|
|
15
15
|
const root = resolve(targetDir)
|
|
16
16
|
const name = basename(root).toLowerCase().replace(/[^a-z0-9-_]/g, '-') || 'kay-app'
|
|
17
17
|
const template: ScaffoldTemplate = options.template ?? 'minimal'
|
|
18
|
-
const kayVersion = options.kayVersion ?? '^2.0.0-beta.
|
|
18
|
+
const kayVersion = options.kayVersion ?? '^2.0.0-beta.3'
|
|
19
19
|
|
|
20
20
|
if (existsSync(root) && readdirSync(root).length > 0 && !options.force)
|
|
21
21
|
throw new Error(`kay new: directory "${root}" is not empty (pass --force to overwrite)`)
|
|
@@ -416,14 +416,13 @@ export default defineConfig({
|
|
|
416
416
|
],
|
|
417
417
|
})
|
|
418
418
|
`
|
|
419
|
-
files['src/routes/_layout.tsx'] = `import type {
|
|
420
|
-
import type { KayChild } from 'kay'
|
|
419
|
+
files['src/routes/_layout.tsx'] = `import type { LayoutProps } from 'kay'
|
|
421
420
|
import { Button, Cluster, Toaster } from '@kayjs/ui'
|
|
422
421
|
import { Head } from 'kay'
|
|
423
422
|
import '../styles/app.css'
|
|
424
423
|
|
|
425
|
-
export default function Layout({ children, locals }:
|
|
426
|
-
const user = locals
|
|
424
|
+
export default function Layout({ children, locals }: LayoutProps) {
|
|
425
|
+
const user = locals.user
|
|
427
426
|
return (
|
|
428
427
|
<>
|
|
429
428
|
<Head>
|
|
@@ -505,10 +504,10 @@ export default function LoginPage() {
|
|
|
505
504
|
|
|
506
505
|
export default auth.guard({ redirectTo: '/login' })
|
|
507
506
|
`
|
|
508
|
-
files['src/routes/dashboard/index.tsx'] = `import type {
|
|
507
|
+
files['src/routes/dashboard/index.tsx'] = `import type { PageProps } from 'kay'
|
|
509
508
|
import { Card, CardBody, CardHeader } from '@kayjs/ui'
|
|
510
509
|
|
|
511
|
-
export default async function DashboardPage({ locals }:
|
|
510
|
+
export default async function DashboardPage({ locals }: PageProps) {
|
|
512
511
|
return (
|
|
513
512
|
<Card>
|
|
514
513
|
<CardHeader>Dashboard</CardHeader>
|
package/src/compiler/analyze.ts
CHANGED
|
@@ -12,7 +12,7 @@ import type {
|
|
|
12
12
|
} from 'oxc-parser'
|
|
13
13
|
import { parseSync, visitorKeys } from 'oxc-parser'
|
|
14
14
|
import { checkBareSignals, checkHydrateMode, checkMistakes, mistake } from './mistakes'
|
|
15
|
-
import { EVENT_ATTR, formatLoc, isComponentTag, isJsxNode, jsxTagName, unwrapTsExpr } from './shared'
|
|
15
|
+
import { EVENT_ATTR, formatCodeFrame, formatDiagnostic, formatLoc, isComponentTag, isJsxNode, jsxTagName, unwrapTsExpr } from './shared'
|
|
16
16
|
|
|
17
17
|
export type AstNode = Node
|
|
18
18
|
|
|
@@ -25,6 +25,7 @@ const REACTIVE_IMPORTS = new Set([
|
|
|
25
25
|
'selector',
|
|
26
26
|
'store',
|
|
27
27
|
'island',
|
|
28
|
+
'useAction',
|
|
28
29
|
])
|
|
29
30
|
|
|
30
31
|
// Primitives whose result is reactive state another module can import and read.
|
|
@@ -45,6 +46,7 @@ export interface ReexportInfo {
|
|
|
45
46
|
exported: string
|
|
46
47
|
local: string
|
|
47
48
|
source: string
|
|
49
|
+
start: number
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
export interface ModuleAnalysis {
|
|
@@ -52,9 +54,12 @@ export interface ModuleAnalysis {
|
|
|
52
54
|
program: Program
|
|
53
55
|
isInteractive: boolean
|
|
54
56
|
interactiveReason: string | undefined
|
|
57
|
+
interactiveOffset?: number | undefined
|
|
55
58
|
hasAsyncDefault: boolean
|
|
56
59
|
hasLoadExport: boolean
|
|
57
60
|
hasJsx: boolean
|
|
61
|
+
/** Precomputed set of AST nodes containing JSX (itself included), for O(1) subtree checks during codegen. */
|
|
62
|
+
jsxNodes: WeakSet<Node>
|
|
58
63
|
hasPrerender: boolean
|
|
59
64
|
hasPaths: boolean
|
|
60
65
|
usesRenderContext: boolean
|
|
@@ -76,6 +81,49 @@ export interface ModuleAnalysis {
|
|
|
76
81
|
warnings: string[]
|
|
77
82
|
}
|
|
78
83
|
|
|
84
|
+
export function computeJsxNodes(program: Program): WeakSet<Node> {
|
|
85
|
+
const jsxNodes = new WeakSet<Node>()
|
|
86
|
+
const visit = (node: unknown): boolean => {
|
|
87
|
+
if (node === null || typeof node !== 'object')
|
|
88
|
+
return false
|
|
89
|
+
if (Array.isArray(node)) {
|
|
90
|
+
let any = false
|
|
91
|
+
for (let i = 0; i < node.length; i++) {
|
|
92
|
+
if (visit(node[i]))
|
|
93
|
+
any = true
|
|
94
|
+
}
|
|
95
|
+
return any
|
|
96
|
+
}
|
|
97
|
+
const ast = node as Node
|
|
98
|
+
const type = ast.type
|
|
99
|
+
if (typeof type !== 'string')
|
|
100
|
+
return false
|
|
101
|
+
let found = isJsxNode(ast)
|
|
102
|
+
const keys = visitorKeys[type]
|
|
103
|
+
if (keys !== undefined) {
|
|
104
|
+
const rec = ast as unknown as Record<string, unknown>
|
|
105
|
+
for (let i = 0; i < keys.length; i++) {
|
|
106
|
+
const val = rec[keys[i]!]
|
|
107
|
+
if (val !== null && typeof val === 'object' && visit(val))
|
|
108
|
+
found = true
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
for (const [key, val] of Object.entries(ast)) {
|
|
113
|
+
if (key === 'start' || key === 'end' || key === 'type')
|
|
114
|
+
continue
|
|
115
|
+
if (val !== null && typeof val === 'object' && visit(val))
|
|
116
|
+
found = true
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (found)
|
|
120
|
+
jsxNodes.add(ast)
|
|
121
|
+
return found
|
|
122
|
+
}
|
|
123
|
+
visit(program)
|
|
124
|
+
return jsxNodes
|
|
125
|
+
}
|
|
126
|
+
|
|
79
127
|
export function walk(node: unknown, visit: (node: Node) => boolean | void): void {
|
|
80
128
|
if (node === null || typeof node !== 'object')
|
|
81
129
|
return
|
|
@@ -387,9 +435,12 @@ export function parseTsx(filename: string, code: string): Program {
|
|
|
387
435
|
const result = parseSync(filename, code, { lang, sourceType: 'module' })
|
|
388
436
|
if (result.errors.length > 0) {
|
|
389
437
|
const first = result.errors[0]!
|
|
390
|
-
const
|
|
438
|
+
const label = first.labels?.[0]
|
|
439
|
+
const offset = label?.start ?? 0
|
|
391
440
|
const loc = formatLoc(filename, code, offset)
|
|
392
|
-
|
|
441
|
+
const frame = formatCodeFrame(code, offset, label?.end)
|
|
442
|
+
const header = formatDiagnostic(loc, `kay compiler: syntax error: ${first.message}`)
|
|
443
|
+
throw new Error(frame ? `${header}\n${frame}` : header)
|
|
393
444
|
}
|
|
394
445
|
return result.program
|
|
395
446
|
}
|
|
@@ -468,6 +519,7 @@ function isIslandCall(node: Node, islandLocalName: string | undefined, kayNamesp
|
|
|
468
519
|
export function analyzeModule(filename: string, code: string, program: Program = parseTsx(filename, code)): ModuleAnalysis {
|
|
469
520
|
let isInteractive = false
|
|
470
521
|
let interactiveReason: string | undefined
|
|
522
|
+
let interactiveOffset: number | undefined
|
|
471
523
|
let hasAsyncDefault = false
|
|
472
524
|
let hasLoadExport = false
|
|
473
525
|
let hasJsx = false
|
|
@@ -496,10 +548,11 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
496
548
|
// The action() calls kay gives a URL to: the initialisers of top-level `export const`s.
|
|
497
549
|
const registeredActions = new Set<Node>()
|
|
498
550
|
|
|
499
|
-
const markInteractive = (reason: string) => {
|
|
551
|
+
const markInteractive = (reason: string, offset?: number) => {
|
|
500
552
|
if (!isInteractive) {
|
|
501
553
|
isInteractive = true
|
|
502
554
|
interactiveReason = reason
|
|
555
|
+
interactiveOffset = offset
|
|
503
556
|
}
|
|
504
557
|
}
|
|
505
558
|
|
|
@@ -530,7 +583,7 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
530
583
|
const local = spec.local.name
|
|
531
584
|
specifiers.push({ local, imported })
|
|
532
585
|
if (KAY_SOURCES.has(source) && REACTIVE_IMPORTS.has(imported))
|
|
533
|
-
markInteractive(`imports ${imported} from '${source}'
|
|
586
|
+
markInteractive(`imports ${imported} from '${source}'`, spec.start)
|
|
534
587
|
if (KAY_SOURCES.has(source) && imported === 'island')
|
|
535
588
|
islandLocalName = local
|
|
536
589
|
if (KAY_SOURCES.has(source) && STATE_PRIMITIVES.has(imported))
|
|
@@ -558,7 +611,7 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
558
611
|
}
|
|
559
612
|
|
|
560
613
|
const readIslandHydrate = (call: CallExpression): void => {
|
|
561
|
-
markInteractive('island() wrapper')
|
|
614
|
+
markInteractive('island() wrapper', call.start)
|
|
562
615
|
const opts = call.arguments[1]
|
|
563
616
|
if (opts !== undefined && opts.type === 'ObjectExpression') {
|
|
564
617
|
for (const prop of opts.properties) {
|
|
@@ -588,7 +641,7 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
588
641
|
}
|
|
589
642
|
else if (stmt.type === 'ExportAllDeclaration') {
|
|
590
643
|
if (stmt.exportKind !== 'type')
|
|
591
|
-
reexports.push({ exported: '*', local: '*', source: stmt.source.value })
|
|
644
|
+
reexports.push({ exported: '*', local: '*', source: stmt.source.value, start: stmt.start })
|
|
592
645
|
}
|
|
593
646
|
else if (stmt.type === 'ExportDefaultDeclaration') {
|
|
594
647
|
exportedNames.push('default')
|
|
@@ -619,7 +672,7 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
619
672
|
const local = exportedName(spec.local)
|
|
620
673
|
if (exported !== undefined && local !== undefined) {
|
|
621
674
|
exportedNames.push(exported)
|
|
622
|
-
reexports.push({ exported, local, source: src })
|
|
675
|
+
reexports.push({ exported, local, source: src, start: stmt.start })
|
|
623
676
|
if (exported === 'load')
|
|
624
677
|
hasLoadExport = true
|
|
625
678
|
if (exported === 'paths')
|
|
@@ -732,13 +785,13 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
732
785
|
}
|
|
733
786
|
// A handler or ref on a component tag is a prop for that component; only a DOM element binds it in the browser.
|
|
734
787
|
if (!isComponent && tag !== '' && (EVENT_ATTR.test(attrName) || attrName === 'ref'))
|
|
735
|
-
markInteractive(`uses ${attrName}
|
|
788
|
+
markInteractive(`uses ${attrName}`, attr.start)
|
|
736
789
|
}
|
|
737
790
|
}
|
|
738
791
|
if (node.type === 'MemberExpression' && !node.computed && node.object.type === 'Identifier' && kayNamespaces.has(node.object.name) && node.property.type === 'Identifier') {
|
|
739
792
|
const member = node.property.name
|
|
740
793
|
if (REACTIVE_IMPORTS.has(member))
|
|
741
|
-
markInteractive(`calls ${node.object.name}.${member}
|
|
794
|
+
markInteractive(`calls ${node.object.name}.${member}`, node.start)
|
|
742
795
|
if (STATE_PRIMITIVES.has(member))
|
|
743
796
|
stateCallees.add(`${node.object.name}.${member}`)
|
|
744
797
|
if (member === 'signal' || member === 'computed')
|
|
@@ -795,9 +848,11 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
795
848
|
warnings,
|
|
796
849
|
isInteractive,
|
|
797
850
|
interactiveReason,
|
|
851
|
+
interactiveOffset,
|
|
798
852
|
hasAsyncDefault,
|
|
799
853
|
hasLoadExport: hasLoadExport || loadDeclarations(program).length > 0,
|
|
800
854
|
hasJsx,
|
|
855
|
+
jsxNodes: hasJsx ? computeJsxNodes(program) : new WeakSet<Node>(),
|
|
801
856
|
hasPrerender,
|
|
802
857
|
hasPaths,
|
|
803
858
|
usesRenderContext,
|