From 123569431c0e303c1aca8b275b2d1bc3c68a3a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20=C3=96zbek?= Date: Sat, 5 Oct 2024 02:40:26 +0200 Subject: [PATCH 1/3] Update README.md (#157) Clarify that `cssbundling` isn't used if you run `rails new --css=tailwind` or `--css=dart` --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e7c24f..a3b2647 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,7 @@ You must already have node and yarn installed on your system. You will also need 1. Run `./bin/bundle add cssbundling-rails` 2. Run `./bin/rails css:install:[tailwind|bootstrap|bulma|postcss|sass]` -Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with `rails new myapp --css [tailwind|bootstrap|bulma|postcss|sass]`. - +Or, in Rails 7+, you can preconfigure your new application to use `cssbundling-rails` for `bootstrap`, `bulma` or `postcss` with `rails new myapp --css [bootstrap|bulma|postcss]`. ## FAQ @@ -31,6 +30,8 @@ Or, in Rails 7+, you can preconfigure your new application to use a specific bun If you're already relying on Node to process your JavaScript, this gem is the way to go. But if you're using [the default import map setup in Rails 7+](https://github.com/rails/importmap-rails/), you can avoid having to deal with Node at all by using the standalone versions of Tailwind CSS and Dart Sass that are available through [tailwindcss-rails](https://github.com/rails/tailwindcss-rails/) and [dartsass-rails](https://github.com/rails/dartsass-rails/). It's simpler, fewer moving parts, and still has all the functionality. +In Rails 7+, you can preconfigure your new application to use `tailwindcss-rails` and `dartsass-rails` with `rails new myapp --css [tailwind|sass]`. + ### 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" %>`. From df70d230264312a2dab2c44846c2b48c728a5594 Mon Sep 17 00:00:00 2001 From: Andy Leverenz Date: Mon, 24 Feb 2025 06:24:58 -0600 Subject: [PATCH 2/3] 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 --- README.md | 13 +++++++++---- lib/install/helpers.rb | 4 ++++ 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 -------- 6 files changed, 18 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/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/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..3b7181e 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" + "#{bundler_x_cmd} @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 393e7560e021ca330ffa4a4399e68984b306df15 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 24 Feb 2025 13:29:11 +0100 Subject: [PATCH 3/3] Bump version for 1.4.2 --- Gemfile.lock | 2 +- lib/cssbundling/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index bbda716..29b8821 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - cssbundling-rails (1.4.1) + cssbundling-rails (1.4.2) railties (>= 6.0.0) GEM diff --git a/lib/cssbundling/version.rb b/lib/cssbundling/version.rb index 72f5ad2..d46c887 100644 --- a/lib/cssbundling/version.rb +++ b/lib/cssbundling/version.rb @@ -1,3 +1,3 @@ module Cssbundling - VERSION = "1.4.1" + VERSION = "1.4.2" end