I recently published a tiny CSS minifier named cssmin, after creating it for use on my site. All I really wanted was to strip my (verbose and ASCII-art-laden) comments and excess whitespace. I hear there are smarter things you might want to do if your CSS files are long or if you actually have more than two of them on your site; neither applies to me.
My Web publishing pipeline consists mainly of make
and (for index and RSS
generation) perl
, so I wanted something fast, local, and command-line
oriented. I looked online a bit, but the first couple pages of search results
didn’t seem like good fits. Online minifier tools were easy to rule out,
publishing-platform integration and configurability were of no value in this
case, and dependencies on third-party libraries or scripting tools just
introduced overhead where I didn’t want it.
Ultimately, this kind of tool is easily written in plain C. Buffered character input, simple logic (in this case, a small state machine), buffered character output. And all the characters relevant to CSS tokenization are in the 7-bit ASCII overlap with Unicode, so assuming UTF-8, this would really be a byte filter. So I wrote it.
The initial version took just a few minutes to write (I didn’t time myself, but far less than an hour), and did the basics. I added some tests (one beautiful aspect of a Unix-style text filter is that a shell script makes a fine test framework), and refined it further as I used it or consulted the spec, but the overall time investment has been pretty minor. There are probably bugs, but with such a small program, there can’t be many. One source file, zero dependencies other than the standard C library, no installation.
I should probably write a manpage.