{"id":1733,"date":"2019-12-30T10:59:33","date_gmt":"2019-12-30T09:59:33","guid":{"rendered":"https:\/\/www.speich.net\/articles\/?p=1733"},"modified":"2019-12-30T11:48:05","modified_gmt":"2019-12-30T10:48:05","slug":"automatically-minify-css-with-phpstorm","status":"publish","type":"post","link":"https:\/\/www.speich.net\/articles\/en\/2019\/12\/30\/automatically-minify-css-with-phpstorm\/","title":{"rendered":"Minify CSS with PhpStorm automatically"},"content":{"rendered":"\n<p>Installation guide for PhpStorm 2019.3.1 to minify CSS files automatically after editing using the <a rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/css\/csso-cli\" target=\"_blank\">command line interface for CSSO<\/a> as a file watcher. You could also use <a rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/yui\/yuicompressor\" target=\"_blank\">yuicompressor<\/a> instead, but version 2.4.8 <a rel=\"noopener noreferrer\" href=\"http:\/\/chokes on CSS @keyframes rules\" target=\"_blank\">chokes on CSS @keyframes rules<\/a>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Install and integrate Node.js into PhpStorm<\/h2>\n\n\n\n<p>In&nbsp; case you have not already done so, download and install <a href=\"https:\/\/nodejs.org\/en\/\">Node.js<\/a> with the package manager <a href=\"https:\/\/www.npmjs.com\/get-npm\">npm<\/a> and integrate both into PhpStorm. See my post on <a href=\"https:\/\/www.speich.net\/articles\/en\/2016\/04\/10\/use-jsdoc-3-and-phpstorm-to-generate-javascript-documentation\/\">Use JSDoc 3 and PhpStorm to generate JavaScript documentation<\/a> for more detail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install the command line interface for CSSO<\/h2>\n\n\n\n<p>For some reason, the command line interface for <a href=\"https:\/\/github.com\/css\/csso\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"CSSO (opens in a new tab)\">CSSO<\/a> is not available via PhpStorm&#8217;s npm integration. Install it via the console:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>$ npm install -g csso-cli<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Setup the file watcher for csso-cli<\/h2>\n\n\n\n<p>Press Ctrl + Alt + S to bring the Settings window up in PhpStorm, select File Watchers under Tools, then click the + icon and choose the CSSO CSS Optimizer template:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"504\" src=\"https:\/\/www.speich.net\/wp\/wp-content\/uploads\/2019\/12\/screenshot-csso-cli-01-1024x504.png\" alt=\"\" class=\"wp-image-1751\" srcset=\"https:\/\/www.speich.net\/wp\/wp-content\/uploads\/2019\/12\/screenshot-csso-cli-01-1024x504.png 1024w, https:\/\/www.speich.net\/wp\/wp-content\/uploads\/2019\/12\/screenshot-csso-cli-01-300x148.png 300w, https:\/\/www.speich.net\/wp\/wp-content\/uploads\/2019\/12\/screenshot-csso-cli-01.png 1419w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Screenshot of PhpStorm&#8217;s File Watchers settings<br><\/figcaption><\/figure><\/div>\n\n\n\n<p>PhpStorm will prefill all the necessary information. Make sure the Program points to your installed csso-cli. In case the CSSO template was missing, just select &lt;custom&gt; and fill in the following information into the fields:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"784\" src=\"https:\/\/www.speich.net\/wp\/wp-content\/uploads\/2019\/12\/screenshot-csso-cli-02-1024x784.png\" alt=\"\" class=\"wp-image-1753\" srcset=\"https:\/\/www.speich.net\/wp\/wp-content\/uploads\/2019\/12\/screenshot-csso-cli-02-1024x784.png 1024w, https:\/\/www.speich.net\/wp\/wp-content\/uploads\/2019\/12\/screenshot-csso-cli-02-300x230.png 300w, https:\/\/www.speich.net\/wp\/wp-content\/uploads\/2019\/12\/screenshot-csso-cli-02.png 1094w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Screenshot of PhpStorm&#8217;s New Watcher Dialog<\/figcaption><\/figure><\/div>\n\n\n\n<p>That&#8217;s all there is. Every time you change a CSS file now, a minified version is created automatically. If you want to force the file watcher, you can bring it up by pressing: Ctrl + Shift + A, then start typing &#8220;run file watcher&#8221;.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Installation guide for PhpStorm 2019.3.1 to minify CSS files automatically after editing using the command line interface for CSSO as a file watcher. You could also use yuicompressor instead, but version 2.4.8 chokes on CSS @keyframes rules.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[248],"tags":[75],"class_list":["post-1733","post","type-post","status-publish","format-standard","hentry","category-cascading-style-sheets-css","tag-phpstorm","entry"],"_links":{"self":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/1733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/comments?post=1733"}],"version-history":[{"count":9,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/1733\/revisions"}],"predecessor-version":[{"id":2148,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/1733\/revisions\/2148"}],"wp:attachment":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/media?parent=1733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/categories?post=1733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/tags?post=1733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}