File tree 4 files changed +29
-4
lines changed 4 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ where you will put your components
51
51
// = require react_ujs
52
52
// = require components
53
53
```
54
- - create a ` server_rendering.js ` manifest file
54
+ - create a ` server_rendering.js ` manifest file and precompile it with ` config/initializers/react_server_rendering.rb ` . (Use ` --skip-server-rendering ` if you don't want this.)
55
55
56
56
## Usage
57
57
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 )
@@ -70,6 +71,18 @@ def copy_directory(dir)
70
71
assert_file server_rendering_file_path , %r{//= require ./components\n }
71
72
end
72
73
74
+ test "creates server rendering initializer" do
75
+ run_generator
76
+ initializer_path = "config/initializers/react_server_rendering.rb"
77
+ assert_file ( initializer_path , %r{Rails.application.config.assets.precompile \+ = \[ "server_rendering.js"\] } )
78
+ end
79
+
80
+ test "skipping server rendering" do
81
+ run_generator %w( --skip-server-rendering )
82
+ assert_no_file "config/initializers/react_server_rendering.rb"
83
+ assert_no_file "app/assets/javascripts/server_rendering.js"
84
+ end
85
+
73
86
def init_application_js ( content )
74
87
FileUtils . mkdir_p destination_root + '/app/assets/javascripts/'
75
88
File . write destination_root + '/app/assets/javascripts/application.js' , content
You can’t perform that action at this time.
0 commit comments