File tree 3 files changed +21
-8
lines changed
3 files changed +21
-8
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ The entire logic for DropCSS is this [~60 line file](https://github.com/leeoniya
17
17
### Install
18
18
19
19
```
20
- npm install --save-dev dropcss
20
+ npm install -D dropcss
21
21
```
22
22
23
23
---
@@ -45,17 +45,28 @@ let css = `
45
45
}
46
46
` ;
47
47
48
+ const whitelist = / \b (?:#foo| \. bar)\b / ;
49
+
50
+ let dropped = new Set ();
51
+
48
52
let cleansedCSS = dropcss ({
49
53
html,
50
54
css,
51
- keep : (sel ) => {
52
- // test selector against some whitelist
53
- // and return `true` to retain it
54
- return / #foo/ .test (sel);
55
+ shouldDrop : (sel ) => {
56
+ if (whitelist .test (sel))
57
+ return false ;
58
+ else {
59
+ dropped .add (sel);
60
+ return true ;
61
+ }
55
62
},
56
63
})
57
64
```
58
65
66
+ ` shouldDrop ` is called for every CSS selector that could not be matched in the ` html ` .
67
+ Return ` false ` to retain it or ` true ` to drop it.
68
+ Additionally, this callback can be used to log all removed selectors.
69
+
59
70
---
60
71
### Features
61
72
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " dropcss" ,
3
- "version" : " 0.1 .0" ,
3
+ "version" : " 0.2 .0" ,
4
4
"description" : " Unused CSS Cleaner" ,
5
5
"main" : " ./src/dropcss.js" ,
6
6
"scripts" : {
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ const adapter = require("./adapter");
6
6
// https://developer.mozilla.org/en-US/docs/Web/CSS/pseudo-classes
7
7
const pseudoClassNonTransient = / : (?: f i r s t | l a s t | n t h | o n l y | n o t | e m p t y ) \b / ; // |lang
8
8
9
+ const doDrop = sel => true ;
10
+
9
11
function dropcss ( opts ) {
10
12
const htmlAst = parse ( opts . html ) ;
11
13
@@ -15,7 +17,7 @@ function dropcss(opts) {
15
17
parseAtrulePrelude : false
16
18
} ) ;
17
19
18
- const keep = opts . keep || ( ( ) => false ) ;
20
+ const shouldDrop = opts . shouldDrop || doDrop ;
19
21
20
22
csstree . walk ( cssAst , function ( node , item , list ) {
21
23
if ( node . type == "Rule" ) {
@@ -33,7 +35,7 @@ function dropcss(opts) {
33
35
// remove any empty leftovers eg :not() - [tabindex="-1"]:focus:not(:focus-visible)
34
36
. replace ( / : [ a - z - ] + \( \) / gm, '' ) ;
35
37
36
- if ( domSel == '' || CSSselect . selectOne ( domSel , htmlAst . childNodes , { adapter} ) || keep ( sel ) )
38
+ if ( domSel == '' || CSSselect . selectOne ( domSel , htmlAst . childNodes , { adapter} ) || shouldDrop ( sel ) !== true )
37
39
pre . push ( sel ) ;
38
40
} ) ;
39
41
You can’t perform that action at this time.
0 commit comments