Ant task for combining multiple JavaScript files into one and minifying them using Dojo Shrinksafe

In the Combining and minifying multiple JavaScript files into one on the fly, entry i built a Servlet that can be used to combine multiple JavaScript files into one. But its always better to combine multiple JavaScript into one during the build time instead of run time.

So this is the Ant task that you can add to your build file which can do the combining and minfiying for you


<target name="shrinksafe" >
<concat destfile="WebContent/js/combined.js">
<fileset dir="WebContent/js" includes="*.js" />
</concat>
<java fork="true" jar="${shrinksafe.lib.dir}/shrinksafe.jar"
dir="build/WebContent/js"
output="WebContent/js/combined.min.js">
<classpath>
<pathelement location="${shrinksafe.lib.dir}/js.jar"/>
</classpath>
<arg value="combined.js" />
</java>
</target>


The task can be broken into two pieces first you will have to combine multiple JavaScript files into one and i am using concat element for it, what it does is find all the JavaScript files inside WebContent/js and combine them together into combined.js which will be saved in WebContent/js folder.

Once the combined JavaScript file is ready i am executing the shrinksafe.jar, passing it the combined.js as argument and the output of this command would be saved into combined.min.js. Hereafter whenever you want to include the combined javascript include combined.min.js on your page.

3 comments:

Bob Flavin said...

When combining a bunch of dojo widget files with 'requires' statements, it seems that you have to combine them in the 'right' order because processing the require statement causes the file to be loaded separately.

I want to cause all the widget files and their dependent files to be merged into one large file to reduce the number of files that have to be loaded.

How do you do that?

Unknown said...

I created a library combining Granule library http://code.google.com/p/granule/

It accounts for 'requires' for google closure. But it
can be improved to support dojo as well if somebody interested in it.

Please request in the issues
http://code.google.com/p/granule/issues/list

Abhi said...

Thanks for info
Web Design Company in Bangalore
Website development in Bangalore