js脚本如果不压缩很容易让人破解你的前端逻辑,网上各式各样的压缩工具有很多,我利用uglifyjs自己编写了一个意见压缩js的shell脚本。为了不影响我们开发者调试,就把压缩的js新保存为.min的js。
在这里要说明一下,需要在nginx中配置访问的时候注意,若不能访问到min的js就rewrite到未压缩的js。所以这里调试的时候只需要删除待min的js就可以了。话不多少,直接贴代码。
#!/bin/sh
# ex:uglifyjs -c -m --mangle-props true --pure-funcs console.log /data/deploy/jztz/jztz/js/bet-monitor.js
# ex java -jar /data/soft/yuicompressor-2.4.8.jar --type js --charset utf-8 /data/deploy/jztz/jztz/js/bet-monitor.js
folder=/data/deploy/$1
log_file=/data/scripts/run_uglifyjs.log
#CMD=uglifyjs
#OPT=" -c -m --mangle-props true --pure-funcs console.log "
#OPT=" -c --pure-funcs console.log "
CMD="java -jar /data/soft/yuicompressor-2.4.8.jar"
OPT="--type js --charset utf-8 "
echo "###################################"
echo "### folder: $folder"
echo "### cmd : $CMD"
echo "### opt : $OPT"
echo "###################################"
for file in `find $folder -name '*.js' ! -name "amazeui.js" ! -name "count.js" ! -name '*min*' ! -name '*jquery*' ! -path "*/webuploader-*" ! -path "*/mobile1.0/*" ! -path '*/jquery/*' ! -path '*/bootstrap*' ! -path '*/ckeditor*' ! -path '*/assets/*' ! -path '*datatable*' ! -path '*/layer-*' ! -path '*/mobiscroll-*' ! -path '*/echarts-*' ! -path '*/ckfinder*' ! -path '*/jquery-*' ! -path '*/ueditor/*' ! -path '*/ligerUI/*' ! -path '*/jqGrid/*' ! -path '*/layui/*'`
do
minfile=$file.min
if [ -f $minfile ];then
#判断 时间是否超过2小时,否则跳过,不重新生成
#time=`stat $file | grep Modif | awk -F':' '{print $2":"$3":"$4}'`
#timediff=`expr $now - $time`
#if [ $timediff -lt 7200 ] ;then
continue;
#fi
fi
linecount=`cat $file | wc -l`
if [ $linecount -gt 10 ] ;then
echo "`date +'%F %T'` $CMD -o $minfile $OPT ${file}" | tee -a $log_file
$CMD -o $minfile $OPT ${file} 2>&1 | tee -a $log_file
fi
done
if [ -n "$2" ] ;then
echo "`date +'%F %T'` compress css files" | tee -a $log_file
CMD="java -jar /data/soft/yuicompressor-2.4.8.jar"
OPT="--type css --charset utf-8 "
for file in `find $folder -name '*.css' ! -name '*min*' ! -name '*jquery*' ! -path "*/webuploader-*" ! -path "*/mobile1.0/*" ! -path '*/jquery/*' ! -path '*/bootstrap*' ! -path '*/ckeditor*' ! -path '*/assets/*' ! -path '*datatable*' ! -path '*/layer-*' ! -path '*/mobiscroll-*' ! -path '*/echarts-*' ! -path '*/ckfinder*' ! -path '*/jquery-*' ! -path '*/ueditor/*' ! -path '*/ligerUI/*' ! -path '*/jqGrid/*' ! -path '*/layui/*'`
do
minfile=$file.min
if [ -f $minfile ];then
continue;
fi
linecount=`cat $file | wc -l`
if [ $linecount -gt 10 ] ;then
echo "`date +'%F %T'` $CMD -o $minfile $OPT ${file}" | tee -a $log_file
$CMD -o $minfile $OPT ${file} 2>&1 | tee -a $log_file
fi
done
fi
chown nobody.nobody -R $folder