Skip to content

Instantly share code, notes, and snippets.

@defong
Forked from rabin-io/pre-compress-web-assets
Created April 21, 2017 15:44
Show Gist options
  • Save defong/c111bc2c5d5d7b0e2a9e2b6e7bcca6ad to your computer and use it in GitHub Desktop.
Save defong/c111bc2c5d5d7b0e2a9e2b6e7bcca6ad 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