PyCharm 2020.1 Help

Minifying JavaScript

PyCharm integrates with Closure Compiler 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. Make sure you have Node.js on your computer.

  2. Make sure the File Watchers bundled plugin is enabled on the Plugins page, see Managing plugins for details.

  3. If you are going to use Closure Compiler, download and install Java Runtime Environment (JRE), version 7.

Installing a minification tool

Compressing the code

To compress your code automatically, you need to configure the minification tool as a PyCharm File Watcher.

To create a File Watcher

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to Tools | File Watchers.

  2. On the File Watchers page that opens, click Add icon and select 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.

    • 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

By default, minification starts as soon as a JavaScript file in the File Watcher's scope is changed and saved. You can specify other events that invoke the File Watcher.

The generated minified code is stored in a separate file with the name of the source JavaScript file and the extension min.js. The location of this generated file is defined in the Output paths to refresh field of the New Watcher dialog. However, in the Project Tree, the file with the minified code is shown under the source JavaScript file which is displayed as a node. To change this default presentation, configure file nesting in the Project tool window.

Last modified: 09 July 2020