From 9c7179008b9c2db11a7908036b06f63dceeeb6db Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 2 May 2025 16:21:52 -0400 Subject: [PATCH 01/12] Pin the npm upgrade task to @tailwindcss/upgrade@4.1.4 because the 4.1.5 upgrade tool introduced a dependency on tailwind that I can't figure out how to resolve. Gah, this entire experience has been entirely frustrating as a downstream maintainer, FML --- README.md | 4 ++-- lib/install/upgrade_tailwindcss.rb | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7188adf..158b175 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Then, run `bin/rails tailwindcss:upgrade`. Among other things, this will try to - If present, moves `app/assets/stylesheets/application.tailwind.css` to `app/assets/tailwind/application.css`. - Removes unnecessary `stylesheet_link_tag "tailwindcss"` tags from the application layout. - Removes references to the Inter font from the application layout. -- Runs the upstream upgrader (note: requires `npx` to run the one-time upgrade, but highly recommended). +- Runs v4.1.4 of the upstream upgrader (note: requires `npx` to run the one-time upgrade, but highly recommended). @@ -145,7 +145,7 @@ $ bin/rails tailwindcss:upgrade remove app/assets/stylesheets/application.tailwind.css 10.9.0 Running the upstream Tailwind CSS upgrader - run npx @tailwindcss/upgrade --force --config /home/user/myapp/config/tailwind.config.js from "." + run npx @tailwindcss/upgrade@4.1.4 --force --config /home/user/myapp/config/tailwind.config.js from "." ≈ tailwindcss v4.0.0 │ Searching for CSS files in the current directory and its subdirectories… │ ↳ Linked `./config/tailwind.config.js` to `./app/assets/tailwind/application.css` diff --git a/lib/install/upgrade_tailwindcss.rb b/lib/install/upgrade_tailwindcss.rb index 004d9a8..d6ecada 100644 --- a/lib/install/upgrade_tailwindcss.rb +++ b/lib/install/upgrade_tailwindcss.rb @@ -45,8 +45,14 @@ end if system("npx --version") + # We're pinning to v4.1.4 because v4.1.5 of the upgrade tool introduces a dependency version check + # on tailwind and I haven't been able to figure out how to get that to work reliably and I am + # extremely frustrated with the whole thing. See #544 + # + # At some point we will probably need to unpin this at which point I am sincerely hoping that + # someone else will do it. say "Running the upstream Tailwind CSS upgrader" - command = Shellwords.join(["npx", "@tailwindcss/upgrade", "--force", "--config", TAILWIND_CONFIG_PATH.to_s]) + command = Shellwords.join(["npx", "@tailwindcss/upgrade@4.1.4", "--force", "--config", TAILWIND_CONFIG_PATH.to_s]) success = run(command, abort_on_failure: false) unless success say "The upgrade tool failed!", :red From 114fca5b4e235dd9cb14f54161f75032fd247681 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 2 May 2025 16:45:32 -0400 Subject: [PATCH 02/12] version bump to v4.2.3 --- CHANGELOG.md | 7 +++++++ lib/tailwindcss/version.rb | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0f90da..d662d7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # `tailwindcss-rails` Changelog +## v4.2.3 / 2025-05-02 + +### Improved + +* The "tailwindcss:upgrade" task now pins to v4.1.4 of the `@tailwindcss/upgrade` tool. #544 @flavorjones + + ## v4.2.2 / 2025-04-05 ### Improved diff --git a/lib/tailwindcss/version.rb b/lib/tailwindcss/version.rb index 8a0f50d..7ab2db8 100644 --- a/lib/tailwindcss/version.rb +++ b/lib/tailwindcss/version.rb @@ -1,3 +1,3 @@ module Tailwindcss - VERSION = "4.2.2" + VERSION = "4.2.3" end From 3f6d06d0981435c5cdd37951925e31fe70c6870e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 5 May 2025 15:12:32 +0000 Subject: [PATCH 03/12] build(deps): bump tailwindcss-ruby from 4.1.4 to 4.1.5 Bumps [tailwindcss-ruby](https://github.com/flavorjones/tailwindcss-ruby) from 4.1.4 to 4.1.5. - [Release notes](https://github.com/flavorjones/tailwindcss-ruby/releases) - [Changelog](https://github.com/flavorjones/tailwindcss-ruby/blob/main/CHANGELOG.md) - [Commits](https://github.com/flavorjones/tailwindcss-ruby/compare/v4.1.4...v4.1.5) --- updated-dependencies: - dependency-name: tailwindcss-ruby dependency-version: 4.1.5 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- Gemfile.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ec55187..bb16d40 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,11 +140,11 @@ GEM io-console (~> 0.5) securerandom (0.4.1) stringio (3.1.5) - tailwindcss-ruby (4.1.4) - tailwindcss-ruby (4.1.4-aarch64-linux-gnu) - tailwindcss-ruby (4.1.4-arm64-darwin) - tailwindcss-ruby (4.1.4-x86_64-darwin) - tailwindcss-ruby (4.1.4-x86_64-linux-gnu) + tailwindcss-ruby (4.1.5) + tailwindcss-ruby (4.1.5-aarch64-linux-gnu) + tailwindcss-ruby (4.1.5-arm64-darwin) + tailwindcss-ruby (4.1.5-x86_64-darwin) + tailwindcss-ruby (4.1.5-x86_64-linux-gnu) thor (1.3.2) timeout (0.4.3) tzinfo (2.0.6) From 9d0a9f5e81aac890cbc6476f49515d9d45dc8abe Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 12 May 2025 14:17:03 +0000 Subject: [PATCH 04/12] build(deps): bump tailwindcss-ruby from 4.1.5 to 4.1.6 Bumps [tailwindcss-ruby](https://github.com/flavorjones/tailwindcss-ruby) from 4.1.5 to 4.1.6. - [Release notes](https://github.com/flavorjones/tailwindcss-ruby/releases) - [Changelog](https://github.com/flavorjones/tailwindcss-ruby/blob/main/CHANGELOG.md) - [Commits](https://github.com/flavorjones/tailwindcss-ruby/compare/v4.1.5...v4.1.6) --- updated-dependencies: - dependency-name: tailwindcss-ruby dependency-version: 4.1.6 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- Gemfile.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index bb16d40..f84bada 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,11 +140,11 @@ GEM io-console (~> 0.5) securerandom (0.4.1) stringio (3.1.5) - tailwindcss-ruby (4.1.5) - tailwindcss-ruby (4.1.5-aarch64-linux-gnu) - tailwindcss-ruby (4.1.5-arm64-darwin) - tailwindcss-ruby (4.1.5-x86_64-darwin) - tailwindcss-ruby (4.1.5-x86_64-linux-gnu) + tailwindcss-ruby (4.1.6) + tailwindcss-ruby (4.1.6-aarch64-linux-gnu) + tailwindcss-ruby (4.1.6-arm64-darwin) + tailwindcss-ruby (4.1.6-x86_64-darwin) + tailwindcss-ruby (4.1.6-x86_64-linux-gnu) thor (1.3.2) timeout (0.4.3) tzinfo (2.0.6) From 401b00b61c78c7dd7dfe33142d9a6660b8f4546c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 19 May 2025 14:16:45 +0000 Subject: [PATCH 05/12] build(deps): bump tailwindcss-ruby from 4.1.6 to 4.1.7 Bumps [tailwindcss-ruby](https://github.com/flavorjones/tailwindcss-ruby) from 4.1.6 to 4.1.7. - [Release notes](https://github.com/flavorjones/tailwindcss-ruby/releases) - [Changelog](https://github.com/flavorjones/tailwindcss-ruby/blob/main/CHANGELOG.md) - [Commits](https://github.com/flavorjones/tailwindcss-ruby/compare/v4.1.6...v4.1.7) --- updated-dependencies: - dependency-name: tailwindcss-ruby dependency-version: 4.1.7 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- Gemfile.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f84bada..9b0500d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,11 +140,11 @@ GEM io-console (~> 0.5) securerandom (0.4.1) stringio (3.1.5) - tailwindcss-ruby (4.1.6) - tailwindcss-ruby (4.1.6-aarch64-linux-gnu) - tailwindcss-ruby (4.1.6-arm64-darwin) - tailwindcss-ruby (4.1.6-x86_64-darwin) - tailwindcss-ruby (4.1.6-x86_64-linux-gnu) + tailwindcss-ruby (4.1.7) + tailwindcss-ruby (4.1.7-aarch64-linux-gnu) + tailwindcss-ruby (4.1.7-arm64-darwin) + tailwindcss-ruby (4.1.7-x86_64-darwin) + tailwindcss-ruby (4.1.7-x86_64-linux-gnu) thor (1.3.2) timeout (0.4.3) tzinfo (2.0.6) From a66db017913c3097f8a4197408894c76d15a33d6 Mon Sep 17 00:00:00 2001 From: Sergey Moiseev Date: Thu, 12 Jun 2025 17:22:30 +0300 Subject: [PATCH 06/12] Implement Engines Support (#554) * Implement Engines Support * Correction in README.md * Remove engine test * Rename to engine.css * Initial test cleanup * Tests cleanup * Remove non-needed dup * Correction * Correct docs * doc: update README to indicate engine support is experimental --------- Co-authored-by: Mike Dalessio --- Gemfile.lock | 2 +- README.md | 82 ++++++++++++------------- lib/tailwindcss-rails.rb | 1 + lib/tailwindcss/engine.rb | 4 ++ lib/tailwindcss/engines.rb | 23 +++++++ test/lib/tailwindcss/engines_test.rb | 89 ++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 46 deletions(-) create mode 100644 lib/tailwindcss/engines.rb create mode 100644 test/lib/tailwindcss/engines_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index 9b0500d..3a61813 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - tailwindcss-rails (4.2.2) + tailwindcss-rails (4.2.3) railties (>= 7.0.0) tailwindcss-ruby (~> 4.0) diff --git a/README.md b/README.md index 158b175..274fae8 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,9 @@ * [Using Tailwind plugins](#using-tailwind-plugins) * [Using with PostCSS](#using-with-postcss) * [Custom inputs or outputs](#custom-inputs-or-outputs) + * [Rails Engines support (Experimental)](#rails-engines-support-experimental) - [Troubleshooting](#troubleshooting) + * [The `watch` command is hanging](#the-watch-command-is-hanging) * [Lost keystrokes or hanging when using terminal-based debugging tools (e.g. IRB, Pry, `ruby/debug`...etc.) with the Puma plugin](#lost-keystrokes-or-hanging-when-using-terminal-based-debugging-tools-eg-irb-pry-rubydebugetc-with-the-puma-plugin) * [Running in a docker container exits prematurely](#running-in-a-docker-container-exits-prematurely) * [Conflict with sassc-rails](#conflict-with-sassc-rails) @@ -43,12 +45,11 @@ With Rails 7 you can generate a new application preconfigured with Tailwind CSS This gem depends on the `tailwindcss-ruby` gem to install a working Tailwind CLI executable. - ### Choosing a specific version of `tailwindcss` The `tailwindcss-ruby` gem is declared as a floating dependency of this gem, so by default you will get the most recent stable version. However, you can select a specific version of Tailwind CSS by pinning that gem to the analogous version in your application's `Gemfile`. For example, -``` ruby +```ruby gem "tailwindcss-rails" # pin to tailwindcss version 3.4.13 @@ -59,7 +60,6 @@ gem "tailwindcss-ruby", "3.4.13" You can also use a local (npm-based) installation if you prefer, please go to https://github.com/flavorjones/tailwindcss-ruby for more information. - ## Upgrading your application from Tailwind v3 to v4 v4.x of this gem has been updated to work with Tailwind v4, including providing some help with upgrading your application. @@ -72,27 +72,25 @@ This gem will help with some of the mechanics of the upgrade: - update some local project files to meet some Tailwind CSS v4 conventions, - attempt to run the [upstream v4 upgrade tool](https://tailwindcss.com/docs/upgrade-guide#using-the-upgrade-tool). - ### You don't _have_ to upgrade Keep in mind that you don't _need_ to upgrade. You can stay on Tailwind CSS v3 for the foreseeable future if you prefer not to migrate now, or if your migration runs into problems. If you don't want to upgrade, then pin your application to v3.3.1 of this gem: -``` ruby +```ruby # Gemfile gem "tailwindcss-rails", "~> 3.3.1" # which transitively pins tailwindcss-ruby to v3 ``` If you're on an earlier version of this gem, `<= 3.3.0`, then make sure you're pinning the version of **both** `tailwindcss-rails` and `tailwindcss-ruby`: -``` ruby +```ruby # Gemfile gem "tailwindcss-rails", "~> 3.3" gem "tailwindcss-ruby", "~> 3.4" # only necessary with tailwindcss-rails <= 3.3.0 ``` - ### Upgrade steps > [!WARNING] @@ -100,8 +98,8 @@ gem "tailwindcss-ruby", "~> 3.4" # only necessary with tailwindcss-rails <= 3.3. First, update to `tailwindcss-rails` v4.0.0 or higher. This will also ensure you're transitively depending on `tailwindcss-ruby` v4. -```html -# Gemfile +```ruby +# Gemfile gem "tailwindcss-rails", "~> 4.0" # which transitively pins tailwindcss-ruby to v4 ``` @@ -114,7 +112,7 @@ gem "tailwindcss-rails", "~> 4.0" # which transitively pins tailwindcss-ruby to If you want to migrate CSS class names for v4 (this is an optional step!), jump to [Updating CSS class names for v4](#updating-css-class-names-for-v4) before continuing. -Then, run `bin/rails tailwindcss:upgrade`. Among other things, this will try to run the official Tailwind upgrade utility. It requires `npx` in order to run, but it's a one-time operation and is *highly recommended* for a successful upgrade. +Then, run `bin/rails tailwindcss:upgrade`. Among other things, this will try to run the official Tailwind upgrade utility. It requires `npx` in order to run, but it's a one-time operation and is _highly recommended_ for a successful upgrade.
Here's a detailed list of what the upgrade task does. @@ -131,7 +129,7 @@ Then, run `bin/rails tailwindcss:upgrade`. Among other things, this will try to
Here's what that upgrade looks like on a vanilla Rails app. -``` sh +```sh $ bin/rails tailwindcss:upgrade apply /path/to/tailwindcss-rails/lib/install/upgrade_tailwindcss.rb Removing references to 'defaultTheme' from /home/user/myapp/config/tailwind.config.js @@ -166,12 +164,11 @@ $ bin/rails tailwindcss:upgrade Done in 56ms run bundle install --quiet ``` -
+
If this doesn't succeed, it's likely that you've customized your Tailwind configuration and you'll need to do some work to make sure your application upgrades. Please read the [official upgrade guide](https://tailwindcss.com/docs/upgrade-guide) and try following the additional steps in [Updating CSS class names for v4](#updating-css-class-names-for-v4). - ### Troubleshooting a v4 upgrade You may want to check out [TailwindCSS v4 - upgrade experience report · rails/tailwindcss-rails · Discussion #450](https://github.com/rails/tailwindcss-rails/discussions/450) if you're having trouble upgrading. @@ -182,7 +179,6 @@ We know there are some cases we haven't addressed with the upgrade task: We'll try to improve the upgrade process over time, but for now you may need to do some manual work to upgrade. - ### Updating CSS class names for v4 > [!NOTE] @@ -208,9 +204,9 @@ With some additional manual work the upstream upgrade tool will update your appl "@tailwindcss/aspect-ratio": "^0.4.2", "@tailwindcss/container-queries": "^0.1.1", "@tailwindcss/forms": "^0.5.10", - "@tailwindcss/typography": "^0.5.16" + "@tailwindcss/typography": "^0.5.16", // And so on... - } + }, } ``` @@ -280,12 +276,10 @@ This gem also generates a `Procfile.dev` file which will run both the rails serv The `tailwindcss:build` is automatically attached to `assets:precompile`, so before the asset pipeline digests the files, the Tailwind output will be generated. - ### Building for testing The `tailwindcss:build` task is automatically attached to the `test:prepare` Rake task. This task runs before test commands. If you run `bin/rails test` in your CI environment, your Tailwind output will be generated before tests run. - ### Building unminified assets If you want unminified assets, you can: @@ -303,7 +297,6 @@ While you're developing your application, you want to run Tailwind in "watch" mo - or run `rails tailwindcss:watch` as a separate process, - or run `bin/dev` which uses [Foreman](https://github.com/ddollar/foreman) - #### Puma plugin This gem ships with a Puma plugin. To use it, add this line to your `puma.rb` configuration: @@ -314,7 +307,6 @@ plugin :tailwindcss if ENV.fetch("RAILS_ENV", "development") == "development" and then running `rails server` (or just `puma`) will run the Tailwind watch process in the background. - #### Run `rails tailwindcss:watch` This is a flexible command, which can be run with a few different options. @@ -331,38 +323,35 @@ If you are running `rails tailwindcss:watch` as a process in a Docker container, If you are running `rails tailwindcss:watch` in a docker container without a tty, pass the `always` argument to the task to instruct tailwindcss to keep the watcher alive even when `stdin` is closed: `rails tailwindcss:watch[always]`. If you use `bin/dev` then you should modify your `Procfile.dev`. - #### Foreman Running `bin/dev` invokes Foreman to start both the Tailwind watch process and the rails server in development mode based on your `Procfile.dev` file. - ### Using Tailwind plugins If you want to use Tailwind plugins, they can be installed using `package.json`. Using Yarn: -``` sh +```sh [ ! -f package.json ] && yarn init yarn add daisyui # example ``` Using npm: -``` sh +```sh npm init npm add daisyui # example ``` Than use `@plugin` annotation in `app/assets/tailwind/application.css`: -``` css +```css @import "tailwindcss"; @plugin "daisyui"; ``` - ### Using with PostCSS If you want to use PostCSS as a preprocessor, create a custom `postcss.config.js` in your project root directory, and that file will be loaded by Tailwind automatically. @@ -374,8 +363,8 @@ For example, to enable nesting: export default { plugins: { "@tailwindcss/postcss": {}, - } -} + }, +}; ``` ⚠ Note that PostCSS is a JavaScript tool with its own prerequisites! By default `tailwindcss-rails` does not require any JavaScript tooling, so in order to use PostCSS, a `package.json` with dependencies for your plugins and a package manager like `yarn` or `npm` is required, for example: @@ -395,11 +384,18 @@ export default { Then you can use yarn or npm to install the dependencies. - ### Custom inputs or outputs If you need to use a custom input or output file, you can run `bundle exec tailwindcss` to access the platform-specific executable, and give it your own build options. +### Rails Engines support (Experimental) + +_This feature is experimental and may change in the future. If you have feedback, please join the [discussion](https://github.com/rails/tailwindcss-rails/discussions/355)._ + +If you have Rails Engines in your application that use Tailwind CSS and provide an `app/assets/tailwind//engine.css` file, entry point files will be created for each of them in `app/assets/builds/tailwind/.css` so they can be included in your host application's Tailwind CSS by adding `@import "../builds/tailwind/"` to your `app/assets/tailwind/application.css` file. + +> [!IMPORTANT] +> You must `@import` the engine CSS files in your `app/assets/tailwind/application.css` for the engine to be included in the build. By default, no engine CSS files are imported, and you must opt-in to using the file in your build. ## Troubleshooting @@ -410,7 +406,7 @@ When having trouble with `tailwindcss:build` or `tailwindcss:watch`, the first t Here's what that looks like: -``` sh +```sh $ bin/rails tailwindcss:build[verbose] Running: /path/to/tailwindcss-ruby-4.0.17-x86_64-linux-gnu/exe/x86_64-linux-gnu/tailwindcss -i /home/flavorjones/code/oss/tailwindcss-rails/My Workspace/test-install/app/assets/tailwind/application.css -o /home/flavorjones/code/oss/tailwindcss-rails/My Workspace/test-install/app/assets/builds/tailwind.css --minify @@ -432,34 +428,28 @@ There is a [known issue](https://github.com/tailwindlabs/tailwindcss/issues/1724 Please try uninstalling `watchman` and try running the watch task again. - ### Lost keystrokes or hanging when using terminal-based debugging tools (e.g. IRB, Pry, `ruby/debug`...etc.) with the Puma plugin We've addressed the issue and you can avoid the problem by upgrading `tailwindcss-rails` to [v2.4.1](https://github.com/rails/tailwindcss-rails/releases/tag/v2.4.1) or later versions. - ### Running in a docker container exits prematurely If you are running `rails tailwindcss:watch` as a process in a Docker container, set `tty: true` in `docker-compose.yml` for the appropriate container to keep the watch process running. If you are running `rails tailwindcss:watch` in a docker container without a tty, pass the `always` argument to the task to instruct tailwindcss to keep the watcher alive even when `stdin` is closed: `rails tailwindcss:watch[always]`. If you use `bin/dev` then you should modify your `Procfile.dev`. - ### Conflict with sassc-rails Tailwind uses modern CSS features that are not recognized by the `sassc-rails` extension that was included by default in the Gemfile for Rails 6. In order to avoid any errors like `SassC::SyntaxError`, you must remove that gem from your Gemfile. - ### Class names must be spelled out For Tailwind to work, your class names need to be spelled out. If you need to make sure Tailwind generates class names that don't exist in your content files or that are programmatically composed, use the [safelist option](https://tailwindcss.com/docs/content-configuration#safelisting-classes). - ### `ERROR: Cannot find the tailwindcss executable` for supported platform See https://github.com/flavorjones/tailwindcss-ruby for help. - ### Using asset-pipeline assets In Rails, you want to use [assets from the asset pipeline to get fingerprinting](https://guides.rubyonrails.org/asset_pipeline.html#fingerprinting-versioning-with-digest-based-urls). However, Tailwind isn't aware of those assets. @@ -468,20 +458,22 @@ To use assets from the pipeline, use `url(image.svg)`. [Since Sprockets v3.3.0]( ```js module.exports = { - theme: { - extend: { - backgroundImage: { - 'image': "url('image.svg')" - } - } - } -} + theme: { + extend: { + backgroundImage: { + image: "url('image.svg')", + }, + }, + }, +}; ``` The inline version also works: ```html -
Has the image as it's background
+
+ Has the image as it's background +
``` ## License diff --git a/lib/tailwindcss-rails.rb b/lib/tailwindcss-rails.rb index 2b86ff1..bdfb592 100644 --- a/lib/tailwindcss-rails.rb +++ b/lib/tailwindcss-rails.rb @@ -2,5 +2,6 @@ module Tailwindcss end require_relative "tailwindcss/version" +require_relative "tailwindcss/engines" require_relative "tailwindcss/engine" require_relative "tailwindcss/commands" diff --git a/lib/tailwindcss/engine.rb b/lib/tailwindcss/engine.rb index 7b88c5f..001004f 100644 --- a/lib/tailwindcss/engine.rb +++ b/lib/tailwindcss/engine.rb @@ -15,5 +15,9 @@ class Engine < ::Rails::Engine config.app_generators do |g| g.template_engine :tailwindcss end + + config.after_initialize do + Tailwindcss::Engines.bundle + end end end diff --git a/lib/tailwindcss/engines.rb b/lib/tailwindcss/engines.rb new file mode 100644 index 0000000..d1d6bf6 --- /dev/null +++ b/lib/tailwindcss/engines.rb @@ -0,0 +1,23 @@ +module Tailwindcss + module Engines + class << self + def bundle + FileUtils.mkdir_p(Rails.root.join("app/assets/builds/tailwind")) + Rails::Engine.subclasses.select do |engine| + engine.root.join("app/assets/tailwind/#{engine.engine_name}/engine.css").exist? + end.each do |engine| + file_path = Rails.root.join("app/assets/builds/tailwind/#{engine.engine_name}.css") + FileUtils.rm(file_path) if File.exist?(file_path) + template = <<~TEMPLATE + /* DO NOT MODIFY THIS FILE, it was auto-generated by tailwindcss-rails */ + + @import "#{engine.root.join("app/assets/tailwind/#{engine.engine_name}/engine.css")}"; + TEMPLATE + File.open(file_path, 'w') do |file| + file.puts template + end + end + end + end + end +end diff --git a/test/lib/tailwindcss/engines_test.rb b/test/lib/tailwindcss/engines_test.rb new file mode 100644 index 0000000..f4c1b0d --- /dev/null +++ b/test/lib/tailwindcss/engines_test.rb @@ -0,0 +1,89 @@ +require "test_helper" +require "minitest/mock" + +class Tailwindcss::EnginesTest < ActiveSupport::TestCase + def setup + super + @tmpdir_path = Pathname.new(TAILWINDCSS_TEST_APP_ROOT) + @builds_dir = @tmpdir_path.join("app/assets/builds/tailwind") + end + + test "bundle creates the builds directory" do + Rails.stub(:root, @tmpdir_path) do + Tailwindcss::Engines.bundle + assert Dir.exist?(@builds_dir), "Expected directory #{@builds_dir} to be created" + end + end + + test "bundle generates CSS files for engine's tailwind assets" do + Rails.stub(:root, @tmpdir_path) do + setup_mock_engine("mock_engine", @tmpdir_path) + + Tailwindcss::Engines.bundle + + css_file_path = @builds_dir.join("mock_engine.css") + assert File.exist?(css_file_path), "Expected file #{css_file_path} to be created" + + content = File.read(css_file_path) + assert_match(/DO NOT MODIFY THIS FILE/, content) + assert_match(/@import ".*\/app\/assets\/tailwind\/mock_engine\/engine.css"/, content) + end + end + + test "bundle removes existing files before generating new ones" do + Rails.stub(:root, @tmpdir_path) do + setup_mock_engine("mock_engine", @tmpdir_path) + + FileUtils.mkdir_p(@builds_dir) + css_file_path = @builds_dir.join("mock_engine.css") + File.write(css_file_path, "OLD CONTENT") + + Tailwindcss::Engines.bundle + + content = File.read(css_file_path) + assert_no_match(/OLD CONTENT/, content) + assert_match(/DO NOT MODIFY THIS FILE/, content) + end + end + + test "bundle only processes engines with tailwind assets" do + Rails.stub(:root, @tmpdir_path) do + setup_mock_engine("engine_with_assets", @tmpdir_path) + + Class.new(Rails::Engine) do + define_singleton_method(:engine_name) { "engine_without_assets" } + define_singleton_method(:root) { Pathname.new(Dir.mktmpdir) } + end + + Tailwindcss::Engines.bundle + + assert File.exist?(@builds_dir.join("engine_with_assets.css")), "Expected CSS file for engine with assets" + refute File.exist?(@builds_dir.join("engine_without_assets.css")), "Expected no CSS file for engine without assets" + end + end + + test "bundle handles multiple engines" do + Rails.stub(:root, @tmpdir_path) do + setup_mock_engine("engine1", @tmpdir_path) + setup_mock_engine("engine2", @tmpdir_path) + + Tailwindcss::Engines.bundle + + assert File.exist?(@builds_dir.join("engine1.css")), "Expected CSS file for engine1" + assert File.exist?(@builds_dir.join("engine2.css")), "Expected CSS file for engine2" + end + end + + private + + def setup_mock_engine(name, root_path) + tailwind_dir = root_path.join("app/assets/tailwind/#{name}") + FileUtils.mkdir_p(tailwind_dir) + File.write(tailwind_dir.join("engine.css"), "/* Test CSS */") + + Class.new(Rails::Engine) do + define_singleton_method(:engine_name) { name } + define_singleton_method(:root) { root_path } + end + end +end From ae58bb0a5e42c10b7937b4a3a933f242c686f83e Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 12 Jun 2025 10:25:38 -0400 Subject: [PATCH 07/12] version bump to v4.3.0.rc1 --- CHANGELOG.md | 7 +++++++ lib/tailwindcss/version.rb | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d662d7a..2c11a6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # `tailwindcss-rails` Changelog +## v4.3.0.rc1 + +### Improved + +* Experimental support for Rails engines. Feedback is welcome at #355. #554 @bopm + + ## v4.2.3 / 2025-05-02 ### Improved diff --git a/lib/tailwindcss/version.rb b/lib/tailwindcss/version.rb index 7ab2db8..b4acf0d 100644 --- a/lib/tailwindcss/version.rb +++ b/lib/tailwindcss/version.rb @@ -1,3 +1,3 @@ module Tailwindcss - VERSION = "4.2.3" + VERSION = "4.3.0.rc1" end From 7e0b4d669af33e9d0e830ef08fd22ea34159bca6 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 12 Jun 2025 10:36:42 -0400 Subject: [PATCH 08/12] dev: update Gemfile.lock with new version --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3a61813..1defe8d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - tailwindcss-rails (4.2.3) + tailwindcss-rails (4.3.0.rc1) railties (>= 7.0.0) tailwindcss-ruby (~> 4.0) From f26db33751df017a695f17ddaf7b14c85a7a953a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 12 Jun 2025 14:37:53 +0000 Subject: [PATCH 09/12] build(deps): bump tailwindcss-ruby from 4.1.7 to 4.1.8 Bumps [tailwindcss-ruby](https://github.com/flavorjones/tailwindcss-ruby) from 4.1.7 to 4.1.8. - [Release notes](https://github.com/flavorjones/tailwindcss-ruby/releases) - [Changelog](https://github.com/flavorjones/tailwindcss-ruby/blob/main/CHANGELOG.md) - [Commits](https://github.com/flavorjones/tailwindcss-ruby/compare/v4.1.7...v4.1.8) --- updated-dependencies: - dependency-name: tailwindcss-ruby dependency-version: 4.1.8 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- Gemfile.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 1defe8d..22cbba3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,11 +140,11 @@ GEM io-console (~> 0.5) securerandom (0.4.1) stringio (3.1.5) - tailwindcss-ruby (4.1.7) - tailwindcss-ruby (4.1.7-aarch64-linux-gnu) - tailwindcss-ruby (4.1.7-arm64-darwin) - tailwindcss-ruby (4.1.7-x86_64-darwin) - tailwindcss-ruby (4.1.7-x86_64-linux-gnu) + tailwindcss-ruby (4.1.8) + tailwindcss-ruby (4.1.8-aarch64-linux-gnu) + tailwindcss-ruby (4.1.8-arm64-darwin) + tailwindcss-ruby (4.1.8-x86_64-darwin) + tailwindcss-ruby (4.1.8-x86_64-linux-gnu) thor (1.3.2) timeout (0.4.3) tzinfo (2.0.6) From 7c668844dbbc0fa594cb519070c64ff41119d76b Mon Sep 17 00:00:00 2001 From: Sergey Moiseev Date: Mon, 16 Jun 2025 05:05:30 +0300 Subject: [PATCH 10/12] Fix read-only file system issue (#556) * Fix read-only file system issue * Changes due to review * Restore original formatting * Changes due to review * Remove new line * Clarify "engines" rake task description --------- Co-authored-by: Mike Dalessio --- README.md | 2 +- lib/tailwindcss/engine.rb | 4 ---- lib/tasks/build.rake | 9 +++++++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 274fae8..450a177 100644 --- a/README.md +++ b/README.md @@ -392,7 +392,7 @@ If you need to use a custom input or output file, you can run `bundle exec tailw _This feature is experimental and may change in the future. If you have feedback, please join the [discussion](https://github.com/rails/tailwindcss-rails/discussions/355)._ -If you have Rails Engines in your application that use Tailwind CSS and provide an `app/assets/tailwind//engine.css` file, entry point files will be created for each of them in `app/assets/builds/tailwind/.css` so they can be included in your host application's Tailwind CSS by adding `@import "../builds/tailwind/"` to your `app/assets/tailwind/application.css` file. +If you have Rails Engines in your application that use Tailwind CSS and provide an `app/assets/tailwind//engine.css` file, entry point files will be created for each of them in `app/assets/builds/tailwind/.css` on the first build/watch invocation or manual call for `rails tailwindcss:engines` so they can be included in your host application's Tailwind CSS by adding `@import "../builds/tailwind/"` to your `app/assets/tailwind/application.css` file. > [!IMPORTANT] > You must `@import` the engine CSS files in your `app/assets/tailwind/application.css` for the engine to be included in the build. By default, no engine CSS files are imported, and you must opt-in to using the file in your build. diff --git a/lib/tailwindcss/engine.rb b/lib/tailwindcss/engine.rb index 001004f..7b88c5f 100644 --- a/lib/tailwindcss/engine.rb +++ b/lib/tailwindcss/engine.rb @@ -15,9 +15,5 @@ class Engine < ::Rails::Engine config.app_generators do |g| g.template_engine :tailwindcss end - - config.after_initialize do - Tailwindcss::Engines.bundle - end end end diff --git a/lib/tasks/build.rake b/lib/tasks/build.rake index 603c805..d176f2c 100644 --- a/lib/tasks/build.rake +++ b/lib/tasks/build.rake @@ -1,6 +1,6 @@ namespace :tailwindcss do desc "Build your Tailwind CSS" - task build: :environment do |_, args| + task build: [:environment, :engines] do |_, args| debug = args.extras.include?("debug") verbose = args.extras.include?("verbose") @@ -12,7 +12,7 @@ namespace :tailwindcss do end desc "Watch and build your Tailwind CSS on file changes" - task watch: :environment do |_, args| + task watch: [:environment, :engines] do |_, args| debug = args.extras.include?("debug") poll = args.extras.include?("poll") always = args.extras.include?("always") @@ -26,6 +26,11 @@ namespace :tailwindcss do rescue Interrupt puts "Received interrupt, exiting tailwindcss:watch" if args.extras.include?("verbose") end + + desc "Create Tailwind CSS entry point files for Rails Engines" + task engines: :environment do + Tailwindcss::Engines.bundle + end end Rake::Task["assets:precompile"].enhance(["tailwindcss:build"]) From c05cbdcf5094c8ae37fb7357b3292aabc5b2cfdf Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 15 Jun 2025 22:01:52 -0400 Subject: [PATCH 11/12] version bump to v4.3.0.rc2 --- CHANGELOG.md | 4 ++-- lib/tailwindcss/version.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c11a6f..95fd82a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ # `tailwindcss-rails` Changelog -## v4.3.0.rc1 +## v4.3.0.rc2 ### Improved -* Experimental support for Rails engines. Feedback is welcome at #355. #554 @bopm +* Experimental support for Rails engines. Feedback is welcome at #355. #554, #556 @bopm ## v4.2.3 / 2025-05-02 diff --git a/lib/tailwindcss/version.rb b/lib/tailwindcss/version.rb index b4acf0d..d1d65d1 100644 --- a/lib/tailwindcss/version.rb +++ b/lib/tailwindcss/version.rb @@ -1,3 +1,3 @@ module Tailwindcss - VERSION = "4.3.0.rc1" + VERSION = "4.3.0.rc2" end From 71b5fe500c5bdfd73766cdb1ea4ce506b5213e22 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 16 Jun 2025 16:00:29 +0000 Subject: [PATCH 12/12] build(deps): bump tailwindcss-ruby from 4.1.8 to 4.1.10 Bumps [tailwindcss-ruby](https://github.com/flavorjones/tailwindcss-ruby) from 4.1.8 to 4.1.10. - [Release notes](https://github.com/flavorjones/tailwindcss-ruby/releases) - [Changelog](https://github.com/flavorjones/tailwindcss-ruby/blob/main/CHANGELOG.md) - [Commits](https://github.com/flavorjones/tailwindcss-ruby/compare/v4.1.8...v4.1.10) --- updated-dependencies: - dependency-name: tailwindcss-ruby dependency-version: 4.1.10 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- Gemfile.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 22cbba3..dc9444f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,11 +140,11 @@ GEM io-console (~> 0.5) securerandom (0.4.1) stringio (3.1.5) - tailwindcss-ruby (4.1.8) - tailwindcss-ruby (4.1.8-aarch64-linux-gnu) - tailwindcss-ruby (4.1.8-arm64-darwin) - tailwindcss-ruby (4.1.8-x86_64-darwin) - tailwindcss-ruby (4.1.8-x86_64-linux-gnu) + tailwindcss-ruby (4.1.10) + tailwindcss-ruby (4.1.10-aarch64-linux-gnu) + tailwindcss-ruby (4.1.10-arm64-darwin) + tailwindcss-ruby (4.1.10-x86_64-darwin) + tailwindcss-ruby (4.1.10-x86_64-linux-gnu) thor (1.3.2) timeout (0.4.3) tzinfo (2.0.6)