minimalcss is a Node API and cli app to analyze the minimal CSS needed for initial load. One of it's killer features is that all CSS parsing is done the "proper way". Meaning, it's reduced down to an AST that can be iterated over, mutated and serialized back to CSS as a string.
Thanks to this, together with my contributors @stereobooster and @lahmatiy, minimalcss
can now figure out which @font-face
rules are redundant and can be "safely" removed. It can make a big difference on web performance. Either because it prevents expensive network requests of downloading some https://fonts.gstatic.com/s/lato/v14/hash.woff2
or downloading base64
encoded fonts.
For example, this very blog uses Semantic UI which is a wonderful CSS framework. But it's quite expensive and contains a bunch of base64
encoded fonts. The Ratings module uses a @font-face
rule that weighes about 15KB.
Sure, you don't have to download and insert semanticui.min.css
in your HTML but it's just sooo convenient. Especially when there's tools like minimalcss
that allows you to be "lazy" but get that perfect first load web performance thing.
So, the CSS when doing a search looks like this:
126KB of CSS (gzipped) transferred and 827KB of CSS parsed.
Let's run this through minimalcss
instead:
$ minimalcss.js --verbose -o /tmp/peterbe.search.css "https://www.peterbe.com/search?q=searching+for+something" $ ls -lh /tmp/peterbe.search.css -rw-r--r-- 1 peterbe wheel 27K Jan 22 09:59 /tmp/peterbe.search.css $ head -n 14 /tmp/peterbe.search.css /* Generated 2018-01-22T14:59:05.871Z by minimalcss. Took 4.43 seconds to generate 26.85 KB of CSS. Based on 3 stylesheets totalling 827.01 KB. Options: { "urls": [ "https://www.peterbe.com/search?q=searching+for+something" ], "debug": false, "loadimages": false, "withoutjavascript": false, "viewport": null } */
And let's simulate it being gzipped:
$ gzip /tmp/peterbe.search.css $ ls -lh /tmp/peterbe.search.css.gz -rw-r--r-- 1 peterbe wheel 6.0K Jan 22 09:59 /tmp/peterbe.search.css.gz
Wow! Instead of downloading 27KB you only need 6KB. CSS parsing isn't as expensive as JavaScript parsing but it's nevertheless a saving of 827KB - 27KB = 800KB of CSS for the browser to not have to worry about. That's awesome!
By the way, the produced minimal CSS contains a lot of license preamble as left over from the fact that the semanticui.min.css
is made up of components. See the gist itself.
Out of the total size of 27KB (uncompressed) 8KB is just the license preambles. minimalcss
does not attempt to touch that when it minifies but you could easily add your own little tooling to re-write it, since there's a lot of repetition and save another ~7KB. However, all that repetition compresses well so it might not be worth it.