klimbot
December 23, 2021, 7:05am
1
I’ve recently found out about minify-ing files for the web (html/javascript/css) and have noticed a decent space saving (40% roughly for me).
Was wondering if there was some way to minify my html/javascript/css at build time and only include the minified files in my image instead of the full files in my repo.
If there is no way to do it by default I’ll probably rig up a bash script,
klimbot
December 24, 2021, 5:59am
3
Thanks for the tip @nliviu .
Pretty crude, but I’ve got this working for now which seems to serve my purpose:
#!/bin/bash
# exit when any command fails
set -e
git_diff="git diff"
output=$(eval "$git_diff")
check_for_uncommited_changes () {
if [ -z "$output" ]; then
echo "No uncommited changes, proceeding..."
return 1
else
echo "Uncommited changes detected, exiting..."
return 0
fi
}
minimise_javascript () {
echo "Minimising Javascript..."
./node_modules/uglify-js/bin/uglifyjs fs/script.js --mangle --output fs/script.js
return $?
}
minimise_css () {
echo "Minimising CSS..."
./node_modules/css-minify/bin/css-minify.js -f fs/layout.css -o fs/
rm fs/layout.css
mv fs/layout.min.css fs/layout.css
return $?
}
minimise_html () {
echo "Minimising HTML..."
./node_modules/html-minifier/cli.js fs/index.html -o fs/index.html --file-ext html --remove-comments --collapse-whitespace --minify-js true --minify-css true
return $?
}
if check_for_uncommited_changes; then
exit 1
else
minimise_javascript
minimise_css
minimise_html
echo "Success minifying..."
mos build --local --platform esp32
git checkout HEAD -- fs/layout.css
git checkout HEAD -- fs/script.js
git checkout HEAD -- fs/index.html
fi