1. 文件精简(minification)
1.1 YUI Compressor
语法:
java -jar yuicompressor.jar [options] [file] -o [outputfile]
例如:
java -jar yuicompressor.jar --preserve-semi core/core.js -o core/core-min.js
使用 Ant:
<target name="minify">
<apply executable="java" failonerror="true">
<fileset dir="${src.dir}" includes="*.js"/>
<mapper type="glob" from="*.js" to="${build.dir}/*-min.js"/>
<arg line="-jar"/>
<arg path="${yuicompressor}"/>
<arg line="${yuicompressor.options}"/>
<srcfile/>
<arg line="-o"/>
<targetfile/>
</apply>
</target>
注:新版本的 Ant 已经废弃 <apply> 任务,可以使用 <exec> 任务或 <java> 任务,关于 <java> 任务的用法,可以参考我在第15章读书笔记中的例子。
1.2 Closure Compiler
语法:
java -jar compiler.jar [options] --js [file] --js_output_file [outputfile]
例如:
java -jar compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --js core/core.js --js_output_file core/core-min.js
使用 Ant:
<target name="minify">
<apply executable="java" failonerror="true">
<fileset dir="${src.dir}" includes="*.js"/>
<mapper type="glob" from="*.js" to="${build.dir}/*-min.js"/>
<arg line="-jar"/>
<arg path="${closure}"/>
<arg line="${closure.options}"/>
<arg line="--js"/>
<srcfile/>
<arg line="--js_output_file"/>
<targetfile/>
</apply>
</target>
2. 文件压缩
略。