New CSS minifier: miniMinifier
Opened this issue · 5 comments
Hello, I'd like to add my css minifier to the list, but sadly it's not node based. it's just an experiment (I tried to make a minifier as short as possible), but it has very interesting results:
given a string to minify "str", just run:
for(i=9;i--;)str=str.replace(/^\s|\s$|\/\*[^]*?\*\/|\s*;*([^\:\w.#\x27"\s*-])\s*|(:) /g,"$1$2")
(removes comments, whitespaces and semicolons)
or a little more complete:
for(i=9;i--;)str=str.replace(/([^#\d\w\(._\x27"-])((0)[a-z%]+|0(\.))|^\s|\s$|\/\*[^]*?\*\/|\s*;*([^\:\w.#)\x27"\s*-])\s*|\s(\))|(:) |[^}]*{}/gi,"$1$3$4$5$6$7").replace(/rgb\((\d+),(\d+),(\d+)\)|#[a-f0-9]{6}/gi,function(a,c,d,b){if(b)for(i in a="#",p=[c,d,b])a+=("0"+parseInt(p[i]).toString(16)).slice(-2);return(p=a[2]+a[4]+a[6])==a[1]+a[3]+a[5]?"#"+p:a})
(removes comments, whitespaces and semicolons, leading zeros, units after zero, empty rules, converts rgb colors to hex and preserve ie8 compatibility)
You'll find more info, demo and test files on: http://xem.github.io/miniMinifier/css/
If someone can help me convert this / those functions in node, that would be very nice.
(I don't know how to do it yet ^^)
Thanks!
Interesting thing. That's surely gonna be fast.
You write your code and export it. Then add a package.json file.
Where is the code right now published?
the code is published here: https://github.com/xem/miniMinifier/
If it was embedded inside a function, that would be:
minify=str=>{for(i=9;i--;)str=str.replace(/([^#\d\w\(._\x27"-])((0)[a-z%]+|0(\.))|^\s|\s$|\/\*[^]*?\*\/|\s*;*([^\:\w.#)\x27"\s*-])\s*|\s(\))|(:) |[^}]*{}/gi,"$1$3$4$5$6$7").replace(/rgb\((\d+),(\d+),(\d+)\)|#[a-f0-9]{6}/gi,function(a,c,d,b){if(b)for(i in a="#",p=[c,d,b])a+=("0"+parseInt(p[i]).toString(16)).slice(-2);return(p=a[2]+a[4]+a[6])==a[1]+a[3]+a[5]?"#"+p:a});return str}