Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Rails 6.1, TailwindCSS 2.0 & PostCSS 8.0 setup.

Notifications You must be signed in to change notification settings

davidteren/rails61-tailwind2

Repository files navigation

Setting up TailwindCSS 2.0 with PostCSS 8

We will create a new app running Ruby on Rails 6.1 using Ruby 3.0.0 with TailwindCSS 2.0. In addition to the above we will setup the additional TailwindCSS plugins for forms, typography and aspect-ratio as well as the Inter var font. This setup also preps you app for use with TailwindUI.

This has been tested with Rails 6.1.0 running on Ruby 3.0.0.

readme_images/Untitled.png

readme_images/Untitled%201.png

Create a new Rails app.

rails new <app_name> -d postgresql

Once Bundle and Yarn have run take the steps below.

Change directories into the new project

cd <app_name>

Note at the time of writing Webpacker and PostCSS 8 are problematic so we will remove that from our app and point to the a known working release of Webpacker

Remove the Webpacker node package

yarn remove @rails/webpacker

Add the following node package

yarn add rails/webpacker#b6c2180

In your Gemfile, change the Webpacker gem

gem 'webpacker', '~> 5.0'

to

gem "webpacker", github: "rails/webpacker", ref: 'b6c2180'

Run bundler

bundle

Install TailwindsCSS, PostCSS, Autopfrefixer and the Tailwind plugins

yarn add tailwindcss postcss autoprefixer @tailwindcss/forms @tailwindcss/typography @tailwindcss/aspect-ratio

Now we need to run the following to create the application.scss in app/javascript/stylesheets

mkdir app/javascript/stylesheets && touch app/javascript/stylesheets/application.scss

In app/javascript/stylesheets/application.scss add the following

@import "tailwindcss/base";

@import "tailwindcss/components";

@import "tailwindcss/utilities";

In app/javascript/packs/application.js add the following

require("stylesheets/application.scss")

readme_images/Untitled%202.png

In postcss.config.js require TailwindCSS

require('tailwindcss'),  

readme_images/Untitled%203.png

Then add the stylesheet_pack_tag and Inter font to app/views/layouts/application.html.erb

<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<link rel="stylesheet" href="https://rsms.me/inter/inter.css"

readme_images/Untitled%204.png

Scaffold the TailwindCSS config.

npx tailwindcss init --full

Add the plugins to tailwind.config.js

require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
require('@tailwindcss/aspect-ratio'),

readme_images/Untitled%205.png

And add the Inter font to tailwind.config.js

'Inter var',

readme_images/Untitled%206.png

To test it's all working let's generate a page

rails g controller Home index

Add the following to app/views/home/index.html.erb

<div class="font-sans bg-white h-screen flex flex-col w-full">
	<div class="h-screen bg-gradient-to-r from-green-400 to-blue-500">
		<div class="px-4 py-48">
			<div class="relative w-full text-center">
				<h1
					class="animate-pulse font-bold text-gray-200 text-2xl mb-6">
				Your TailwindCSS setup is working if this pulses...
				</h1>
			</div>
		</div>
	</div>
</div>

Add the root path to point at the above /home/index in config/routes.rb

root 'home#index'

Now in your Terminal run

bin/webpack-dev-server

Then in another tab in your Terminal run

rails db:create db:migrate

Then start the Rails server

rails s

Open http://localhost:3000 in your browser

readme_images/CleanShot_2021-01-04_at_22.09.37.gif

You should see the above pulsing text effect.

About

Rails 6.1, TailwindCSS 2.0 & PostCSS 8.0 setup.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published