prunelle 0.1.1 → 0.5.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.
Files changed (88) hide show
  1. package/llms.txt +23 -19
  2. package/package.json +8 -2
  3. package/src/a11y/declaration.ts +335 -0
  4. package/src/a11y/index.ts +446 -0
  5. package/src/a11y/rgaa-map.ts +184 -0
  6. package/src/attrs.ts +11 -3
  7. package/src/build/bundle.ts +3 -2
  8. package/src/build/client-entries.ts +3 -2
  9. package/src/build/client-plugin.ts +8 -4
  10. package/src/build/client-prune.ts +11 -2
  11. package/src/build/compile.ts +2 -1
  12. package/src/build/ecoindex.ts +229 -0
  13. package/src/build/index.ts +121 -44
  14. package/src/build/manifest.ts +66 -5
  15. package/src/build/output.ts +2 -2
  16. package/src/build/plugin-host.ts +91 -33
  17. package/src/build/precompress.ts +15 -2
  18. package/src/build/prerender.ts +2 -2
  19. package/src/build/report.ts +23 -2
  20. package/src/build/resolve.ts +4 -3
  21. package/src/build/routes.ts +175 -10
  22. package/src/build/static.ts +71 -17
  23. package/src/build/status-pages.ts +127 -0
  24. package/src/build/style-bundles.ts +338 -0
  25. package/src/cli/check-forms.ts +654 -0
  26. package/src/cli/check-links.ts +619 -0
  27. package/src/cli/check.ts +52 -5
  28. package/src/cli/commands.ts +86 -4
  29. package/src/cli/docs.ts +402 -0
  30. package/src/cli/inspect.ts +31 -2
  31. package/src/cli/mcp-tools.ts +480 -0
  32. package/src/cli/mcp.ts +205 -0
  33. package/src/cli/new.ts +2 -2
  34. package/src/cli/render.ts +261 -0
  35. package/src/cli/upgrade-names.ts +73 -13
  36. package/src/cli/upgrade.ts +137 -16
  37. package/src/compiler/analyze.ts +17 -3
  38. package/src/compiler/cacheability.ts +592 -0
  39. package/src/compiler/compliance-rgaa.ts +502 -0
  40. package/src/compiler/compliance.ts +120 -221
  41. package/src/compiler/dev-source.ts +87 -0
  42. package/src/compiler/dom.ts +7 -7
  43. package/src/compiler/ssr.ts +135 -37
  44. package/src/dev/error-log.ts +151 -0
  45. package/src/dev/index.ts +60 -29
  46. package/src/dev/open-editor.ts +204 -0
  47. package/src/dev/reload.ts +5 -3
  48. package/src/dev/supervisor.ts +3 -0
  49. package/src/dom/action.ts +4 -1
  50. package/src/dom/island.ts +11 -10
  51. package/src/plugins/config.ts +4 -4
  52. package/src/plugins/index.ts +2 -2
  53. package/src/plugins/public.ts +5 -2
  54. package/src/plugins/services.ts +62 -0
  55. package/src/plugins/types.ts +32 -0
  56. package/src/server/action-protect.ts +198 -0
  57. package/src/server/action-registry.ts +1 -1
  58. package/src/server/action.ts +82 -27
  59. package/src/server/errors.ts +291 -62
  60. package/src/server/form-schema.ts +343 -0
  61. package/src/server/health.ts +244 -0
  62. package/src/server/http.ts +86 -19
  63. package/src/server/index.ts +158 -219
  64. package/src/server/param-matchers.ts +112 -0
  65. package/src/server/plugin-host.ts +40 -14
  66. package/src/server/public.ts +3 -3
  67. package/src/server/rate-limit.ts +58 -35
  68. package/src/server/request-body.ts +1 -1
  69. package/src/server/route-cache.ts +365 -0
  70. package/src/server/route-table.ts +302 -6
  71. package/src/server/router.ts +125 -40
  72. package/src/server/routing.ts +187 -61
  73. package/src/server/security.ts +208 -32
  74. package/src/server/serve-handlers.ts +21 -7
  75. package/src/ssr/context.ts +49 -0
  76. package/src/ssr/index.ts +120 -1
  77. package/src/test/browser.ts +6 -0
  78. package/src/test/index.ts +17 -3
  79. package/src/ts-plugin/diagnostics.ts +378 -0
  80. package/src/ts-plugin/fixes.ts +364 -0
  81. package/src/ts-plugin/index.ts +194 -0
  82. package/templates/_shared/AGENTS.md +2 -2
  83. package/templates/_shared/mcp.json +11 -0
  84. package/templates/_shared/src/routes/legal.tsx +5 -0
  85. package/templates/_shared/tsconfig.json +5 -0
  86. package/templates/blog/prunelle.config.ts +1 -1
  87. package/templates/blog/src/routes/index.tsx +4 -0
  88. package/ts-plugin.cjs +4277 -0
@@ -0,0 +1,338 @@
1
+ // Consolidates per-route stylesheets into at most one site-wide sheet (cached across pages) plus one per-route sheet,
2
+ // both content-hashed and immutable, preserving cascade order (layers, @import/@charset, plugin CSS before app CSS).
3
+
4
+ import type { RouteManifestEntry, StatusPageManifestEntry } from './manifest'
5
+ import { createHash } from 'node:crypto'
6
+ import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
7
+ import { join, posix } from 'node:path'
8
+
9
+ const CHARSET_RE = /@charset\s+["'][^"']+["']\s*;/gi
10
+ const IMPORT_RE = /^@import\s+(?:url\([^)]*\)|["'][^"']+["'])[^;]*;/gm
11
+ const CSS_URL_RE = /url\(\s*(["']?)([^)"']+)\1\s*\)/g
12
+ const ABS_OR_DATA_URL_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#)/i
13
+
14
+ /** Safe lossless CSS minifier: strips comments and redundant whitespace while preserving strings, calc() operators, and data URIs. */
15
+ export function minifyCss(css: string): string {
16
+ const out: string[] = []
17
+ let i = 0
18
+ const n = css.length
19
+ let pendingSpace = false
20
+
21
+ while (i < n) {
22
+ const ch = css[i]!
23
+ // Quoted string
24
+ if (ch === '"' || ch === '\'') {
25
+ if (pendingSpace && out.length > 0 && needsSpaceBetween(out[out.length - 1]!, ch))
26
+ out.push(' ')
27
+ pendingSpace = false
28
+ const quote = ch
29
+ const start = i
30
+ i++
31
+ while (i < n) {
32
+ if (css[i] === '\\') {
33
+ i += 2
34
+ continue
35
+ }
36
+ if (css[i] === quote) {
37
+ i++
38
+ break
39
+ }
40
+ i++
41
+ }
42
+ out.push(css.slice(start, i))
43
+ continue
44
+ }
45
+ // Comment
46
+ if (ch === '/' && css[i + 1] === '*') {
47
+ const end = css.indexOf('*/', i + 2)
48
+ i = end === -1 ? n : end + 2
49
+ continue
50
+ }
51
+ // Unquoted url(...) — keep verbatim inside parens
52
+ if ((ch === 'u' || ch === 'U') && css.slice(i, i + 4).toLowerCase() === 'url(') {
53
+ if (pendingSpace && out.length > 0 && needsSpaceBefore(out[out.length - 1]!, 'u'))
54
+ out.push(' ')
55
+ pendingSpace = false
56
+ const start = i
57
+ i += 4
58
+ while (i < n && /\s/.test(css[i]!))
59
+ i++
60
+ if (css[i] !== '"' && css[i] !== '\'') {
61
+ while (i < n && css[i] !== ')')
62
+ i++
63
+ if (i < n)
64
+ i++
65
+ out.push(css.slice(start, i))
66
+ continue
67
+ }
68
+ out.push('url(')
69
+ continue
70
+ }
71
+ // Whitespace
72
+ if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r' || ch === '\f') {
73
+ pendingSpace = true
74
+ i++
75
+ continue
76
+ }
77
+ if (pendingSpace && out.length > 0) {
78
+ const prev = out[out.length - 1]!
79
+ if (needsSpaceBetween(prev, ch))
80
+ out.push(' ')
81
+ }
82
+ pendingSpace = false
83
+ // Drop trailing semicolon right before `}`
84
+ if (ch === '}' && out.length > 0 && out[out.length - 1] === ';')
85
+ out.pop()
86
+ out.push(ch)
87
+ i++
88
+ }
89
+ return out.join('').trim()
90
+ }
91
+
92
+ function needsSpaceBefore(prev: string, nextChar: string): boolean {
93
+ const p = prev[prev.length - 1]!
94
+ return !'{};:,>~+('.includes(p) && !'{}'.includes(nextChar)
95
+ }
96
+
97
+ function needsSpaceBetween(prev: string, nextChar: string): boolean {
98
+ const p = prev[prev.length - 1]!
99
+ // Preserve spaces around `+` and `-` (required inside CSS calc()/min()/max()/clamp()).
100
+ if (p === '+' || nextChar === '+' || p === '-' || nextChar === '-') {
101
+ if (p === ';' || p === '{' || p === '}' || p === ':' || p === ',' || p === '(')
102
+ return false
103
+ return true
104
+ }
105
+ if ('{};,>~'.includes(p) || '{};,>~)'.includes(nextChar))
106
+ return false
107
+ if (p === ':' && nextChar !== ':')
108
+ return false
109
+ if (p === '(')
110
+ return false
111
+ return true
112
+ }
113
+
114
+ function rebaseRelativeUrls(css: string, relFile: string): string {
115
+ const dir = posix.dirname(relFile)
116
+ if (dir === '.' || dir === '')
117
+ return css
118
+ return css.replace(CSS_URL_RE, (full, quote: string, target: string) => {
119
+ const trimmed = target.trim()
120
+ if (trimmed === '' || ABS_OR_DATA_URL_RE.test(trimmed))
121
+ return full
122
+ const rebased = posix.normalize(posix.join(dir, trimmed))
123
+ return `url(${quote}${rebased}${quote})`
124
+ })
125
+ }
126
+
127
+ function combineCssFiles(clientDir: string, relFiles: readonly string[], minify: boolean): string {
128
+ let hasCharset = false
129
+ const imports: string[] = []
130
+ const bodies: string[] = []
131
+
132
+ for (const rel of relFiles) {
133
+ let text = readFileSync(join(clientDir, rel), 'utf8')
134
+ text = text.replace(CHARSET_RE, () => {
135
+ hasCharset = true
136
+ return ''
137
+ })
138
+ text = text.replace(IMPORT_RE, (stmt) => {
139
+ imports.push(stmt.trim())
140
+ return ''
141
+ })
142
+ text = rebaseRelativeUrls(text.trim(), rel)
143
+ if (text !== '')
144
+ bodies.push(text)
145
+ }
146
+
147
+ const parts: string[] = []
148
+ if (hasCharset)
149
+ parts.push('@charset "UTF-8";')
150
+ if (imports.length > 0)
151
+ parts.push(imports.join('\n'))
152
+ parts.push(bodies.join('\n'))
153
+ const joined = parts.join('\n')
154
+ return minify ? minifyCss(joined) : joined
155
+ }
156
+
157
+ function shortHash(text: string): string {
158
+ return createHash('sha256').update(text).digest('hex').slice(0, 8)
159
+ }
160
+
161
+ export interface ConsolidateStylesheetsOptions {
162
+ clientDir: string
163
+ assetPrefix: string
164
+ minify: boolean
165
+ routes: RouteManifestEntry[]
166
+ statusPages: StatusPageManifestEntry[]
167
+ globalStyles: string[]
168
+ assets: string[]
169
+ styleUrls: string[]
170
+ }
171
+
172
+ /**
173
+ * Merges local stylesheets so each page links at most one site-wide sheet (cached across navigations)
174
+ * plus at most one per-route sheet (deduped by content hash), keeping cascade order unchanged.
175
+ */
176
+ export function consolidateStylesheets(options: ConsolidateStylesheetsOptions): void {
177
+ const { clientDir, assetPrefix, minify, routes, statusPages, globalStyles, assets, styleUrls } = options
178
+ const prefix = `${assetPrefix.replace(/\/$/, '')}/`
179
+
180
+ const toLocalRel = (url: string): string | undefined => {
181
+ if (!url.startsWith(prefix) || !url.endsWith('.css'))
182
+ return undefined
183
+ const rel = url.slice(prefix.length)
184
+ return existsSync(join(clientDir, rel)) ? rel : undefined
185
+ }
186
+
187
+ const emittedBundles = new Map<string, string>()
188
+ const emitBundle = (kind: 'site' | 'route', relFiles: readonly string[]): string => {
189
+ const key = `${kind}:${relFiles.join('\0')}`
190
+ const existing = emittedBundles.get(key)
191
+ if (existing !== undefined)
192
+ return existing
193
+ const css = combineCssFiles(clientDir, relFiles, minify)
194
+ const relOut = `${kind}-${shortHash(css)}.css`
195
+ writeFileSync(join(clientDir, relOut), css)
196
+ if (!assets.includes(relOut))
197
+ assets.push(relOut)
198
+ const url = `${prefix}${relOut}`
199
+ emittedBundles.set(key, url)
200
+ return url
201
+ }
202
+
203
+ const originalLocalCss = new Set<string>()
204
+ for (const url of [...globalStyles, ...styleUrls]) {
205
+ const rel = toLocalRel(url)
206
+ if (rel !== undefined)
207
+ originalLocalCss.add(rel)
208
+ }
209
+ for (const r of routes) {
210
+ for (const url of r.styles ?? []) {
211
+ const rel = toLocalRel(url)
212
+ if (rel !== undefined)
213
+ originalLocalCss.add(rel)
214
+ }
215
+ }
216
+ for (const sp of statusPages) {
217
+ for (const url of sp.styles) {
218
+ const rel = toLocalRel(url)
219
+ if (rel !== undefined)
220
+ originalLocalCss.add(rel)
221
+ }
222
+ }
223
+
224
+ // 1. Consolidate global site-wide local sheets into at most one hashed immutable sheet.
225
+ const siteLocalUrls: string[] = []
226
+ const siteLocalRels: string[] = []
227
+ for (const url of globalStyles) {
228
+ const rel = toLocalRel(url)
229
+ if (rel !== undefined) {
230
+ siteLocalUrls.push(url)
231
+ siteLocalRels.push(rel)
232
+ }
233
+ }
234
+ const siteLocalSet = new Set(siteLocalUrls)
235
+ const siteBundleUrl = siteLocalRels.length >= 2
236
+ ? emitBundle('site', siteLocalRels)
237
+ : siteLocalUrls[0]
238
+
239
+ if (siteBundleUrl !== undefined && siteLocalUrls.length > 0) {
240
+ const nextGlobal: string[] = []
241
+ let inserted = false
242
+ for (const url of globalStyles) {
243
+ if (siteLocalSet.has(url)) {
244
+ if (!inserted) {
245
+ nextGlobal.push(siteBundleUrl)
246
+ inserted = true
247
+ }
248
+ }
249
+ else {
250
+ nextGlobal.push(url)
251
+ }
252
+ }
253
+ globalStyles.splice(0, globalStyles.length, ...nextGlobal)
254
+ }
255
+
256
+ // 2. Rewrite each route & status page to at most [siteBundle, routeBundle] (plus any external URLs in order).
257
+ const rewriteList = (list: string[]): string[] => {
258
+ if (list.length === 0)
259
+ return list
260
+ const hasAllSite = siteBundleUrl !== undefined
261
+ && siteLocalUrls.length > 0
262
+ && siteLocalUrls.every(u => list.includes(u))
263
+
264
+ // Separate site-wide local sheets from route-specific local sheets while preserving external URLs.
265
+ const routeLocalUrls: string[] = []
266
+ const routeLocalRels: string[] = []
267
+ for (const url of list) {
268
+ if (hasAllSite && siteLocalSet.has(url))
269
+ continue
270
+ const rel = toLocalRel(url)
271
+ if (rel !== undefined) {
272
+ routeLocalUrls.push(url)
273
+ routeLocalRels.push(rel)
274
+ }
275
+ }
276
+
277
+ const routeLocalSet = new Set(routeLocalUrls)
278
+ const routeBundleUrl = routeLocalRels.length >= 2
279
+ ? emitBundle('route', routeLocalRels)
280
+ : routeLocalUrls[0]
281
+
282
+ const out: string[] = []
283
+ let siteInserted = false
284
+ let routeInserted = false
285
+ for (const url of list) {
286
+ if (hasAllSite && siteLocalSet.has(url)) {
287
+ if (!siteInserted) {
288
+ out.push(siteBundleUrl!)
289
+ siteInserted = true
290
+ }
291
+ continue
292
+ }
293
+ if (routeLocalSet.has(url)) {
294
+ if (!routeInserted && routeBundleUrl !== undefined) {
295
+ out.push(routeBundleUrl)
296
+ routeInserted = true
297
+ }
298
+ continue
299
+ }
300
+ out.push(url)
301
+ }
302
+ return out
303
+ }
304
+
305
+ for (const route of routes) {
306
+ if (route.styles !== undefined)
307
+ route.styles = rewriteList(route.styles)
308
+ }
309
+ for (const sp of statusPages)
310
+ sp.styles = rewriteList(sp.styles)
311
+
312
+ // 3. Collect live referenced stylesheets and prune superseded intermediate CSS files.
313
+ const referencedUrls = new Set<string>(globalStyles)
314
+ for (const route of routes) {
315
+ for (const u of route.styles ?? [])
316
+ referencedUrls.add(u)
317
+ }
318
+ for (const sp of statusPages) {
319
+ for (const u of sp.styles)
320
+ referencedUrls.add(u)
321
+ }
322
+ const referencedRels = new Set<string>()
323
+ for (const u of referencedUrls) {
324
+ if (u.startsWith(prefix))
325
+ referencedRels.add(u.slice(prefix.length))
326
+ }
327
+
328
+ for (const rel of originalLocalCss) {
329
+ if (referencedRels.has(rel))
330
+ continue
331
+ rmSync(join(clientDir, rel), { force: true })
332
+ const idx = assets.indexOf(rel)
333
+ if (idx !== -1)
334
+ assets.splice(idx, 1)
335
+ }
336
+
337
+ styleUrls.splice(0, styleUrls.length, ...referencedUrls)
338
+ }