iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦾

Setting up a Rails 6.1 + Simpacker + PostCSS + Tailwind CSS Environment Using Only yarn

に公開
2021/02/10

It's not that I hate module bundlers.

TL;DR

  • I'm just going to try running a Rails 6.1 project that incorporates the following article:
  • Created a Node.js script that outputs manifest.json for Simpacker | Junya Kitayama | zenn
  • I will skip the details of Rails setup or how to run rails new under docker-compose this time. Please check the following for more information:
  • Try Rails 6.1 + devise by running rails new under docker-compose | Junya Kitayama | zenn

Introducing Simpacker without webpack

Open your Gemfile and add the following:

Gemfile
...(omitted)
group :development do
    gem "simpacker"  # Add this under group :development do
...(omitted)

In the app container's bash (or your local terminal if you've set up Rails locally):

bundle install
bundle exec rails simpacker:install
...
Simpacker successfully installed 🎉 🍰

We will remove webpack. For now, empty the devDependencies in package.json.

package.json
package.json
{
  "private": true,
  "devDependencies": {
  }
}

Since rails simpacker:install adds things via npm, we will delete unnecessary files and run yarn again.
In your terminal:

rm -rf node_modules/
rm package-lock.json
rm webpack.config.js
yarn install

This sets up Simpacker without any extra packages.

I'm doing the same thing here. It might be easier to follow the steps here when working under docker-compose.

Introducing TailwindCSS + PostCSS

Run the following in your terminal:

yarn add tailwindcss
yarn run tailwindcss init -p
yarn add postcss postcss-cli postcss-import postcss-nested --dev

Configure your postcss.config.js like this:

postcss.config.js
module.exports = {
  plugins: [
    require('postcss-import'),
    require('tailwindcss'),
    require('postcss-nested'),
    require('autoprefixer'),
  ],
}

I'm doing something similar here as well.

Prepare a Node.js script to output manifest.json for Simpacker

Place this file in the project root with the name output_manifest_js.js.

https://gist.github.com/JUNKI555/8f5a3616be630840b65dbb13769c51c3

Prepare scripts in package.json

It looks like this:

package.json
{
  "private": true,
  "scripts": {
    "cleancss": "rimraf ./public/packs/css/",
    "postcss": "postcss ./app/assets/stylesheets/**/*.css -d ./public/packs/css/",
    "output_manifest": "node output_manifest_js.js public"
  },
  "devDependencies": {
    "autoprefixer": "^10.0.4",
    "rimraf": "^3.0.2",
    "postcss": "^8.1.14",
    "postcss-cli": "^8.3.0",
    "postcss-import": "^13.0.0",
    "postcss-nested": "^5.0.2",
    "tailwindcss": "^2.0.1",
    "yarn-run-all": "^3.1.1"
  }
}

*If there are any differences between your local package.json and the devDependencies at this point, please run yarn install after editing as shown above.

You can use the scripts like this:

  • yarn cleancss
    • Deletes everything under ./public/packs/css/. Use this when you change file locations.
  • yarn postcss
    • Runs PostCSS transpilation and outputs to ./public/packs/css/.
  • yarn output_manifest
    • Generates public/packs/manifest.json.

Prepare CSS using TailwindCSS

Prepare your CSS under app/assets/stylesheets as follows:

app/assets/stylesheets/tailwindcss.css
/* CSS to enable all tailwindcss classes. Useful during development. */
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
app/assets/stylesheets/application.css
nav {
  /* Use @apply to group tailwiindcss classes together */
  @apply py-2 border-b-4 border-pink-400;

  .nav-button {
    @apply h-12 px-3 py-2 m-2 text-indigo-100 transition-colors duration-150 bg-indigo-700 rounded-lg hover:bg-indigo-800;
  }
}

You can also apply CSS to Rails ERB files.

app/views/home/index.html.erb
<h1 class="m-2 w-3/12 border-8 border-pink-400">Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>

For link_to, it looks like this:

<%= link_to "Link Text", "Path", class: "class-name" %>

Load CSS with stylesheet_pack_tag

For example, in app/views/layouts/application.html.erb, you can load the CSS prepared above using stylesheet_pack_tag like this:

app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title>App</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_pack_tag "application", media: "all" %>
    <%= stylesheet_pack_tag "tailwindcss", media: "all" %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

Simpacker uses the stylesheet_pack_tag helper to generate links to the corresponding files from the prepared public/packs/manifest.json.

Execute PostCSS Transpilation

All that's left is to edit the CSS as needed and transpile it each time.
In your terminal:

yarn postcss
yarn output_manifest

If you find it tedious to run transpilation every time, you can add the -w option to the postcss script in package.json to watch for changes.

package.json
    "postcss": "postcss ./app/assets/stylesheets/**/*.css -d ./public/packs/css/ -w",

(Personally, I prefer transpiling manually each time.)

All set! Let's try running it.

For example, if you apply styles to a devise practice app like this, the appearance will change as follows.

From this:

To this!

I referred to this for the button styles. TailwindCSS is very convenient.

Supplement: Enabling TailwindCSS VSCode Extension in ERB

Add the following to your settings.json to have VSCode treat .erb files as HTML. Don't forget to restart VSCode after setting this.

settings.json
    "files.associations": {
        "*.html.erb": "html"
    }

Summary

For small projects, I like this approach because the setup is easier than using webpack.

Here is the repository for this project:
https://github.com/JUNKI555/rails_authentication_practice01

Discussion