1
1
module ReactOnRails
2
2
class EnsureAssetsCompiled
3
- COMPILED_DIR_NAMES = %w( javascripts stylesheets fonts images ) . freeze
3
+ class << self
4
+ attr_accessor :has_been_run
5
+ @has_been_run = false
6
+ end
4
7
5
8
def self . build
6
9
client_dir = Rails . root . join ( "client" )
7
- compiled_dirs = COMPILED_DIR_NAMES . map { |dir | Rails . root . join ( "app" , "assets" , dir , "generated" ) }
8
- checker = WebpackAssetsStatusChecker . new ( client_dir : client_dir , compiled_dirs : compiled_dirs )
10
+ compiled_dirs = ReactOnRails . configuration . generated_assets_dirs
11
+
12
+ assets_checker = WebpackAssetsStatusChecker . new ( client_dir : client_dir , compiled_dirs : compiled_dirs )
13
+ process_checker = WebpackProcessChecker . new
9
14
compiler = WebpackAssetsCompiler . new
10
- new ( checker , compiler )
15
+
16
+ new ( assets_checker , compiler , process_checker )
11
17
end
12
18
13
- attr_reader :webpack_assets_checker , :webpack_assets_compiler , :assets_have_been_compiled
19
+ attr_reader :webpack_assets_checker , :webpack_assets_compiler , :webpack_process_checker , : assets_have_been_compiled
14
20
15
- def initialize ( webpack_assets_checker , webpack_assets_compiler )
21
+ def initialize ( webpack_assets_checker , webpack_assets_compiler , webpack_process_checker )
16
22
@webpack_assets_compiler = webpack_assets_compiler
17
23
@webpack_assets_checker = webpack_assets_checker
18
- @assets_have_been_compiled = false
24
+ @webpack_process_checker = webpack_process_checker
19
25
end
20
26
21
27
def call
22
- should_skip_compiling = assets_have_been_compiled || @webpack_assets_checker . up_to_date?
23
- webpack_assets_compiler . compile unless should_skip_compiling
24
- @assets_have_been_compiled = true
28
+ loop do
29
+ should_skip_compiling = self . class . has_been_run || @webpack_assets_checker . up_to_date?
30
+ break if should_skip_compiling
31
+
32
+ if webpack_process_checker . running?
33
+ sleep 1
34
+ else
35
+ webpack_assets_compiler . compile
36
+ break
37
+ end
38
+ end
39
+
40
+ self . class . has_been_run = true
25
41
end
26
42
end
27
43
28
44
class WebpackAssetsCompiler
29
45
def compile
30
46
compile_type ( :client )
31
- compile_type ( :server ) if ReactOnRails . configuration . server_bundle_js_file . present ?
47
+ compile_type ( :server ) if Utils . server_rendering_is_enabled ?
32
48
end
33
49
34
50
private
@@ -37,11 +53,45 @@ def compile_type(type)
37
53
puts "\n \n Building Webpack #{ type } -rendering assets..."
38
54
build_output = `cd client && npm run build:#{ type } `
39
55
40
- if build_output =~ /error/i
41
- fail "Error in building assets!\n #{ build_output } "
42
- end
56
+ fail "Error in building assets!\n #{ build_output } " unless Utils . last_process_completed_successfully?
57
+
58
+ puts "Webpack #{ type } -rendering assets built. If you are frequently running\n " \
59
+ "tests, you can run webpack in watch mode to speed up this process.\n " \
60
+ "See the official documentation:\n " \
61
+ "https://github.com/shakacode/react_on_rails/blob/master/docs/additional_reading/rspec_configuration.md\n \n "
62
+ end
63
+ end
64
+
65
+ class WebpackProcessChecker
66
+ def running?
67
+ client_running = check_running_for_type ( "client" )
68
+ return client_running unless Utils . server_rendering_is_enabled?
69
+
70
+ server_running = check_running_for_type ( "server" )
71
+ fail_if_only_running_for_one_type ( client_running , server_running )
72
+
73
+ client_running && server_running
74
+ end
75
+
76
+ private
77
+
78
+ # We only want to do this if server rendering is enabled.
79
+ def fail_if_only_running_for_one_type ( client_running , server_running )
80
+ return unless client_running ^ server_running
81
+ fail "\n \n Error: detected webpack is not running for both types of assets:\n " \
82
+ "***Webpack Client Process Running?: #{ client_running } \n " \
83
+ "***Webpack Server Process Running?: #{ server_running } "
84
+ end
85
+
86
+ def check_running_for_type ( type )
87
+ type = type . to_sym
88
+
89
+ response = `pgrep -fl 'bin/webpack\s (\\ -w|\\ -\\ -watch)\s \\ -\\ -config\s .*#{ type } .*\\ .js'`
90
+ is_running = Utils . last_process_completed_successfully?
91
+
92
+ puts "#{ type } webpack process is running: #{ response . ai } " if is_running
43
93
44
- puts "Webpack #{ type } -rendering assets built. \n \n "
94
+ is_running
45
95
end
46
96
end
47
97
end
0 commit comments