Last active
November 9, 2020 14:59
-
-
Save rguliev/81b6b74ef921d8b03ea698556fceeedc to your computer and use it in GitHub Desktop.
A pre-commit hook to minify js & css files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A pre-commit hook to update minified versions of | |
# css/js files on commit | |
# Go to the project's root | |
cd "$(dirname "$0")" | |
cd ../../ | |
# Set up compression command | |
COMPRESSOR="./node_modules/uglify-js/bin/uglifyjs" | |
# Make sure compressor exists | |
if [[ ! -f $COMPRESSOR ]] ; then | |
echo 'ERROR: Compressor is not installed! Aborting...' | |
exit 1 | |
fi | |
BRANCH=`git rev-parse --abbrev-ref HEAD` | |
if [ "$BRANCH" == 'master' ] | |
then | |
# For each css and js file | |
# If you want only certain files to be compressed | |
# Change it to: for fname in file1 file2 file3; do | |
# TODO: It would be more effective to update only changed files, | |
# i.e. take list of files from staging and minify them instead of all js/css files | |
find . -regextype posix-extended \ | |
-iregex '.+\.(css|js)$' \ | |
-not -iregex '.+\.min\.(css|js)' \ | |
-not -path "*node_modules*" \ | |
-print0 | while IFS= read -r -d $'\0' fname | |
do | |
echo "Minifying: $fname" | |
# Name of minified version: myscript.js -> myscript.min.js | |
minified_name="${fname%.*}.min.${fname##*.}" | |
# Minify the file | |
$COMPRESSOR --rename "$fname" > $minified_name | |
# Add the file to commit | |
git add $minified_name | |
done | |
fi |
Thanks for the info and help!
Yes, I'm using these building tools as well.
Then I doubt you should use this code:) But it is up to you of course. I do not know all the details.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
The instruction I gave, is for your local repository (on your machine where you do your development) not for remote repository providers like GitHub, GitLab, Bitbucket, etc.. If you want to use it on your remote repository, you probably will need to use a CI tool (Travis CI, GitLab CI, etc).
Just remove that
if
block:)This is probably because of your OS or
find
version. Have you tried to google it? I found this. Maybe it will help you