iTranslated by AI
Setting up a Rails 6.1 + Simpacker + PostCSS + Tailwind CSS Environment Using Only yarn
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 newunder docker-compose this time. Please check the following for more information: - Try Rails 6.1 + devise by running
rails newunder docker-compose | Junya Kitayama | zenn
Introducing Simpacker without webpack
Open your Gemfile and add the following:
...(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
{
"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.
- Try Rails 6.1 + devise by running
rails newunder docker-compose | Junya Kitayama | zenn
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:
module.exports = {
plugins: [
require('postcss-import'),
require('tailwindcss'),
require('postcss-nested'),
require('autoprefixer'),
],
}
I'm doing something similar here as well.
- Nov 2020 Edition: Building a TailwindCSS + PostCSS development environment with yarn | Junya Kitayama | zenn
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.
- Created a Node.js script that outputs manifest.json for Simpacker | Junya Kitayama | zenn
Prepare scripts in package.json
It looks like this:
{
"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.
- Deletes everything under
-
yarn postcss- Runs PostCSS transpilation and outputs to
./public/packs/css/.
- Runs PostCSS transpilation and outputs to
-
yarn output_manifest- Generates
public/packs/manifest.json.
- Generates
Prepare CSS using TailwindCSS
Prepare your CSS under app/assets/stylesheets as follows:
/* CSS to enable all tailwindcss classes. Useful during development. */
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
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.
<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" %>
- About the Rails link_to method | Qiita
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:
<!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.
- simpacker/lib/simpacker/helper.rb | hokaccha / simpacker | GitHub
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.
"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.
- Buttons | Tailwind CSS Starter Kit
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.
"files.associations": {
"*.html.erb": "html"
}
- Associating specific files with a language in VSCode | Qiita
- Not working in rails project (.erb files) | Issue #87 | tailwindlabs/tailwindcss-intellisense
Summary
For small projects, I like this approach because the setup is easier than using webpack.
Here is the repository for this project:
Discussion