From be03a48663b28da579420d26c890f49a7b491c39 Mon Sep 17 00:00:00 2001 From: Nathaniel Watts Date: Fri, 29 Aug 2025 18:49:30 -0500 Subject: [PATCH] Allow customizing input/output via env variables The Rails `assets:precompile` task internally calls `bin/rails tailwindcss:build`, which uses hardcoded input/output file paths. Previously, the only way to customize these paths was to bypass the Rails integration entirely and use the Tailwind CLI directly (via `bundle exec tailwindcss`). This change introduces TAILWINDCSS_INPUT_FILE and TAILWINDCSS_OUTPUT_FILE environment variables that allow users to override the default paths while still using the standard `assets:precompile` workflow. This is useful for projects with non-standard asset structures or when integrating with custom build pipelines. --- lib/tailwindcss/commands.rb | 15 +++++++++++++-- test/lib/tailwindcss/commands_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/tailwindcss/commands.rb b/lib/tailwindcss/commands.rb index 99ad30e0..9b6bc0e2 100644 --- a/lib/tailwindcss/commands.rb +++ b/lib/tailwindcss/commands.rb @@ -2,6 +2,9 @@ module Tailwindcss module Commands + INPUT_FILE = "app/assets/tailwind/application.css" + OUTPUT_FILE = "app/assets/builds/tailwind.css" + class << self def compile_command(debug: false, **kwargs) debug = ENV["TAILWINDCSS_DEBUG"].present? if ENV.key?("TAILWINDCSS_DEBUG") @@ -9,8 +12,8 @@ def compile_command(debug: false, **kwargs) command = [ Tailwindcss::Ruby.executable(**kwargs), - "-i", rails_root.join("app/assets/tailwind/application.css").to_s, - "-o", rails_root.join("app/assets/builds/tailwind.css").to_s, + "-i", rails_root.join(input_file).to_s, + "-o", rails_root.join(output_file).to_s, ] command << "--minify" unless (debug || rails_css_compressor?) @@ -38,6 +41,14 @@ def command_env(verbose:) def rails_css_compressor? defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present? end + + def input_file + ENV.fetch("TAILWINDCSS_INPUT_FILE", INPUT_FILE) + end + + def output_file + ENV.fetch("TAILWINDCSS_OUTPUT_FILE", OUTPUT_FILE) + end end end end diff --git a/test/lib/tailwindcss/commands_test.rb b/test/lib/tailwindcss/commands_test.rb index d09481a4..4487293b 100644 --- a/test/lib/tailwindcss/commands_test.rb +++ b/test/lib/tailwindcss/commands_test.rb @@ -95,6 +95,31 @@ def setup end end + test ".compile_command file with & without input/output ENV variables" do + begin + Rails.stub(:root, File) do # Rails.root won't work in this test suite + ENV["TAILWINDCSS_INPUT_FILE"] = nil + ENV["TAILWINDCSS_OUTPUT_FILE"] = nil + + command = Tailwindcss::Commands.compile_command.join(" ") + + assert_includes(command, "-i #{Tailwindcss::Commands::INPUT_FILE}") + assert_includes(command, "-o #{Tailwindcss::Commands::OUTPUT_FILE}") + + ENV["TAILWINDCSS_INPUT_FILE"] = "path/to/my-input-file" + ENV["TAILWINDCSS_OUTPUT_FILE"] = "path/to/my-output-file" + + command = Tailwindcss::Commands.compile_command.join(" ") + + assert_includes(command, "-i path/to/my-input-file") + assert_includes(command, "-o path/to/my-output-file") + end + ensure + ENV.delete('TAILWINDCSS_INPUT_FILE') + ENV.delete('TAILWINDCSS_OUTPUT_FILE') + end + end + test ".watch_command" do Rails.stub(:root, File) do # Rails.root won't work in this test suite actual = Tailwindcss::Commands.watch_command