Skip to content

Commit

Permalink
feat: tailwind will not minify when Rails CSS compression is on
Browse files Browse the repository at this point in the history
to avoid double-compression which breaks styles.

Fixes #243

See rails/rails#47553
  • Loading branch information
flavorjones committed Mar 2, 2023
1 parent 05c5753 commit 8159bd8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/tailwindcss/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def compile_command(debug: false, **kwargs)
"-o", Rails.root.join("app/assets/builds/tailwind.css").to_s,
"-c", Rails.root.join("config/tailwind.config.js").to_s,
].tap do |command|
command << "--minify" unless debug
command << "--minify" unless (debug || rails_css_compressor?)
end
end

Expand All @@ -72,6 +72,10 @@ def watch_command(poll: false, **kwargs)
command << "-p" if poll
end
end

def rails_css_compressor?
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
end
end
end
end
4 changes: 2 additions & 2 deletions lib/tasks/build.rake
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace :tailwindcss do
desc "Build your Tailwind CSS"
task :build do |_, args|
task build: :environment do |_, args|
debug = args.extras.include?("debug")
command = Tailwindcss::Commands.compile_command(debug: debug)
puts command.inspect if args.extras.include?("verbose")
system(*command, exception: true)
end

desc "Watch and build your Tailwind CSS on file changes"
task :watch do |_, args|
task watch: :environment do |_, args|
debug = args.extras.include?("debug")
poll = args.extras.include?("poll")
command = Tailwindcss::Commands.watch_command(debug: debug, poll: poll)
Expand Down
18 changes: 18 additions & 0 deletions test/lib/tailwindcss/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ def mock_exe_directory(platform)
end
end

test ".compile_command when Rails compression is on" do
mock_exe_directory("sparc-solaris2.8") do |dir, executable|
Rails.stub(:root, File) do # Rails.root won't work in this test suite
Tailwindcss::Commands.stub(:rails_css_compressor?, true) do
actual = Tailwindcss::Commands.compile_command(exe_path: dir)
assert_kind_of(Array, actual)
refute_includes(actual, "--minify")
end

Tailwindcss::Commands.stub(:rails_css_compressor?, false) do
actual = Tailwindcss::Commands.compile_command(exe_path: dir)
assert_kind_of(Array, actual)
assert_includes(actual, "--minify")
end
end
end
end

test ".watch_command" do
mock_exe_directory("sparc-solaris2.8") do |dir, executable|
Rails.stub(:root, File) do # Rails.root won't work in this test suite
Expand Down

0 comments on commit 8159bd8

Please sign in to comment.