From d91c6eefcd340d18a58e334ea73e888c512b77ec Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 12 Feb 2023 12:25:31 -0500 Subject: [PATCH 1/2] refactor: put rake task arg handling into methods --- lib/tailwindcss/commands.rb | 8 ++++++++ lib/tasks/build.rake | 6 +++--- test/lib/tailwindcss/commands_test.rb | 12 ++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/tailwindcss/commands.rb b/lib/tailwindcss/commands.rb index e7f34cb5..c162924b 100644 --- a/lib/tailwindcss/commands.rb +++ b/lib/tailwindcss/commands.rb @@ -72,6 +72,14 @@ def watch_command(poll: false, **kwargs) command << "-p" if poll end end + + def debug_option(rake_task_args_extras) + rake_task_args_extras.include?("debug") + end + + def poll_option(rake_task_args_extras) + rake_task_args_extras.include?("poll") + end end end end diff --git a/lib/tasks/build.rake b/lib/tasks/build.rake index bd407214..757d66df 100644 --- a/lib/tasks/build.rake +++ b/lib/tasks/build.rake @@ -1,7 +1,7 @@ namespace :tailwindcss do desc "Build your Tailwind CSS" task :build do |_, args| - debug = args.extras.include?("debug") + debug = Tailwindcss::Commands.debug_option(args.extras) command = Tailwindcss::Commands.compile_command(debug: debug) puts command.inspect if args.extras.include?("verbose") system(*command, exception: true) @@ -9,8 +9,8 @@ namespace :tailwindcss do desc "Watch and build your Tailwind CSS on file changes" task :watch do |_, args| - debug = args.extras.include?("debug") - poll = args.extras.include?("poll") + debug = Tailwindcss::Commands.debug_option(args.extras) + poll = Tailwindcss::Commands.poll_option(args.extras) command = Tailwindcss::Commands.watch_command(debug: debug, poll: poll) puts command.inspect if args.extras.include?("verbose") system(*command) diff --git a/test/lib/tailwindcss/commands_test.rb b/test/lib/tailwindcss/commands_test.rb index 7ff0c5c6..73151758 100644 --- a/test/lib/tailwindcss/commands_test.rb +++ b/test/lib/tailwindcss/commands_test.rb @@ -84,4 +84,16 @@ def mock_exe_directory(platform) end end end + + test ".debug_option" do + refute(Tailwindcss::Commands.debug_option([])) + assert(Tailwindcss::Commands.debug_option(["debug"])) + refute(Tailwindcss::Commands.debug_option(["poll"])) + end + + test ".poll_option" do + refute(Tailwindcss::Commands.poll_option([])) + refute(Tailwindcss::Commands.poll_option(["debug"])) + assert(Tailwindcss::Commands.poll_option(["poll"])) + end end From 6425cb73f565dc4ad918291b1575341be845d567 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 12 Feb 2023 12:31:47 -0500 Subject: [PATCH 2/2] feat: allow users to specify "never minification" --- README.md | 5 +++ lib/tailwindcss/commands.rb | 11 ++++++- test/lib/tailwindcss/commands_test.rb | 45 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db1a12cd..1a80e8d7 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,11 @@ If you want unminified assets, you can pass a `debug` argument to the rake task, Note that you can combine task options, e.g. `rails tailwindcss:watch[debug,poll]`. +### Never minify assets + +If you _always_ want unminified assets, you can set the environment variable `TAILWINDCSS_RAILS_NO_MINIFY` to something that [looks truthy](https://api.rubyonrails.org/classes/ActiveModel/Type/Boolean.html) (hint: "t" or "1" will work). + + ### Custom inputs or outputs 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. diff --git a/lib/tailwindcss/commands.rb b/lib/tailwindcss/commands.rb index c162924b..7d0f9d94 100644 --- a/lib/tailwindcss/commands.rb +++ b/lib/tailwindcss/commands.rb @@ -74,12 +74,21 @@ def watch_command(poll: false, **kwargs) end def debug_option(rake_task_args_extras) - rake_task_args_extras.include?("debug") + rake_task_args_extras.include?("debug") || truthy_string_value(ENV['TAILWINDCSS_RAILS_NO_MINIFY']) end def poll_option(rake_task_args_extras) rake_task_args_extras.include?("poll") end + + def truthy_string_value(value) + if !value.present? || + ["f", "F", "false", "FALSE", "off", "OFF", "0"].include?(value) + false + else + true + end + end end end end diff --git a/test/lib/tailwindcss/commands_test.rb b/test/lib/tailwindcss/commands_test.rb index 73151758..cee9b9d1 100644 --- a/test/lib/tailwindcss/commands_test.rb +++ b/test/lib/tailwindcss/commands_test.rb @@ -18,6 +18,14 @@ def mock_exe_directory(platform) end end + def with_env(env) + before = ENV.to_h.dup + env.each { |k, v| ENV[k] = v } + yield + ensure + ENV.replace(before) + end + test ".executable returns the absolute path to the binary" do mock_exe_directory("sparc-solaris2.8") do |dir, executable| expected = File.expand_path(File.join(dir, "sparc-solaris2.8", "tailwindcss")) @@ -89,6 +97,14 @@ def mock_exe_directory(platform) refute(Tailwindcss::Commands.debug_option([])) assert(Tailwindcss::Commands.debug_option(["debug"])) refute(Tailwindcss::Commands.debug_option(["poll"])) + + with_env({"TAILWINDCSS_RAILS_NO_MINIFY" => "f"}) do + refute(Tailwindcss::Commands.debug_option([])) + end + + with_env({"TAILWINDCSS_RAILS_NO_MINIFY" => "t"}) do + assert(Tailwindcss::Commands.debug_option([])) + end end test ".poll_option" do @@ -96,4 +112,33 @@ def mock_exe_directory(platform) refute(Tailwindcss::Commands.poll_option(["debug"])) assert(Tailwindcss::Commands.poll_option(["poll"])) end + + test ".truthy_string_value" do + [ + nil, + "", + "F", "FALSE", + "f", "false", + "off", "OFF", + "0", + ].each do |value| + refute( + Tailwindcss::Commands.truthy_string_value(value), + "#{value.inspect} should be falsey", + ) + end + + [ + "T", "TRUE", + "t", "true", + "on", "ON", + "1", + "anything not falsey really", + ].each do |value| + assert( + Tailwindcss::Commands.truthy_string_value(value), + "#{value.inspect} should be truthy", + ) + end + end end