{"id":579,"date":"2019-11-13T11:35:53","date_gmt":"2019-11-13T11:35:53","guid":{"rendered":"http:\/\/nicholshayes.co.uk\/blog\/?p=579"},"modified":"2019-12-19T15:19:09","modified_gmt":"2019-12-19T15:19:09","slug":"enabling-postcss-with-rails-6","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=579","title":{"rendered":"Enabling PostCSS with Rails 6"},"content":{"rendered":"\n<p><a href=\"http:\/\/tool for transforming CSS with JavaScript\">PostCSS<\/a> is a tool for transforming CSS with&nbsp;JavaScript. To enable it in a Rails application, I needed to make the following changes to my app. Please note that my app does not use turbolinks.<\/p>\n\n\n\n<p><strong>\/app\/views\/layouts\/application.html.erb<\/strong><\/p>\n\n\n\n<p> Update so that the style sheet directive in the header uses <em>stylesheet_pack_tag<\/em>. That is, replace:<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">&amp;lt;%= stylesheet_link_tag 'application', media: 'all' %&gt;<\/div><\/div>\n\n<\/pre>\n\n\n\n<p>With:<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">&amp;lt;%= stylesheet_pack_tag 'application', media: 'all' %&gt;<\/div><\/div>\n\n<\/pre>\n\n\n\n<p>Sorry  &#8211; opening brackets aren&#8217;t rendering nicely using the code include I&#8217;m using in this blog. Both declarations should start with a less than chevron.<\/p>\n\n\n\n<p><strong>\/package.json<\/strong><\/p>\n\n\n\n<p>Add postcss dependencies to package.json:<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;height:300px;\"><div class=\"text codecolorer\">{<br \/>\n&nbsp; &quot;name&quot;: &quot;my_app&quot;,<br \/>\n&nbsp; &quot;private&quot;: true,<br \/>\n&nbsp; &quot;dependencies&quot;: {<br \/>\n&nbsp; &nbsp; &quot;@rails\/actioncable&quot;: &quot;^6.0.0-alpha&quot;,<br \/>\n&nbsp; &nbsp; &quot;@rails\/activestorage&quot;: &quot;^6.0.0-alpha&quot;,<br \/>\n&nbsp; &nbsp; &quot;@rails\/ujs&quot;: &quot;^6.0.0-alpha&quot;,<br \/>\n&nbsp; &nbsp; &quot;@rails\/webpacker&quot;: &quot;^4.0.7&quot;,<br \/>\n&nbsp; &nbsp; &quot;polyfill-nodelist-foreach&quot;: &quot;^1.0.1&quot;,<br \/>\n&nbsp; &nbsp; &quot;postcss-browser-reporter&quot;: &quot;^0.6.0&quot;,<br \/>\n&nbsp; &nbsp; &quot;postcss-import&quot;: &quot;^12.0.1&quot;,<br \/>\n&nbsp; &nbsp; &quot;postcss-inline-svg&quot;: &quot;^4.1.0&quot;,<br \/>\n&nbsp; &nbsp; &quot;postcss-preset-env&quot;: &quot;^6.7.0&quot;,<br \/>\n&nbsp; &nbsp; &quot;postcss-reporter&quot;: &quot;^6.0.1&quot;,<br \/>\n&nbsp; &nbsp; &quot;postcss-svgo&quot;: &quot;^4.0.2&quot;<br \/>\n&nbsp; },<br \/>\n&nbsp; &quot;version&quot;: &quot;0.1.0&quot;,<br \/>\n&nbsp; &quot;devDependencies&quot;: {<br \/>\n&nbsp; &nbsp; &quot;webpack-dev-server&quot;: &quot;^3.9.0&quot;<br \/>\n&nbsp; }<br \/>\n}<\/div><\/div>\n\n<\/pre>\n\n\n\n<p><strong>\/postcss.config.js<\/strong><\/p>\n\n\n\n<p>Add the following content to a file called\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">postcss.config.js<\/div><\/div>\n\nin  the root of the app:<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;height:300px;\"><div class=\"text codecolorer\">module.exports = () =&gt; ({<br \/>\n&nbsp; plugins: [<br \/>\n&nbsp; &nbsp; require(&quot;postcss-import&quot;),<br \/>\n&nbsp; &nbsp; require(&quot;postcss-preset-env&quot;)({<br \/>\n&nbsp; &nbsp; &nbsp; autoprefixer: {},<br \/>\n&nbsp; &nbsp; &nbsp; features: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;focus-within&quot;: true,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;nesting-rules&quot;: true,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;color-mod-function&quot;: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unresolved: &quot;warn&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; },<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &quot;custom-properties&quot;: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; preserve: false,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; warnings: true<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; }),<br \/>\n&nbsp; &nbsp; require(&quot;postcss-browser-reporter&quot;),<br \/>\n&nbsp; &nbsp; require(&quot;postcss-reporter&quot;)<br \/>\n&nbsp; ]<br \/>\n});<\/div><\/div>\n\n<\/pre>\n\n\n\n<p>I believe that the rails webpacker stack already contains the PostCSS packages, but you may have to install them via yarn. I didn&#8217;t but then I have been trying to get this to work for a couple of days and may have installed them via another mechanism.<\/p>\n\n\n\n<p>With the the above changes in place, I was then able to load PostCSS files. Note that it was simplest to do this via the<em> javascript<\/em> folder as they needed to be accessed relative to packs <em>application.js<\/em> file. <\/p>\n\n\n\n<p>So as an example, I created the following file:<\/p>\n\n\n\n<p><strong>\/app\/javascript\/packs\/test.css<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">html,<br \/>\nbody {<br \/>\n&nbsp; background: lightyellow;<br \/>\n}<\/div><\/div>\n\n<\/pre>\n\n\n\n<p>Then to enable it I had to modify <em>\/app\/javascript\/packs\/application.js<\/em> by adding the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">import '.\/test.css'<\/div><\/div>\n\n<\/pre>\n\n\n\n<p>When I ran my app, the background turned yellow as expected.<\/p>\n\n\n\n<p>One thing to note is that once these changes have been put in place, the sass stylesheets in <em>\/app\/stylesheets<\/em> are no longer loaded by the app.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostCSS is a tool for transforming CSS with&nbsp;JavaScript. To enable it in a Rails application, I needed to make the following changes to my app. Please note that my app does not use turbolinks. \/app\/views\/layouts\/application.html.erb Update so that the style &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=579\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/579"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=579"}],"version-history":[{"count":6,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/579\/revisions"}],"predecessor-version":[{"id":587,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/579\/revisions\/587"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}