// you’re reading...

Development PHP 

Combine, minify and distribute JavaScript Files for your Application

It’s very easy to Combine, minify and distribute JavaScript Files for your Application if you have shell access to your server.

I wrote a simple Script which will combine all your JS Files to a single on, an run the shell Tool, JSMIN to minify the code.

The Script will distribute your combine file to a target Folder and create a single template File which you can include in your application header, so your clients will always get the latest file version.

Required Tools:
* the Bash
* JSMIN (http://www.jsmin.com)

following shell code:


export SRC=/var/www/app
export SCRIPTS=$SRC/webroot/js
export VERSION_FILE=$SRC/js_combine_counter
export TPL_ELEMENT_FILE=$SRC/views/elements/js_combine.ctp
export DIST_DIR=/tmp/dist
export PRODUCTION_FILENAME=.all.min.js
export HEAD_COMMAND="head -n 1"
export JSMIN_COMMAND="./jsmin"

rm -fr $DIST_DIR
mkdir -p $DIST_DIR

# read in the current version

# increment the value
newVersion=$(( oldVersion + 1))

echo $newVersion > $VERSION_FILE

echo "<script src="$PRODUCTION_JS_DIR/$newVersion$PRODUCTION_FILENAME" type="text/javascript"><!--mce:0--></script>" &gt; $TPL_ELEMENT_FILE

# concatenate all code into one file
cat $SCRIPTS/jquery.bgiframe.min.js $SCRIPTS/jquery.cookie.js $SCRIPTS/superfish.js $SCRIPTS/formvalidator/form.js $SCRIPTS/ab.js &gt; $DIST_DIR/all_combine.js

# now minify it
`$JSMIN_COMMAND &lt; $DIST_DIR/all_combine.js &gt; $DIST_DIR/all.min.js`

# install it

# clean up
rm -fr $DIST_DIR


This in the personal Blog from Fabian Siebler, a Lead Developer and Projektmanager from Germany.