JavaScript build options / tools

These days I find myself working more and more on the client side and so my JS files tend to get bigger and bigger. I got to the point where most HTML pages have half a dozen or more imported JS in the header and I realized that it hurts load times.

I recently discovered this script that allows multiple JS files to be loaded with a single HTTP request. It's written in PHP and is a fan of Django, which I plan to rewrite in Python. I am planning on using HTTP redirection on a preliminarily constrained and merged file and was wondering what the 301 would cost. Please let me know if this is a silly idea.

On the other hand, I was a bit worried about injecting scripting logic into the static files service and I was wondering if there is a viable build alternative, like an ant task that concatenates and minifies JS files and replaces multiple JS loaded in the HTML header with one big one like the script does.

+1


a source to share


2 answers


For PHP, I certainly prefer to do it dynamically, because if you introduce a build step, you lose one of the main benefits of using PHP. In fact, at the risk of self-promotion, I wrote a Javascript boost in PHP about this very issue.

Of course, other technologies may vary.

Again, this is PHP, but it's not just a piece of code for you (although you can skip straight to part 6 if you just need some fully working code) and can make a difference to you in terms of identifying problems and doing things right and why you do them this way.



I prefer to have bundles of Javascript files (maybe just one for the whole application) and then each page just activates the behavior it wants using standard means, but all the code bodies are in a larger cached and minified JS file. This works the fastest and is a good way to go.

If you want this to be part of the build process, which is a sensible solution, if you have a build process anyway, then I suggest you downsize your code. There are many tools for this. Take a look at YUI Compressor .

If you are doing a static compiler for JS files, all the other issues mentioned above, such as gzipping and related issues, are still relevant.

+4


a source


The YUI compressor is a good choice. If you want to learn how to set up an Ant-based build process, check out this tutorial: http://www.javascriptr.com/2009/07/21/setting-up-a-javascript-build-process/



As a Ruby based alternative, I would recommend Sprockets

0


a source







All Articles