Skip to content

Commit d4e1acf

Browse files
committed
Implement Engines support
1 parent 3a5fedc commit d4e1acf

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ Then you can use yarn or npm to install the dependencies.
346346
347347
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.
348348
349+
## Rails Engines support
350+
351+
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:
352+
353+
- The engine must have `tailwindcss-rails` as gem dependency.
354+
- 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.
349355
350356
## Troubleshooting
351357

lib/tailwindcss/commands.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,40 @@ def watch_command(always: false, poll: false, **kwargs)
3131
def rails_css_compressor?
3232
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
3333
end
34+
35+
def engines_tailwindcss_roots
36+
return [] unless defined?(Rails)
37+
38+
Rails::Engine.subclasses.select do |engine|
39+
begin
40+
spec = Gem::Specification.find_by_name(engine.engine_name)
41+
spec.dependencies.any? { |d| d.name == 'tailwindcss-rails' }
42+
rescue Gem::MissingSpecError
43+
false
44+
end
45+
end.map do |engine|
46+
[
47+
Rails.root.join("app/assets/tailwind/#{engine.engine_name}/application.css"),
48+
engine.root.join("app/assets/tailwind/#{engine.engine_name}/application.css")
49+
].select(&:exist?).compact.first.to_s
50+
end.compact
51+
end
52+
53+
def enhance_command(command)
54+
engine_roots = Tailwindcss::Commands.engines_tailwindcss_roots
55+
if engine_roots.any?
56+
Tempfile.create('tailwind.css') do |file|
57+
file.write(engine_roots.map { |root| "@import \"#{root}\";" }.join("\n"))
58+
file.write("\n@import \"#{Rails.root.join('app/assets/tailwind/application.css')}\";\n")
59+
file.rewind
60+
transformed_command = command.dup
61+
transformed_command[2] = file.path
62+
yield transformed_command if block_given?
63+
end
64+
else
65+
yield command if block_given?
66+
end
67+
end
3468
end
3569
end
3670
end

lib/tasks/build.rake

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ namespace :tailwindcss do
33
task build: :environment do |_, args|
44
debug = args.extras.include?("debug")
55
command = Tailwindcss::Commands.compile_command(debug: debug)
6-
puts command.inspect if args.extras.include?("verbose")
7-
system(*command, exception: true)
6+
Tailwindcss::Commands.enhance_command(command) do |transformed_command|
7+
puts transformed_command.inspect if args.extras.include?("verbose")
8+
system(*transformed_command, exception: true)
9+
end
810
end
911

1012
desc "Watch and build your Tailwind CSS on file changes"
@@ -13,8 +15,10 @@ namespace :tailwindcss do
1315
poll = args.extras.include?("poll")
1416
always = args.extras.include?("always")
1517
command = Tailwindcss::Commands.watch_command(always: always, debug: debug, poll: poll)
16-
puts command.inspect if args.extras.include?("verbose")
17-
system(*command)
18+
Tailwindcss::Commands.enhance_command(command) do |transformed_command|
19+
puts transformed_command.inspect if args.extras.include?("verbose")
20+
system(*transformed_command)
21+
end
1822
rescue Interrupt
1923
puts "Received interrupt, exiting tailwindcss:watch" if args.extras.include?("verbose")
2024
end

0 commit comments

Comments
 (0)