File tree 2 files changed +77
-0
lines changed
2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -171,6 +171,13 @@ The inline version also works:
171
171
<section class =" bg-[url('image.svg')]" >Has the image as it's background</section >
172
172
```
173
173
174
+ ## Puma plugin
175
+ We provide a Puma plugin if you want to run the Tailwind watcher together with Puma and have Puma monitor and manage it. You just need to add
176
+ ``` ruby
177
+ plugin :tailwindcss
178
+ ```
179
+ to your ` puma.rb ` configuration.
180
+
174
181
## License
175
182
176
183
Tailwind for Rails is released under the [ MIT License] ( https://opensource.org/licenses/MIT ) .
Original file line number Diff line number Diff line change
1
+ require "puma/plugin"
2
+
3
+ Puma ::Plugin . create do
4
+ attr_reader :puma_pid , :tailwind_pid , :log_writer
5
+
6
+ def start ( launcher )
7
+ return unless development?
8
+
9
+ @log_writer = launcher . log_writer
10
+ @puma_pid = $$
11
+ @tailwind_pid = fork do
12
+ Thread . new { monitor_puma }
13
+ system ( *Tailwindcss ::Commands . watch_command )
14
+ end
15
+
16
+ launcher . events . on_stopped { stop_solid_queue }
17
+
18
+ in_background do
19
+ monitor_tailwind
20
+ end
21
+ end
22
+
23
+ private
24
+ def stop_solid_queue
25
+ Process . waitpid ( tailwind_pid , Process ::WNOHANG )
26
+ log "Stopping tailwind..."
27
+ Process . kill ( :INT , tailwind_pid ) if tailwind_pid
28
+ Process . wait ( tailwind_pid )
29
+ rescue Errno ::ECHILD , Errno ::ESRCH
30
+ end
31
+
32
+ def monitor_puma
33
+ monitor ( :puma_dead? , "Detected Puma has gone away, stopping tailwind..." )
34
+ end
35
+
36
+ def monitor_tailwind
37
+ monitor ( :tailwind_dead? , "Detected tailwind has gone away, stopping Puma..." )
38
+ end
39
+
40
+ def monitor ( process_dead , message )
41
+ loop do
42
+ if send ( process_dead )
43
+ log message
44
+ Process . kill ( :INT , $$)
45
+ break
46
+ end
47
+ sleep 2
48
+ end
49
+ end
50
+
51
+ def tailwind_dead?
52
+ Process . waitpid ( tailwind_pid , Process ::WNOHANG )
53
+ false
54
+ rescue Errno ::ECHILD , Errno ::ESRCH
55
+ true
56
+ end
57
+
58
+ def puma_dead?
59
+ Process . ppid != puma_pid
60
+ end
61
+
62
+ def log ( ...)
63
+ log_writer . log ( ...)
64
+ end
65
+
66
+ def development?
67
+ ( ENV [ "RAILS_ENV" ] . presence || ENV [ "RACK_ENV" ] . presence || "development" ) ==
68
+ "development"
69
+ end
70
+ end
You can’t perform that action at this time.
0 commit comments