lilcss
Distill out css bloat by parsing static files for selectors
Last updated 3 years ago by amongiants .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install lilcss 
SYNC missed versions from official npm registry.

lilcss

Distill out css bloat by parsing static files for selectors

npm i lilcss

Warning: This is a work in progress, things might break...

Example

Let's run lilcss via the command line, passing in our css file containing a bunch of unused styles, and all of our site templates. We'll pipe the results to a new file.

$ lilcss fat.css -f templates/*.js > lil.css
<summary>fat.css</summary>
.c1{width:8.333333333333332%}
.c2{width:16.666666666666664%}
.c3{width:25%}
.c4{width:33.33333333333333%}
.c5{width:41.66666666666667%}
.c6{width:50%}
.c7{width:58.333333333333336%}
.c8{width:66.66666666666666%}
.c9{width:75%}
.c10{width:83.33333333333334%}
.c11{width:91.66666666666666%}
.c12{width:100%}
.df{display:flex}
.db{display:block}
.dib{display:inline-block}
.di{display:inline}
.dt{display:table}
.dtc{display:table-cell}
.dtr{display:table-row}
.dn{display:none}
<summary>templates</summary>
module.exports = html`
  <div class="c2 ${show ? 'db' : 'dn'}"></div>
`
module.exports = html`
  <div class="c4 dib"></div>
  <div class="c8 dib"></div>
`

Our new file will now only contain the css we need:

.c2{width:16.666666666666664%}
.c4{width:33.33333333333333%}
.c8{width:66.66666666666666%}
.db{display:block}
.dib{display:inline-block}
.dn{display:none}

API

lilcss(css, src, options)

Returns the distilled css.

option default controls
ignore [] any selectors to ignore
attr ['class', 'sm', 'md', 'lg', 'xl'] attributes to parse from files

Notes

lilcss aims to solve similar problems as uncss and purifycss but is a much less general solution. To minimize complexity, it is assumed:

  1. A single css file will be parsed
  2. The css file must not be minified prior to being parsed
  3. General selectors, such as body or input will always be preserved
  4. Any attribute selector will be treated as ~=
  5. Only class and attribute selectors are supported
  6. Only HTML-like attributes will be parsed, things like classList.add() are not supported:

Input template to parse:

<div class="c1 dn" sm="c2">

Extracted selectors:

['.c1', '.dn', '[sm~="c2"]']

Anything which does not match these selectors will be removed from the css.

FAQ

Work in progress...

Why?

Removing unused css output is important but the existing tools don't work well for my needs. uncss requires code to be run in a headless browser but this assumes too much about how you are building your site. purifycss does a bit better in that it parses static files, but the results are generally unpredictable and attribute selectors are fully unsupported.

lilcss has specific requirements but this allows the code to stay small and concise. More functionality may be introduced as needed.

Todo

  • [ ] tests
  • [ ] cli more robust
  • [ ] docs

See Also

Current Tags

  • 0.0.0                                ...           latest (3 years ago)

1 Versions

  • 0.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (4)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |