Skip to content

Commit df70d23

Browse files
authored
Update for Tailwind CSS v4 (#164)
* Update for Tailwind CSS v4 Tailwind CSS v4 comes with new configurations and build scripts. This updates the installer for Tailwind to include those changes. * Add support for either bunx or npx
1 parent 1235694 commit df70d23

File tree

6 files changed

+18
-20
lines changed

6 files changed

+18
-20
lines changed

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ This also happens in testing where the bundler attaches to the `test:prepare` ta
1212

1313
That's it!
1414

15-
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.
16-
15+
You can configure your bundler options in the `build:css` script in `package.json` or `postcss.config.js` for PostCSS.
1716

1817
## Installation
1918

@@ -34,7 +33,13 @@ In Rails 7+, you can preconfigure your new application to use `tailwindcss-rails
3433

3534
### How do I import relative CSS files with Tailwind?
3635

37-
If you want to use `@import` statements as part of your Tailwind application.js file, you need to [configure Tailwind to use `postcss` and then `postcss-import`](https://tailwindcss.com/docs/using-with-preprocessors#build-time-imports). But you should also consider simply referring to your other CSS files directly, instead of bundling them all into one big file. It's better for caching, and it's simpler to setup. You can refer to other CSS files by expanding the `stylesheet_link_tag` in `application.html.erb` like so: `<%= stylesheet_link_tag "application", "other", "styles", "data-turbo-track": "reload" %>`.
36+
Tailwind CSS 4 is configured using native CSS. Instead of bundling all your CSS into a single file, consider referencing individual CSS files directly. This approach simplifies setup and improves caching performance. To reference multiple CSS files in Rails, update the stylesheet_link_tag in application.html.erb like this:
37+
38+
```erb
39+
<%= stylesheet_link_tag "application", "other", "styles", "data-turbo-track": "reload" %>
40+
```
41+
42+
This ensures your files are properly linked and ready to use.
3843

3944
### How do I avoid SassC::SyntaxError exceptions on existing projects?
4045

@@ -58,7 +63,7 @@ Rails.application.config.assets.css_compressor = nil
5863
Watch out - if you precompile your files locally, those will be served over the dynamically created ones you expect. The solution:
5964

6065
```shell
61-
rails assets:clobber
66+
rails assets:clobber
6267
```
6368

6469
### How do I include 3rd party stylesheets from `node_modules` in my bundle?

lib/install/helpers.rb

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def bundler_run_cmd
99
using_bun? ? "bun run" : "yarn"
1010
end
1111

12+
def bundler_x_cmd
13+
using_bun? ? "bunx" : "npx"
14+
end
15+
1216
def using_bun?
1317
File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
1418
end
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
@tailwind base;
2-
@tailwind components;
3-
@tailwind utilities;
1+
@import "tailwindcss";

lib/install/tailwind/install.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
apply "#{__dir__}/../install.rb"
55

6-
say "Install Tailwind (+PostCSS w/ autoprefixer)"
7-
copy_file "#{__dir__}/tailwind.config.js", "tailwind.config.js"
6+
say "Install Tailwind"
87
copy_file "#{__dir__}/application.tailwind.css", "app/assets/stylesheets/application.tailwind.css"
9-
run "#{bundler_cmd} add tailwindcss@latest postcss@latest autoprefixer@latest"
8+
run "#{bundler_cmd} add tailwindcss@latest @tailwindcss/cli@latest"
109

1110
say "Add build:css script"
1211
add_package_json_script "build:css",
13-
"tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
12+
"#{bundler_x_cmd} @tailwindcss/cli -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"

lib/install/tailwind/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "app",
33
"private": "true",
44
"scripts": {
5-
"build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css"
5+
"build:css": "npx @tailwindcss/cli -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css"
66
}
77
}

lib/install/tailwind/tailwind.config.js

-8
This file was deleted.

0 commit comments

Comments
 (0)