Replaces csso. Compact a CSS string, remove duplicate declarations,
shorten colors, and compute selector specificity.
import { minify, minifyBlock, specificity, syntax } from '@stacksjs/ts-css'
// or:
import { /* … */ } from '@stacksjs/ts-css/optimize'Parse, compress, and re-stringify a full stylesheet.
minify(source: string, options?: MinifyOptions): { css: string, ast: CssNode }minify('.a { margin: 0px 0.5em 10.000px; color: #aabbcc }').css
// → ".a{margin:0 .5em 10px;color:#abc}"Same, but parse source as a declaration list (the body of a style="…"
attribute).
minifyBlock('color: red; color: blue').css
// → "color:blue"| Option | Default | Description |
|---|---|---|
comments |
'exclamation' |
false to strip all, true / 'exclamation' to keep /*!*/ comments, 'first-exclamation' to keep just the first. |
restructure |
true |
(Reserved — currently a no-op; declaration-level minification only.) |
forceMediaMerge |
false |
(Reserved.) |
usage |
null |
Usage hints ({tags, ids, classes, force}) — accepted for API parity. |
Inside any Value, ts-css compresses:
| Input | Output |
|---|---|
0.5em |
.5em |
10.000px |
10px |
0px / 0em / … |
0 |
#aabbcc |
#abc |
#aabbccdd |
#abcd |
rgb(255,0,0) |
#f00 |
url("/x.png") |
url(/x.png) (when safe) |
In each Block, duplicate declarations are folded with last wins
semantics (respecting !important):
.a { color: red; color: blue } → .a { color: blue }
.a { color: red !important;
color: blue } → .a { color: red !important }Comments matching options.comments rule are stripped.
Compute the specificity of a Selector AST.
import { parse, syntax } from '@stacksjs/ts-css'
const sel = parse('#a.b div', { context: 'selector' })
syntax.specificity(sel) // [1, 1, 1]Returns a [a, b, c] tuple per
selectors level 4:
a— id selectorsb— class / attribute / pseudo-class selectorsc— type / pseudo-element selectors
:is(), :not(), :has(), :matches(), :-moz-any(), :-webkit-any()
take the maximum specificity of their arguments.
:where() always contributes [0,0,0].
The syntax.specificity re-export mirrors csso.syntax.specificity so
existing call sites work without changes.
If you want finer control, all the building blocks are exported:
import { compressTree, dedupeDeclarations, removeComments } from '@stacksjs/ts-css/optimize'
import { generate, parse } from '@stacksjs/ts-css/parse'
const ast = parse(input)
removeComments(ast, { exclamation: true })
compressTree(ast)
dedupeDeclarations(ast)
const out = generate(ast)