Using Closure Compiler in Java Code
The Closure Compiler is a tool for making JavaScript download and run faster. It is a true compiler for JavaScript. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript.
Closure Compiler does the following:
- parses your JavaScript
- analyzes it
- removes dead code
- rewrites and minimizes what’s left
- checks syntax, variable references, and types, and warns about common JavaScript pitfalls.
The following page discuss on how to use the Closure Compiler in your java code:
https://gist.github.com/jabley/1262250
However, there is another simpler way to get the job done. Just like how you invoke the closure compiler from command line, do the same thing, but provide the commandline arguments as a array of strings:
This is how you typically invoke it from command-line
java -jar compiler.jar --language_out ES5 --js input1.js input2.js --js_output_file output.min.js |
The following is another super easy way to use in your java code:
For the observant dev, it is obvious that all we are doing here is provide the array of arguments the same way we would provide in the command-line util. The errors and warnings while processing the js files will be written to the standard output.
//have the closure compilerjar on your classpath import com.google.javascript.jscomp.CommandLineRunner; String[] arguments = {"--language_out","ES5","--js","input1.js","input2.js","--js_output_file","output.min.js"}; CommandLineRunner.main(arguments); |
-Rushi