AutoPacker is a project I’ve been hacking around with for the past few weeks and I’ve just gone over the final bits and bobs and have decided it is now time to push it out there. AutoPacker (AP, hereon) is a PHP script to automatically compress your JS files. I built it to make my life as easy as possible when dealing with my JS files — so hopefully it can make things easier for you too.
Why compress though? By packing your files into one, you reduce the number of file downloads — speeding things up for both your users and your server. On top of this, compression means you can more than half the size of the file, making it quicker to download and reducing your bandwidth usage.
I use AutoPacker for ThePCSpy.com and it compresses 173KB into just 48KB!
- PHP 5 (the base packer has been tested on 5.1.2, 5.1.3 & 5.1.4)
- Apache for url-rewritten auto-packing, otherwise anything supporting PHP
Start by extracting the files in the archive into a new directory on your server. For the purposes of this howto, I’ll assume we’re using a directory called /j/ — meaning you should have the following files:
Copy your .js files into /j/
Files are parsed and compressed alphabetically so you need to rename your files so they are in the correct order. For example, if I have 2 files: framework.js and ascript.js — where ascript.js depends on framework — you should rename your files accordingly:
For fully automated packing, rename bak.htaccess to .htaccess
Test your installation by going to /j/pack.php?debug=1
Assuming everything is working, you should get the following output:
Reading directory Checking freshness Last run: 1195292001 1.framework.js last edit: 1195189737 2.ascript.js last edit: 1195296428 Needs regeneration. Reading 1.framework.js Reading 2.ascript.js Writing to unpacked.js Done. Result is ~173KB. Loading Packer Writing to packed.js Done. Result is ~48KB.
You’re now ready to plug your page into /j/packed.js
If you want to have the packed.js and file updated automatically when you edit one of your dependant files, make sure the bak.htaccess is renamed to .htaccess — if you’d rather update manually, just call: /j/pack.php after uploading.
If you’re using a self-compressing framework like Mootools, I suggest you use their uncompressed download for the best compression and speed. Compressing compressed files is possible, it just doesn’t compress as well and performs quite slow.
I hope it works, I hope you like it and I’m open to suggestions if you think it needs something adding to it. Just drop a comment in below.
About Oli: I’m a Django and Python programmer, occasional designer, Ubuntu member, Ask Ubuntu moderator and technical blogger. I occasionally like to rant about subjects I should probably learn more about but I usually mean well.