Created
July 22, 2023 17:33
-
-
Save max1220/130c55ab101b869585a446b82818c000 to your computer and use it in GitHub Desktop.
A simple CSS concatenation/minification script using only bash+sed+gzip.
This file contains 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 | |
set -euo pipefail | |
# first argument is a path to a directory to minify CSS source files from | |
dir_path="${1}" | |
[ -d "${dir_path}" ] | |
css_files=("${dir_path%/}"/*.css) | |
concat_path="${dir_path%/}.css" | |
minified_path="${dir_path%/}.min.css" | |
gz_path="${dir_path%/}.min.css.gz" | |
echo "CSS Source files:" | |
printf "\t'%q'\n" "${css_files[@]}" | |
# concatenate | |
cat "${css_files[@]}" > "${concat_path}" | |
echo -ne "Concatenated size:\n\t" | |
wc -c "${concat_path}" | |
# minify | |
sed "/\/\*.*\*\//d;/\/\*/,/\*\// d" "${concat_path}" | tr -d "\t" | tr -d "\n" > "${minified_path}" | |
sed -i "s/: /:/g" "${minified_path}" | |
sed -i "s/ {/{/g" "${minified_path}" | |
sed -i "s/ > />/g" "${minified_path}" | |
echo -ne "Minified size:\n\t" | |
wc -c "${minified_path}" | |
# gzip | |
gzip --best < "${minified_path}" > "${gz_path}" | |
echo -ne "gzip'ed size:\n\t" | |
wc -c "${gz_path}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this, simple pass it a directory containing CSS files, they are concatenated in alphabetical order and minified(removes spaces, comments), e.g.:
Caveats(minifier):