From 5d844621ba73db929bb28d7a149f41a45a50d82e Mon Sep 17 00:00:00 2001 From: Andy Leverenz Date: Fri, 24 Jan 2025 14:10:44 -0600 Subject: [PATCH 1/2] 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. --- README.md | 13 +++++++++---- lib/install/tailwind/application.tailwind.css | 4 +--- lib/install/tailwind/install.rb | 7 +++---- lib/install/tailwind/package.json | 2 +- lib/install/tailwind/tailwind.config.js | 8 -------- 5 files changed, 14 insertions(+), 20 deletions(-) delete mode 100644 lib/install/tailwind/tailwind.config.js diff --git a/README.md b/README.md index a3b2647..5413d03 100644 --- a/README.md +++ b/README.md @@ -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. ## Installation @@ -34,7 +33,13 @@ In Rails 7+, you can preconfigure your new application to use `tailwindcss-rails ### How do I import relative CSS files with Tailwind? -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" %>`. +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: + +```erb +<%= stylesheet_link_tag "application", "other", "styles", "data-turbo-track": "reload" %> +``` + +This ensures your files are properly linked and ready to use. ### How do I avoid SassC::SyntaxError exceptions on existing projects? @@ -58,7 +63,7 @@ Rails.application.config.assets.css_compressor = nil Watch out - if you precompile your files locally, those will be served over the dynamically created ones you expect. The solution: ```shell -rails assets:clobber +rails assets:clobber ``` ### How do I include 3rd party stylesheets from `node_modules` in my bundle? diff --git a/lib/install/tailwind/application.tailwind.css b/lib/install/tailwind/application.tailwind.css index b5c61c9..f1d8c73 100644 --- a/lib/install/tailwind/application.tailwind.css +++ b/lib/install/tailwind/application.tailwind.css @@ -1,3 +1 @@ -@tailwind base; -@tailwind components; -@tailwind utilities; +@import "tailwindcss"; diff --git a/lib/install/tailwind/install.rb b/lib/install/tailwind/install.rb index f8e530d..7aee2b8 100644 --- a/lib/install/tailwind/install.rb +++ b/lib/install/tailwind/install.rb @@ -3,11 +3,10 @@ apply "#{__dir__}/../install.rb" -say "Install Tailwind (+PostCSS w/ autoprefixer)" -copy_file "#{__dir__}/tailwind.config.js", "tailwind.config.js" +say "Install Tailwind" copy_file "#{__dir__}/application.tailwind.css", "app/assets/stylesheets/application.tailwind.css" -run "#{bundler_cmd} add tailwindcss@latest postcss@latest autoprefixer@latest" +run "#{bundler_cmd} add tailwindcss@latest @tailwindcss/cli@latest" say "Add build:css script" add_package_json_script "build:css", - "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify" + "npx @tailwindcss/cli -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify" diff --git a/lib/install/tailwind/package.json b/lib/install/tailwind/package.json index 32f598a..662b0f1 100644 --- a/lib/install/tailwind/package.json +++ b/lib/install/tailwind/package.json @@ -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" } } diff --git a/lib/install/tailwind/tailwind.config.js b/lib/install/tailwind/tailwind.config.js deleted file mode 100644 index 4bca89f..0000000 --- a/lib/install/tailwind/tailwind.config.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - content: [ - './app/views/**/*.html.erb', - './app/helpers/**/*.rb', - './app/assets/stylesheets/**/*.css', - './app/javascript/**/*.js' - ] -} From a0df0b7511f9caf68f774e9bfa15eb1f8a112c85 Mon Sep 17 00:00:00 2001 From: Andy Leverenz Date: Sat, 25 Jan 2025 06:10:01 -0600 Subject: [PATCH 2/2] Add support for either bunx or npx --- lib/install/helpers.rb | 4 ++++ lib/install/tailwind/install.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/install/helpers.rb b/lib/install/helpers.rb index dd4e974..a2b9e6a 100644 --- a/lib/install/helpers.rb +++ b/lib/install/helpers.rb @@ -9,6 +9,10 @@ def bundler_run_cmd using_bun? ? "bun run" : "yarn" end + def bundler_x_cmd + using_bun? ? "bunx" : "npx" + end + def using_bun? File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock')) end diff --git a/lib/install/tailwind/install.rb b/lib/install/tailwind/install.rb index 7aee2b8..3b7181e 100644 --- a/lib/install/tailwind/install.rb +++ b/lib/install/tailwind/install.rb @@ -9,4 +9,4 @@ say "Add build:css script" add_package_json_script "build:css", - "npx @tailwindcss/cli -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify" + "#{bundler_x_cmd} @tailwindcss/cli -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"