开发者

YUI-Compressor: result file is empty

I am using the YUI Compressor library to minify CSS and JavaScript files. I directly use the classes CssCompressor and JavaScriptCompressor.

Unfortunatly some of the resulting files are empty without any warnings or exceptions.

I already tried it with the versions:

  • yuicompressor-2.4.2.jar
  • yuicompressor-2.4.6.jar
  • yuicompressor-2.4.7pre.jar

My used code is:

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();
    }
}

Files which do not work (are empty afterwards) are e.g.

  • http://code.google.com/p/open-cooliris/source/browse/trunk/fancy/jquery.fancybox.css?r=2
  • http://nodejs.org/sh_main.js

I know there are some bugs within the YUICompressor using media but could this be in relation wi开发者_StackOverflow社区th the empty results?


I had the same problem. In my case it stemmed from that my javascript code was not ECMA valid (we use a variable named double which is not allowed according to the ECMA rules).

I did not have the courage to check if your js is valid but trying to compress different parts of your js file can easily lead you to the problem if it exists.


Well, after a while of debugging I figured out a solution.

The problem was not the YUI Compressor it self but it was the FileWriter given to the method.

  • Flushing an closing the FileWriter should solve the problem with empty result files
  • since I only need the minified String for further processing I now use a StringWriter with closing and flushing
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