prunelle 0.1.1 → 0.5.2
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/llms.txt +23 -19
- package/package.json +8 -2
- package/src/a11y/declaration.ts +335 -0
- package/src/a11y/index.ts +446 -0
- package/src/a11y/rgaa-map.ts +184 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +80 -36
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +8 -4
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +11 -9
- package/src/build/ecoindex.ts +229 -0
- package/src/build/index.ts +174 -108
- package/src/build/islands.ts +2 -2
- package/src/build/manifest.ts +66 -5
- package/src/build/output.ts +2 -2
- package/src/build/plugin-host.ts +91 -33
- package/src/build/precompress.ts +15 -2
- package/src/build/prerender.ts +128 -46
- package/src/build/report.ts +23 -2
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static.ts +137 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +654 -0
- package/src/cli/check-links.ts +619 -0
- package/src/cli/check.ts +52 -5
- package/src/cli/commands.ts +86 -4
- package/src/cli/docs.ts +402 -0
- package/src/cli/inspect.ts +31 -2
- package/src/cli/mcp-tools.ts +480 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +2 -2
- package/src/cli/render.ts +261 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +137 -16
- package/src/compiler/analyze.ts +20 -6
- package/src/compiler/cacheability.ts +592 -0
- package/src/compiler/compliance-rgaa.ts +502 -0
- package/src/compiler/compliance.ts +120 -221
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +12 -11
- package/src/compiler/expr-context.ts +167 -57
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +56 -17
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +216 -84
- package/src/dev/error-log.ts +151 -0
- package/src/dev/index.ts +84 -34
- package/src/dev/open-editor.ts +204 -0
- package/src/dev/protocol.ts +1 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +42 -25
- package/src/dev/watchers.ts +47 -11
- package/src/dom/action.ts +4 -1
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +2 -2
- package/src/dom/island.ts +11 -10
- package/src/plugins/config.ts +4 -4
- package/src/plugins/index.ts +2 -2
- package/src/plugins/public.ts +5 -2
- package/src/plugins/services.ts +62 -0
- package/src/plugins/types.ts +32 -0
- package/src/server/action-protect.ts +198 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +82 -27
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +91 -9
- package/src/server/errors.ts +303 -65
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +128 -31
- package/src/server/index.ts +230 -260
- package/src/server/loader.ts +5 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +123 -88
- package/src/server/public.ts +3 -3
- package/src/server/rate-limit.ts +58 -35
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +423 -0
- package/src/server/route-table.ts +339 -8
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +196 -63
- package/src/server/security.ts +208 -32
- package/src/server/serve-handlers.ts +21 -7
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +61 -8
- package/src/server/stream.ts +41 -22
- package/src/ssr/context.ts +85 -3
- package/src/ssr/index.ts +120 -1
- package/src/ssr/region.ts +31 -19
- package/src/test/browser.ts +6 -0
- package/src/test/index.ts +17 -3
- package/src/ts-plugin/diagnostics.ts +378 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +5 -0
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/index.tsx +4 -0
- package/ts-plugin.cjs +4277 -0
|
@@ -0,0 +1,526 @@
|
|
|
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 CSS_URL_RE = /url\(\s*(["']?)([^)"']+)\1\s*\)/g
|
|
10
|
+
const IMPORT_STR_RE = /^(@import\s+)(["'])([^"']+)\2/i
|
|
11
|
+
const ABS_OR_DATA_URL_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#)/i
|
|
12
|
+
const BUN_CSS_HASH_RE = /-[a-z0-9]{8}\.css$/i
|
|
13
|
+
|
|
14
|
+
function isTokenChar(ch: string): boolean {
|
|
15
|
+
const code = ch.charCodeAt(0)
|
|
16
|
+
return (code >= 48 && code <= 57) // 0-9
|
|
17
|
+
|| (code >= 65 && code <= 90) // A-Z
|
|
18
|
+
|| (code >= 97 && code <= 122) // a-z
|
|
19
|
+
|| ch === '_' || ch === '-' || ch === '%' || ch === '#' || code >= 128
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function needsCommentSpace(prev: string, nextChar: string): boolean {
|
|
23
|
+
const p = prev[prev.length - 1]!
|
|
24
|
+
if (isTokenChar(p) && isTokenChar(nextChar))
|
|
25
|
+
return true
|
|
26
|
+
if ((p === '+' || nextChar === '+') && !'{};:,('.includes(p) && !'{};:,)'.includes(nextChar))
|
|
27
|
+
return true
|
|
28
|
+
if (isTokenChar(p) && nextChar === '(')
|
|
29
|
+
return true
|
|
30
|
+
if (p === ')' && isTokenChar(nextChar))
|
|
31
|
+
return true
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** True when `out[endIdx]` is the `:` of a CSS custom property declaration (`--foo:`). */
|
|
36
|
+
function isCustomPropColon(out: readonly string[], endIdx: number): boolean {
|
|
37
|
+
if (endIdx < 2 || out[endIdx] !== ':')
|
|
38
|
+
return false
|
|
39
|
+
let j = endIdx - 1
|
|
40
|
+
while (j >= 0) {
|
|
41
|
+
const t = out[j]!
|
|
42
|
+
if (t === '{' || t === ';' || t === '}' || t === ' ')
|
|
43
|
+
break
|
|
44
|
+
j--
|
|
45
|
+
}
|
|
46
|
+
return out[j + 1] === '-' && out[j + 2] === '-'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function readQuotedString(css: string, start: number): number {
|
|
50
|
+
const quote = css[start]!
|
|
51
|
+
const n = css.length
|
|
52
|
+
let i = start + 1
|
|
53
|
+
while (i < n) {
|
|
54
|
+
if (css[i] === '\\') {
|
|
55
|
+
i += 2
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
if (css[i] === quote)
|
|
59
|
+
return i + 1
|
|
60
|
+
i++
|
|
61
|
+
}
|
|
62
|
+
return i
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function readUnquotedUrl(css: string, start: number): { token: string, next: number } {
|
|
66
|
+
const n = css.length
|
|
67
|
+
let i = start + 4
|
|
68
|
+
while (i < n && /\s/.test(css[i]!))
|
|
69
|
+
i++
|
|
70
|
+
if (css[i] === '"' || css[i] === '\'')
|
|
71
|
+
return { token: 'url(', next: i }
|
|
72
|
+
while (i < n && css[i] !== ')')
|
|
73
|
+
i++
|
|
74
|
+
if (i < n)
|
|
75
|
+
i++
|
|
76
|
+
return { token: css.slice(start, i), next: i }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function maybePushSeparator(out: string[], ch: string, pendingSpace: boolean, pendingComment: boolean, forUrl = false): void {
|
|
80
|
+
if (out.length === 0)
|
|
81
|
+
return
|
|
82
|
+
const prev = out[out.length - 1]!
|
|
83
|
+
if (pendingSpace) {
|
|
84
|
+
if (!forUrl && (ch === ';' || ch === '}') && isCustomPropColon(out, out.length - 1))
|
|
85
|
+
out.push(' ')
|
|
86
|
+
else if (forUrl ? needsSpaceBefore(prev, ch) : needsSpaceBetween(prev, ch))
|
|
87
|
+
out.push(' ')
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
if (pendingComment && needsCommentSpace(prev, ch))
|
|
91
|
+
out.push(' ')
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Safe lossless CSS minifier: strips comments and redundant whitespace while preserving strings, calc() operators, custom property space toggles, and data URIs. */
|
|
95
|
+
export function minifyCss(css: string): string {
|
|
96
|
+
const out: string[] = []
|
|
97
|
+
let i = 0
|
|
98
|
+
const n = css.length
|
|
99
|
+
let pendingSpace = false
|
|
100
|
+
let pendingComment = false
|
|
101
|
+
|
|
102
|
+
while (i < n) {
|
|
103
|
+
const ch = css[i]!
|
|
104
|
+
if (ch === '"' || ch === '\'') {
|
|
105
|
+
maybePushSeparator(out, ch, pendingSpace, pendingComment)
|
|
106
|
+
pendingSpace = false
|
|
107
|
+
pendingComment = false
|
|
108
|
+
const next = readQuotedString(css, i)
|
|
109
|
+
out.push(css.slice(i, next))
|
|
110
|
+
i = next
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
if (ch === '/' && css[i + 1] === '*') {
|
|
114
|
+
const end = css.indexOf('*/', i + 2)
|
|
115
|
+
i = end === -1 ? n : end + 2
|
|
116
|
+
pendingComment = true
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
if ((ch === 'u' || ch === 'U') && css.slice(i, i + 4).toLowerCase() === 'url(') {
|
|
120
|
+
maybePushSeparator(out, 'u', pendingSpace, pendingComment, true)
|
|
121
|
+
pendingSpace = false
|
|
122
|
+
pendingComment = false
|
|
123
|
+
const parsed = readUnquotedUrl(css, i)
|
|
124
|
+
out.push(parsed.token)
|
|
125
|
+
i = parsed.next
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r' || ch === '\f') {
|
|
129
|
+
pendingSpace = true
|
|
130
|
+
i++
|
|
131
|
+
continue
|
|
132
|
+
}
|
|
133
|
+
maybePushSeparator(out, ch, pendingSpace, pendingComment)
|
|
134
|
+
pendingSpace = false
|
|
135
|
+
pendingComment = false
|
|
136
|
+
// Drop trailing semicolon right before `}`, except for custom property space toggles (`--x: ;}`).
|
|
137
|
+
if (ch === '}' && out.length > 0 && out[out.length - 1] === ';') {
|
|
138
|
+
const isSpaceToggle = out.length >= 4 && out[out.length - 2] === ' ' && isCustomPropColon(out, out.length - 3)
|
|
139
|
+
if (!isSpaceToggle)
|
|
140
|
+
out.pop()
|
|
141
|
+
}
|
|
142
|
+
out.push(ch)
|
|
143
|
+
i++
|
|
144
|
+
}
|
|
145
|
+
return out.join('').trim()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function needsSpaceBefore(prev: string, nextChar: string): boolean {
|
|
149
|
+
const p = prev[prev.length - 1]!
|
|
150
|
+
return !'{};:,>~+('.includes(p) && !'{}'.includes(nextChar)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function needsSpaceBetween(prev: string, nextChar: string): boolean {
|
|
154
|
+
const p = prev[prev.length - 1]!
|
|
155
|
+
// Preserve spaces around `+` and `-` (required inside CSS calc()/min()/max()/clamp()).
|
|
156
|
+
if (p === '+' || nextChar === '+' || p === '-' || nextChar === '-') {
|
|
157
|
+
if (p === ';' || p === '{' || p === '}' || p === ':' || p === ',' || p === '(')
|
|
158
|
+
return false
|
|
159
|
+
return true
|
|
160
|
+
}
|
|
161
|
+
if ('{};,>~'.includes(p) || '{};,>~)'.includes(nextChar))
|
|
162
|
+
return false
|
|
163
|
+
if (p === ':' && nextChar !== ':')
|
|
164
|
+
return false
|
|
165
|
+
if (p === '(')
|
|
166
|
+
return false
|
|
167
|
+
return true
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function rebaseRelativeUrls(css: string, relFile: string): string {
|
|
171
|
+
const dir = posix.dirname(relFile)
|
|
172
|
+
if (dir === '.' || dir === '')
|
|
173
|
+
return css
|
|
174
|
+
return css.replace(CSS_URL_RE, (full, quote: string, target: string) => {
|
|
175
|
+
const trimmed = target.trim()
|
|
176
|
+
if (trimmed === '' || ABS_OR_DATA_URL_RE.test(trimmed))
|
|
177
|
+
return full
|
|
178
|
+
const rebased = posix.normalize(posix.join(dir, trimmed))
|
|
179
|
+
return `url(${quote}${rebased}${quote})`
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function rebaseImportStatement(stmt: string, relFile: string): string {
|
|
184
|
+
const rebasedUrls = rebaseRelativeUrls(stmt, relFile)
|
|
185
|
+
const dir = posix.dirname(relFile)
|
|
186
|
+
if (dir === '.' || dir === '')
|
|
187
|
+
return rebasedUrls
|
|
188
|
+
return rebasedUrls.replace(IMPORT_STR_RE, (full, head: string, quote: string, target: string) => {
|
|
189
|
+
const trimmed = target.trim()
|
|
190
|
+
if (trimmed === '' || ABS_OR_DATA_URL_RE.test(trimmed))
|
|
191
|
+
return full
|
|
192
|
+
return `${head}${quote}${posix.normalize(posix.join(dir, trimmed))}${quote}`
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function skipWsAndComments(css: string, start: number): number {
|
|
197
|
+
let i = start
|
|
198
|
+
const n = css.length
|
|
199
|
+
while (i < n) {
|
|
200
|
+
const ch = css[i]!
|
|
201
|
+
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r' || ch === '\f') {
|
|
202
|
+
i++
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
if (ch === '/' && css[i + 1] === '*') {
|
|
206
|
+
const end = css.indexOf('*/', i + 2)
|
|
207
|
+
i = end === -1 ? n : end + 2
|
|
208
|
+
continue
|
|
209
|
+
}
|
|
210
|
+
break
|
|
211
|
+
}
|
|
212
|
+
return i
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Finds the `;` terminating a prologue at-rule (`@charset`, `@import`, `@layer` statement), or -1 if `{` appears first or unclosed. */
|
|
216
|
+
function findStatementEnd(css: string, start: number): number {
|
|
217
|
+
let i = start
|
|
218
|
+
const n = css.length
|
|
219
|
+
let depth = 0
|
|
220
|
+
while (i < n) {
|
|
221
|
+
const ch = css[i]!
|
|
222
|
+
if (ch === '"' || ch === '\'') {
|
|
223
|
+
const quote = ch
|
|
224
|
+
i++
|
|
225
|
+
while (i < n) {
|
|
226
|
+
if (css[i] === '\\') {
|
|
227
|
+
i += 2
|
|
228
|
+
continue
|
|
229
|
+
}
|
|
230
|
+
if (css[i] === quote) {
|
|
231
|
+
i++
|
|
232
|
+
break
|
|
233
|
+
}
|
|
234
|
+
i++
|
|
235
|
+
}
|
|
236
|
+
continue
|
|
237
|
+
}
|
|
238
|
+
if (ch === '/' && css[i + 1] === '*') {
|
|
239
|
+
const end = css.indexOf('*/', i + 2)
|
|
240
|
+
i = end === -1 ? n : end + 2
|
|
241
|
+
continue
|
|
242
|
+
}
|
|
243
|
+
if (ch === '(')
|
|
244
|
+
depth++
|
|
245
|
+
else if (ch === ')' && depth > 0)
|
|
246
|
+
depth--
|
|
247
|
+
else if (depth === 0 && ch === '{')
|
|
248
|
+
return -1
|
|
249
|
+
else if (depth === 0 && ch === ';')
|
|
250
|
+
return i
|
|
251
|
+
i++
|
|
252
|
+
}
|
|
253
|
+
return -1
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function splitCssPrologue(css: string): { hasCharset: boolean, prologue: string[], body: string } {
|
|
257
|
+
let hasCharset = false
|
|
258
|
+
const prologue: string[] = []
|
|
259
|
+
let i = 0
|
|
260
|
+
const n = css.length
|
|
261
|
+
|
|
262
|
+
while (i < n) {
|
|
263
|
+
const at = skipWsAndComments(css, i)
|
|
264
|
+
if (at >= n || css[at] !== '@')
|
|
265
|
+
return { hasCharset, prologue, body: css.slice(at) }
|
|
266
|
+
const head = css.slice(at, at + 10).toLowerCase()
|
|
267
|
+
const isCharset = head.startsWith('@charset') && /[\s"']/.test(css[at + 8] ?? '')
|
|
268
|
+
const isImport = head.startsWith('@import') && /[\s"'u]/i.test(css[at + 7] ?? '')
|
|
269
|
+
const isLayer = head.startsWith('@layer') && /[\s/;]/.test(css[at + 6] ?? '')
|
|
270
|
+
if (!isCharset && !isImport && !isLayer)
|
|
271
|
+
return { hasCharset, prologue, body: css.slice(at) }
|
|
272
|
+
const end = findStatementEnd(css, at)
|
|
273
|
+
if (end === -1)
|
|
274
|
+
return { hasCharset, prologue, body: css.slice(at) }
|
|
275
|
+
const stmt = css.slice(at, end + 1).trim()
|
|
276
|
+
if (isCharset)
|
|
277
|
+
hasCharset = true
|
|
278
|
+
else
|
|
279
|
+
prologue.push(stmt)
|
|
280
|
+
i = end + 1
|
|
281
|
+
}
|
|
282
|
+
return { hasCharset, prologue, body: '' }
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function combineCssFiles(clientDir: string, relFiles: readonly string[], minify: boolean): string {
|
|
286
|
+
let hasCharset = false
|
|
287
|
+
const prologue: string[] = []
|
|
288
|
+
const bodies: string[] = []
|
|
289
|
+
|
|
290
|
+
for (const rel of relFiles) {
|
|
291
|
+
const raw = readFileSync(join(clientDir, rel), 'utf8')
|
|
292
|
+
const split = splitCssPrologue(raw)
|
|
293
|
+
if (split.hasCharset)
|
|
294
|
+
hasCharset = true
|
|
295
|
+
for (const stmt of split.prologue)
|
|
296
|
+
prologue.push(stmt.slice(0, 7).toLowerCase() === '@import' ? rebaseImportStatement(stmt, rel) : stmt)
|
|
297
|
+
const body = rebaseRelativeUrls(split.body.trim(), rel)
|
|
298
|
+
if (body !== '')
|
|
299
|
+
bodies.push(body)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const parts: string[] = []
|
|
303
|
+
if (hasCharset)
|
|
304
|
+
parts.push('@charset "UTF-8";')
|
|
305
|
+
if (prologue.length > 0)
|
|
306
|
+
parts.push(prologue.join('\n'))
|
|
307
|
+
if (bodies.length > 0)
|
|
308
|
+
parts.push(bodies.join('\n'))
|
|
309
|
+
const joined = parts.join('\n')
|
|
310
|
+
return minify ? minifyCss(joined) : joined
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function shortHash(text: string): string {
|
|
314
|
+
return createHash('sha256').update(text).digest('hex').slice(0, 8)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export interface ConsolidateStylesheetsOptions {
|
|
318
|
+
clientDir: string
|
|
319
|
+
assetPrefix: string
|
|
320
|
+
minify: boolean
|
|
321
|
+
isDev?: boolean | undefined
|
|
322
|
+
srcCssToUrl?: ReadonlyMap<string, string> | undefined
|
|
323
|
+
capturePruned?: boolean | undefined
|
|
324
|
+
routes: RouteManifestEntry[]
|
|
325
|
+
statusPages: StatusPageManifestEntry[]
|
|
326
|
+
globalStyles: string[]
|
|
327
|
+
assets: string[]
|
|
328
|
+
styleUrls: string[]
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export interface ConsolidateStylesheetsResult {
|
|
332
|
+
/** Consolidated bundle filenames (`site-*.css`, `route-*.css`) emitted by this pass. */
|
|
333
|
+
emitted: Set<string>
|
|
334
|
+
/** Raw bytes of pruned intermediate CSS files (populated when `capturePruned` is true). */
|
|
335
|
+
pruned: Map<string, Uint8Array>
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
interface SiteStyleRun {
|
|
339
|
+
urls: string[]
|
|
340
|
+
bundleUrl: string
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function collectOriginalLocalCss(
|
|
344
|
+
globalStyles: readonly string[],
|
|
345
|
+
styleUrls: readonly string[],
|
|
346
|
+
routes: readonly RouteManifestEntry[],
|
|
347
|
+
statusPages: readonly StatusPageManifestEntry[],
|
|
348
|
+
toLocalRel: (url: string) => string | undefined,
|
|
349
|
+
): Set<string> {
|
|
350
|
+
const out = new Set<string>()
|
|
351
|
+
const addAll = (urls: readonly string[] | undefined): void => {
|
|
352
|
+
for (const url of urls ?? []) {
|
|
353
|
+
const rel = toLocalRel(url)
|
|
354
|
+
if (rel !== undefined)
|
|
355
|
+
out.add(rel)
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
addAll(globalStyles)
|
|
359
|
+
addAll(styleUrls)
|
|
360
|
+
for (const r of routes)
|
|
361
|
+
addAll(r.styles)
|
|
362
|
+
for (const sp of statusPages)
|
|
363
|
+
addAll(sp.styles)
|
|
364
|
+
return out
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function pruneSupersededCss(
|
|
368
|
+
originalLocalCss: ReadonlySet<string>,
|
|
369
|
+
globalStyles: readonly string[],
|
|
370
|
+
routes: readonly RouteManifestEntry[],
|
|
371
|
+
statusPages: readonly StatusPageManifestEntry[],
|
|
372
|
+
prefix: string,
|
|
373
|
+
clientDir: string,
|
|
374
|
+
assets: string[],
|
|
375
|
+
styleUrls: string[],
|
|
376
|
+
capturePruned: boolean,
|
|
377
|
+
): Map<string, Uint8Array> {
|
|
378
|
+
const referencedUrls = new Set<string>(globalStyles)
|
|
379
|
+
for (const route of routes) {
|
|
380
|
+
for (const u of route.styles ?? [])
|
|
381
|
+
referencedUrls.add(u)
|
|
382
|
+
}
|
|
383
|
+
for (const sp of statusPages) {
|
|
384
|
+
for (const u of sp.styles)
|
|
385
|
+
referencedUrls.add(u)
|
|
386
|
+
}
|
|
387
|
+
const referencedRels = new Set<string>()
|
|
388
|
+
for (const u of referencedUrls) {
|
|
389
|
+
if (u.startsWith(prefix))
|
|
390
|
+
referencedRels.add(u.slice(prefix.length))
|
|
391
|
+
}
|
|
392
|
+
const pruned = new Map<string, Uint8Array>()
|
|
393
|
+
for (const rel of originalLocalCss) {
|
|
394
|
+
if (referencedRels.has(rel))
|
|
395
|
+
continue
|
|
396
|
+
const abs = join(clientDir, rel)
|
|
397
|
+
if (capturePruned && existsSync(abs))
|
|
398
|
+
pruned.set(rel, readFileSync(abs))
|
|
399
|
+
rmSync(abs, { force: true })
|
|
400
|
+
const idx = assets.indexOf(rel)
|
|
401
|
+
if (idx !== -1)
|
|
402
|
+
assets.splice(idx, 1)
|
|
403
|
+
}
|
|
404
|
+
styleUrls.splice(0, styleUrls.length, ...referencedUrls)
|
|
405
|
+
return pruned
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Merges contiguous runs of local stylesheets so each page links at most one site-wide sheet
|
|
410
|
+
* (cached across navigations) plus per-route sheets without reordering across external stylesheets.
|
|
411
|
+
*/
|
|
412
|
+
export function consolidateStylesheets(options: ConsolidateStylesheetsOptions): ConsolidateStylesheetsResult {
|
|
413
|
+
const { clientDir, assetPrefix, minify, isDev = false, srcCssToUrl, capturePruned = false, routes, statusPages, globalStyles, assets, styleUrls } = options
|
|
414
|
+
const prefix = `${assetPrefix.replace(/\/$/, '')}/`
|
|
415
|
+
const urlToSrcCss = new Map<string, string>()
|
|
416
|
+
for (const [src, url] of srcCssToUrl ?? [])
|
|
417
|
+
urlToSrcCss.set(url, src)
|
|
418
|
+
|
|
419
|
+
const toLocalRel = (url: string): string | undefined => {
|
|
420
|
+
if (!url.startsWith(prefix) || !url.endsWith('.css'))
|
|
421
|
+
return undefined
|
|
422
|
+
const rel = url.slice(prefix.length)
|
|
423
|
+
return existsSync(join(clientDir, rel)) ? rel : undefined
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const emitted = new Set<string>()
|
|
427
|
+
const emittedBundles = new Map<string, string>()
|
|
428
|
+
const emitBundle = (kind: 'site' | 'route', relFiles: readonly string[], sourceUrls: readonly string[]): string => {
|
|
429
|
+
const key = `${kind}:${relFiles.join('\0')}`
|
|
430
|
+
const existing = emittedBundles.get(key)
|
|
431
|
+
if (existing !== undefined)
|
|
432
|
+
return existing
|
|
433
|
+
const css = combineCssFiles(clientDir, relFiles, minify)
|
|
434
|
+
const relOut = kind === 'route' && isDev
|
|
435
|
+
? `route-${shortHash(sourceUrls.map(u => urlToSrcCss.get(u) ?? u.replace(BUN_CSS_HASH_RE, '')).join('\0')).slice(0, 6)}-${shortHash(css)}.css`
|
|
436
|
+
: `${kind}-${shortHash(css)}.css`
|
|
437
|
+
writeFileSync(join(clientDir, relOut), css)
|
|
438
|
+
emitted.add(relOut)
|
|
439
|
+
if (!assets.includes(relOut))
|
|
440
|
+
assets.push(relOut)
|
|
441
|
+
const url = `${prefix}${relOut}`
|
|
442
|
+
emittedBundles.set(key, url)
|
|
443
|
+
return url
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const originalLocalCss = collectOriginalLocalCss(globalStyles, styleUrls, routes, statusPages, toLocalRel)
|
|
447
|
+
|
|
448
|
+
// 1. Consolidate contiguous local runs in globalStyles without reordering across external stylesheets.
|
|
449
|
+
const siteRuns: SiteStyleRun[] = []
|
|
450
|
+
const nextGlobal: string[] = []
|
|
451
|
+
let gUrls: string[] = []
|
|
452
|
+
let gRels: string[] = []
|
|
453
|
+
const flushGlobalRun = (): void => {
|
|
454
|
+
if (gUrls.length === 0)
|
|
455
|
+
return
|
|
456
|
+
const bundleUrl = gRels.length >= 2 ? emitBundle('site', gRels, gUrls) : gUrls[0]!
|
|
457
|
+
siteRuns.push({ urls: gUrls, bundleUrl })
|
|
458
|
+
nextGlobal.push(bundleUrl)
|
|
459
|
+
gUrls = []
|
|
460
|
+
gRels = []
|
|
461
|
+
}
|
|
462
|
+
for (const url of globalStyles) {
|
|
463
|
+
const rel = toLocalRel(url)
|
|
464
|
+
if (rel !== undefined) {
|
|
465
|
+
gUrls.push(url)
|
|
466
|
+
gRels.push(rel)
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
flushGlobalRun()
|
|
470
|
+
nextGlobal.push(url)
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
flushGlobalRun()
|
|
474
|
+
globalStyles.splice(0, globalStyles.length, ...nextGlobal)
|
|
475
|
+
|
|
476
|
+
const matchSiteRunAt = (list: readonly string[], idx: number): SiteStyleRun | undefined =>
|
|
477
|
+
siteRuns.find(run => idx + run.urls.length <= list.length && run.urls.every((u, k) => list[idx + k] === u))
|
|
478
|
+
|
|
479
|
+
// 2. Rewrite each route & status page, consolidating only contiguous local runs to preserve cascade order.
|
|
480
|
+
const rewriteList = (list: string[]): string[] => {
|
|
481
|
+
if (list.length === 0)
|
|
482
|
+
return list
|
|
483
|
+
const out: string[] = []
|
|
484
|
+
let rUrls: string[] = []
|
|
485
|
+
let rRels: string[] = []
|
|
486
|
+
const flushRouteRun = (): void => {
|
|
487
|
+
if (rUrls.length === 0)
|
|
488
|
+
return
|
|
489
|
+
out.push(rRels.length >= 2 ? emitBundle('route', rRels, rUrls) : rUrls[0]!)
|
|
490
|
+
rUrls = []
|
|
491
|
+
rRels = []
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
for (let i = 0; i < list.length;) {
|
|
495
|
+
const siteRun = matchSiteRunAt(list, i)
|
|
496
|
+
if (siteRun !== undefined) {
|
|
497
|
+
flushRouteRun()
|
|
498
|
+
out.push(siteRun.bundleUrl)
|
|
499
|
+
i += siteRun.urls.length
|
|
500
|
+
continue
|
|
501
|
+
}
|
|
502
|
+
const url = list[i++]!
|
|
503
|
+
const rel = toLocalRel(url)
|
|
504
|
+
if (rel !== undefined) {
|
|
505
|
+
rUrls.push(url)
|
|
506
|
+
rRels.push(rel)
|
|
507
|
+
}
|
|
508
|
+
else {
|
|
509
|
+
flushRouteRun()
|
|
510
|
+
out.push(url)
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
flushRouteRun()
|
|
514
|
+
return out
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
for (const route of routes) {
|
|
518
|
+
if (route.styles !== undefined)
|
|
519
|
+
route.styles = rewriteList(route.styles)
|
|
520
|
+
}
|
|
521
|
+
for (const sp of statusPages)
|
|
522
|
+
sp.styles = rewriteList(sp.styles)
|
|
523
|
+
|
|
524
|
+
const pruned = pruneSupersededCss(originalLocalCss, globalStyles, routes, statusPages, prefix, clientDir, assets, styleUrls, capturePruned)
|
|
525
|
+
return { emitted, pruned }
|
|
526
|
+
}
|