我正在使用 YUI Compressor 库来缩小 CSS 和 JavaScript 文件。我直接使用 CssCompressor 和 JavaScriptCompressor 类。
不幸的是,一些生成的文件是空的,没有任何警告或异常。
我已经尝试过以下版本:
- yuicompressor-2.4.2.jar
- yuicompressor-2.4.6.jar
- yuicompressor-2.4.7pre.jar
我使用的代码是:
public static void compress(File file) {
try {
long start = System.currentTimeMillis();
File targetFile = new File("results", file.getName() + ".min");
Writer writer = new FileWriter(targetFile);
if (file.getName().endsWith(".css")) {
CssCompressor cssCompressor = new CssCompressor(new FileReader(file));
cssCompressor.compress(writer, -1);
} else if (file.getName().endsWith(".js")) {
JavaScriptCompressor jsCompressor = new JavaScriptCompressor(new FileReader(file), new MyErrorReporter());
jsCompressor.compress(writer, -1, true, false, false, true);
}
long end = System.currentTimeMillis();
System.out.println("\t compressed " + file.getName() + " within " + (end - start) + " milliseconds");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
不起作用的文件(之后为空)例如
- http://code.google.com/p/open-cooliris/source/browse/trunk/fancy/jquery.fancybox.css?r=2
- http://nodejs.org/sh_main.js
我知道使用媒体的 YUICompressor 中存在一些错误,但这可能与空结果有关吗?