天天看點

用指令行對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
           

繼續閱讀