Skip to content

Commit f04ad40

Browse files
committed
build: Add cssPrune/cssKeep options
1 parent 270ed00 commit f04ad40

63 files changed

Lines changed: 271 additions & 274 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ var fs = require("fs")
2424
banner: "",
2525
cat: true,
2626
assets: "{h}.{ext}",
27+
cssKeep: ["is-"],
28+
cssPrune: true,
2729
fetch: true,
2830
jsmin: "",
2931
min: "",

lib/build.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//- --out Output file
1313
//- --readme Replace readme tags in file
1414
//- --ver Override version string
15+
//- --cssPrune Remove unused CSS classes (default: true)
16+
//- --cssKeep Class prefixes to always keep (default: is)
1517
//- --worker Update worker file
1618
//-
1719
//- Examples
@@ -36,7 +38,7 @@ var child = require("child_process")
3638
}
3739
, httpRe = /^https?(?=:)/
3840
, cssOpts = {
39-
color: true, import: true,
41+
calc: true, color: true, import: true,
4042
prefix: {
4143
// Need for -moz/ms/o- have dropped before 2020. Properties that still needed -webkit- around 2020:
4244
"appearance": ["-webkit-"],
@@ -47,6 +49,7 @@ var child = require("child_process")
4749
}
4850
, jsMinCmd
4951
, fileHashes
52+
, classes
5053
, linked = module.filename.indexOf(process.cwd()) !== 0
5154

5255
if (linked) {
@@ -187,6 +190,7 @@ function html(opts, next) {
187190
} catch(e) {}
188191

189192
$$("[exclude]").forEach(remove)
193+
classes = new Set()
190194

191195
following("cat", "cat-drop", function(el, siblings) {
192196
if (opts.cat === false) {
@@ -273,13 +277,25 @@ function html(opts, next) {
273277
write("", cacheFile, JSON.stringify(cache, null, 2))
274278
}
275279

280+
cssOpts.root = inDir
281+
cssOpts.url = urlFn
282+
if (opts.cssPrune !== false) {
283+
$$("[class]").forEach(function(el) {
284+
el.className.split(/\s+/).forEach(function(c) { if (c) classes.add(c) })
285+
})
286+
$$("script[type='ui'][src]").forEach(function(el) {
287+
if (!el._txt && !el.textContent.trim()) {
288+
parseView(cli.readFile(path.resolve(inDir, getSrc(el))), "ui", {}, null)
289+
}
290+
})
291+
var keep = opts.cssKeep && opts.cssKeep.length ? new RegExp("^(" + opts.cssKeep.join("|") + ")") : null
292+
cssOpts.prune = { keep: classes, keepRe: keep }
293+
}
294+
276295
$$("[src]:not([src^='data:']),[href]:not(a,base,[href^='data:'])").forEach(function(el) {
277296
if (el._txt) return
278297
cpOut(el)
279298
})
280-
281-
cssOpts.root = inDir
282-
cssOpts.url = urlFn
283299
next(doc.toString({ css: cssOpts }))
284300

285301
function assetUrl(u) {
@@ -451,9 +467,13 @@ function view2js(content) {
451467

452468
function cssMin(str, urlFn) {
453469
var sheet = new CSSStyleSheet()
470+
, prune = cssOpts.prune
454471
sheet.replaceSync(str)
455472
cssOpts.url = urlFn
456-
return CSS.minify(sheet, cssOpts)
473+
cssOpts.prune = null
474+
var out = CSS.minify(sheet, cssOpts)
475+
cssOpts.prune = prune
476+
return out
457477
}
458478

459479
function parseView(content, extTo, lastMinEl, urlFn) {
@@ -475,6 +495,10 @@ function parseView(content, extTo, lastMinEl, urlFn) {
475495
} else if (map.hasOwnProperty(line)) last = i - 1
476496
}
477497

498+
if (classes) arr.forEach(function(line) {
499+
line.replace(/\.([-\w]+)/g, function(_, c) { classes.add(c) })
500+
})
501+
478502
var out = ""
479503
if ((line = map["%css"])) {
480504
if (lastMinEl.css) {
@@ -504,10 +528,6 @@ function viewMin(str, attrs) {
504528
, templateRe = /([ \t]*)(%?)((?:("|')(?:\\\4|.)*?\4|[-\w:.#[\]]=?)*)[ \t]*([>^;@|\\\/]|!?=|)(([\])}]?).*?([[({]?))(?=\x1f|\n|$)+/g
505529
, parent = 0
506530
, stack = [-1]
507-
, jsBlock = -1
508-
, jsBlockBuf = ""
509-
, jsBlockPrefix = ""
510-
, jsBlockIndent = ""
511531
, joinBindingsRe = /(\n +)([;^][^\n]+)\1(?=[;^])/g
512532
, joinSelectorRe = /(\n +)([\w:.#[](?:[^"' \n]|("|')(?:\\\3|(?!\3).)*?\3)*)\1 (?=[;^])/g
513533

@@ -529,16 +549,7 @@ function viewMin(str, attrs) {
529549
}
530550
stack.shift()
531551
}
532-
if (jsBlock >= 0 && q <= jsBlock) {
533-
jsBlockBuf += "\n" + all.slice(indent.length)
534-
out[parent] += jsBlockIndent + jsBlockPrefix + clean(jsBlockBuf) + "\n"
535-
jsBlock = -1
536-
return
537-
}
538-
539-
if (jsBlock >= 0) {
540-
jsBlockBuf += "\n" + all.slice(indent.length)
541-
} else if (!isString(out[parent])) {
552+
if (!isString(out[parent])) {
542553
out[parent]._j += all + "\n"
543554
} else if (plugin && (name === "todo def")) {
544555
out[parent] += all
@@ -553,16 +564,8 @@ function viewMin(str, attrs) {
553564
} else {
554565
if (op === "/") return
555566
all = all.slice(indent.length)
556-
if (mapStart && (op === ";" || op === "^" || op === "@")) {
557-
jsBlock = q
558-
jsBlockIndent = " ".repeat(indent.length)
559-
name = all.lastIndexOf("function")
560-
jsBlockPrefix = name > 0 ? clean(all.slice(0, name)) + " " : clean(all.slice(0, all.lastIndexOf(mapStart)))
561-
jsBlockBuf = all.slice(name > 0 ? name : all.lastIndexOf(mapStart))
562-
} else {
563-
if (op === ";" || op === "^" || op === "@") all = clean(all)
564-
out[parent] += " ".repeat(indent.length) + all + "\n"
565-
}
567+
if (op === ";" || op === "^" || op === "@") all = clean(all)
568+
out[parent] += " ".repeat(indent.length) + all + "\n"
566569
}
567570
}
568571
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"test": "node ./cli.js t test/index.js"
2323
},
2424
"dependencies": {
25-
"@litejs/dom": "26.4.0"
25+
"@litejs/dom": "26.4.3"
2626
},
2727
"bin": {
2828
"lj": "./cli.js"

test/build.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ describe("build", function() {
99
cli.rmrf("test/data/temp")
1010

1111
describe("file {0}", [
12-
["build-cp"],
1312
["build-inline"],
1413
["build-simple"],
1514
["build-ui-css"],

test/data/build-cp/a.css

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/data/build-cp/dev.html

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/data/build-inline/dev.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
</style>
3838
</head>
3939
<body >
40+
<p class="base grid a b c"></p>
4041
<script src="lib1.js" cat="dummy/load.js" inline></script>
4142
<script src="a.js" cat></script>
4243
<script cat>var b = 2</script>

test/data/build-simple/a.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
img {
2+
background: url("a.svg");
3+
}
4+

0 commit comments

Comments
 (0)