Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
optimize-openwrt.sh
#!/bin/bash
dl_and_install_build_prereq() {
# Prerequisites: https://openwrt.org/docs/guide-developer/quickstart-build-images
# Download and install prerequisites for compiling firmware
sudo apt-get -y update && sudo apt-get -y upgrade
sudo apt -y install subversion g++ zlib1g-dev build-essential git python time
sudo apt -y install libncurses5-dev gawk gettext unzip file libssl-dev wget
sudo apt -y install libelf-dev ecj fastjar java-propose-classpath
}
generate_buildenv() {
# Prepare build enviroment
wget -qO openwrt.tar.gz https://github.com/openwrt/openwrt/archive/v18.06.4.tar.gz
tar -xvzf openwrt.tar.gz
mv openwrt-* openwrt
rm -f openwrt.tar.gz
}
dl_and_install_feeds() {
# Download and install package feeds
./scripts/feeds update -a
./scripts/feeds install -a
rm -f .config .config.old
}
minify_feeds() {
# Download and install prerequisites for minification
dl_and_install_prereq() {
sudo apt-get -y update && sudo apt-get -y upgrade
sudo apt-get -y install nodejs npm lua5.2 # Required for luamin
sudo npm install csso-cli -g
sudo npm install uglify-js -g
sudo npm install html-minifier -g
git clone https://github.com/stravant/LuaMinify.git luamin
mv luamin/* ./
rm -rf luamin
}
minify(){
# luapak file directory
LUA_MINIFIER="lua ./CommandLineMinify.lua"
# if you want to enable it for specific directory set it here
CURRENT_DIR="./feeds"
# First argument must be css, js, lua, htm, html, svg
TYPE=$1
# Status messages
ERROR_DISPLAY="\t\033[31;40m\033[1m[error]\033[0m"
OK_DISPLAY="\t\033[1;32;40m\033[1m[ok]\033[0m"
[ -z $TYPE ] && \
echo -e "First argument missing, it should be css, js, or html. $ERROR_DISPLAY" && \
exit 0
[ $TYPE != 'js' ] && [ $TYPE != 'css' ] && [ $TYPE != 'lua' ] && [[ ! $TYPE =~ htm? ]] && [ $TYPE != 'svg' ] && \
echo -e "First argument must be css, js, or lua. $ERROR_DISPLAY" && \
exit 0
echo -e "Minifying all $TYPE files recursively in $CURRENT_DIR $OK_DISPLAY"
for file in `find $CURRENT_DIR -name "*.$TYPE"`
do
# Get the current file directory
FILE_DIRECTORY=$(dirname $file)
# Get the basename of the current directory
BASE_DIR_NAME=`basename $FILE_DIRECTORY`
# Get the current file name
BASE_FILE_NAME=`basename $file`
MINIFIED_FILE_NAME=$BASE_FILE_NAME
# Minified directory path for the current file
MINIFIED_FILE_DIRECTORY="$FILE_DIRECTORY/"
MINIFIED_OUTPUT_FILE="$MINIFIED_FILE_DIRECTORY/$MINIFIED_FILE_NAME"
#echo -e "Compressing $file $OK_DISPLAY"
[ $TYPE == 'css' ] && \
csso $file --comments none -o $MINIFIED_OUTPUT_FILE && \
echo -e "Compressing $file $OK_DISPLAY"
[ $TYPE == 'js' ] && \
uglifyjs -o $MINIFIED_OUTPUT_FILE $file && \
echo -e "Compressing $file $OK_DISPLAY"
[ $TYPE == 'lua' ] && \
$LUA_MINIFIER $MINIFIED_OUTPUT_FILE $file && \
echo -e "Compressing $file $OK_DISPLAY"
[[ $TYPE =~ htm? || $TYPE == 'svg' ]] && \
html-minifier --remove-comments --minify-css true --minify-js true $file -o $MINIFIED_OUTPUT_FILE && \
echo -e "Compressing $file $OK_DISPLAY"
done
}
dl_and_install_prereq
# YUICompressor does not minify all files; csso and uglify work better!
minify css
minify js
# HTML minification. Remove comments only! Any other setting breaks LuCI GUI.
#minify htm
minify html
minify svg
# LuaPak breaks LuCI; LuaMinify works better!
minify lua
}
dl_and_install_build_prereq
generate_buildenv
cd openwrt
dl_and_install_feeds
minify_feeds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment