This script has:
- the internet connection is not required
- requires Java >= 1.4)
How to use the YUI Compressor?
At the shell/cmd execute the following code x.y.z. represents the version number:
java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js
myfile.js is file which will be compressed
-o will place the output into ‘file’ or a file pattern.
myfile-min.js is the output file
Is possible to compress more then one file at once?
I didn’t dig in to the documentation if this is possible or not, because i would like to compress files in different locations into one compressed file. I wrote for this solution batch/shell script.
The script checks first if compressed file exists. If it does, then it removes the current compressed file.
if [ -e js/myfile-min.js ] then rm js/myfile-min.js fi
Then script merges files into uncompressed
cat js/file-1.js js/file-2js js/sub/file-1.js js/sub2/file-1.js > js/myfile-min.js
The last step is, that we compress the
java -jar yuicompressor-2.4.8.jar js/myfile.js -o js/myfile-min.js
Here is the complete code for the Unix users:
if [ -e js/myfile-min.js ] then rm js/myfile-min.js fi cat js/file-1.js js/file-2.js js/sub/file-1.js js/sub2/file-1.js > js/myfile-min.js java -jar yuicompressor-2.4.8.jar js/myfile-min.js -o js/myfile-min.js
and the solution for the Windows users:
IF EXIST js\myfile-min.js del js\myfile-min.js type js\file-1.js js\file-2.js js\sub\file-1.js js\sub2\file-1.js > js\myfile-min.js java -jar yuicompressor-2.4.8.jar js\myfile-min.js -o js\myfile-min.js
The solution for the CSS file is almost the same. Just add option
java -jar yuicompressor-2.4.8.jar --type css css/style.css -o css/style-min.css
KineticJS – file size: 484.713 bytes
JsMini: 133.779 bytes
JSCompress: 105.079 bytes
UglifyJS 2: 103.741 bytes
blimptontech: 105.079 bytes
YUI Compressor: 105.484 bytes
The Statistics – file size: 12.658 bytes
JsMini: File was not compressed
JSCompress: 7.529 bytes
UglifyJS 2: 7.533 bytes
blimptontech: 7.529 bytes
YUI Compressor: 7.537 bytes
We will have to wait for ‘The best compressor tool’ and compress our files with different tools and check which one compressed the best.
November 7, 2013: The post was updated with the comparison between the online compressors, YUI Compressor and the UglifyJS 2 library.