From 6a622efb67cae0cfbd1039db7b163f47424dc9be Mon Sep 17 00:00:00 2001 From: Jan Nash Date: Sun, 4 May 2025 09:53:43 +0200 Subject: [PATCH] feat: make input and output filenames for build and watch configurable --- lib/tailwindcss/commands.rb | 26 +++++++++++++++++++++++--- lib/tasks/build.rake | 17 +++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/tailwindcss/commands.rb b/lib/tailwindcss/commands.rb index 99ad30e..4c255cb 100644 --- a/lib/tailwindcss/commands.rb +++ b/lib/tailwindcss/commands.rb @@ -3,14 +3,16 @@ module Tailwindcss module Commands class << self - def compile_command(debug: false, **kwargs) + def compile_command(debug: false, namespace: nil, **kwargs) debug = ENV["TAILWINDCSS_DEBUG"].present? if ENV.key?("TAILWINDCSS_DEBUG") rails_root = defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd) + input_filename, output_filename = get_input_and_output_filename(namespace) + 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("app/assets/tailwind/#{input_filename}.css").to_s, + "-o", rails_root.join("app/assets/builds/#{output_filename}.css").to_s, ] command << "--minify" unless (debug || rails_css_compressor?) @@ -35,9 +37,27 @@ def command_env(verbose:) end end + private + def rails_css_compressor? defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present? end + + def get_input_and_output_filename(namespace) + if namespace != nil + unless File.exist?("app/assets/tailwind/#{namespace}.css") + raise "No file named #{namespace}.css exists in app/assets/tailwind" + end + + if namespace == "application" + 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." + end + end + + input_filename = namespace || "application" + output_filename = namespace || "tailwind" + return input_filename, output_filename + end end end end diff --git a/lib/tasks/build.rake b/lib/tasks/build.rake index 603c805..541c0d0 100644 --- a/lib/tasks/build.rake +++ b/lib/tasks/build.rake @@ -4,7 +4,9 @@ namespace :tailwindcss do debug = args.extras.include?("debug") verbose = args.extras.include?("verbose") - command = Tailwindcss::Commands.compile_command(debug: debug) + namespace = get_namespace_from_extras(args.extras) + + command = Tailwindcss::Commands.compile_command(debug: debug, namespace: namespace) env = Tailwindcss::Commands.command_env(verbose: verbose) puts "Running: #{Shellwords.join(command)}" if verbose @@ -18,7 +20,9 @@ namespace :tailwindcss do always = args.extras.include?("always") verbose = args.extras.include?("verbose") - command = Tailwindcss::Commands.watch_command(always: always, debug: debug, poll: poll) + namespace = get_namespace_from_extras(args.extras) + + command = Tailwindcss::Commands.watch_command(always: always, debug: debug, namespace: namespace, poll: poll) env = Tailwindcss::Commands.command_env(verbose: verbose) puts "Running: #{Shellwords.join(command)}" if verbose @@ -26,6 +30,15 @@ namespace :tailwindcss do rescue Interrupt puts "Received interrupt, exiting tailwindcss:watch" if args.extras.include?("verbose") end + + private + + def get_namespace_from_extras(extras) + namespace_key_and_value = extras.detect{ |a| a.start_with?('namespace=') } + if namespace_key_and_value != nil + namespace_key_and_value.split('=')[1] + end + end end Rake::Task["assets:precompile"].enhance(["tailwindcss:build"])