├── .github └── dependabot.yml ├── .gitignore ├── .gitmodules ├── minify.sh ├── package.json ├── readme.md └── renovate.json /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "21:00" 8 | open-pull-requests-limit: 10 9 | ignore: 10 | - dependency-name: clean-css-cli 11 | versions: 12 | - 5.0.0 13 | - 5.0.1 14 | - 5.1.0 15 | - 5.2.0 16 | - 5.2.1 17 | - 5.2.2 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "ColorEchoForShell"] 2 | path = ColorEchoForShell 3 | url = https://github.com/PeterDaveHello/ColorEchoForShell.git 4 | -------------------------------------------------------------------------------- /minify.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | MYPATH=$(dirname "$0") 3 | 4 | . "$MYPATH/ColorEchoForShell/dist/ColorEcho.bash" 5 | 6 | #determinate path to compress and generate map or not 7 | if [ -z "$1" ]; then 8 | TARGET='.' 9 | NP=false 10 | else 11 | if [ "$1" = "--no-map" ]; then 12 | TARGET='.' 13 | NP=true 14 | else 15 | TARGET="$1" 16 | if [ ! -z "$2" ] && [ "$2" = "--no-map" ]; then 17 | NP=true 18 | else 19 | NP=false 20 | fi 21 | fi 22 | fi 23 | 24 | types=(js css) 25 | #For storing the URL of API to minify the files 26 | declare -A urls 27 | urls[js]="https://javascript-minifier.com/raw" 28 | urls[css]="https://cssminifier.com/raw" 29 | 30 | echo.BoldCyan "Scaning direcotry..." 31 | 32 | #list all the directories except git directory 33 | for dir in $(find "$TARGET" -type d | grep -E -v '\.git(\/|$)'); do 34 | if [ ! -w "$dir" ]; then 35 | echo.BoldRed "$dir is not writable, ignore it." 36 | continue 37 | fi 38 | ( 39 | cd "$dir" || exit 40 | for filetype in "${types[@]}"; do 41 | echo.Green "Finding $filetype to be compressed under $dir ..." 42 | #list js/css files exclude already minified files 43 | for filename in $(ls *."$filetype" 2> /dev/null | sed "s/\.$filetype$//g" | grep -v '\.min$'); do 44 | do_min=0 45 | #check if exist a minified version 46 | if [ -f "${filename}.min.$filetype" ]; then 47 | #already exist a minified version, compare the modify time to decide compressing or not 48 | if [ "${filename}.min.${filetype}" -ot "${filename}.${filetype}" ]; then 49 | do_min=1 50 | fi 51 | elif [ "$(awk 'END{print NR}' "$filename.$filetype")" -ge 15 ] || [ "$(grep -Ec $'^(\t|\ )' "$filename.$filetype")" -ge 5 ]; then 52 | do_min=1 53 | fi 54 | MAP_OP="" 55 | if [ 0 -lt $do_min ]; then 56 | echo.Magenta "Compressing $filename.$filetype ..." 57 | if [ ! "$NP" = "true" ]; then 58 | MAP_OP="--source-map" 59 | fi 60 | if [ "$filetype" = "css" ]; then 61 | "$MYPATH/node_modules/clean-css-cli/bin/cleancss" --compatibility $MAP_OP --s0 -o "${filename}.min.$filetype" "$filename.$filetype" 62 | else 63 | "$MYPATH/node_modules/uglify-js/bin/uglifyjs" --mangle --compress if_return=true $MAP_OP -o "${filename}.min.$filetype" "${filename}.$filetype" || "$MYPATH/node_modules/uglify-es/bin/uglifyjs" --mangle --compress if_return=true $MAP_OP -o "${filename}.min.$filetype" "${filename}.$filetype" 64 | fi 65 | if [ ! $? -eq 0 ]; then 66 | echo.Red "local compressor failed, now try to compress with javascript-/cssminifier.com" 67 | curl -X POST -L --max-redirs 0 -sS -f -o "${filename}.min.$filetype" --data-urlencode "input@${filename}.$filetype" ${urls[$filetype]} 68 | fi 69 | fi 70 | done 71 | done 72 | ) 73 | done 74 | 75 | echo.BoldGreen "All done!" 76 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web-minify-helper", 3 | "version": "0.0.1", 4 | "description": "Help minify web assets", 5 | "dependencies": { 6 | "clean-css-cli": "^4.1.11", 7 | "uglify-es": "^3.3.10", 8 | "uglify-js": "^3.4.6" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/PeterDaveHello/web-minify-helper.git" 13 | }, 14 | "keywords": [ 15 | "web", 16 | "minify" 17 | ], 18 | "author": "Peter Dave Hello", 19 | "license": "GPL-2.0" 20 | } 21 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Web minify helper! 2 | 3 | [![Gitter chat](https://badges.gitter.im/PeterDaveHello/web-minify-helper.svg)](https://gitter.im/PeterDaveHello/web-minify-helper) 4 | [![dependencies Status](https://david-dm.org/PeterDaveHello/web-minify-helper/status.svg)](https://david-dm.org/PeterDaveHello/web-minify-helper) 5 | 6 | Let me help you minify css and js files automatically and easily! 7 | 8 | This is only a shell script; it depends on bash shell, grep, curl and nodejs/npm. 9 | 10 | It is based on [clean-css](https://github.com/jakubpawlowicz/clean-css), [uglify-js](https://github.com/mishoo/UglifyJS2) and [javascript-minifier.com](https://javascript-minifier.com)/[cssminifier.com](https://cssminifier.com). 11 | 12 | Feel free to contribute to the project if you want! 13 | 14 | ## Requirements 15 | 16 | - Bash shell 17 | - grep 18 | - curl 19 | - Node.js >= (4.0) 20 | 21 | ## How to use? 22 | 23 | ```sh 24 | $ git clone --recursive https://github.com/PeterDaveHello/web-minify-helper.git 25 | $ cd web-minify-helper 26 | $ npm install 27 | ``` 28 | 29 | First go to the target directory you want to minify, then run the script(`path_of_script/minify.sh`). Or you can pass the directory's path as a parameter to the script! 30 | 31 | You may even place the script file (or make a link to it) under the `$HOME/bin` directory, allowing you to conveniently run this script from any directory! 32 | 33 | ## Explanation 34 | 35 | The script will scan all the directories under the current working directory, except path with '.git'. 36 | 37 | Then, it will check all the js & css files to see if they already have a minified version (currently, it recognizes it by looking for a `.min` in the filename, before the file extension). 38 | 39 | If the file does not have a minified version, it will minify it. 40 | 41 | Furthermore, even if there is a minified version available, the script will compare the last modified time between the original file and its minified version. If the original one is newer than the minified one -- which means the minified file is older than the original -- then it will replace the old minified file with a new minified version! 42 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ] 5 | } 6 | --------------------------------------------------------------------------------