Skip to content

Commit 6a622ef

Browse files
committed
feat: make input and output filenames for build and watch configurable
1 parent 114fca5 commit 6a622ef

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

lib/tailwindcss/commands.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
module Tailwindcss
44
module Commands
55
class << self
6-
def compile_command(debug: false, **kwargs)
6+
def compile_command(debug: false, namespace: nil, **kwargs)
77
debug = ENV["TAILWINDCSS_DEBUG"].present? if ENV.key?("TAILWINDCSS_DEBUG")
88
rails_root = defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
99

10+
input_filename, output_filename = get_input_and_output_filename(namespace)
11+
1012
command = [
1113
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,
14+
"-i", rails_root.join("app/assets/tailwind/#{input_filename}.css").to_s,
15+
"-o", rails_root.join("app/assets/builds/#{output_filename}.css").to_s,
1416
]
1517

1618
command << "--minify" unless (debug || rails_css_compressor?)
@@ -35,9 +37,27 @@ def command_env(verbose:)
3537
end
3638
end
3739

40+
private
41+
3842
def rails_css_compressor?
3943
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
4044
end
45+
46+
def get_input_and_output_filename(namespace)
47+
if namespace != nil
48+
unless File.exist?("app/assets/tailwind/#{namespace}.css")
49+
raise "No file named #{namespace}.css exists in app/assets/tailwind"
50+
end
51+
52+
if namespace == "application"
53+
raise "application.css is the default input file for tailwind and cannot be used as namespace. If you want the normal behaviour of tailwindcss (to compile application.css into tailwind.css), just omit the namespace parameter."
54+
end
55+
end
56+
57+
input_filename = namespace || "application"
58+
output_filename = namespace || "tailwind"
59+
return input_filename, output_filename
60+
end
4161
end
4262
end
4363
end

lib/tasks/build.rake

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ namespace :tailwindcss do
44
debug = args.extras.include?("debug")
55
verbose = args.extras.include?("verbose")
66

7-
command = Tailwindcss::Commands.compile_command(debug: debug)
7+
namespace = get_namespace_from_extras(args.extras)
8+
9+
command = Tailwindcss::Commands.compile_command(debug: debug, namespace: namespace)
810
env = Tailwindcss::Commands.command_env(verbose: verbose)
911
puts "Running: #{Shellwords.join(command)}" if verbose
1012

@@ -18,14 +20,25 @@ namespace :tailwindcss do
1820
always = args.extras.include?("always")
1921
verbose = args.extras.include?("verbose")
2022

21-
command = Tailwindcss::Commands.watch_command(always: always, debug: debug, poll: poll)
23+
namespace = get_namespace_from_extras(args.extras)
24+
25+
command = Tailwindcss::Commands.watch_command(always: always, debug: debug, namespace: namespace, poll: poll)
2226
env = Tailwindcss::Commands.command_env(verbose: verbose)
2327
puts "Running: #{Shellwords.join(command)}" if verbose
2428

2529
system(env, *command)
2630
rescue Interrupt
2731
puts "Received interrupt, exiting tailwindcss:watch" if args.extras.include?("verbose")
2832
end
33+
34+
private
35+
36+
def get_namespace_from_extras(extras)
37+
namespace_key_and_value = extras.detect{ |a| a.start_with?('namespace=') }
38+
if namespace_key_and_value != nil
39+
namespace_key_and_value.split('=')[1]
40+
end
41+
end
2942
end
3043

3144
Rake::Task["assets:precompile"].enhance(["tailwindcss:build"])

0 commit comments

Comments
 (0)