A YuiCompressorFilter for Phing

by Sander Marechal

I have been playing with Phing quite a lot lately. Phing is a build system that looks and acts a lot like Apache Ant, except that it is written in PHP. That's a great thing for PHP developers like me, because it makes it much easier to extend the system with custom extensions.

I am going to write several useful extensions, the first of which is a YuiCompressorFilter. Phing already has support for a JavaScrip minifier in the form of the JsMinTask, but the yui-compressor is more useful. Not only does it usually compress better than JsMin, it can also compress CSS files. Also, because my YuiCompressor extension is implemented as a filter instead of a task you can do fancy things like minifying and concatenating files in a single step.

Here's an example task that concatenates and minifies your JavaScript files in a single step:

  1. <target name="js-compress">
  2.     <delete file="${project.basedir}/build/js/main.js" />
  3.     <append destFile="${project.basedir}/build/js/main.js">
  4.         <filterchain>
  5.             <filterreader classname="path.to.filters.YuiCompressorFilter">
  6.                 <param name="type" value="js" />
  7.                 <param name="preserve-semi" value="true" />
  8.             </filterreader>
  9.         </filterchain>
  10.         <filelist dir="src/js" files="forms.js,validation.js,gallery.js" />
  11.     </append>
  12. </target>

The YuiCompressorTask supports all options supported by the yui-compressor itself. Full source code and documentation is available on github.com/sandermarechal/phing-ext.

Creative Commons Attribution-ShareAlike


#1 Andre Flitsch (http://www.pixelperfect.at)

Thanks very much for this filter, i was looking for something like this, and it has saved me the trouble of making it myself!


Comments have been retired for this article.