Skip to content

Instantly share code, notes, and snippets.

@sandeepone
Forked from rabin-io/pre-compress-web-assets
Created December 21, 2016 21:34
Show Gist options
  • Save sandeepone/004c005d0f7e3e1f53986c8fe47f883d to your computer and use it in GitHub Desktop.
Save sandeepone/004c005d0f7e3e1f53986c8fe47f883d to your computer and use it in GitHub Desktop.
Recursively pre-compress (gzip) CSS/JavaScript/webfont assets for use Nginx and the HttpGzipStaticModule module.
#!/bin/bash
###########################################################################
# Usage:
# pre-compress-web-assets [.|folder_name]
#
# This script will recursively look for the files in the $ext variable
# and compress/re-compress them.
#
# By default it will look into the current working folder,
# but one can provide a path for the script to crawl.
#
###########################################################################
# Flags
set -e # break on error.
set -u # break on using undefined variable.
###########################################################################
# Settings
VERSION=20160425172300
###########################################################################
base_name=$(basename "${0}")
print_fail() {
echo "$*"
exit1
}
self_update() {
gits_url='https://gist.githubusercontent.com/rabin-io/63000f48d1f9170f17ea5f73bcf84d66/raw/pre-compress-web-assets'
full_path=$(realpath "${0}")
tempfile=$(mktemp "/tmp/${base_name}.XXXXXX")
wget -q "${gits_url}" -O "${tempfile}"
gits_version=$(grep "^VERSION=" "${tempfile}" | awk -F'=' '{print $2}')
local_version=$(grep "^VERSION=" "${full_path}" | awk -F'=' '{print $2}')
if [[ ${gits_version} -ge ${local_version} ]];
then
cp -v "${tempfile}" "${full_path}"
else
echo "Nothing to update."
exit 1
fi
}
compressResource() {
gzip -c9 "${1}" > "${1}.gz"
touch -c --reference="${1}" "${1}.gz"
echo "Compressed: ${1} > ${1}.gz"
}
if [[ $1 == '--selfupdate' || $1 == '--self-update' ]]; then
self_update
else
appDir=${1-${PWD?WTF}}
echo "Preocessing ${appDir}"
ext="css|js|eot|svg|ttf|woff"
# fetch all existing gzipped CSS/JavaScript/webfont files and remove files that do not have a base uncompressed file
find "$appDir" -type f -regextype posix-extended -iregex ".*\.(${ext})\.gz$" -print0 | while read -d '' compressFile
do
if [[ ! -f ${compressFile%.gz} ]];
then
# remove orphan gzipped file
rm "${compressFile}" && echo "Removed: ${compressFile}"
fi
done
# fetch all source CSS/JS/webfont files - excluding *.src.* variants (pre-minified CSS/JavaScript)
# gzip each file and give timestamp identical to that of the uncompressed source file
find "$appDir" -type f -regextype posix-extended \( -iregex ".*\.(${ext})$" \) \( ! -name "*.src.css" -and ! -name "*.src.js" \) -print0 | while read -d '' sourceFile
do
if [[ -f "${sourceFile}.gz" ]];
then
# only re-gzip if source file is different in timestamp to the existing gzip file
if [[ (${sourceFile} -nt "${sourceFile}.gz") || (${sourceFile} -ot "${sourceFile}.gz") ]]; then
# re-compress
compressResource "${sourceFile}"
fi
else
compressResource "${sourceFile}"
fi
done
#
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment