Skip to content

Commit 10a0d80

Browse files
committed
build: Add cssPurge/cssKeep options
1 parent 0c46145 commit 10a0d80

61 files changed

Lines changed: 263 additions & 245 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+
cssPurge: true,
2729
fetch: true,
2830
jsmin: "",
2931
min: "",

lib/build.js

Lines changed: 28 additions & 4 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+
//- --cssPurge Remove unused CSS classes (default: true)
16+
//- --cssKeep Class prefixes to always keep (default: is)
1517
//- --worker Update worker file
1618
//-
1719
//- Examples
@@ -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.cssPurge !== 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.used = { classes, 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+
, used = cssOpts.used
454471
sheet.replaceSync(str)
455472
cssOpts.url = urlFn
456-
return CSS.minify(sheet, cssOpts)
473+
cssOpts.used = null
474+
var out = CSS.minify(sheet, cssOpts)
475+
cssOpts.used = used
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) {

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.2"
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)