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

  • I simply wanted to write down a brief remark to appreciate you for all the fabulous ways you are giving out at this website. My considerable internet search has at the end of the day been compensated with good quality facts and strategies to talk about with my family. I would claim that many of us visitors actually are definitely fortunate to exist in a magnificent network with so many outstanding professionals with interesting principles. I feel truly blessed to have discovered the website and look forward to so many more pleasurable moments reading here. Thank you again for everything.

Leave a Comment

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