6. April 2011

Combine, minify and distribute JavaScript Files for your Application


Warning: Use of undefined constant jq_syntax_htmlentities - assumed 'jq_syntax_htmlentities' (this will throw an Error in a future version of PHP) in /kunden/364674_22305/siebler/subdomains/daily/www/wp-content/plugins/jquery-syntax/jquery-syntax.php on line 37

Warning: Use of undefined constant jq_syntax_htmlentities - assumed 'jq_syntax_htmlentities' (this will throw an Error in a future version of PHP) in /kunden/364674_22305/siebler/subdomains/daily/www/wp-content/plugins/jquery-syntax/jquery-syntax.php on line 38

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:

#!/bin/bash

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 PRODUCTION_JS_DIR=/js
export HEAD_COMMAND="head -n 1"
export JSMIN_COMMAND="./jsmin"

rm -fr $DIST_DIR
mkdir -p $DIST_DIR

# read in the current version
oldVersion=`$HEAD_COMMAND $VERSION_FILE`

# 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
cp $DIST_DIR/all.min.js $SCRIPTS/$newVersion$PRODUCTION_FILENAME

# clean up
rm -fr $DIST_DIR




SHARE:
Development, PHP 0 Replies to “Combine, minify and distribute JavaScript Files for your Application”