From 7ae7a06ad89a6137e6d4a22d5cdfb55f4bc35d44 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 24 Jan 2025 12:20:49 -0500 Subject: [PATCH] fix: unnecessary stylesheet_link_tag for tailwind In Rails 8, propshaft detects `:app` and loads all the css files under app/assets/stylesheets, so we may not need this explicit tag for tailwind. On installation, omit the tailwind stylesheet link tag if `:app` is already being passed to stylesheet_link_tag in the layout. On upgrade, remove the tailwind stylesheet link tag if `:app` is already being passed to stylesheet_link_tag in the layout. --- lib/install/install_tailwindcss.rb | 11 +++++++---- lib/install/upgrade_tailwindcss.rb | 6 ++++++ test/integration/user_install_test.sh | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/install/install_tailwindcss.rb b/lib/install/install_tailwindcss.rb index 61fe3fac..8957bc0e 100644 --- a/lib/install/install_tailwindcss.rb +++ b/lib/install/install_tailwindcss.rb @@ -2,11 +2,14 @@ CENTERING_CONTAINER_INSERTION_POINT = /^\s*<%= yield %>/.freeze if APPLICATION_LAYOUT_PATH.exist? - say "Add Tailwindcss include tags and container element in application layout" - insert_into_file APPLICATION_LAYOUT_PATH.to_s, <<~ERB.indent(4), before: /^\s*<%= stylesheet_link_tag/ - <%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %> - ERB + unless File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag :app/) + say "Add Tailwindcss include tags in application layout" + insert_into_file APPLICATION_LAYOUT_PATH.to_s, <<~ERB.indent(4), before: /^\s*<%= stylesheet_link_tag/ + <%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %> + ERB + end + say "Add Tailwindcss container element in application layout" if File.open(APPLICATION_LAYOUT_PATH).read =~ /\n\s*<%= yield %>\n\s*<\/body>/ insert_into_file APPLICATION_LAYOUT_PATH.to_s, %(
\n ), before: CENTERING_CONTAINER_INSERTION_POINT insert_into_file APPLICATION_LAYOUT_PATH.to_s, %(\n
), after: CENTERING_CONTAINER_INSERTION_POINT diff --git a/lib/install/upgrade_tailwindcss.rb b/lib/install/upgrade_tailwindcss.rb index 53440aee..631e05bc 100644 --- a/lib/install/upgrade_tailwindcss.rb +++ b/lib/install/upgrade_tailwindcss.rb @@ -18,6 +18,12 @@ end if APPLICATION_LAYOUT_PATH.exist? + if File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag :app/) && + File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag "tailwind"/) + say "Remove unnecessary stylesheet_link_tag from application layout" + gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{^\s*<%= stylesheet_link_tag "tailwind".*%>$}, "" + end + if File.read(APPLICATION_LAYOUT_PATH).match?(/"inter-font"/) say "Strip Inter font CSS from application layout" gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{, "inter-font"}, "" diff --git a/test/integration/user_install_test.sh b/test/integration/user_install_test.sh index 07ac8182..d670a106 100755 --- a/test/integration/user_install_test.sh +++ b/test/integration/user_install_test.sh @@ -36,7 +36,8 @@ bundle binstubs --all bin/rails tailwindcss:install # TEST: tailwind was installed correctly -grep -q tailwind app/views/layouts/application.html.erb +grep -q "
> Rakefile