|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# A development tool that use yuicompressor to compress JS files. |
| 4 | +# |
| 5 | +# |
| 6 | +# Requirement: |
| 7 | +# - wget |
| 8 | +# - JRE |
| 9 | +# - yuicompressor › https://github.com/yui/yuicompressor |
| 10 | +# |
| 11 | +# |
| 12 | +# Usage: bash /path/to/js-compress.sh |
| 13 | +# |
| 14 | +# Process: |
| 15 | +# input: /path/to/js/source.js --> output: /path/to/js/dist/source.min.js |
| 16 | +# |
| 17 | +# v2.0 |
| 18 | +# https://github.com/cotes2020/jekyll-theme-chirpy |
| 19 | +# © 2020 Cotes Chung |
| 20 | +# MIT Licensed |
| 21 | + |
| 22 | + |
| 23 | +set -eu |
| 24 | + |
| 25 | +PROJ_HOME=$(dirname $(dirname $(realpath "$0"))) |
| 26 | + |
| 27 | +YUICOMPRESSOR_SRC=https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar |
| 28 | +YUICOMPRESSOR=${PROJ_HOME}/tools/package/yuicompressor-2.4.8.jar |
| 29 | +JS_ROOT=${PROJ_HOME}/assets/js/ |
| 30 | +JS_SRC=${JS_ROOT}_src # JS source files |
| 31 | +JS_DEST=${JS_ROOT}dist # Compressed output directory |
| 32 | +PREFIX_LEN=${#JS_ROOT} # To beautify the log |
| 33 | + |
| 34 | + |
| 35 | +function init() { |
| 36 | + if [[ ! -f $YUICOMPRESSOR ]]; then |
| 37 | + if [[ ! -d "${PROJ_HOME}/tools/package/" ]]; then |
| 38 | + mkdir -p "${PROJ_HOME}/tools/package/" |
| 39 | + fi |
| 40 | + wget "$YUICOMPRESSOR_SRC" -P "${PROJ_HOME}/tools/package/" -q |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +function compress() { |
| 45 | + # $1 is the source dir |
| 46 | + # $2 is the destination dir |
| 47 | + # $3 is the sub dir of source dir, nullable |
| 48 | + if [[ -z ${3:+unset} ]] |
| 49 | + then |
| 50 | + sub_dir="" |
| 51 | + else |
| 52 | + sub_dir="$3/" |
| 53 | + fi |
| 54 | + |
| 55 | + for item in $(ls $1) |
| 56 | + do |
| 57 | + src="$1/$item" |
| 58 | + if [[ -d "$src" ]]; then |
| 59 | + compress $src $2 $item # recursion |
| 60 | + else |
| 61 | + if [[ ! -d "$2/${sub_dir}" ]]; then |
| 62 | + mkdir -p $2/${sub_dir} |
| 63 | + fi |
| 64 | + output=$2/${sub_dir}${item%.*}.min.js |
| 65 | + echo "java -jar $(basename $YUICOMPRESSOR) ${src:$PREFIX_LEN} -o ${output:$PREFIX_LEN}" |
| 66 | + java -jar $YUICOMPRESSOR $src -o $output |
| 67 | + fi |
| 68 | + done |
| 69 | + |
| 70 | + sub_dir="" # clean up for next recursion. |
| 71 | +} |
| 72 | + |
| 73 | +init |
| 74 | + |
| 75 | +compress $JS_SRC $JS_DEST |
0 commit comments