-
Notifications
You must be signed in to change notification settings - Fork 97
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
Conversation
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" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 |
- bundle add cssbundling-rails - bin/rails css:install:tailwind ref: rails/cssbundling-rails#164
@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 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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.
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. |
=================== 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
* 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
@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> |
This is a pull request based on #163
Tailwind CSS v4 has new configurations that are simpler than v3 and prior.
Summary of changes:
tailwind.config.js
file@tailwind
directives for normal@import "tailwindcss"
support.npx
and@tailwindcss/cli
to build cssLet me know if you see any red flags!