YUI Compressor is a utility that can minify/compress JavaScript and/or Cascading Style Sheets to reduce the file payload size for web pages. Available in Java and .NET.
YUI Compressor
The Yahoo UI team created a utility program called YUI Compressor to help reduce the file sizes of their JavaScript and Cascading Style Sheets.
The idea spawned as part of the engineering process to help speed up the experience for users on the Yahoo! network.
The utility is designed in Java and is generally run using the commandline. The code is available on GitHub to the public.
Along with minification, it can also have the option to obfuscate any JavaScript code and/or Cascading Style Sheets.
On June 7th 2008, a .NET port of YUI Compressor was uploaded to CodePlex to help .NET developers deploy web applications while leverging the beauty of YUI Compressor without having to rely on having Java installed on the deployment machine.
Example of using YUI Compressor API for Java (taken from Teamextension):
public static void compressJavaScript(String inputFilename, String outputFilename, Options o) throws IOException {
Reader in = null;
Writer out = null;
try {
in = new InputStreamReader(new FileInputStream(inputFilename), o.charset);
JavaScriptCompressor compressor = new JavaScriptCompressor(in, new YuiCompressorErrorReporter());
in.close();
in = null;
out = new OutputStreamWriter(new FileOutputStream(outputFilename), o.charset);
compressor.compress(out, o.lineBreakPos, o.munge, o.verbose, o.preserveAllSemiColons, o.disableOptimizations);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}