Mac下用Closure compiler

Closure compiler是google前端开发的javascript代码压缩工具。

首先安装closure compiler: http://code.google.com/closure/compiler/docs/gettingstarted_app.html

执行时,报”java.lang.UnsupportedClassVersionError:Bad version number in .class file” 错,这是由于编译的jre跟我本机的jre版本不同。但用mac的software update升级最高只能升到1.5,还是报错。经百般搜索之后,发现这篇文章:

http://blog.adsdevshop.com/2008/02/26/installing-the-jdk-16-on-mac-os-x/

原来这个soylatte就是jdk 1.6。解压缩后,需要改环境变量$PATH,改的方法:

cd  ~/
vim .profile
. ./.profile

然后试一下:
java -version
java version “1.6.0_03-p3″
Java(TM) SE Runtime Environment (build 1.6.0_03-p3-landonf_03_feb_2008_02_12-b00)
Java HotSpot(TM) Server VM (build 1.6.0_03-p3-landonf_03_feb_2008_02_12-b00, mixed mode)

成了。试一下closure compiler:
java -jar ~/library/closure-compiler/compiler.jar –help
不报错了。看看压缩的效果:
java -jar ~/Library/closure-compiler/compiler.jar –js yui.js  –js_output_file yui-min.js

14K 11 10 18:17 yui-min.js
83K 11  6 14:29 yui.js

Closure的另一个工具Closure Inspector也很不错,见:http://code.google.com/closure/compiler/docs/inspector.html
其中source map功能很方便调试,但需要用closure compiler创建一个map文件:

java -jar ~/Library/closure-compiler/compiler.jar –js yui.js –create_source_map ./yui-map.js –js_output_file yui-min.js

需要注意的是,用closure compiler高级模式压缩可能会有风险,@lifesinger 写了一篇很不错的介绍 :http://lifesinger.org/blog/2009/11/closure-compiler-vs-yuicompressor/

Comments

  1. [...] java -jar compiler.jar –js myfile.js –js_output_file myfile-min.js 相关介绍:http://hikejun.com/blog/?p=476 支持多个文件合并: java -jar compiler.jar –js myfile-1.js –js myfile-2.js [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">