Skip to content

Update for Tailwind CSS v4 #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2025
Merged

Update for Tailwind CSS v4 #164

merged 2 commits into from
Feb 24, 2025

Conversation

justalever
Copy link
Contributor

@justalever justalever commented Jan 24, 2025

This is a pull request based on #163

Tailwind CSS v4 has new configurations that are simpler than v3 and prior.

Summary of changes:

  • Removes tailwind.config.js file
  • Swaps legacy @tailwind directives for normal @import "tailwindcss" support.
  • Uses npx and @tailwindcss/cli to build css
  • Updates README

Let me know if you see any red flags!

Tailwind CSS v4 comes with new configurations and build scripts. This updates the installer for Tailwind to include those changes.
@@ -2,6 +2,6 @@
"name": "app",
"private": "true",
"scripts": {
"build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css"
"build:css": "npx @tailwindcss/cli -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really necessary to run npx if you add @tailwindcss/cli as a depedency?
Also, would this also be a place to use bunx if using bun?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, something like npx or bunx is necessary to run the new @tailwindcss/cli package. The Tailwind docs have this, so I was going off that.

To be honest, I'm not sure this package.json file is necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary because cssbundling-rails and jsbundling-rails are enhancing some rake tasks like precompile:assets to call this build and build:css task present in the package.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@navidemad I mean the package.json file inside the lib/install/tailwind directory. I see no reference to it in rake tasks or elsewhere. I could be mistaken, of course.

Copy link

@gabrielso gabrielso Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is that by using npx directly in the script, you are assuming Node.js is installed, which is not true if you are, for instance, developing on a Dev Container with Bun instead of Node.js.

I see you added @tailwindcss/cli as a dependency... and the install process seems to generate the script using the correct command but it's not clear what this package.json file actually does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it's not clear what this package.json file actually does.

Correct, this legacy file was added prior to this pull request. I updated it to match the main one that gets copied. I'm not sure we need this file at all. I am in favor of removing it unless there are objections.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless there are objections

Objection, your honor 🧑‍⚖️😅

If you are using importmaps, then package.json is indeed not needed (and thus it's not even included in the generated code of a new 'omakase' app).

But if you are using a JS bundler (-j bun|webpack|esbuild|rollup), you need package.json. That's where yarn keeps its dependencies, and even commands referenced in Procfile.dev.

So yeah, please don't drop it.

Copy link
Contributor Author

@justalever justalever Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if you are using a JS bundler (-j bun|webpack|esbuild|rollup), you need package.json. That's where yarn keeps its dependencies, and even commands referenced in Procfile.dev.

Thanks for chiming in! There's already one that gets copied at the root of the lib/install directory and is appended to via the tailwind installer task when running the installer. So I'm confused by the need for the additional package.json file inside lib/install/tailwind as referenced here.

@akimthedream
Copy link

Is there anything i can help with to advance with this PR? Is there anything missing? For me, all the changes are working. Hope to see rails new ... -css tailwind running smooth again soon 😊

saboyutaka added a commit to shunjuio/gql-chat that referenced this pull request Feb 13, 2025
- bundle add cssbundling-rails
- bin/rails css:install:tailwind

ref: rails/cssbundling-rails#164
@richigonzalez
Copy link

@justalever I applied these changes to a new rails 8 app (rails new test-tailwind --css=tailwind --javascript=esbuild), and while tailwind now works, it is trying to fetch http://localhost:3000/assets/tailwindcss, which of course fails:
image

Btw, thank you for this, @justalever, I had already spent about 10 hours trying to understand why it didn't work.

@@ -12,8 +12,7 @@ This also happens in testing where the bundler attaches to the `test:prepare` ta

That's it!

You can configure your bundler options in the `build:css` script in `package.json` or via the installer-generated `tailwind.config.js` for Tailwind or `postcss.config.js` for PostCSS.

You can configure your bundler options in the `build:css` script in `package.json` or `postcss.config.js` for PostCSS.
Copy link

@trinitytakei trinitytakei Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there even a postcss.config.js now? In my understanding, it was dropped in v4 (as a separate package) and merged into tailwind itself?

Copy link
Contributor Author

@justalever justalever Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can install postcss independently with cssbundling-rails (bin/rails css:install:postcss)

@tailwind base;
@tailwind components;
@tailwind utilities;
@import "tailwindcss";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be useful to open a separate PR just for this change, in case #164 takes a while to get merged.

This would be useful right away, regardless of how you install tailwind. Those installation woes can be worked around in various ways, but this one can't be - you just have to replace it every time, all the time.

The only question is: do we want to be backward compatible with 3.x? I don't think so (since all the generators I'm aware of are using tailwindcss@latest, which is 4.x now)

However, if backward compatibility is not a concern, I don't see why this couldn't be accepted right away as a separate PR.

@justalever
Copy link
Contributor Author

@justalever I applied these changes to a new rails 8 app (rails new test-tailwind --css=tailwind --javascript=esbuild), and while tailwind now works, it is trying to fetch http://localhost:3000/assets/tailwindcss, which of course fails: image

Btw, thank you for this, @justalever, I had already spent about 10 hours trying to understand why it didn't work.

This looks like something from the tailwindcss-rails gem, no? That writes to a different directory in the stylesheets folder as far as I'm aware.

trinitytakei pushed a commit to trinitytakei/inco-0 that referenced this pull request Feb 23, 2025
===================
Command line flags:
===================

-j esbuild -c tailwind

============
Ingredients:
============

* amazing print (https://alpha.railsnew.io/ingredients/276395358)

  Amazing print

* Built-in Rails 8 Authentication (https://alpha.railsnew.io/ingredients/276395359)

  Generates Rails Authentication

* Tailwindcss forms (https://alpha.railsnew.io/ingredients/276395360)

  Make TailwindCSS Forms nicer!

* Tailwind 4.0 incompatibility fixes (https://alpha.railsnew.io/ingredients/276395361)

  At the time of writing, running this template is necessary to fix the way tailwindcss is imported.

It will be dropped once this PR gets merged: rails/cssbundling-rails#164
trinitytakei pushed a commit to trinitytakei/inco-0 that referenced this pull request Feb 23, 2025
* Tailwind 4.0 incompatibility fixes (https://alpha.railsnew.io/ingredients/276395361)

  At the time of writing, running this template is necessary to fix the way tailwindcss is imported.

It will be dropped once this PR gets merged: rails/cssbundling-rails#164
@dhh dhh merged commit df70d23 into rails:main Feb 24, 2025
@jethrodaniel
Copy link

@justalever I applied these changes to a new rails 8 app (rails new test-tailwind --css=tailwind --javascript=esbuild), and while tailwind now works, it is trying to fetch http://localhost:3000/assets/tailwindcss, which of course fails: image

Btw, thank you for this, @justalever, I had already spent about 10 hours trying to understand why it didn't work.

@richigonzalez You can fix that issue like so:

diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 9c6c431..542ca72 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -17,8 +17,7 @@
     <link rel="icon" href="/icon.svg" type="image/svg+xml">
     <link rel="apple-touch-icon" href="/icon.png">
 
-    <%# Includes all stylesheet files in app/assets/stylesheets %>
-    <%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
+    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
     <%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %>
   </head>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants