Skip to content

Commit a4ca02e

Browse files
committed
Rename undocumented opts.used to opts.prune
1 parent 58037e4 commit a4ca02e

4 files changed

Lines changed: 29 additions & 18 deletions

File tree

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,22 @@ xhr.send();
5353
// Minify CSS
5454
import { CSS } from "@litejs/dom/css.js";
5555
const sheet = new CSSStyleSheet()
56-
sheet.replaceSync(".a { color: hsl(0 0% 100%) }")
57-
console.log(CSS.minify(sheet, { color: true }))
58-
// .a{color:#fff}
56+
sheet.replaceSync(":root{--gap:10px} .a { color: hsl(0 0% 100%); top: calc(var(--gap) + 5px) }")
57+
console.log(CSS.minify(sheet, { var: true, calc: true, color: true }))
58+
// .a{color:#fff;top:15px}
5959
```
6060

61+
### CSS.minify options
62+
63+
- **calc** – resolve static `calc()` expressions, e.g. `calc(10px + 5px)` → `15px`
64+
- **color** – shorten `rgb()`/`hsl()` to hex, e.g. `rgb(255,0,0)` → `#f00`
65+
- **import** – inline `@import` rules, rewriting relative URLs
66+
- **prefix** – add vendor prefixes, e.g. `{ "mask-image": ["-webkit-"] }`
67+
- **prune** – remove unused selectors `{ keep: Set, keepRe: RegExp }`
68+
- **root** – base directory for resolving `@import` paths
69+
- **url** – URL rewrite callback `(path) => newPath`
70+
- **var** – inline CSS custom properties from `:root`
71+
6172
## Contributing
6273

6374
Follow [Coding Style Guide](https://github.com/litejs/litejs/wiki/Style-Guide),

css.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ var URL = global.URL || require("url").URL
4040
return CSS.minify(imported, opts)
4141
}
4242

43-
if (opts && opts.used && sel) {
44-
sel = filterSelectors(sel, opts.used)
43+
if (opts && opts.prune && sel) {
44+
sel = filterSelectors(sel, opts.prune)
4545
if (!sel) return ""
4646
}
4747

@@ -66,11 +66,11 @@ var URL = global.URL || require("url").URL
6666
}
6767
}
6868
, filterSelectors = (text, used) => {
69-
var classes = used.classes
70-
, keep = used.keep
69+
var keep = used.keep
70+
, keepRe = used.keepRe
7171
return require("./selector.js").selectorSplit(text).filter(sel => {
7272
for (var m, classRe = /\.([-\w]+)/g; (m = classRe.exec(sel)); ) {
73-
if (!(classes && classes.has(m[1]) || keep && keep.test(m[1]))) return false
73+
if (!(keep && keep.has(m[1]) || keepRe && keepRe.test(m[1]))) return false
7474
}
7575
return true
7676
}).join(",")

test/css.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe("css.js {0}", describe.env === "browser" ?
109109
var DOMParser = DOM.DOMParser
110110
, doc = new DOMParser().parseFromString("<html><head><style>.unused{color:red}</style></head><body></body></html>")
111111
assert
112-
.equal(doc.toString({ css: { used: { classes: new Set(["btn"]) } } }), "<html><head></head><body></body></html>")
112+
.equal(doc.toString({ css: { prune: { keep: new Set(["btn"]) } } }), "<html><head></head><body></body></html>")
113113
.end()
114114
})
115115

@@ -194,26 +194,26 @@ describe("css.js {0}", describe.env === "browser" ?
194194
assert.equal(minify(sheet, opts), expected).end()
195195
})
196196

197-
test("used {i} '{1}'", [
198-
[ { used: { classes: new Set(["btn"]) } },
197+
test("prune {i} '{1}'", [
198+
[ { prune: { keep: new Set(["btn"]) } },
199199
".btn{color:red}.nav{top:1}",
200200
".btn{color:red}" ],
201-
[ { used: { classes: new Set(["btn"]) } },
201+
[ { prune: { keep: new Set(["btn"]) } },
202202
".btn,.nav{color:red}",
203203
".btn{color:red}" ],
204-
[ { used: { classes: new Set(["btn", "nav"]) } },
204+
[ { prune: { keep: new Set(["btn", "nav"]) } },
205205
".btn .nav{color:red}.unused{top:1}",
206206
".btn .nav{color:red}" ],
207-
[ { used: { classes: new Set(["btn"]) } },
207+
[ { prune: { keep: new Set(["btn"]) } },
208208
".btn .nav{color:red}",
209209
"" ],
210-
[ { used: { keep: /^is-/ } },
210+
[ { prune: { keepRe: /^is-/ } },
211211
".is-active{color:red}.btn{top:1}",
212212
".is-active{color:red}" ],
213-
[ { used: { classes: new Set(["btn"]), keep: /^icon-/ } },
213+
[ { prune: { keep: new Set(["btn"]), keepRe: /^icon-/ } },
214214
".btn{color:red}.icon-star{top:1}.unused{left:0}",
215215
".btn{color:red}\n.icon-star{top:1}" ],
216-
[ { used: { classes: new Set(["btn"]) } },
216+
[ { prune: { keep: new Set(["btn"]) } },
217217
"body{margin:0}.btn{color:red}",
218218
"body{margin:0}\n.btn{color:red}" ],
219219
], (opts, text, expected, assert) => {

test/readme.out.ok

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<h1 id="123" class="large"><b>hello world</b></h1>
22
<h1 id=123 class=large><b>hello world</b></h1>
3-
.a{color:#fff}
3+
.a{color:#fff;top:15px}
44
LiteJS

0 commit comments

Comments
 (0)