镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit f27abc5

Browse files
committed
perf: actually minify preflight and keyframes in minified output
Minified builds only whitespace-collapsed the @layer cw-base preflight and @Keyframes blocks — the license comment and per-declaration spacing survived, inflating every minified stylesheet. Both now run through a proper block minifier (comments stripped, syntax-character whitespace collapsed); the preflight alone shrinks by about a third.
1 parent 807b74d commit f27abc5

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

‎packages/crosswind/src/generator.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,23 @@ const NOT_VARIANT_SELECTORS: Record<string, string> = {
14031403
}
14041404

14051405
// Pre-computed prefix variants (these modify the selector prefix, not suffix)
1406+
// Minify a static CSS block: strip comments, then collapse whitespace
1407+
// around syntax characters. Used for preflight and keyframes, which were
1408+
// previously only whitespace-collapsed (comments and per-declaration
1409+
// spacing survived into "minified" output).
1410+
function minifyCSSBlock(css: string): string {
1411+
return css
1412+
.replace(/\/\*[\s\S]*?\*\//g, '')
1413+
.replace(/\s+/g, ' ')
1414+
.replace(/\s*\{\s*/g, '{')
1415+
.replace(/\s*\}\s*/g, '}')
1416+
.replace(/;\s*/g, ';')
1417+
.replace(/:\s+/g, ':')
1418+
.replace(/,\s+/g, ',')
1419+
.replace(/;\}/g, '}')
1420+
.trim()
1421+
}
1422+
14061423
// Separator between stacked at-rule wrappers in a rule-group key
14071424
// (@media + @supports + @container). Never appears in CSS text.
14081425
const AT_RULE_SEPARATOR = '\u0001'
@@ -2702,7 +2719,7 @@ export class CSSGenerator {
27022719
// placeholders sitting flush against the border). Utilities stay
27032720
// unlayered, so they still win over author styles as before.
27042721
const layered = `@layer cw-base {\n${preflightCSS}\n}`
2705-
parts.push(minify ? layered.replace(/\s+/g, ' ').trim() : layered)
2722+
parts.push(minify ? minifyCSSBlock(layered) : layered)
27062723
}
27072724
}
27082725

@@ -2725,7 +2742,7 @@ export class CSSGenerator {
27252742
for (const name of this.usedKeyframes) {
27262743
const kf = KEYFRAMES[name]
27272744
if (kf) {
2728-
parts.push(minify ? kf.replace(/\s+/g, ' ').replace(/\s*\{\s*/g, '{').replace(/\s*\}\s*/g, '}').replace(/;\s*/g, ';').trim() : kf)
2745+
parts.push(minify ? minifyCSSBlock(kf) : kf)
27292746
}
27302747
}
27312748
}

0 commit comments

Comments
 (0)