File tree 3 files changed +28
-3
lines changed
3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -11,9 +11,16 @@ class InstallGenerator < ::Rails::Generators::Base
11
11
default : false ,
12
12
desc : 'Skip Git keeps'
13
13
14
+ class_option :skip_server_rendering ,
15
+ type : :boolean ,
16
+ default : false ,
17
+ desc : "Don't generate server_rendering.js or config/initializers/react_server_rendering.rb"
18
+
14
19
def create_directory
15
20
empty_directory 'app/assets/javascripts/components'
16
- create_file 'app/assets/javascripts/components/.gitkeep' unless options [ :skip_git ]
21
+ if !options [ :skip_git ]
22
+ create_file 'app/assets/javascripts/components/.gitkeep'
23
+ end
17
24
end
18
25
19
26
def inject_react
@@ -49,8 +56,11 @@ def create_components
49
56
end
50
57
51
58
def create_server_rendering
52
- file_path = 'app/assets/javascripts/server_rendering.js'
53
- template ( "server_rendering.js" , file_path )
59
+ return if options [ :skip_server_rendering ]
60
+ manifest_path = "app/assets/javascripts/server_rendering.js"
61
+ template ( "server_rendering.js" , manifest_path )
62
+ initializer_path = "config/initializers/react_server_rendering.rb"
63
+ template ( "react_server_rendering.rb" , initializer_path )
54
64
end
55
65
56
66
private
Original file line number Diff line number Diff line change
1
+ # To render React components in production, precompile the server rendering manifest:
2
+ Rails . application . config . assets . precompile += [ "server_rendering.js" ]
Original file line number Diff line number Diff line change 4
4
class InstallGeneratorTest < Rails ::Generators ::TestCase
5
5
destination File . join ( Rails . root , 'tmp' , 'generator_test_output' )
6
6
tests React ::Generators ::InstallGenerator
7
+ setup :prepare_destination
7
8
8
9
def copy_directory ( dir )
9
10
source = Rails . root . join ( dir )
@@ -71,6 +72,18 @@ def copy_directory(dir)
71
72
assert_file server_rendering_file_path , %r{//= require ./components\n }
72
73
end
73
74
75
+ test "creates server rendering initializer" do
76
+ run_generator
77
+ initializer_path = "config/initializers/react_server_rendering.rb"
78
+ assert_file ( initializer_path , %r{Rails.application.config.assets.precompile \+ = \[ "server_rendering.js"\] } )
79
+ end
80
+
81
+ test "skipping server rendering" do
82
+ run_generator %w( --skip-server-rendering )
83
+ assert_no_file "config/initializers/react_server_rendering.rb"
84
+ assert_no_file "app/assets/javascripts/server_rendering.js"
85
+ end
86
+
74
87
def init_application_js ( content )
75
88
FileUtils . mkdir_p destination_root + '/app/assets/javascripts/'
76
89
File . write destination_root + '/app/assets/javascripts/application.js' , content
You can’t perform that action at this time.
0 commit comments