How To Use Google Closure Compiler to Compress JavaScript code
Closure Compiler is a JavaScript checker and optimizer. It checks syntax, variable references, types, and warns about common JavaScript pitfalls. This article shows you how to use it to compress JavaScript code. This makes our code run faster, smaller in size, etc.
1. Installation
You are able to download it on Closure Compiler site or use this direct link compiler-latest.zip.
Because it is written in Java, so ensure that you have installed JDK 7+ on your machine before running it.
2. Usage
Let’s assume you want to compress myapp.js
file.
Now run the following command:
java -jar closure-compiler.jar --js myapp.js --create_source_map myapp.min.js.map --js_output_file myapp.min.js
The command will create you the below new files:
myapp.min.js
myapp.min.js.map
If you want to learn more about other flags and options for the Closure Compiler, execute the jar with the --help
flag:
java -jar closure-compiler.jar --help
3. Q&A
Q: I am getting the below warning while compiling my javascript file:
ERROR - "arguments.callee" cannot be used in strict mode
A: That warning occurred because arguments.callee
is deprecated in ES5. So to solve that, you should add the --warning_level=QUIET
option to the command.