IntelliJ IDEA 2019.1 Help

Minifying JavaScript

IntelliJ IDEA integrates with Closure Compiler, YUI Compressor, and UglifyJS which lets you compress your JavaScript application sources on the fly.

Minification or compression means removing all unnecessary characters, such as spaces, new lines, comments without changing the functionality of the source code. During development and debugging, these characters make code easier to read. At the production stage they only increase the size of code to be transferred.

Before you start

  1. Download and install Node.js.

  2. If you are going to use YUI Compressor or Closure Compiler, download and install Java Runtime Environment (JRE):

    • JRE version 1.4 or higher for YUI Compressor.

    • JRE version 7 for Closure Compiler.

Installing a minification tool

  • To install UglifyJS, open the built-in IntelliJ IDEA Terminal (Alt+F12) and type npm install uglify-js or npm install uglify-js@<version>. Learn more from the 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 IntelliJ IDEA File Watcher.

To create a File Watcher

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), and click File Watchers under Tools. The File Watchers page opens.

  2. Click Add icon and choose the relevant File Watcher template from the list.

  3. In the New Watcher dialog that opens, specify the path to the relevant executable file or jar archive in the Program field:

    • compiler.jar for Closure Compiler.

    • yuicompressor-<version>.jar for YUI Compressor JS.

    • uglifyjs.cmd for UglifyJS.

  4. Optionally, customize the behaviour of the File Watcher.

  5. Make sure the checkbox next to the File Watcher is selected, which indicates that the File Watcher is enabled.

Running a compressor

When a minification File Watcher is enabled, minification starts automatically as soon as a JavaScript file in the File Watcher's scope is changed or saved.

IntelliJ IDEA creates a separate file with the generated output. The file has the name of the source JavaScript file and the extension min.js. The location of the generated file is defined in the Output paths to refresh field of the New Watcher dialog. However, in the Project Tree, by default it is shown under the source JavaScript file which is now displayed as a node. To change the default presentation, configure file nesting in the Project tool window.

Last modified: 20 June 2019

See Also