|
5 | 5 | [](http://commitizen.github.io/cz-cli/) |
6 | 6 | [](https://david-dm.org/patrickhulce/nukecss) |
7 | 7 |
|
8 | | -Eliminates unused CSS rules. |
| 8 | +Eliminates unused CSS rules. Built for single-page apps from the ground up. Inspired by [purifycss](https://github.com/purifycss/purifycss) and [uncss](https://github.com/giakki/uncss). |
| 9 | + |
| 10 | +## How It Works |
| 11 | +* Parses the CSS with [gonzales-pe](https://github.com/tonyganch/gonzales-pe) and walks the AST to find the IDs, classes, and DOM types used in selectors. |
| 12 | +* Parses HTML and JavaScript sources to find rule usage in strings and attributes, falling back to simple RegExp search when parsing fails. |
| 13 | +* Removes rules whose selectors cannot be found in the source set. |
| 14 | + |
| 15 | +## Usage |
| 16 | +`npm install --save nukecss` |
| 17 | + |
| 18 | +```js |
| 19 | +const nukecss = require('nukecss') |
| 20 | + |
| 21 | +const javascript = 'const jsignored = "js-class other-class"' |
| 22 | +const javascript2 = 'const woah = ["still", "works"].join("-")' |
| 23 | +const html = '<div id="primary" class="html-class">html-ignored</div>' |
| 24 | +const css = ` |
| 25 | +.jsignored { color: white; } |
| 26 | +.html-ignored { color: white; } |
| 27 | +.js-class { color: white; } |
| 28 | +.other-class { color: white; } |
| 29 | +.still-works { color: white; } |
| 30 | +#primary { color: white; } |
| 31 | +#primary > .unused { color: white; } |
| 32 | +.also-unused { color: white; } |
| 33 | +` |
| 34 | + |
| 35 | +const nuked = nukecss([ |
| 36 | + {content: javascript, type: 'js'}, |
| 37 | + {content: javascript2, type: 'js'}, |
| 38 | + {content: html, type: 'html'}, |
| 39 | +], css) |
| 40 | +> console.log(nuked) |
| 41 | +.js-class { color: white; } |
| 42 | +.other-class { color: white; } |
| 43 | +.still-works { color: white; } |
| 44 | +#primary { color: white; } |
| 45 | +``` |
0 commit comments