views:

720

answers:

8

I'd like a tool that will:

a) roll a bunch of separate js files into one

b) minify my js

Any ideas?

+6  A: 

From an answer I made a bit ago...

The YUI Compressor is a tool I use, it compresses both JS and CSS well, and it is written in Java (so you can work it into a build process via ant). Someone's even made an online version of it.

It doesn't handle rolling the files together, but that's not really difficult - you can do that yourself by simply concatenating the files. For example, on *nix you could rollup and compress with a simple shell script:

# Target all your js files somehow, this is just an example
cat *.js > rollup.js

# Compress with YUI Compressor
java -jar yuicompressor-x.y.z.jar -o rollup-min.js rollup.js
Daniel Lew
Thanks Daniel. This will get me started (though I'll need to look in to controlling the order of concatination so dependant files load correctly).
morgancodes
Ah, that is a problem. I know that YUI rolled their own custom ant builder (http://yuilibrary.com/projects/builder), you might be able to glean some wisdom from there.
Daniel Lew
Google's Closure compiler is supposed to be better than YUI.http://code.google.com/intl/no/closure/compiler/
Kimble
+1  A: 

jQuery has two build systems. One based on make and on based on ant. With either of two is very easy to do what you want.

ant based build is more portable (at least on windows)

dfa
Can I build *my* code with this tool? So it seems this only provides a way to build your own jQuery.
Török Gábor
+4  A: 

Dojo's custom builder is pretty awesome. You can use it to create one or several javascript files, via a simple configuration file. I use it in conjunction with scons to build my site.

apphacker
+2  A: 

super naive build file based on ant. This build.xml file is assuming that you've js files in directory "js":

<?xml version="1.0" encoding="UTF-8"?>
<project  default="minify" basedir=".">

    <target name="prepare">
        <mkdir dir="dist"/>
    </target>

    <target name="clean">
        <delete dir="dist"/>
    </target>

    <target name="concat" depends="prepare">
        <concat destfile="dist/mylib-debug.js" fixlastline="true">
            <fileset dir="js" includes="*.js" />
        </concat>
    </target>

    <target name="minify" depends="concat">
        <!-- 
          -- invoke http://inconspicuous.org/projects/jsmin/jsmin.java
          -- or invoke YUICompressor.jar on dist/mylib-debug.js
          -->
    </target>
</project>
dfa
+1  A: 

YUICompressor is definitely a great way to go (as suggested earlier). A more robust and complete solution with Ant tie in is discussed on the creator of YUICompressor's blog: http://www.julienlecomte.net/blog/2007/09/16/. He deals with JS, CSS, and caching issues.

big lep
A: 

This looks pretty interesting to me. http://mojo.codehaus.org/javascript-maven-tools/javascript-maven-plugin/index.html

morgancodes
+1  A: 

May I also suggest sprockets http://www.getsprockets.com/.

jonnii
A: 

A Django-specific solution is http://code.google.com/p/django-compress/, which can combine scripts and minify the results using jsmin. It can also combine stylesheets and minify the result using CSSTidy.

ken