Lossy CSS compression for fun and loss (or profit)
blog.danieljanus.pl
blog.danieljanus.pl
For example, add some code to your live site that periodically samples the DOM from a random subset of real user - for every element that's actually visible on the page, see what style rules apply to it. Count those up and get a big histogram.
Then let that feed into the compression. The least-used style rules on obscure pages should be the most compressed. Find a way to have those "reuse" other style rules that are close enough. Your most important content would be the least degraded.
I’ll try and give it a shot.
That in turn could give guidance for how you could pattern match rules even further
Also there are some false positives, for example if you set a variable twice and only read it later, then both variable declaration are marked as used while the first one was never read and is therefore useless.
There's to my knowledge no tool that automates this process. I wish there was one.
The only one I found is doei [1] but it's not finished and it just tries on a couple of hardcoded media queries. It's far from a simple problem but I'm sure someone can do it.
1. Approximate colours by reducing colours to their three letter hex-codes
2. Detect repetative rules and use native mixins.
3. Ugglify the CSS names, although you'd need to edit the HTML accordingly.
`.a{color: #FFFFFF;} .b{color:FFFFFE;}` ==> `.a,.b{color:#FFF;}`
then, if that is really all it does it can be called .white
next round trip it can look if multiple .b share a parent without other font colors.
.white then becomes #wrapper or body.
To do that I think you'd need to do the factorization on the CSS properties alone, and then apply the values in a predetermined order. But would be cool/fun to test out!
As others have said, this might be an interesting way to start zapping bloated CSS assets on aging codebases.
Amen
This was true if you don't co-locate CSS and code that uses it. Remove the code that imports the modular CSS and you remove the CSS. Interesting project nonetheless (even written in Clojure).
E.g ML training datasets. Huggingface is filled with interesting stuff often in the gigabytes in size. I'm sure skimming out what follows patterns of redundant or less useful to ML learning algorithms could offer a nice catalogue of datasets quick to download and cleanup. Image sets for ML learning: loss of irrelevant channels and whatnot. Weather forcasting or historical dumps etc
a { ... }
article a { ... }
.card a { ... }It's admittedly a pretty trivial page to test it on, but it works.
I think this could be a useful tool in the IDE. Imagine vscode highlighting a line and suggesting another place to put it instead. Neat.
https://danieljanus.pl/index30.html vs https://danieljanus.pl/
He started building a small straight to the point example and didn't show the result.