Saturday, 21 June 2008

Compressing Javascript files in netbeans 6.1

Netbeans 6.1 Javascript editor has some fantastic features and is my editor of choice, however it has a feature missing I would like to see implemented.

I have recently been the lead developer on a corporate time sheet web application developed using netbeans and the excellent Extjs library front end, Java middle tier with an Oracle backend database.

The company I work for has a global presence around the world and some countries have less developed network infrastructure with often narrow bandwidth and slow links.

To let the users in these countries get the best from my web applications it was deemed necessary to compress/minify script files, in my case Javascript and may be good practice.

Avoiding Re-inventing the Wheel
There are a variety of tools available to do this, however it is an extra manual step in the build process which could quite easily be prone to error and being able to do it as part of the build process automaticlly would be a great enhancement to netbeans build process especialy where a project contains many javascript files.

So I have made a suggestion as an enhancement request. The idea is to compress script files during the netbeans build process so that war files only contain compressed javascript leaving the editable source intact.

Investigating the nb build process
With some spare time today, knowing nothing about the actual build process in netbeans I decided to investigate a solution myself.

So this is what I have ascertained to date in a couple of hours :-
  • netbeans uses ant build scripts to do the actual building process.
  • you can write your own ant tasks.
  • each netbeans project has its own build script.
  • The build script contains place holders for pre/post build and distribution ant tasks.
So whats the solution then?
  • Learn how to build ant tasks and hook them into build.xml for my projects.
  • Build an ant task that compresses javascript files. (easy in Java)
  • Set up the build script so that it runs during the build part of the process but before the distribution part so that compressed javascript files are included in the war file.
  • Share the solution with the nb community.
Doing this will leave the readable source intact and editable from within netbeans or your source code control such as cvs etc and provide compressed javascript files in your web applications.

OK, there must be some down side to this too. From my point of view:-
  • I have to spend the time learning ant (not really a downer for me) time is though....
  • Debugging might be an issue if the source is actually modified on the fly during building.
  • I know nothing about configuring netbeans so compression could be switched off etc.
watch this space for a working solution.....

No comments: