// 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


3 Responses to “Combine, minify and distribute JavaScript Files for your Application”

  1. Nice Script! Thank you!

    Posted by Jens H. | April 11, 2011, 8:23 pm
  2. Great, thank you!

    Posted by Roland | March 12, 2012, 5:06 pm
  3. I read a lot of interesting articles here. Probably you spend a lot
    of time writing, i know how to save you a lot of work, there is an online tool
    that creates high quality, google friendly articles in minutes,
    just type in google – laranitas free content source

    Posted by Gary | September 17, 2014, 5:59 pm

Post a comment


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