该项目首页:http://code.google.com/intl/zh-CN/closure/compiler/
下载地址:http://closure-compiler.googlecode.com/files/compiler-latest.zip
下载后解压,即可看到compiler.jar;
简单的测试:
新建一个测试js文件hello.js
function tFun(str){
var isFlag=false;
if(str.length>100){
isFlag=false;
}else{
isFlag=true;
}
if(isFlag){
alert("woyo.length>100");
}else{
alert("woyo.length<=100");
}
}
tFun("12313");
执行如下命令:
java -jar compiler.jar --js hell.js --js_output_file hello-compiled.js
得到hello-compiled.js文件如下:
function tFun(a){var b=!1;(b=a.length>100?!1:!0)?alert("woyo.length>100"):alert("woyo.length<=100")}tFun("12313");
我们只是使用了Closure Compiler的简单压缩,也是安全的压缩,Closure Compiler还提供了高级的压缩,这样操作是有风险的,所以就使用了,一般使用简单压缩就可以了。