Skip to content

Commit bc0af61

Browse files
committed
Implement Engines support
1 parent f722f19 commit bc0af61

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ Then you can use yarn or npm to install the dependencies.
400400
401401
If you need to use a custom input or output file, you can run `bundle exec tailwindcss` to access the platform-specific executable, and give it your own build options.
402402
403+
## Rails Engines support
404+
405+
If you have Rails Engines in your application that use Tailwind CSS, they will be automatically included in the Tailwind build as long as they conform to next conventions:
406+
407+
- The engine must have `tailwindcss-rails` as gem dependency.
408+
- 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.
403409
404410
## Troubleshooting
405411

lib/tailwindcss/commands.rb

+34
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,40 @@ def command_env(verbose:)
3838
def rails_css_compressor?
3939
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
4040
end
41+
42+
def engines_tailwindcss_roots
43+
return [] unless defined?(Rails)
44+
45+
Rails::Engine.subclasses.select do |engine|
46+
begin
47+
spec = Gem::Specification.find_by_name(engine.engine_name)
48+
spec.dependencies.any? { |d| d.name == 'tailwindcss-rails' }
49+
rescue Gem::MissingSpecError
50+
false
51+
end
52+
end.map do |engine|
53+
[
54+
Rails.root.join("app/assets/tailwind/#{engine.engine_name}/application.css"),
55+
engine.root.join("app/assets/tailwind/#{engine.engine_name}/application.css")
56+
].select(&:exist?).compact.first.to_s
57+
end.compact
58+
end
59+
60+
def enhance_command(command)
61+
engine_roots = Tailwindcss::Commands.engines_tailwindcss_roots
62+
if engine_roots.any?
63+
Tempfile.create('tailwind.css') do |file|
64+
file.write(engine_roots.map { |root| "@import \"#{root}\";" }.join("\n"))
65+
file.write("\n@import \"#{Rails.root.join('app/assets/tailwind/application.css')}\";\n")
66+
file.rewind
67+
transformed_command = command.dup
68+
transformed_command[2] = file.path
69+
yield transformed_command if block_given?
70+
end
71+
else
72+
yield command if block_given?
73+
end
74+
end
4175
end
4276
end
4377
end

lib/tasks/build.rake

+10-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ namespace :tailwindcss do
55
verbose = args.extras.include?("verbose")
66

77
command = Tailwindcss::Commands.compile_command(debug: debug)
8-
env = Tailwindcss::Commands.command_env(verbose: verbose)
9-
puts "Running: #{Shellwords.join(command)}" if verbose
8+
Tailwindcss::Commands.enhance_command(command) do |transformed_command|
9+
env = Tailwindcss::Commands.command_env(verbose: verbose)
10+
puts "Running: #{Shellwords.join(command)}" if verbose
1011

11-
system(env, *command, exception: true)
12+
system(env, *command, exception: true)
13+
end
1214
end
1315

1416
desc "Watch and build your Tailwind CSS on file changes"
@@ -19,10 +21,12 @@ namespace :tailwindcss do
1921
verbose = args.extras.include?("verbose")
2022

2123
command = Tailwindcss::Commands.watch_command(always: always, debug: debug, poll: poll)
22-
env = Tailwindcss::Commands.command_env(verbose: verbose)
23-
puts "Running: #{Shellwords.join(command)}" if verbose
24+
Tailwindcss::Commands.enhance_command(command) do |transformed_command|
25+
env = Tailwindcss::Commands.command_env(verbose: verbose)
26+
puts "Running: #{Shellwords.join(command)}" if verbose
2427

25-
system(env, *command)
28+
system(env, *command)
29+
end
2630
rescue Interrupt
2731
puts "Received interrupt, exiting tailwindcss:watch" if args.extras.include?("verbose")
2832
end

0 commit comments

Comments
 (0)