Pack JavaScript and CSS files

10 June 2010

One of the client-side optimization steps is JavaScript and CSS files compression and packaging.

The best choice is to use YUI compressor. In order to automate routine task and save some time for blogging :) I’ve written a simple bash script:

#!/bin/bash
# Javascript source
SRC="./src/"
RES="./project.min.js"

# declare script array
ARRAY=( 'jquery' 'jquery-ui' 'jquery.tooltip' 'jquery.colorbox')
# get number of elements in the array
ELEMENTS=${#ARRAY[@]}

rm $RES

# echo each element in array
# for loop
for (( i=0;i<$ELEMENTS;i++)); do
    echo "Compress file '${ARRAY[${i}]}'"
    echo "/*${ARRAY[${i}]}*/" >> $RES
    java -jar yuicompressor-2.4.2.jar --charset utf-8 $SRC${ARRAY[${i}]}.js >> $RES
done

Launch the script and wait until compilation is finished. Here you go: we’ve got single compressed script that can facilitate server’s work.

Similar script for CSS is done where the only difference is in the path. You may combine these scripts into one but I have no desire to overcompress CSS in addition to JS changes.

  • I appreciate, cause I found exactly what I was looking for. You have ended my 4 day long hunt! God Bless you man. Have a nice day. Bye

Leave a Comment

Your email address will not be published. Required fields are marked *