The term minification or compression means removing all unnecessary characters, such as spaces, new lines, comments without changing the functionality of the source code. At the development and debugging stage, these characters improve the code readability. However at the production stage, they become unnecessary for code execution but only increase the size of code to be transferred.
Before you start
Installing a minification tool
- To install UglifyJS, open the built-in WebStorm Terminal (Alt+F12) and type
npm install uglify-jsor
npm install uglify-js@<version>. Learn more from UglifyJS Official website.
- To install YUI Compressor, follow the instructions on YUI Compressor Official website.
- To install Closure Compiler, follow the instructions on Closure Compiler Official website.
Compressing the code
To compress your code on the fly, you need to configure the minification tool as a WebStorm File Watcher.
To create a File Watcher
- In the Settings/Preferences dialog (Ctrl+Alt+S), and click File Watchers under Tools. The File Watchers page opens.
- Click and choose the relevant File Watcher template from the list.
- In the New Watcher dialog that opens, specify the path to the relevant executable file or
.jararchive in the Program field:
compiler.jarfor Closure Compiler.
yuicompressor-<version>.jarfor YUI Compressor JS.
- Optionally, customize the behaviour of the File Watcher.
- Make sure the checkbox next to the File Watcher is selected, which indicates that the File Watcher is enabled.
Running a compressor