CSS minification in one line:
$ cat sourcefile.css | sed -e 's/^[ \t]*//g; s/[ \t]*$//g; s/\([:{;,]\) /\1/g; s/ {/{/g; s/\/\*.*\*\///g; /^$/d' | sed -e :a -e '$!N; s/\n\(.\)/\1/; ta' >target.css
With comments:
$ cat sourcefile.css | sed -e ' s/^[ \t]*//g; # remove leading space s/[ \t]*$//g; # remove trailing space s/\([:{;,]\) /\1/g; # remove space after a colon, brace, semicolon, or comma s/ {/{/g; # remove space before a semicolon s/\/\*.*\*\///g; # remove comments /^$/d # remove blank lines ' | sed -e :a -e '$!N; s/\n\(.\)/\1/; ta # remove all newlines ' > target.css
Using this script, I was able to chop about 29% (10K) off our master.css file. Assumes lines end in semicolons that should end in semicolons. May not play well with certain freakish outdated CSS hacks. Use at your own risk, and always test throughly before releasing into the wild.
1 comment:
Should you have the new line removal before removing comments, or does this code handle multiple line comments?
Post a Comment