PhpStorm 2018.2 Help

Minifying CSS

You can minify CSS using the YUI Compressor tool right from PhpStorm. 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.

Installing and configuring the YUI Compressor

  • Open the embedded Terminal (View | Tool Windows | Terminal or Alt+F12), and type npm install yuicompressor at the command prompt.

Compressing the CSS code

To compress your code on the fly, you need to configure the compressor as a PhpStorm File Watcher.

To create a File Watcher

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), and then click File Watchers under Tools. The File Watchers page that opens, shows the list of File Watchers that are already configured in the project.

  2. Click the Add button or press Alt+Insert and choose the YUI Compressor CSS predefined template from the list.

  3. In the Program text box, specify the path to the yuicompressor-<version>.jar file. If you installed the tool through the Node Package Manager, PhpStorm locates the required file itself and fills in the field automatically. Otherwise, type the path manually or click Browse button and choose the file location in the dialog box that opens.

  4. Proceed as described on page Using File Watchers.

Minifying the code

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

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

Last modified: 21 November 2018

See Also