A good while ago I started thinking about how best to optimise web pages, which led me to the Yahoo! Exceptional Performance team. The first recommendation was to minimise HTTP requests. One way of doing this is to combine all JavaScript and all CSS files used on a page into single files. This can make maintainability an issue unless you have good build and release processes.

I came across Tim Harpers Bundle-fu plugin for Ruby on Rails and thought that there would be something similar for PHP (which is what I work in most of the time). After a bit of searching I had found a couple of methods for doing this but they all seemed to involve altering the way that files were included so I thought I’d come up with something myself that I could easily make into a WordPress plugin.

I’ve finally got something working fairly robustly thanks to output buffering and some brain-melting regular expressions. It may look like a cat has jumped all over my keyboard but there’s actually some clever lookaround stuff going on – which could probably be optimised too. The PHP class will collate all JavaScript files and inpage code snippets and do the same with your CSS. It will also optionally refactor your code (to move JavaScript to the bottom and CSS to the top), minify and obfuscate things using the Yahoo! compressor.

Initial tests of bundling the assets of my blog proved to yield some good results. I have several plugins enabled, all of which add their own files and bundling saves a round trip to the server for each one. I had a look at things using the LORI and Firebug Firefox browser plugins and the bundling shaves a few seconds off the page loading process. There is obviously an initial hit the first time that the bundle is generated but caching the results negates this issue.

Just the same as Bundle fu, the bundles will be regenerated if any of the files contained in it are changed. It also won’t work for JavaScript that is included by a utility script like Scriptaculous for example and at the moment it will bundle up any print style sheets that you use too which will probably give you some weird results.

Check out the code of the first version beta of Bundle PHP.

Related Posts:

  • No Related Posts