Skip to content

Commit 169e7f0

Browse files
committed
Move application.tailwind.css to a dir ignored by propshaft
This file should not be included in the stylesheet link tags served to user agents. Fixes #470
1 parent 11438d6 commit 169e7f0

File tree

7 files changed

+52
-38
lines changed

7 files changed

+52
-38
lines changed

README.md

+19-21
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ Then, run the `tailwindcss:upgrade` task. Among other things, this will try to r
101101
Here's what the upgrade task does:
102102

103103
- Cleans up some things in the generated `config/tailwind.config.js`.
104-
- Runs the upstream upgrader (note: requires `npx` to run the one-time upgrade, but highly recommended).
105-
- Removes references to the Inter font from the application layout.
106104
- If present, moves `config/postcss.config.js` to the root directory.
105+
- If present, moves `app/assets/stylesheets/application.tailwind.css` to `app/assets/tailwind`.
106+
- Removes unnecessary `stylesheet_link_tag "tailwindcss"` tags from the application layout.
107+
- Removes references to the Inter font from the application layout.
108+
- Runs the upstream upgrader (note: requires `npx` to run the one-time upgrade, but highly recommended).
107109

108110
Here's what that upgrade looks like on a vanilla Rails app:
109111

@@ -112,29 +114,34 @@ $ bin/rails tailwindcss:upgrade
112114
apply /path/to/tailwindcss-rails/lib/install/upgrade_tailwindcss.rb
113115
Removing references to 'defaultTheme' from /home/user/myapp/config/tailwind.config.js
114116
gsub config/tailwind.config.js
117+
Strip Inter font CSS from application layout
118+
gsub app/views/layouts/application.html.erb
119+
Remove unnecessary stylesheet_link_tag from application layout
120+
gsub app/views/layouts/application.html.erb
121+
Moving application.tailwind.css to /home/user/myapp/app/assets/tailwind
122+
create app/assets/tailwind/application.tailwind.css
123+
remove app/assets/stylesheets/application.tailwind.css
124+
10.9.0
115125
Running the upstream Tailwind CSS upgrader
116126
run npx @tailwindcss/upgrade@next --force --config /home/user/myapp/config/tailwind.config.js from "."
117127
≈ tailwindcss v4.0.0
118128
│ Searching for CSS files in the current directory and its subdirectories…
119-
│ ↳ Linked `./config/tailwind.config.js` to `./app/assets/stylesheets/application.tailwind.css`
129+
│ ↳ Linked `./config/tailwind.config.js` to `./app/assets/tailwind/application.tailwind.css`
120130
│ Migrating JavaScript configuration files…
121131
│ ↳ The configuration file at `./config/tailwind.config.js` could not be automatically migrated to the new CSS
122132
│ configuration format, so your CSS has been updated to load your existing configuration file.
123133
│ Migrating templates…
124134
│ ↳ Migrated templates for configuration file: `./config/tailwind.config.js`
125135
│ Migrating stylesheets…
126-
│ ↳ Migrated stylesheet: `./app/assets/stylesheets/application.tailwind.css`
136+
│ ↳ Migrated stylesheet: `./app/assets/tailwind/application.tailwind.css`
127137
│ ↳ No PostCSS config found, skipping migration.
128138
│ Updating dependencies…
129139
│ Could not detect a package manager. Please manually update `tailwindcss` to v4.
130140
│ Verify the changes and commit them to your repository.
131-
Strip Inter font CSS from application layout
132-
gsub app/views/layouts/application.html.erb
133141
Compile initial Tailwind build
134142
run rails tailwindcss:build from "."
135143
≈ tailwindcss v4.0.0
136-
137-
Done in 52ms
144+
Done in 56ms
138145
run bundle install --quiet
139146
```
140147
@@ -157,9 +164,11 @@ We'll try to improve the upgrade process over time, but for now you may need to
157164
158165
### Configuration and commands
159166
160-
#### Input file: `app/assets/stylesheets/application.tailwind.css`
167+
#### Input file: `app/assets/tailwind/application.tailwind.css`
168+
169+
The `tailwindcss:install` task will generate a Tailwind input file in `app/assets/tailwind/application.tailwind.css`. This is where you import the plugins you want to use and where you can setup your custom `@apply` rules.
161170
162-
The installer will generate a Tailwind input file in `app/assets/stylesheets/application.tailwind.css`. This is where you import the plugins you want to use and where you can setup your custom `@apply` rules.
171+
The location of this file changed in v4, from `app/assets/stylesheets` to `app/assets/tailwind`. The `tailwindcss:upgrade` task will move it for you.
163172
164173
#### Output file: `app/assets/builds/tailwind.css`
165174
@@ -335,17 +344,6 @@ The inline version also works:
335344
<section class="bg-[url('image.svg')]">Has the image as it's background</section>
336345
```
337346
338-
### Conflict with pre-existing asset pipeline stylesheets
339-
340-
If you get a warning `Unrecognized at-rule or error parsing at-rule ‘@tailwind’.` in the browser console after installation, you are incorrectly double-processing `application.tailwind.css`. This is a misconfiguration, even though the styles will be fully effective in many cases.
341-
342-
The file `application.tailwind.css` is installed when running `rails tailwindcss:install` and is placed alongside the common `application.css` in `app/assets/stylesheets`. Because the `application.css` in a newly generated Rails app includes a `require_tree .` directive, the asset pipeline incorrectly processes `application.tailwind.css`, where it should be taken care of by `tailwindcss`. The asset pipeline ignores TailwindCSS's at-directives, and the browser can't process them.
343-
344-
To fix the warning, you can either remove the `application.css`, if you don't plan to use the asset pipeline for stylesheets, and instead rely on TailwindCSS completely for styles. This is what this installer assumes.
345-
346-
Or, if you do want to keep using the asset pipeline in parallel, make sure to remove the `require_tree .` line from the `application.css`.
347-
348-
349347
## License
350348
351349
Tailwind for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).

