From cffe06f44e695afc7c5e154ff8ec0084875240a6 Mon Sep 17 00:00:00 2001 From: welpo Date: Thu, 17 Aug 2023 23:56:21 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(pre-commit):=20show=20which=20?= =?UTF-8?q?JS=20compressor=20is=20best?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .githooks/pre-commit | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 08dbefc..fbb694a 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -15,8 +15,8 @@ # Stops you from commiting: # # - a draft .md file # # - a file with a "TODO" # -# - a JS file without a minified version. # -# - a minified JS file that isn't as small as it can be. # +# - a JS file without a minified version # +# - a minified JS file that isn't as small as it can be # ################################################################################# # Function to exit the script with an error message. @@ -53,10 +53,30 @@ function has_minified_version() { function is_minified() { local file="$1" - # Check if the file is as small as it can be. - terser --compress --mangle -- "$file" | wc -c | grep -q "^$(wc -c < "$file")$" + + # Original file size. + local original_size=$(wc -c < "$file") + + # File size after compression with terser. + local terser_size=$(terser --compress --mangle -- "$file" | wc -c) + + # File size after compression with uglifyjs. + local uglifyjs_size=$(uglifyjs --compress --mangle -- "$file" | wc -c) + + # Check if the file is already as small as or smaller than both minified versions. + if (( original_size <= terser_size && original_size <= uglifyjs_size )); then + return 0 + fi + + # If the file isn't as small as it can be, suggest the better compressor in the error message + if (( terser_size < uglifyjs_size )); then + error_exit "Minified JS file $file isn't as small as it can be with terser! Consider using terser for better compression." + else + error_exit "Minified JS file $file isn't as small as it can be with uglifyjs! Consider using uglifyjs for better compression." + fi } + # Check if the script is being run from the root of the repo. if [[ ! $(git rev-parse --show-toplevel) == $(pwd) ]]; then error_exit "This script must be run from the root of the repo." @@ -80,7 +100,7 @@ fi # Get the newly added and modified files. all_changed_files=$(git diff --cached --name-only --diff-filter=AM) -# Loop through each newly added or modified .md or .png file for PNG optimization and draft checking. +# Loop through all newly added or modified files. for file in $all_changed_files; do # If the file is a PNG and png_compressor is set, compress it and add it to the commit. if [[ "$file" == *.png ]] && [[ -n "$png_compressor" ]]; then @@ -107,8 +127,9 @@ for file in $all_changed_files; do fi # If the file is a minified JS file and it isn't as small as it can be, abort the commit. - if [[ "$file" == *.min.js ]] && ! is_minified "$file"; then - error_exit "Minified JS file $file isn't as small as it can be! Did you minify it?" + # Error message shows which minifier is best for the file. + if [[ "$file" == *.min.js ]]; then + is_minified "$file" fi done