# Set up Tailwind CSS JIT in a Rails project to compile styles 20x faster

> How to switch to Tailwind CSS JIT when using Ruby on Rails and Webpacker.

- Date: 2021-04-06T00:00:00.000Z
- Authors: Vladimir Dementyev
- Categories: Rails
- URL: https://evilmartians.com/chronicles/set-up-tailwind-css-jit-in-a-rails-project-to-compile-styles-20x-faster

---

Learn how to switch to the latest and greatest just-in-time compilation feature from the freshly released [Tailwind CSS 2.1](https://github.com/tailwindlabs/tailwindcss/releases/tag/v2.1.0) in a modern Rails app with styles managed by Webpacker. Only two small commits are required to adopt an entirely different development experience.

[Tailwind CSS][tailwind] gained quite a lot of traction in the world of Ruby and Rails. We even have the official `tailwindcss-rails` [gem][tailwind-gem] blessed by DHH himself that you can use with good ol' Asset Pipeline if, for some reason, you stay away from Webpacker, ES6 modules, and processing CSS with webpack.

I joined the _Tailwind army_ about a year ago when I started working on a new [demo application for AnyCable][anycable-demo]. I was amazed by the powerful yet simple development flow: fast prototyping via [Tailwind Play][], _componentization_ via `@apply`, and the built-in [PurgeCSS](https://purgecss.com/) integration for smaller compiled CSS files.

Everything seemed perfect except for the one thing: a bit slower re-compilation in development (using `webpack-dev-server`) than I expected. The reason was that Webpack had to recompile all the Tailwind CSS styles whenever _anything changed_ in the bundle. You can read more about the problem and the possible workaround [in this article](https://rubyyagi.com/solve-slow-webpack-compilation/).

Tailwind team [knew about the problem](https://github.com/tailwindlabs/tailwindcss/issues/443), and they came up with an elegant solution—they've built an [entire just-in-time compiler](https://tailwindcss.com/docs/just-in-time-mode) for utility classes!

Watch [this video](https://www.youtube.com/watch?v=3O_3X7InOw8) to learn more.

[Tailwind CSS JIT][tailwind-jit] is just out of the labs and still has the _experimental_ status, but for my small demo project it has already proved to be useful. Take a look at the live reload times before and after switching to JIT:

2.5MB of CSS takes up to 4.5 seconds to compile, which is no way to live. Now see below for how much we can gain after switching to just-in-time compilation.

> Under 50 KB and the range of 100-200 ms! Now we are cooking with gas!

Now, let's see how to make it happen in your code.

## How to use Tailwind CSS JIT in a Rails app?

- **[Commit 1](https://github.com/anycable/anycable_rails_demo/commit/c8c4fea833e3f2a80ac8593c8d5bb84ddc6c1e32)**: Upgrade to Webpacker 6 (still in beta). I followed [the official guide](https://github.com/rails/webpacker/blob/master/docs/v6_upgrade.md). The only thing that I had to add was the `ENV["WEBPACK_DEV_SERVER"] = "true"` line in my `bin/webpack-dev-server` (it seems to be already [fixed](https://github.com/rails/webpacker/commit/f56f8adf21a0238b9102cb6e4a20e7e18f5d3f28), but hasn't been released yet).

- **[Commit 2](https://github.com/anycable/anycable_rails_demo/commit/26fd6443d554334935f616e063b850d656607360)**: Upgrade to Tailwind CSS 2.1 and [PostCSS 8.0](https://evilmartians.com/chronicles/postcss-8-plugin-migration) using [this guide](https://tailwindcss.com/docs/upgrading-to-v2#install-tailwind-css-v2-0-and-post-css-8). The final stroke is to turn on JIT mode in the `tailwind.config.js`:

```diff
 // tailwind.config.js
 module.exports = {
+   mode: 'jit',
    purge: {
```

That's it! My development server is now reloading instantly.

## System tests gotcha and workaround

I then tried to run my system tests that call `asset:precompile`, and the compilation froze forever. I tried to run `bin/webpack` and found that assets are successfully getting built, but the process doesn't exit 🤔.

After digging through issues on GitHub, I found something relevant: [Gulp task not finishing](https://github.com/tailwindlabs/tailwindcss-jit/issues/195). It turned out that Tailwind JIT automatically watches for files when `NODE_ENV=development` and does this in the _foreground_ (that's why the process freezes). The solution is simple: add a `TAILWIND_MODE=build` environment variable in the context of your bundler. Luckily, with Webpacker, there's a clear API to set that variable just in a context of a compiler and forget about it:

```ruby
# config/application.rb
Webpacker::Compiler.env["TAILWIND_MODE"] = "build"
```

Now everything works! (And that single line is the only reason I wrote this short post 🙂)

---

[Talk to us](https://evilmartians.com/contact-us) if you want to supercharge your digital products (including but not limited to Rails applications) and improve the development experience for your team with our help.

[tailwind]: https://tailwindcss.com
[tailwind-gem]: https://github.com/rails/tailwindcss-rails
[anycable-demo]: https://github.com/anycable/anycable_rails_demo
[tailwind-jit]: https://tailwindcss.com/docs/just-in-time-mode#enabling-jit-mode
[tailwind play]: https://play.tailwindcss.com

---

This article was written by [Evil Martians](https://evilmartians.com), a product development consultancy trusted by teams from early-stage startups to public companies. If you're working through a hard problem, [we can help](https://evilmartians.com/contact-us)
