Sunday, May 20, 2012

yui-compressor from rhino

I'm using rhino for a build dependency on a project I'm working on. Rhino is building a javascript application for me and part of that process is running yui-compressor - so this post is about calling yui-compressor from Rhino.

I couldn't get loadClass to accept the 'resolve' boolean so I've been reduced to the following to get something working:



 var COMPRESS = new Object();
    COMPRESS['yuicompress'] = ["-jar", JSLIBS+'/bin/yuicompressor-2.4.8pre.jar'];
var YUICOMPRESS_OPTIONS = [
  "--nomunge", //               Minify only, do not obfuscate
  "--preserve-semi", //          Preserve all semicolons
  "--disable-optimizations", //   Disable all micro optimizations
];

     var compressCMD = COMPRESS['yuicompress'];
    if (YUICOMPRESS_OPTIONS.length > 0) {
        if (YUICOMPRESS_OPTIONS.length == 1) {
            compressCMD.push(YUICOMPRESS_OPTIONS);
        }
        else {
            compressCMD.concat(YUICOMPRESS_OPTIONS);
        }
    }
    compressCMD.push([infile]);
    var options = new Array();
    options['args'] = compressCMD;
    options['output'] = "";
    runCommand("java", options);

    //print(options['output']);
    var w = new java.io.FileWriter(outfile);
    try
    {
        w.write(options['output']);
    }
    finally
    {
        w.close();
    }
    return;