Skip to content

Commit be03a48

Browse files
committed
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.
1 parent 7fb4975 commit be03a48

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/tailwindcss/commands.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
module Tailwindcss
44
module Commands
5+
INPUT_FILE = "app/assets/tailwind/application.css"
6+
OUTPUT_FILE = "app/assets/builds/tailwind.css"
7+
58
class << self
69
def compile_command(debug: false, **kwargs)
710
debug = ENV["TAILWINDCSS_DEBUG"].present? if ENV.key?("TAILWINDCSS_DEBUG")
811
rails_root = defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
912

1013
command = [
1114
Tailwindcss::Ruby.executable(**kwargs),
12-
"-i", rails_root.join("app/assets/tailwind/application.css").to_s,
13-
"-o", rails_root.join("app/assets/builds/tailwind.css").to_s,
15+
"-i", rails_root.join(input_file).to_s,
16+
"-o", rails_root.join(output_file).to_s,
1417
]
1518

1619
command << "--minify" unless (debug || rails_css_compressor?)
@@ -38,6 +41,14 @@ def command_env(verbose:)
3841
def rails_css_compressor?
3942
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
4043
end
44+
45+
def input_file
46+
ENV.fetch("TAILWINDCSS_INPUT_FILE", INPUT_FILE)
47+
end
48+
49+
def output_file
50+
ENV.fetch("TAILWINDCSS_OUTPUT_FILE", OUTPUT_FILE)
51+
end
4152
end
4253
end
4354
end

test/lib/tailwindcss/commands_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ def setup
9595
end
9696
end
9797

98+
test ".compile_command file with & without input/output ENV variables" do
99+
begin
100+
Rails.stub(:root, File) do # Rails.root won't work in this test suite
101+
ENV["TAILWINDCSS_INPUT_FILE"] = nil
102+
ENV["TAILWINDCSS_OUTPUT_FILE"] = nil
103+
104+
command = Tailwindcss::Commands.compile_command.join(" ")
105+
106+
assert_includes(command, "-i #{Tailwindcss::Commands::INPUT_FILE}")
107+
assert_includes(command, "-o #{Tailwindcss::Commands::OUTPUT_FILE}")
108+
109+
ENV["TAILWINDCSS_INPUT_FILE"] = "path/to/my-input-file"
110+
ENV["TAILWINDCSS_OUTPUT_FILE"] = "path/to/my-output-file"
111+
112+
command = Tailwindcss::Commands.compile_command.join(" ")
113+
114+
assert_includes(command, "-i path/to/my-input-file")
115+
assert_includes(command, "-o path/to/my-output-file")
116+
end
117+
ensure
118+
ENV.delete('TAILWINDCSS_INPUT_FILE')
119+
ENV.delete('TAILWINDCSS_OUTPUT_FILE')
120+
end
121+
end
122+
98123
test ".watch_command" do
99124
Rails.stub(:root, File) do # Rails.root won't work in this test suite
100125
actual = Tailwindcss::Commands.watch_command

0 commit comments

Comments
 (0)