Hi, all!

i threw the following together, have gotten good use out of it, and
thought someone else out there might find it useful. It's used like
this:

<script src='include/jquery.js.php?
version=1.3.2&plugins=cookie,html,tipbox-mod' type='text/javascript'></
script>

As one can surmise, this script emits the JS code for jQuery and all
requested plugins. If passed no 'version' parameter then it only
returns the plugins (which can be used to load them individually,
after jQuery has already been loaded).

>From the above example, "include" is the directory containing the
following files:

- jquery.js.php (see code, below)
- jquery-1.3.2.js (1.3.2== the 'version' param passed above)
- jquery.cookie.js
- jquery.html.js
- jquery.topbox-mod.js

It will prefer files named "*.min.js" or "*.pack.js" over "*.js", but
it also gzips the output using ob_gzhandler, so minimizing/packing
brings little benefit (unless you also remove the ob_xxx() lines).

The implementation (only tested with PHP5 - might work in PHP4):

<?php
/**
 jQuery_pack_sources() emits JS source code for jQuery and, optionaly,
 jquery plugins which follow the naming convention of
jquery.PLUGIN.js.
 It looks for $_REQUEST['plugins'] or $_ENV['plugins'],
 expecting a space/comma-delimited list of jquery plugins emit.
 (Reminder: $_ENV is checked so that i can use the from the command-
line.)

 The $jqueryVersion param specifies the version of the core to use,
and
 should be in the the form X.Y.Z, such that jquery-X.Y.Z.js is the
file
 where jquery lives. If it is null then only plugins are emitted.

Author: Stephan Beal (http://wanderinghorse.net/home/stephan/)
License: Public Domain.
*/
function jQuery_pack_sources($jqueryVersion=null)
{
    if( ! $jqueryVersion ) $jqueryVersion = @$_REQUEST['version'];
    $key = 'plugins';
    $mod = @$_REQUEST[$key];
    if( ! $mod ) $mod = @$_ENV[$key];
    if($mod) $mod = preg_split('/(\s|,)/',$mod, -1,
PREG_SPLIT_NO_EMPTY);
    if(!$mod) $mod = array();
    if( $jqueryVersion )
    {
        array_unshift($mod,$jqueryVersion);
    }
    $mod = array_map('trim',$mod);
    $ext = array(
        '.min.js',
        '.pack.js',
        '.js'
    );
    $flist = array();
    foreach( $mod as $m )
    {
        $sep = preg_match('/^\d/',$m) ? '-' : '.'; // kludge for
jquery-X.Y.Z.js
        $got = false;
        foreach( $ext as $ex )
        {
            $fn = 'jquery'.$sep.$m.$ex;
            if( !...@file_exists($fn) ) continue;
            $got = true;
            $flist[] = $fn;
            break;
        }
        if( ! $got )
        {
            throw new Exception("Could not find module [$m] in file
[$fn]!");
        }
        echo "\n";
    }
    echo "/** Start of amalgamation of: ".implode(', ',$flist)." */
\n";
    foreach( $flist as $fn )
    {

        echo "/** Begin file [$fn] **/\n";
        echo file_get_contents($fn);
        echo "/** End file [$fn] **/\n";
    }
    echo "/** End of amalgamation of: ".implode(',',$flist)." */\n";
}
ob_start('ob_gzhandler');
jQuery_pack_sources();
header('Content-Type: text/javascript');
header('Transfer-Length: '.ob_get_length());
ob_end_flush();
exit(0);
?>


Happy hacking!

Reply via email to