Skip to content

Instantly share code, notes, and snippets.

@wheresjames
Last active October 13, 2016 13:04
Show Gist options
  • Save wheresjames/966fd4e8d481f1d6733ff9be870d808b to your computer and use it in GitHub Desktop.
Save wheresjames/966fd4e8d481f1d6733ff9be870d808b to your computer and use it in GitHub Desktop.
Script to minify an entire web site

Script to minify an entire web site

This script will recursively minifify an entire website.

Use

compress-web.sh ./source-dir ./target-dir

You will need the following tools

Copy the jar file in the same folder as compress-web.sh

Install it using npm

Copy the jar file in the same folder as compress-web.sh

#!/bin/bash
exitError()
{
if [[ 0 -lt ${#@} ]]; then
echo
echo !!! Error: $@
echo
fi
exit -1
}
ifError()
{
if [[ 0 -eq $? ]]; then return 0; fi
exitError $@
}
makePath()
{
if [[ -d $1 ]]; then return 0; fi
mkdir -p $1
ifError "Failed to create path : " $1
}
compressWeb()
{
echo .
echo .----------------------------------------------------------
echo . $1
echo .----------------------------------------------------------
if [[ ! -d "$1" ]]; then return 0; fi
makePath $2
FILES=$1/*
for SRC in $FILES
do
FNAME=`basename $SRC`
TGT=$2/$FNAME
if [[ -d $SRC ]]; then
compressWeb $SRC $TGT
else
EXT="${FNAME##*.}"
echo $EXT " : " $SRC" -> " $TGT
case ${EXT,,} in
"js")
java -jar jscc.jar --js_output_file $TGT --js $SRC
if [[ 0 -ne $? ]]; then
echo "!!! Failed to build " $SRC
cp $SRC $TGT
ifError "Failed to copy " $SRC " -> " $TGT
fi
;;
"css")
java -jar yui.jar -o $TGT $SRC
if [[ 0 -ne $? ]]; then
echo "!!! Failed to build " $SRC
cp $SRC $TGT
ifError "Failed to copy " $SRC " -> " $TGT
fi
;;
"htm" | "html")
html-minifier --collapse-whitespace -o $TGT $SRC
if [[ 0 -ne $? ]]; then
echo "!!! Failed to build " $SRC
cp $SRC $TGT
ifError "Failed to copy " $SRC " -> " $TGT
fi
;;
*)
cp $SRC $TGT
ifError "Failed to copy " $SRC " -> " $TGT
;;
esac
fi
done
return 1
}
compressWeb $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment