Skip to content

Commit 876b82b

Browse files
committed
Implement Engines Support
1 parent 9d2127a commit 876b82b

File tree

7 files changed

+325
-66
lines changed

7 files changed

+325
-66
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
tailwindcss-rails (4.2.2)
4+
tailwindcss-rails (4.2.3)
55
railties (>= 7.0.0)
66
tailwindcss-ruby (~> 4.0)
77

README.md

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77
<!-- toc -->
88

99
- [Installation](#installation)
10-
* [Choosing a specific version of `tailwindcss`](#choosing-a-specific-version-of-tailwindcss)
11-
* [Using a local installation of `tailwindcss`](#using-a-local-installation-of-tailwindcss)
10+
- [Choosing a specific version of `tailwindcss`](#choosing-a-specific-version-of-tailwindcss)
11+
- [Using a local installation of `tailwindcss`](#using-a-local-installation-of-tailwindcss)
1212
- [Upgrading your application from Tailwind v3 to v4](#upgrading-your-application-from-tailwind-v3-to-v4)
13-
* [You don't _have_ to upgrade](#you-dont-_have_-to-upgrade)
14-
* [Upgrade steps](#upgrade-steps)
15-
* [Troubleshooting a v4 upgrade](#troubleshooting-a-v4-upgrade)
16-
* [Updating CSS class names for v4](#updating-css-class-names-for-v4)
13+
- [You don't _have_ to upgrade](#you-dont-_have_-to-upgrade)
14+
- [Upgrade steps](#upgrade-steps)
15+
- [Troubleshooting a v4 upgrade](#troubleshooting-a-v4-upgrade)
16+
- [Updating CSS class names for v4](#updating-css-class-names-for-v4)
1717
- [Developing with Tailwindcss](#developing-with-tailwindcss)
18-
* [Configuration and commands](#configuration-and-commands)
19-
* [Building for production](#building-for-production)
20-
* [Building for testing](#building-for-testing)
21-
* [Building unminified assets](#building-unminified-assets)
22-
* [Live rebuild](#live-rebuild)
23-
* [Using Tailwind plugins](#using-tailwind-plugins)
24-
* [Using with PostCSS](#using-with-postcss)
25-
* [Custom inputs or outputs](#custom-inputs-or-outputs)
18+
- [Configuration and commands](#configuration-and-commands)
19+
- [Building for production](#building-for-production)
20+
- [Building for testing](#building-for-testing)
21+
- [Building unminified assets](#building-unminified-assets)
22+
- [Live rebuild](#live-rebuild)
23+
- [Using Tailwind plugins](#using-tailwind-plugins)
24+
- [Using with PostCSS](#using-with-postcss)
25+
- [Custom inputs or outputs](#custom-inputs-or-outputs)
2626
- [Troubleshooting](#troubleshooting)
27-
* [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)
28-
* [Running in a docker container exits prematurely](#running-in-a-docker-container-exits-prematurely)
29-
* [Conflict with sassc-rails](#conflict-with-sassc-rails)
30-
* [Class names must be spelled out](#class-names-must-be-spelled-out)
31-
* [`ERROR: Cannot find the tailwindcss executable` for supported platform](#error-cannot-find-the-tailwindcss-executable-for-supported-platform)
32-
* [Using asset-pipeline assets](#using-asset-pipeline-assets)
27+
- [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)
28+
- [Running in a docker container exits prematurely](#running-in-a-docker-container-exits-prematurely)
29+
- [Conflict with sassc-rails](#conflict-with-sassc-rails)
30+
- [Class names must be spelled out](#class-names-must-be-spelled-out)
31+
- [`ERROR: Cannot find the tailwindcss executable` for supported platform](#error-cannot-find-the-tailwindcss-executable-for-supported-platform)
32+
- [Using asset-pipeline assets](#using-asset-pipeline-assets)
3333
- [License](#license)
3434

3535
<!-- tocstop -->
@@ -43,12 +43,11 @@ With Rails 7 you can generate a new application preconfigured with Tailwind CSS
4343

4444
This gem depends on the `tailwindcss-ruby` gem to install a working Tailwind CLI executable.
4545

46-
4746
### Choosing a specific version of `tailwindcss`
4847

4948
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,
5049

51-
``` ruby
50+
```ruby
5251
gem "tailwindcss-rails"
5352

5453
# pin to tailwindcss version 3.4.13
@@ -59,7 +58,6 @@ gem "tailwindcss-ruby", "3.4.13"
5958

6059
You can also use a local (npm-based) installation if you prefer, please go to https://github.com/flavorjones/tailwindcss-ruby for more information.
6160

62-
6361
## Upgrading your application from Tailwind v3 to v4
6462

6563
v4.x of this gem has been updated to work with Tailwind v4, including providing some help with upgrading your application.
@@ -72,27 +70,25 @@ This gem will help with some of the mechanics of the upgrade:
7270
- update some local project files to meet some Tailwind CSS v4 conventions,
7371
- attempt to run the [upstream v4 upgrade tool](https://tailwindcss.com/docs/upgrade-guide#using-the-upgrade-tool).
7472

75-
7673
### You don't _have_ to upgrade
7774

7875
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.
7976

8077
If you don't want to upgrade, then pin your application to v3.3.1 of this gem:
8178

82-
``` ruby
79+
```ruby
8380
# Gemfile
8481
gem "tailwindcss-rails", "~> 3.3.1" # which transitively pins tailwindcss-ruby to v3
8582
```
8683

8784
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`:
8885

89-
``` ruby
86+
```ruby
9087
# Gemfile
9188
gem "tailwindcss-rails", "~> 3.3"
9289
gem "tailwindcss-ruby", "~> 3.4" # only necessary with tailwindcss-rails <= 3.3.0
9390
```
9491

95-
9692
### Upgrade steps
9793

9894
> [!WARNING]
@@ -101,8 +97,8 @@ gem "tailwindcss-ruby", "~> 3.4" # only necessary with tailwindcss-rails <= 3.3.
10197
First, update to `tailwindcss-rails` v4.0.0 or higher. This will also ensure you're transitively depending on `tailwindcss-ruby` v4.
10298

10399
```html
104-
# Gemfile
105-
gem "tailwindcss-rails", "~> 4.0" # which transitively pins tailwindcss-ruby to v4
100+
# Gemfile gem "tailwindcss-rails", "~> 4.0" # which transitively pins
101+
tailwindcss-ruby to v4
106102
```
107103

108104
**Update** path references to any existing css files imported in `app/assets/stylesheets/application.tailwind.css` so that they will resolve when the file is moved to `app/assets/tailwind/application.css`.
@@ -114,7 +110,7 @@ gem "tailwindcss-rails", "~> 4.0" # which transitively pins tailwindcss-ruby to
114110

115111
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.
116112

117-
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.
113+
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.
118114

119115
<details>
120116
<summary>Here's a detailed list of what the upgrade task does.</summary>
@@ -131,7 +127,7 @@ Then, run `bin/rails tailwindcss:upgrade`. Among other things, this will try to
131127
<details>
132128
<summary>Here's what that upgrade looks like on a vanilla Rails app.</summary>
133129

134-
``` sh
130+
```sh
135131
$ bin/rails tailwindcss:upgrade
136132
apply /path/to/tailwindcss-rails/lib/install/upgrade_tailwindcss.rb
137133
Removing references to 'defaultTheme' from /home/user/myapp/config/tailwind.config.js
@@ -166,12 +162,11 @@ $ bin/rails tailwindcss:upgrade
166162
Done in 56ms
167163
run bundle install --quiet
168164
```
169-
</details>
170165
166+
</details>
171167
172168
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).
173169
174-
175170
### Troubleshooting a v4 upgrade
176171
177172
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 +177,6 @@ We know there are some cases we haven't addressed with the upgrade task:
182177
183178
We'll try to improve the upgrade process over time, but for now you may need to do some manual work to upgrade.
184179
185-
186180
### Updating CSS class names for v4
187181
188182
> [!NOTE]
@@ -208,9 +202,9 @@ With some additional manual work the upstream upgrade tool will update your appl
208202
"@tailwindcss/aspect-ratio": "^0.4.2",
209203
"@tailwindcss/container-queries": "^0.1.1",
210204
"@tailwindcss/forms": "^0.5.10",
211-
"@tailwindcss/typography": "^0.5.16"
205+
"@tailwindcss/typography": "^0.5.16",
212206
// And so on...
213-
}
207+
},
214208
}
215209
```
216210
@@ -280,12 +274,10 @@ This gem also generates a `Procfile.dev` file which will run both the rails serv
280274
281275
The `tailwindcss:build` is automatically attached to `assets:precompile`, so before the asset pipeline digests the files, the Tailwind output will be generated.
282276
283-
284277
### Building for testing
285278
286279
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.
287280
288-
289281
### Building unminified assets
290282
291283
If you want unminified assets, you can:
@@ -303,7 +295,6 @@ While you're developing your application, you want to run Tailwind in "watch" mo
303295
- or run `rails tailwindcss:watch` as a separate process,
304296
- or run `bin/dev` which uses [Foreman](https://github.com/ddollar/foreman)
305297
306-
307298
#### Puma plugin
308299
309300
This gem ships with a Puma plugin. To use it, add this line to your `puma.rb` configuration:
@@ -314,7 +305,6 @@ plugin :tailwindcss if ENV.fetch("RAILS_ENV", "development") == "development"
314305
315306
and then running `rails server` (or just `puma`) will run the Tailwind watch process in the background.
316307
317-
318308
#### Run `rails tailwindcss:watch`
319309
320310
This is a flexible command, which can be run with a few different options.
@@ -331,38 +321,35 @@ If you are running `rails tailwindcss:watch` as a process in a Docker container,
331321
332322
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`.
333323
334-
335324
#### Foreman
336325
337326
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.
338327
339-
340328
### Using Tailwind plugins
341329
342330
If you want to use Tailwind plugins, they can be installed using `package.json`.
343331
344332
Using Yarn:
345333
346-
``` sh
334+
```sh
347335
[ ! -f package.json ] && yarn init
348336
yarn add daisyui # example
349337
```
350338
351339
Using npm:
352340
353-
``` sh
341+
```sh
354342
npm init
355343
npm add daisyui # example
356344
```
357345
358346
Than use `@plugin` annotation in `app/assets/tailwind/application.css`:
359347
360-
``` css
348+
```css
361349
@import "tailwindcss";
362350
@plugin "daisyui";
363351
```
364352
365-
366353
### Using with PostCSS
367354
368355
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 +361,8 @@ For example, to enable nesting:
374361
export default {
375362
plugins: {
376363
"@tailwindcss/postcss": {},
377-
}
378-
}
364+
},
365+
};
379366
```
380367
381368
⚠ 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,12 +382,14 @@ export default {
395382
396383
Then you can use yarn or npm to install the dependencies.
397384
385+
### Rails Engines support
386+
387+
If you have Rails Engines in your application that use Tailwind CSS and provide an `app/assets/tailwind/<engine_name>/application.css` file, entry point files will be created for each of them in `app/assets/builds/tailwind/<engine_name>.css` so they can be included in your host application's Tailwind CSS by adding `@import "../../assets/builds/tailwind/<engine_name>"` to your `app/assets/tailwind/application.css` file.
398388
399389
### Custom inputs or outputs
400390
401391
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.
402392
403-
404393
## Troubleshooting
405394
406395
When having trouble with `tailwindcss:build` or `tailwindcss:watch`, the first thing you should do is collect some diagnostic information by setting the "verbose" flag, which will emit:
@@ -410,7 +399,7 @@ When having trouble with `tailwindcss:build` or `tailwindcss:watch`, the first t
410399
411400
Here's what that looks like:
412401
413-
``` sh
402+
```sh
414403
$ bin/rails tailwindcss:build[verbose]
415404
416405
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 +421,28 @@ There is a [known issue](https://github.com/tailwindlabs/tailwindcss/issues/1724
432421
433422
Please try uninstalling `watchman` and try running the watch task again.
434423
435-
436424
### Lost keystrokes or hanging when using terminal-based debugging tools (e.g. IRB, Pry, `ruby/debug`...etc.) with the Puma plugin
437425
438426
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.
439427
440-
441428
### Running in a docker container exits prematurely
442429
443430
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.
444431
445432
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`.
446433
447-
448434
### Conflict with sassc-rails
449435
450436
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.
451437
452-
453438
### Class names must be spelled out
454439
455440
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).
456441
457-
458442
### `ERROR: Cannot find the tailwindcss executable` for supported platform
459443
460444
See https://github.com/flavorjones/tailwindcss-ruby for help.
461445
462-
463446
### Using asset-pipeline assets
464447
465448
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 +451,22 @@ To use assets from the pipeline, use `url(image.svg)`. [Since Sprockets v3.3.0](
468451
469452
```js
470453
module.exports = {
471-
theme: {
472-
extend: {
473-
backgroundImage: {
474-
'image': "url('image.svg')"
475-
}
476-
}
477-
}
478-
}
454+
theme: {
455+
extend: {
456+
backgroundImage: {
457+
image: "url('image.svg')",
458+
},
459+
},
460+
},
461+
};
479462
```
480463
481464
The inline version also works:
482465
483466
```html
484-
<section class="bg-[url('image.svg')]">Has the image as it's background</section>
467+
<section class="bg-[url('image.svg')]">
468+
Has the image as it's background
469+
</section>
485470
```
486471
487472
## License

lib/tailwindcss-rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module Tailwindcss
22
end
33

44
require_relative "tailwindcss/version"
5+
require_relative "tailwindcss/engines"
56
require_relative "tailwindcss/engine"
67
require_relative "tailwindcss/commands"

lib/tailwindcss/engine.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ class Engine < ::Rails::Engine
1515
config.app_generators do |g|
1616
g.template_engine :tailwindcss
1717
end
18+
19+
config.after_initialize do
20+
Tailwindcss::Engines.bundle
21+
end
1822
end
1923
end

lib/tailwindcss/engines.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Tailwindcss
2+
module Engines
3+
class << self
4+
def bundle
5+
FileUtils.mkdir_p(Rails.root.join("app/assets/builds/tailwind"))
6+
Rails::Engine.subclasses.select do |engine|
7+
engine.root.join("app/assets/tailwind/#{engine.engine_name}/application.css").exist?
8+
end.each do |engine|
9+
file_path = Rails.root.join("app/assets/builds/tailwind/#{engine.engine_name}.css")
10+
FileUtils.rm(file_path) if File.exist?(file_path)
11+
template = <<~TEMPLATE
12+
/* DO NOT MODIFY THIS FILE, it was auto-generated by tailwindcss-rails */
13+
14+
@import "#{engine.root.join("app/assets/tailwind/#{engine.engine_name}/application.css")}";
15+
TEMPLATE
16+
File.open(file_path, 'w') do |file|
17+
file.puts template
18+
end
19+
end
20+
end
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)