Quantcast
Channel: Developer Blog » yuicompressor
Viewing all articles
Browse latest Browse all 2

JavaScript build process using Ant

$
0
0

Projects written in JavaScript gets more and more complex and after a while you find your self needing a proper build process similar to the ones found for Java or .NET. We recently swapped our custom tools for JS concatenation and compression to one based on Apache Ant. Ant has been around for many years and we used this build tool when we developed Java applications so it has a large feature set and is very stable, well documented and it can be used on multiple operating systems.

But before we could use Ant we needed to rewrite preprocessing capabilities we had in our custom tool as a ant task. Having preprocessor statements in JavaScript is a very powerful thing especially for large projects where you might want to exclude or include a particular feature for a specific build target. We looked around for some existing engine but we couldn’t find any suitable for our specific needs.

Here is an example of a simple preprocessor statement:

// #ifdef somedefine

someLogic();

// #endif

someOtherLogic();

The above statement will only include the someLogic(); call if the somedefine is defined when it’s being preprocessed. This is very similar to languages like C/C++ or C#.

This is how the above code gets filtered using the ant task:

<preprocess infile="file.js" outfile="file.processed.js" defines="somedefine,someotherdefine" />

We also wanted to use the YUICompressor to reduce the size of the script. There where a few existing Ant task implementations for this tool but we didn’t find one that was exactly as we wanted it so we wrote our of task for this as well.

Here is an example on how to compress a simple JS file:

<yuicompress infile="file.js" outfile="file.min.js" />

Both these ant tasks and future tools will be added to a google code project called js-build-tools. If you have your own ant tasks you want to contribute to this project please let us know.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images