天天看点

用命令行对closure compiler进行封装调用

背景

部分JavaScript库只提供src版,min版及其map则需要自己生成。

为简化编译操作,我写了一个封装closure compiler的小批处理,以简化调用参数:

DIY

closurec.bat

@echo off

if exist %~dpnx1 (
        java -jar %~dp0compiler.jar --js_output_file %~n1.min%~x1 --create_source_map %~n1.min.map --js %~nx1
) else (
        echo file does not exist: "%~dpnx1"
)
           

要求:

1. 准备好Java环境,确保命令行窗口中java命令可用

2. 将closurec.bat和compiler.jar放同一目录下

2.1 若需在任意目录执行该脚本,将compiler.jar所在目录加入到环境变量PATH,或将上述两文件复制到已在环境变量PATH的某一目录

用法:

closurec.bat <jsfile>

例子:

C:\Users\Administrator\>closurec.bat jquery.js

将在jquery.js同目录下生成

jquery.min.js

jquery.min.map

该Windows Batch closurec.bat对应的Unix Shell closurec.sh将在后续更新中补上。或谁有需求,有时间,欢迎补充!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

上次承诺的更新

closurec.sh

#!/bin/sh

# args
pnx0=$0
p0=`dirname $pnx0`/

nx1="$1"
if [ $nx1 != "" ]; then
	if [ `dirname $nx1` == "." ]; then
		pnx1="$PWD/$nx1"
		p1="$PWD/"
		nx1="${nx1##*/}"
	else
		pnx1=$nx1
		p1=`dirname $nx1`/
		nx1=`basename $nx1`
	fi
	n1="${nx1%.*}"
	x1=".${nx1##*.}"
else
	echo "one argument required!"
	exit 1;
fi

# main
if [ -f "$pnx1" ]; then
	cd "$p1"
	java -jar "${p0}compiler.jar" --js_output_file "$n1.min$x1" --create_source_map "$n1.min.map" --js "$nx1"
else
	echo "file does not exist: $1"
	exit 1;
fi
           

继续阅读