Skip to content

Commit 61f3596

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 61f3596

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

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

+10-1
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,7 +16,8 @@
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?
@@ -35,6 +38,12 @@
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

+1-1
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

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)