YUI Compressor and ANT – JS Minification
Here is the followup to the CSS minification.. JS minification. How exciting! YUI Compressor can do a lot with your js, including obfuscation. For me i just want it to minify – put all on one line.
Below is my ANT task.
<!-- ============ --> <!-- JS Minification --> <!-- ============ --> <target name="js.minify"> <apply executable="java" parallel="false" dest="${temp.dir}/htdocs/front/resources/js/"> <fileset dir="${temp.dir}/htdocs/front/resources/js/" includes="*.js autocomplete/ item/ test/"> <exclude name="*jquery*" /> </fileset> <arg line="-jar"/> <arg path="lib/yuicompressor-2.4.2.jar"/> <arg line="--nomunge --preserve-semi"/> <srcfile /> <arg line="-o"/> <mapper type="glob" from="*.js" to="*-min.js"/> <targetfile /> </apply> <!-- Replace Existing JS--> <move todir="${temp.dir}/htdocs/front/resources/js/" overwrite="true"> <fileset dir="${temp.dir}/htdocs/front/resources/js/" includes="*.js autocomplete/ item/ test/"> <exclude name="*jquery*"/> </fileset> <mapper type="glob" from="*-min.js" to="*.js"/> </move> </target>
This is very similar to the approach taken with CSS minification with a few important changes. Obviously the options we pass in are different. We specify JS minification with nomunge (minify but do not obfuscaste). Details of the options are spelled out here in the documentation: http://www.julienlecomte.net/yuicompressor/README.
One of the gothcas is to not compress anything that is already compressed like JQuery or anything similar.
This can be controlled when you define the fileset. Notice I specify the root directory – then the subdirectories I want included, I also say look at everything that is *.js.
To exclude jQuery or anything similar use the exclude command and enter a pattern. In my example any jquery files have jquery somewhere in the name.
Hi,
Thanks for the information.
When I ran the minification on all my js files, i got 3 errors.
There is no clue about the file on which it found these errors.
Is there a option to get the file name.
I enabled the “-v” option, it gave additional Info messages, but it does not have a log entry for the start and end of the file.
Any ideas to get the file information?
———————————————————–
[apply] [ERROR] 77:13:missing variable name
[apply]
[apply] [ERROR] 80:56:identifier is a reserved word
[apply]
[apply] [ERROR] 81:59:identifier is a reserved word
[apply]
[apply] [ERROR] 1:0:Compilation produced 3 syntax errors.
[apply] org.mozilla.javascript.EvaluatorException: Compilation produced 3 syntax errors.
[apply] at com.yahoo.platform.yui.compressor.YUICompressor$1.runtimeError(YUICompressor.java:135)
[apply] at org.mozilla.javascript.Parser.parse(Parser.java:410)
[apply] at org.mozilla.javascript.Parser.parse(Parser.java:355)
[apply] at com.yahoo.platform.yui.compressor.JavaScriptCompressor.parse(JavaScriptCompressor.java:312)
[apply] at com.yahoo.platform.yui.compressor.JavaScriptCompressor.(JavaScriptCompressor.java:533)
[apply] at com.yahoo.platform.yui.compressor.YUICompressor.main(YUICompressor.java:112)
[apply] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[apply] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[apply] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[apply] at java.lang.reflect.Method.invoke(Method.java:597)
[apply] at com.yahoo.platform.yui.compressor.Bootstrap.main(Bootstrap.java:20)
[apply] Result: 2
———–
Hmm, I do not know if you can get any more debug info out of it, kind of a bummer.
I don’t know if this helps but you could search your JS files for use of “identifier”. I think YUI is saying you are using this and it is a reserved word.
Only will help you if you have a small number of JS files.