From 3458b9ad82dca168df010939028f29de1b6d2a73 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Fri, 10 Jan 2020 02:39:16 +0800 Subject: [PATCH] Added a new script tool to compress JS. --- tools/js-compress.sh | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 tools/js-compress.sh diff --git a/tools/js-compress.sh b/tools/js-compress.sh new file mode 100755 index 0000000..60bfa2c --- /dev/null +++ b/tools/js-compress.sh @@ -0,0 +1,75 @@ +#!/bin/bash +# +# A development tool that use yuicompressor to compress JS files. +# +# +# Requirement: +# - wget +# - JRE +# - yuicompressor › https://github.com/yui/yuicompressor +# +# +# Usage: bash /path/to/js-compress.sh +# +# Process: +# input: /path/to/js/source.js --> output: /path/to/js/dist/source.min.js +# +# v2.0 +# https://github.com/cotes2020/jekyll-theme-chirpy +# © 2020 Cotes Chung +# MIT Licensed + + +set -eu + +PROJ_HOME=$(dirname $(dirname $(realpath "$0"))) + +YUICOMPRESSOR_SRC=https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar +YUICOMPRESSOR=${PROJ_HOME}/tools/package/yuicompressor-2.4.8.jar +JS_ROOT=${PROJ_HOME}/assets/js/ +JS_SRC=${JS_ROOT}_src # JS source files +JS_DEST=${JS_ROOT}dist # Compressed output directory +PREFIX_LEN=${#JS_ROOT} # To beautify the log + + +function init() { + if [[ ! -f $YUICOMPRESSOR ]]; then + if [[ ! -d "${PROJ_HOME}/tools/package/" ]]; then + mkdir -p "${PROJ_HOME}/tools/package/" + fi + wget "$YUICOMPRESSOR_SRC" -P "${PROJ_HOME}/tools/package/" -q + fi +} + +function compress() { + # $1 is the source dir + # $2 is the destination dir + # $3 is the sub dir of source dir, nullable + if [[ -z ${3:+unset} ]] + then + sub_dir="" + else + sub_dir="$3/" + fi + + for item in $(ls $1) + do + src="$1/$item" + if [[ -d "$src" ]]; then + compress $src $2 $item # recursion + else + if [[ ! -d "$2/${sub_dir}" ]]; then + mkdir -p $2/${sub_dir} + fi + output=$2/${sub_dir}${item%.*}.min.js + echo "java -jar $(basename $YUICOMPRESSOR) ${src:$PREFIX_LEN} -o ${output:$PREFIX_LEN}" + java -jar $YUICOMPRESSOR $src -o $output + fi + done + + sub_dir="" # clean up for next recursion. +} + +init + +compress $JS_SRC $JS_DEST