lib/install/install_tailwindcss.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
22
CENTERING_CONTAINER_INSERTION_POINT = /^\s*<%= yield %>/.freeze
3+
TAILWIND_ASSET_PATH = Rails.root.join("app/assets/tailwind")
34

45
if APPLICATION_LAYOUT_PATH.exist?
56
unless File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag :app/)
@@ -31,9 +32,9 @@
3132
append_to_file(".gitignore", %(\n/app/assets/builds/*\n!/app/assets/builds/.keep\n))
3233
end
3334

34-
unless Rails.root.join("app/assets/stylesheets/application.tailwind.css").exist?
35-
say "Add default app/assets/stylesheets/application.tailwind.css"
36-
copy_file "#{__dir__}/application.tailwind.css", "app/assets/stylesheets/application.tailwind.css"
35+
unless TAILWIND_ASSET_PATH.join("application.tailwind.css").exist?
36+
say "Add default #{TAILWIND_ASSET_PATH}/application.tailwind.css"
37+
copy_file "#{__dir__}/application.tailwind.css", TAILWIND_ASSET_PATH.join("application.tailwind.css")
3738
end
3839

3940
if Rails.root.join("Procfile.dev").exist?

lib/install/upgrade_tailwindcss.rb

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
TAILWIND_CONFIG_PATH = Rails.root.join("config/tailwind.config.js")
22
APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
33
POSTCSS_CONFIG_PATH = Rails.root.join("config/postcss.config.js")
4+
OLD_TAILWIND_ASSET_PATH = Rails.root.join("app/assets/stylesheets")
5+
TAILWIND_ASSET_PATH = Rails.root.join("app/assets/tailwind")
46

57
unless TAILWIND_CONFIG_PATH.exist?
68
say "Default tailwind.config.js is missing!", :red
@@ -14,27 +16,34 @@
1416

1517
if POSTCSS_CONFIG_PATH.exist?
1618
say "Moving PostCSS configuration to application root directory"
17-
FileUtils.mv(POSTCSS_CONFIG_PATH, Rails.root, verbose: true) || abort
19+
copy_file POSTCSS_CONFIG_PATH, Rails.root.join("postcss.config.js")
20+
remove_file POSTCSS_CONFIG_PATH
1821
end
1922

2023
if APPLICATION_LAYOUT_PATH.exist?
21-
if File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag :app/) &&
22-
File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag "tailwind"/)
23-
say "Remove unnecessary stylesheet_link_tag from application layout"
24-
gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{^\s*<%= stylesheet_link_tag "tailwind".*%>$}, ""
25-
end
26-
2724
if File.read(APPLICATION_LAYOUT_PATH).match?(/"inter-font"/)
2825
say "Strip Inter font CSS from application layout"
2926
gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{, "inter-font"}, ""
3027
else
3128
say "Inter font CSS not detected.", :green
3229
end
30+
31+
if File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag :app/) &&
32+
File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag "tailwind"/)
33+
say "Remove unnecessary stylesheet_link_tag from application layout"
34+
gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{^\s*<%= stylesheet_link_tag "tailwind".*%>$}, ""
35+
end
3336
else
3437
say "Default application.html.erb is missing!", :red
3538
say %( Please check your layouts and remove any "inter-font" stylesheet links.)
3639
end
3740

41+
if OLD_TAILWIND_ASSET_PATH.join("application.tailwind.css").exist?
42+
say "Moving application.tailwind.css to #{TAILWIND_ASSET_PATH}"
43+
copy_file OLD_TAILWIND_ASSET_PATH.join("application.tailwind.css"), TAILWIND_ASSET_PATH.join("application.tailwind.css")
44+
remove_file OLD_TAILWIND_ASSET_PATH.join("application.tailwind.css")
45+
end
46+
3847
if system("npx --version")
3948
say "Running the upstream Tailwind CSS upgrader"
4049
command = Shellwords.join(["npx", "@tailwindcss/upgrade@next", "--force", "--config", TAILWIND_CONFIG_PATH.to_s])

lib/tailwindcss/commands.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def compile_command(debug: false, **kwargs)
88

99
command = [
1010
Tailwindcss::Ruby.executable(**kwargs),
11-
"-i", rails_root.join("app/assets/stylesheets/application.tailwind.css").to_s,
11+
"-i", rails_root.join("app/assets/tailwind/application.tailwind.css").to_s,
1212
"-o", rails_root.join("app/assets/builds/tailwind.css").to_s,
1313
]
1414

lib/tailwindcss/engine.rb

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class Engine < ::Rails::Engine
66
Rails.application.config.generators.stylesheets = false
77
end
88

9+
initializer "tailwindcss.exclude_asset_path", after: "propshaft.append_assets_path" do
10+
Rails.application.config.assets.excluded_paths << Rails.root.join("app/assets/tailwind")
11+
end
12+
913
config.app_generators do |g|
1014
g.template_engine :tailwindcss
1115
end

test/integration/user_install_test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bin/rails tailwindcss:install
3737

3838
# TEST: tailwind was installed correctly
3939
grep -q "<main class=\"container" app/views/layouts/application.html.erb
40-
test -a app/assets/stylesheets/application.tailwind.css
40+
test -a app/assets/tailwind/application.tailwind.css
4141

4242
# TEST: rake tasks don't exec (#188)
4343
cat <<EOF >> Rakefile
@@ -46,7 +46,7 @@ task :still_here do
4646
end
4747
EOF
4848

49-
cat >> app/assets/stylesheets/application.tailwind.css <<EOF
49+
cat >> app/assets/tailwind/application.tailwind.css <<EOF
5050
@theme { --color-special: #abc12399; }
5151
EOF
5252

test/integration/user_upgrade_test.sh

+6-4
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ if grep -q inter-font app/views/layouts/application.html.erb ; then
7575
fi
7676

7777
# TEST: moving the postcss file
78-
if [ ! -f postcss.config.js ] ; then
79-
echo "FAIL: postcss.config.js not moved"
80-
exit 1
81-
fi
78+
test ! -a config/postcss.config.js
79+
test -a postcss.config.js
80+
81+
# TEST: moving application.tailwind.css
82+
test ! -a app/assets/stylesheets/application.tailwind.css
83+
test -a app/assets/tailwind/application.tailwind.css
8284

8385
# generate CSS
8486
bin/rails tailwindcss:build[verbose]

0 commit comments

Comments
 (0)