Google 的 Closure-Compiler 压缩 JavaScript 文件默认采用 UTF-8 作为输入编码,US_ASCII 作为输出编码。
–charset VAL : Input and output charset for all files
. By default, we accept UTF-8 as input
and output US_ASCII
因此,压缩汉字(或者日韩文字)之后,文件会变大。
执行命令 java -jar $dir/compiler.jar --js $cache_file --js_output_file $output_file
// 输入文件 var address = '上海'; // 输出文件 var address="\u4e0a\u6d77";
源文件较小时可以忽略这个问题,但源文件有大量汉字,可以指定 charset 避免输出文件过大。
执行命令 java -jar $dir/compiler.jar --js $cache_file --js_output_file $output_file --charset=UTF-8
// 输入文件 var address = '上海'; // 输出文件 var address="上海";
在开发中遇到一个 1.5MB 的地址文件,压缩之后变成 3MB,再经 gzip 压缩变成 780 KB 左右。而源文件经 gzip 之后在 590 KB。