views:

62

answers:

5

Anyone know a PHP scripts that done a good compression for jQuery?

A: 

If you're looking for a Javascript Minifier that works on PHP, check out this: http://github.com/rgrove/jsmin-php/

Joel Alejandro
A: 

Dean Edwards's JS Packer comes in PHP/Perl.

http://dean.edwards.name/download/#packer
Jimmy
+1  A: 

Google Closure Compiler Service for all your javascript minifying needs!

Erik
You can use this from php by calling the command line program.
chris
A: 

I run a website that lets people compress javascript code blocks online, and I would recommend against using the PHP translations of packer or JSMin. They are not very efficient, and the compressed code that comes out the other end often has errors or incorrectly translated or conflicting characters. It has been a source of many headaches.

Either use one of the primary Java JAR files to run all your code through (YUI Compressor, Dojo Shrinksafe, or Google Closure Compiler), or use the Google Closure web service.

Vance Lucas
+1  A: 

JSMin is a PHP class which we've found to work well to minify our JS. Minify is another PHP project which has been built on JSMin and looks pretty interesting (combines multiple files, handles local caching, etc) but I haven't had a chance to test it out yet so can't vouch for how well it works.

Depending on how complex you want your codebase to be, you've a couple of different options as to when or how you use a minifier:

  • minify the JS off-line, and check in both the minified and non-minified versions to your codebase. This will result in extra files floating around your codebase, and at some point someone will edit the un-minified version, forget to re-minimise it, and wonder why their change hasn't taken effect!
  • minify the JS off-line, and store only the minified JS in your codebase. This is not a good idea, as it makes maintenance substantially more difficult in future.
  • another option is to minimise on-the-fly, as the files are requested.

Minimising on the fly can be an attractive option, as it means that you can store your un-compressed JS file in your codebase for easy maintenance, yet get all of the benefits of having minimised JS served.

The problem with this approach is that it can be very computationally expensive to minimise your javascript on every single request.

To get around this, most JS minimisers offer the option of caching the resulting, compressed JS on the server. This means that the first user to request the JS will have trigger the minimising process, but subsequent users will get the compressed file from cache, without the additional overhead required to minimise the JS again.

There are a number of packages available - depending on your webserver of choice - which will take care of all of this for you (Apache example), or if you really want to, you can roll your own solution. Probably not the best idea, but no harm in trying it as a pet project!

At a pseudo-code level, it works basically as follows:

www.example.com/js/jsm.php?js=foo.js (this url can easily be made prettier using url rewrites)

// jsm.php
// On-demand JS minification
// Store result to local cache to reduce overhead from 
// multiple requests

// JSMin class
require( 'jsmin.php' );

// Has this file already been cached and put in cache?
// ($cache could be anything from local file cache, memcache, etc)
if( !$cache->checkFor($requestedFilename) )  
{
    // No.. minimise and put in cache
    $minifiedContents = JSMin::minimiseFile( 
                             file_get_content( $requestedFilename ) 
                                           );
    $cache->putValue($requestedFilename, $minifiedContents);
}

// Now serve minimised js from cache
echo $cache->getValue($requestedFilename);
ConroyP