Skip to content

Instantly share code, notes, and snippets.

@oberhamsi
Created November 5, 2010 08:11
Show Gist options
  • Save oberhamsi/663804 to your computer and use it in GitHub Desktop.
Save oberhamsi/663804 to your computer and use it in GitHub Desktop.
var YUIC = com.yahoo.platform.yui.compressor;
var YUIJ = com.yahoo.platform.yui.org.mozilla.javascript;
var fakeOutStream = new Packages.java.io.Writer({
data: [],
write : function(text) { this.data.push(text);},
readAll : function() { return this.data.join("\n\n");},
});
var errorReporter = new YUIJ.ErrorReporter({
warning: function(message, sourceName, line, lineSource, lineOffset) {
//error("WARN " + message);
},
error: function(message, sourceName, line, lineSource, lineOffset) {
//error("ERROR " + message);
},
runtimeError : function(message, sourceName, line, lineSource, lineOffset) {
fatal("YuiCompressor._compress", "JavaScript compilation error in " + sourceName + ", " + message);
},
});
var inFileSuccess = fileSet.ins.every(function(inFile) {
var inputFilename = YuiCompressor.STATIC_DIR + inFile;
var sourceCharset = app.properties.sourceCharset || "ISO-8859-15";
try {
var inStream = new Packages.java.io.InputStreamReader(new Packages.java.io.FileInputStream(inputFilename), sourceCharset);
} catch (e) {
error("YuiCompressor._compresss", "Can't open input " + inFile + " of " + fileSet.title, e);
return false;
}
if (fileSet.type === "js") {
var jsCompressor = new YUIC.JavaScriptCompressor(inStream, errorReporter);
// linebreak, munge, verbose, preserverAllSemis, disableOptimize
jsCompressor.compress(fakeOutStream, 80, true, true, true, true);
} else if (fileSet.type === "css") {
var cssCompressor = new YUIC.CssCompressor(inStream);
cssCompressor.compress(fakeOutStream, 80);
} else {
error("YuiCompressor._compress", "Unknown filetype, canceling. fileSet " + fileSet.title);
return false;
}
inStream.close();
return true;
}, this);
var outFileSuccess = false;
if (inFileSuccess) {
var dataCompressed = fakeOutStream.readAll();
var outFilename = YuiCompressor.STATIC_DIR + fileSet.out;
var outStream = new helma.File(outFilename);
outStream.remove();
outStream.open();
// helma.File doesn't throw exceptions
outFileSuccess = outStream.write(dataCompressed);
if (outFileSuccess == false) {
fatal("YuiCompressor._compress", "Can't write output " + outFilename);
}
outStream.flush();
outStream.close();
}
return inFileSuccess && outFileSuccess;
@hns
Copy link

hns commented Nov 5, 2010

ah, nice :)

@oberhamsi
Copy link
Author

I just ripped this out of our helma code :) might be useful.. the java API has zero documentation

@hns
Copy link

hns commented Nov 5, 2010

google closure compiler java api is fully documented, but complex as hell:
http://closure-compiler.googlecode.com/svn/trunk/javadoc/index.html

@oberhamsi
Copy link
Author

... and like yuicompiler it comes with its own rhino.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment