Skip to content

Changes in engine support due to review #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: feature/engines-convention-over-configuration-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* [Using Tailwind plugins](#using-tailwind-plugins)
* [Using with PostCSS](#using-with-postcss)
* [Custom inputs or outputs](#custom-inputs-or-outputs)
- [Rails Engines support](#rails-engines-support)
- [Troubleshooting](#troubleshooting)
* [The `watch` command is hanging](#the-watch-command-is-hanging)
* [Lost keystrokes or hanging when using terminal-based debugging tools (e.g. IRB, Pry, `ruby/debug`...etc.) with the Puma plugin](#lost-keystrokes-or-hanging-when-using-terminal-based-debugging-tools-eg-irb-pry-rubydebugetc-with-the-puma-plugin)
* [Running in a docker container exits prematurely](#running-in-a-docker-container-exits-prematurely)
* [Conflict with sassc-rails](#conflict-with-sassc-rails)
Expand Down Expand Up @@ -406,6 +408,14 @@ If you have Rails Engines in your application that use Tailwind CSS, they will b

- The engine must have `tailwindcss-rails` as gem dependency.
- The engine must have a `app/assets/tailwind/<engine_name>/application.css` file or your application must have overridden file in the same location of your application root.
- The engine must register itself in Tailwindcss Rails:
```ruby
initializer 'your_engine.tailwindcss' do |app|
ActiveSupport.on_load(:tailwindcss_rails) do
config.tailwindcss_rails.engines << Your::Engine.engine_name
end
end
```

## Troubleshooting

Expand Down
63 changes: 35 additions & 28 deletions lib/tailwindcss/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
module Tailwindcss
module Commands
class << self
def compile_command(debug: false, **kwargs)
def rails_root
defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
end

def remove_tempfile!
return unless class_variable_defined?(:@@tempfile) && @@tempfile

@@tempfile.close unless @@tempfile.closed?
@@tempfile.unlink if File.exist?(@@tempfile.path)
remove_class_variable(:@@tempfile)
end

def compile_command(input: application_css, debug: false, **kwargs)
debug = ENV["TAILWINDCSS_DEBUG"].present? if ENV.key?("TAILWINDCSS_DEBUG")
rails_root = defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)

command = [
Tailwindcss::Ruby.executable(**kwargs),
"-i", rails_root.join("app/assets/tailwind/application.css").to_s,
"-i", input.to_s,
"-o", rails_root.join("app/assets/builds/tailwind.css").to_s,
]

Expand All @@ -21,6 +32,22 @@ def compile_command(debug: false, **kwargs)
command
end

def application_css
return rails_root.join("app/assets/tailwind/application.css").to_s if engines_roots.empty?

@@tempfile = Tempfile.new("tailwind.application.css")

# Write content to tempfile
engines_roots.each do |root|
@@tempfile.write("@import \"#{root}\";\n")
end
@@tempfile.write("\n@import \"#{rails_root.join('app/assets/tailwind/application.css')}\";\n")
@@tempfile.flush
@@tempfile.close

@@tempfile.path
Comment on lines +38 to +48
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a class variable for temporary file storage may lead to thread-safety issues if compile_command is invoked concurrently. Consider using a method-local variable or thread-local storage for the tempfile.

Suggested change
@@tempfile = Tempfile.new("tailwind.application.css")
# Write content to tempfile
engines_roots.each do |root|
@@tempfile.write("@import \"#{root}\";\n")
end
@@tempfile.write("\n@import \"#{rails_root.join('app/assets/tailwind/application.css')}\";\n")
@@tempfile.flush
@@tempfile.close
@@tempfile.path
tempfile = Tempfile.new("tailwind.application.css")
# Write content to tempfile
engines_roots.each do |root|
tempfile.write("@import \"#{root}\";\n")
end
tempfile.write("\n@import \"#{rails_root.join('app/assets/tailwind/application.css')}\";\n")
tempfile.flush
tempfile.close
tempfile.path

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

end

def watch_command(always: false, poll: false, **kwargs)
compile_command(**kwargs).tap do |command|
command << "-w"
Expand All @@ -39,39 +66,19 @@ def rails_css_compressor?
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
end

def engines_tailwindcss_roots
def engines_roots
return [] unless defined?(Rails)
return [] unless Rails.application&.config&.tailwindcss_rails&.engines

Rails::Engine.subclasses.select do |engine|
begin
spec = Gem::Specification.find_by_name(engine.engine_name)
spec.dependencies.any? { |d| d.name == 'tailwindcss-rails' }
rescue Gem::MissingSpecError
false
end
Rails::Engine.descendants.select do |engine|
engine.engine_name.in?(Rails.application.config.tailwindcss_rails.engines)
end.map do |engine|
[
Rails.root.join("app/assets/tailwind/#{engine.engine_name}/application.css"),
rails_root.join("app/assets/tailwind/#{engine.engine_name}/application.css"),
engine.root.join("app/assets/tailwind/#{engine.engine_name}/application.css")
].select(&:exist?).compact.first.to_s
end.compact
end

def enhance_command(command)
engine_roots = Tailwindcss::Commands.engines_tailwindcss_roots
if engine_roots.any?
Tempfile.create('tailwind.css') do |file|
file.write(engine_roots.map { |root| "@import \"#{root}\";" }.join("\n"))
file.write("\n@import \"#{Rails.root.join('app/assets/tailwind/application.css')}\";\n")
file.rewind
transformed_command = command.dup
transformed_command[2] = file.path
yield transformed_command if block_given?
end
else
yield command if block_given?
end
end
end
end
end
9 changes: 9 additions & 0 deletions lib/tailwindcss/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

module Tailwindcss
class Engine < ::Rails::Engine
config.before_configuration do |app|
app.config.tailwindcss_rails = ActiveSupport::OrderedOptions.new
app.config.tailwindcss_rails.engines = []
end

initializer 'tailwindcss.load_hook' do |app|
ActiveSupport.run_load_hooks(:tailwindcss_rails, app)
end

initializer "tailwindcss.disable_generator_stylesheets" do
Rails.application.config.generators.stylesheets = false
end
Expand Down
19 changes: 9 additions & 10 deletions lib/tasks/build.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ namespace :tailwindcss do
verbose = args.extras.include?("verbose")

command = Tailwindcss::Commands.compile_command(debug: debug)
Tailwindcss::Commands.enhance_command(command) do |transformed_command|
env = Tailwindcss::Commands.command_env(verbose: verbose)
puts "Running: #{Shellwords.join(command)}" if verbose
env = Tailwindcss::Commands.command_env(verbose: verbose)
puts "Running: #{Shellwords.join(command)}" if verbose

system(env, *command, exception: true)
end
system(env, *command, exception: true)
Tailwindcss::Commands.remove_tempfile!
end

desc "Watch and build your Tailwind CSS on file changes"
Expand All @@ -21,14 +20,14 @@ namespace :tailwindcss do
verbose = args.extras.include?("verbose")

command = Tailwindcss::Commands.watch_command(always: always, debug: debug, poll: poll)
Tailwindcss::Commands.enhance_command(command) do |transformed_command|
env = Tailwindcss::Commands.command_env(verbose: verbose)
puts "Running: #{Shellwords.join(command)}" if verbose
env = Tailwindcss::Commands.command_env(verbose: verbose)
puts "Running: #{Shellwords.join(command)}" if verbose

system(env, *command)
end
system(env, *command)
rescue Interrupt
puts "Received interrupt, exiting tailwindcss:watch" if args.extras.include?("verbose")
ensure
Tailwindcss::Commands.remove_tempfile!
end
end

Expand Down
Loading