Posts tagged javascript:

May posted 31 May 2009 and tagged , ,

This function pipes a javascript file to a Python implementation of Douglas Crockford’s handy JSMin, a javascript minifier.

minify() { if [ -f $1 ] then MIN=${1%.[^.]*}.min.js cat $1 | python ~/path/to/jsmin.py > ${1%.[^.]*}.min.js BEFORE=`wc -c <$1` AFTER=`wc -c <$MIN` echo "$BEFORE $1" echo "$AFTER $MIN ($(echo "scale=2; 100*$AFTER/$BEFORE" | bc)%)" else echo "$1 not found" fi }

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
minify() {
  if [ -f $1 ]
  then
    MIN=${1%.[^.]*}.min.js
    cat $1 | python ~/path/to/jsmin.py > ${1%.[^.]*}.min.js
    BEFORE=`wc -c <$1`
    AFTER=`wc -c <$MIN`
    echo "$BEFORE $1"
    echo "$AFTER $MIN ($(echo "scale=2; 100*$AFTER/$BEFORE" | bc)%)"
  else
    echo "$1 not found"
  fi
}

It also prints the minified version’s character count as a percentage of the original’s:

$ minify prototype.js 

  126127 prototype.js
   93689 prototype.min.js (74.28%)