react_on_rails 2.0.0.beta.1 → 2.0.0.beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -0
- data/.dockerignore +2 -0
- data/.eslintignore +7 -0
- data/.eslintrc +19 -0
- data/.gitignore +28 -0
- data/.jscsrc +27 -0
- data/.npmignore +20 -0
- data/.rspec +2 -0
- data/.rubocop.yml +70 -0
- data/.scss-lint.yml +205 -0
- data/.travis.yml +43 -0
- data/CHANGELOG.md +34 -0
- data/Dockerfile_tests +12 -0
- data/Gemfile +40 -0
- data/README.md +365 -0
- data/Rakefile +5 -0
- data/app/helpers/react_on_rails_helper.rb +215 -0
- data/docker-compose.yml +11 -0
- data/docs/LICENSE +21 -0
- data/docs/additional_reading/heroku_deployment.md +23 -0
- data/docs/additional_reading/manual_installation.md +118 -0
- data/docs/additional_reading/node_dependencies_and_npm.md +29 -0
- data/docs/additional_reading/optional_configuration.md +33 -0
- data/docs/additional_reading/react-and-redux.md +36 -0
- data/docs/additional_reading/react_router.md +45 -0
- data/docs/additional_reading/server_rendering_tips.md +11 -0
- data/docs/additional_reading/tips.md +10 -0
- data/docs/additional_reading/webpack.md +46 -0
- data/docs/code_of_conduct.md +13 -0
- data/docs/coding-style/linters.md +64 -0
- data/docs/coding-style/style.md +42 -0
- data/docs/contributing.md +157 -0
- data/docs/generator_testing.md +20 -0
- data/docs/releasing.md +29 -0
- data/lib/generators/USAGE +99 -0
- data/lib/generators/react_on_rails/base_generator.rb +191 -0
- data/lib/generators/react_on_rails/bootstrap_generator.rb +89 -0
- data/lib/generators/react_on_rails/dev_tests_generator.rb +39 -0
- data/lib/generators/react_on_rails/generator_helper.rb +50 -0
- data/lib/generators/react_on_rails/heroku_deployment_generator.rb +30 -0
- data/lib/generators/react_on_rails/install_generator.rb +99 -0
- data/lib/generators/react_on_rails/js_linters_generator.rb +19 -0
- data/lib/generators/react_on_rails/react_no_redux_generator.rb +40 -0
- data/lib/generators/react_on_rails/react_with_redux_generator.rb +51 -0
- data/lib/generators/react_on_rails/ruby_linters_generator.rb +33 -0
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev.tt +4 -0
- data/lib/generators/react_on_rails/templates/base/base/REACT_ON_RAILS.md +16 -0
- data/lib/generators/react_on_rails/templates/base/base/app/controllers/hello_world_controller.rb +5 -0
- data/lib/generators/react_on_rails/templates/base/base/app/views/hello_world/index.html.erb.tt +4 -0
- data/lib/generators/react_on_rails/templates/base/base/client/.babelrc +3 -0
- data/lib/generators/react_on_rails/templates/base/base/client/REACT_ON_RAILS_CLIENT_README.md +3 -0
- data/lib/generators/react_on_rails/templates/base/base/client/app/bundles/HelloWorld/startup/clientRegistration.jsx.tt +5 -0
- data/lib/generators/react_on_rails/templates/base/base/client/index.jade +15 -0
- data/lib/generators/react_on_rails/templates/base/base/client/package.json.tt +104 -0
- data/lib/generators/react_on_rails/templates/base/base/client/server.js +64 -0
- data/lib/generators/react_on_rails/templates/base/base/client/webpack.client.base.config.js.tt +62 -0
- data/lib/generators/react_on_rails/templates/base/base/client/webpack.client.hot.config.js.tt +69 -0
- data/lib/generators/react_on_rails/templates/base/base/client/webpack.client.rails.config.js +42 -0
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +26 -0
- data/lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake.tt +26 -0
- data/lib/generators/react_on_rails/templates/base/base/lib/tasks/linters.rake.tt +88 -0
- data/lib/generators/react_on_rails/templates/base/base/package.json +31 -0
- data/lib/generators/react_on_rails/templates/base/server_rendering/client/app/bundles/HelloWorld/startup/serverRegistration.jsx +4 -0
- data/lib/generators/react_on_rails/templates/base/server_rendering/client/webpack.server.rails.config.js +39 -0
- data/lib/generators/react_on_rails/templates/bootstrap/app/assets/stylesheets/_bootstrap-custom.scss +63 -0
- data/lib/generators/react_on_rails/templates/bootstrap/client/assets/stylesheets/_post-bootstrap.scss +10 -0
- data/lib/generators/react_on_rails/templates/bootstrap/client/assets/stylesheets/_pre-bootstrap.scss +8 -0
- data/lib/generators/react_on_rails/templates/bootstrap/client/assets/stylesheets/_react-on-rails-sass-helper.scss +19 -0
- data/lib/generators/react_on_rails/templates/bootstrap/client/bootstrap-sass.config.js +89 -0
- data/lib/generators/react_on_rails/templates/dev_tests/.rspec +2 -0
- data/lib/generators/react_on_rails/templates/dev_tests/spec/features/hello_world_spec.rb +25 -0
- data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +57 -0
- data/lib/generators/react_on_rails/templates/dev_tests/spec/simplecov_helper.rb +21 -0
- data/lib/generators/react_on_rails/templates/dev_tests/spec/spec_helper.rb +95 -0
- data/lib/generators/react_on_rails/templates/heroku_deployment/.buildpacks +2 -0
- data/lib/generators/react_on_rails/templates/heroku_deployment/Procfile +1 -0
- data/lib/generators/react_on_rails/templates/heroku_deployment/config/puma.rb +15 -0
- data/lib/generators/react_on_rails/templates/js_linters/client/.eslintignore +1 -0
- data/lib/generators/react_on_rails/templates/js_linters/client/.eslintrc +48 -0
- data/lib/generators/react_on_rails/templates/js_linters/client/.jscsrc +18 -0
- data/lib/generators/react_on_rails/templates/no_redux/base/client/app/bundles/HelloWorld/components/HelloWorldWidget.jsx +39 -0
- data/lib/generators/react_on_rails/templates/no_redux/base/client/app/bundles/HelloWorld/containers/HelloWorld.jsx +33 -0
- data/lib/generators/react_on_rails/templates/no_redux/base/client/app/bundles/HelloWorld/startup/HelloWorldAppClient.jsx.tt +8 -0
- data/lib/generators/react_on_rails/templates/no_redux/server_rendering/client/app/bundles/HelloWorld/startup/HelloWorldAppServer.jsx +8 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/bundles/HelloWorld/actions/helloWorldActionCreators.jsx +8 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/bundles/HelloWorld/components/HelloWorldWidget.jsx +48 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/bundles/HelloWorld/constants/helloWorldConstants.jsx +8 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/bundles/HelloWorld/containers/HelloWorld.jsx +43 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/bundles/HelloWorld/reducers/helloWorldReducer.jsx +19 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/bundles/HelloWorld/reducers/index.jsx +14 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/bundles/HelloWorld/startup/HelloWorldAppClient.jsx.tt +19 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/bundles/HelloWorld/store/helloWorldStore.jsx +35 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/app/lib/middlewares/loggerMiddleware.js +20 -0
- data/lib/generators/react_on_rails/templates/redux/server_rendering/client/app/bundles/HelloWorld/startup/HelloWorldAppServer.jsx +19 -0
- data/lib/generators/react_on_rails/templates/ruby_linters/.rubocop.yml +26 -0
- data/lib/generators/react_on_rails/templates/ruby_linters/.scss-lint.yml +205 -0
- data/lib/generators/react_on_rails/templates/ruby_linters/lib/tasks/brakeman.rake +17 -0
- data/lib/generators/react_on_rails/templates/ruby_linters/lib/tasks/ci.rake +33 -0
- data/lib/generators/react_on_rails/templates/ruby_linters/ruby-lint.yml +20 -0
- data/lib/react_on_rails.rb +6 -0
- data/lib/react_on_rails/configuration.rb +60 -0
- data/lib/react_on_rails/engine.rb +7 -0
- data/lib/react_on_rails/prerender_error.rb +31 -0
- data/lib/react_on_rails/server_rendering_pool.rb +110 -0
- data/lib/react_on_rails/version.rb +3 -0
- data/package.json +76 -0
- data/rakelib/docker.rake +33 -0
- data/rakelib/dummy_apps.rake +20 -0
- data/rakelib/example_type.rb +160 -0
- data/rakelib/examples.rake +103 -0
- data/rakelib/examples_config.yml +19 -0
- data/rakelib/lint.rake +37 -0
- data/rakelib/node_package.rake +11 -0
- data/rakelib/run_rspec.rake +65 -0
- data/rakelib/task_helpers.rb +44 -0
- data/react_on_rails.gemspec +31 -0
- data/ruby-lint.yml +24 -0
- metadata +119 -2
data/Rakefile
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# NOTE:
|
|
2
|
+
# For any heredoc JS:
|
|
3
|
+
# 1. The white spacing in this file matters!
|
|
4
|
+
# 2. Keep all #{some_var} fully to the left so that all indentation is done evenly in that var
|
|
5
|
+
require "react_on_rails/prerender_error"
|
|
6
|
+
|
|
7
|
+
module ReactOnRailsHelper
|
|
8
|
+
# react_component_name: can be a React component, created using a ES6 class, or
|
|
9
|
+
# React.createClass, or a
|
|
10
|
+
# `generator function` that returns a React component
|
|
11
|
+
# using ES6
|
|
12
|
+
# let MyReactComponentApp = (props) => <MyReactComponent {...props}/>;
|
|
13
|
+
# or using ES5
|
|
14
|
+
# var MyReactComponentApp = function(props) { return <YourReactComponent {...props}/>; }
|
|
15
|
+
# Exposing the react_component_name is necessary to both a plain ReactComponent as well as
|
|
16
|
+
# a generator:
|
|
17
|
+
# See README.md for how to "register" your react components.
|
|
18
|
+
# See spec/dummy/client/app/startup/serverRegistration.jsx and
|
|
19
|
+
# spec/dummy/client/app/startup/ClientRegistration.jsx for examples of this
|
|
20
|
+
# props: Ruby Hash or JSON string which contains the properties to pass to the react object
|
|
21
|
+
#
|
|
22
|
+
# options:
|
|
23
|
+
# prerender: <true/false> set to false when debugging!
|
|
24
|
+
# trace: <true/false> set to true to print additional debugging information in the browser
|
|
25
|
+
# default is true for development, off otherwise
|
|
26
|
+
# replay_console: <true/false> Default is true. False will disable echoing server rendering
|
|
27
|
+
# logs to browser. While this can make troubleshooting server rendering difficult,
|
|
28
|
+
# so long as you have the default configuration of logging_on_server set to
|
|
29
|
+
# true, you'll still see the errors on the server.
|
|
30
|
+
# raise_on_prerender_error: <true/false> Default to false. True will raise exception on server
|
|
31
|
+
# if the JS code throws
|
|
32
|
+
# Any other options are passed to the content tag, including the id.
|
|
33
|
+
def react_component(component_name, props = {}, options = {})
|
|
34
|
+
# Create the JavaScript and HTML to allow either client or server rendering of the
|
|
35
|
+
# react_component.
|
|
36
|
+
#
|
|
37
|
+
# Create the JavaScript setup of the global to initialize the client rendering
|
|
38
|
+
# (re-hydrate the data). This enables react rendered on the client to see that the
|
|
39
|
+
# server has already rendered the HTML.
|
|
40
|
+
# We use this react_component_index in case we have the same component multiple times on the page.
|
|
41
|
+
react_component_index = next_react_component_index
|
|
42
|
+
react_component_name = component_name.camelize # Not sure if we should be doing this (JG)
|
|
43
|
+
if options[:id].nil?
|
|
44
|
+
dom_id = "#{component_name}-react-component-#{react_component_index}"
|
|
45
|
+
else
|
|
46
|
+
dom_id = options[:id]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Setup the page_loaded_js, which is the same regardless of prerendering or not!
|
|
50
|
+
# The reason is that React is smart about not doing extra work if the server rendering did its job.
|
|
51
|
+
turbolinks_loaded = Object.const_defined?(:Turbolinks)
|
|
52
|
+
|
|
53
|
+
data = { component_name: react_component_name,
|
|
54
|
+
props: props,
|
|
55
|
+
trace: trace(options),
|
|
56
|
+
expect_turbolinks: turbolinks_loaded,
|
|
57
|
+
dom_id: dom_id
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
component_specification_tag =
|
|
61
|
+
content_tag(:div,
|
|
62
|
+
"",
|
|
63
|
+
class: "js-react-on-rails-component",
|
|
64
|
+
style: "display:none",
|
|
65
|
+
data: data)
|
|
66
|
+
|
|
67
|
+
# Create the HTML rendering part
|
|
68
|
+
result = server_rendered_react_component_html(options, props, react_component_name, dom_id)
|
|
69
|
+
|
|
70
|
+
server_rendered_html = result["html"]
|
|
71
|
+
console_script = result["consoleReplayScript"]
|
|
72
|
+
|
|
73
|
+
content_tag_options = options.except(:generator_function, :prerender, :trace,
|
|
74
|
+
:replay_console, :id, :react_component_name,
|
|
75
|
+
:server_side, :raise_on_prerender_error)
|
|
76
|
+
content_tag_options[:id] = dom_id
|
|
77
|
+
|
|
78
|
+
rendered_output = content_tag(:div,
|
|
79
|
+
server_rendered_html.html_safe,
|
|
80
|
+
content_tag_options)
|
|
81
|
+
|
|
82
|
+
# IMPORTANT: Ensure that we mark string as html_safe to avoid escaping.
|
|
83
|
+
<<-HTML.html_safe
|
|
84
|
+
#{component_specification_tag}
|
|
85
|
+
#{rendered_output}
|
|
86
|
+
#{replay_console(options) ? console_script : ''}
|
|
87
|
+
HTML
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def sanitized_props_string(props)
|
|
91
|
+
props.is_a?(String) ? json_escape(props) : props.to_json
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Helper method to take javascript expression and returns the output from evaluating it.
|
|
95
|
+
# If you have more than one line that needs to be executed, wrap it in an IIFE.
|
|
96
|
+
# JS exceptions are caught and console messages are handled properly.
|
|
97
|
+
def server_render_js(js_expression, options = {})
|
|
98
|
+
wrapper_js = <<-JS
|
|
99
|
+
(function() {
|
|
100
|
+
var htmlResult = '';
|
|
101
|
+
var consoleReplayScript = '';
|
|
102
|
+
var hasErrors = false;
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
htmlResult =
|
|
106
|
+
(function() {
|
|
107
|
+
return #{js_expression};
|
|
108
|
+
})();
|
|
109
|
+
} catch(e) {
|
|
110
|
+
htmlResult = ReactOnRails.handleError({e: e, name: null,
|
|
111
|
+
jsCode: '#{escape_javascript(js_expression)}', serverSide: true});
|
|
112
|
+
hasErrors = true;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
consoleReplayScript = ReactOnRails.buildConsoleReplay();
|
|
116
|
+
|
|
117
|
+
return JSON.stringify({
|
|
118
|
+
html: htmlResult,
|
|
119
|
+
consoleReplayScript: consoleReplayScript,
|
|
120
|
+
hasErrors: hasErrors
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
})()
|
|
124
|
+
JS
|
|
125
|
+
|
|
126
|
+
result = ReactOnRails::ServerRenderingPool.server_render_js_with_console_logging(wrapper_js)
|
|
127
|
+
|
|
128
|
+
# IMPORTANT: To ensure that Rails doesn't auto-escape HTML tags, use the 'raw' method.
|
|
129
|
+
html = result["html"]
|
|
130
|
+
console_log_script = result["consoleLogScript"]
|
|
131
|
+
raw("#{html}#{replay_console(options) ? console_log_script : ''}")
|
|
132
|
+
rescue ExecJS::ProgramError => err
|
|
133
|
+
# rubocop:disable Style/RaiseArgs
|
|
134
|
+
raise ReactOnRails::PrerenderError.new(component_name: "N/A (server_render_js called)",
|
|
135
|
+
err: err,
|
|
136
|
+
js_code: wrapper_js)
|
|
137
|
+
# rubocop:enable Style/RaiseArgs
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
private
|
|
141
|
+
|
|
142
|
+
def next_react_component_index
|
|
143
|
+
@react_component_index ||= -1
|
|
144
|
+
@react_component_index += 1
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Returns Array [0]: html, [1]: script to console log
|
|
148
|
+
# NOTE, these are NOT html_safe!
|
|
149
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
|
150
|
+
def server_rendered_react_component_html(options, props, react_component_name, dom_id)
|
|
151
|
+
return { "html" => "", "consoleReplayScript" => "" } unless prerender(options)
|
|
152
|
+
|
|
153
|
+
# On server `location` option is added (`location = request.fullpath`)
|
|
154
|
+
# React Router needs this to match the current route
|
|
155
|
+
|
|
156
|
+
# Make sure that we use up-to-date server-bundle
|
|
157
|
+
ReactOnRails::ServerRenderingPool.reset_pool_if_server_bundle_was_modified
|
|
158
|
+
|
|
159
|
+
# Since this code is not inserted on a web page, we don't need to escape.
|
|
160
|
+
props_string = props.is_a?(String) ? props : props.to_json
|
|
161
|
+
|
|
162
|
+
wrapper_js = <<-JS
|
|
163
|
+
(function() {
|
|
164
|
+
var props = #{props_string};
|
|
165
|
+
return ReactOnRails.serverRenderReactComponent({
|
|
166
|
+
name: '#{react_component_name}',
|
|
167
|
+
domNodeId: '#{dom_id}',
|
|
168
|
+
props: props,
|
|
169
|
+
trace: #{trace(options)},
|
|
170
|
+
location: '#{request.fullpath}'
|
|
171
|
+
});
|
|
172
|
+
})()
|
|
173
|
+
JS
|
|
174
|
+
|
|
175
|
+
result = ReactOnRails::ServerRenderingPool.server_render_js_with_console_logging(wrapper_js)
|
|
176
|
+
|
|
177
|
+
if result["hasErrors"] && raise_on_prerender_error(options)
|
|
178
|
+
# We caught this exception on our backtrace handler
|
|
179
|
+
# rubocop:disable Style/RaiseArgs
|
|
180
|
+
fail ReactOnRails::PrerenderError.new(component_name: react_component_name,
|
|
181
|
+
# Sanitize as this might be browser logged
|
|
182
|
+
props: sanitized_props_string(props),
|
|
183
|
+
err: nil,
|
|
184
|
+
js_code: wrapper_js,
|
|
185
|
+
console_messages: result["consoleReplayScript"])
|
|
186
|
+
# rubocop:enable Style/RaiseArgs
|
|
187
|
+
end
|
|
188
|
+
result
|
|
189
|
+
rescue ExecJS::ProgramError => err
|
|
190
|
+
# This error came from execJs
|
|
191
|
+
# rubocop:disable Style/RaiseArgs
|
|
192
|
+
raise ReactOnRails::PrerenderError.new(component_name: react_component_name,
|
|
193
|
+
# Sanitize as this might be browser logged
|
|
194
|
+
props: sanitized_props_string(props),
|
|
195
|
+
err: err,
|
|
196
|
+
js_code: wrapper_js)
|
|
197
|
+
# rubocop:enable Style/RaiseArgs
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def raise_on_prerender_error(options)
|
|
201
|
+
options.fetch(:raise_on_prerender_error) { ReactOnRails.configuration.raise_on_prerender_error }
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def trace(options)
|
|
205
|
+
options.fetch(:trace) { ReactOnRails.configuration.trace }
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def prerender(options)
|
|
209
|
+
options.fetch(:prerender) { ReactOnRails.configuration.prerender }
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def replay_console(options)
|
|
213
|
+
options.fetch(:replay_console) { ReactOnRails.configuration.replay_console }
|
|
214
|
+
end
|
|
215
|
+
end
|
data/docker-compose.yml
ADDED
data/docs/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Justin Gordon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Heroku Deployment
|
|
2
|
+
The generator has created the necessary files and gems for deployment to Heroku. If you have installed manually, you will need to provide these files yourself:
|
|
3
|
+
|
|
4
|
+
+ `Procfile`: used by Heroku and Foreman to start the Puma server
|
|
5
|
+
+ `.buildpacks`: used to install Ruby and Node environments
|
|
6
|
+
+ `12factor` gem: required by Heroku
|
|
7
|
+
+ `'puma'` gem: recommended Heroku webserver
|
|
8
|
+
+ `config/puma.rb`: Puma webserver config file
|
|
9
|
+
+ `lib/tasks/assets.rake`: This rake task file is provided by the generator regardless of whether the user chose Heroku Deployment as an option. It is highlighted here because it is helpful to understand that this task is what generates your JavaScript bundles in production.
|
|
10
|
+
|
|
11
|
+
## How to Deploy
|
|
12
|
+
|
|
13
|
+
React on Rails requires both a ruby environment (for Rails) and a Node environment (for Webpack), so you will need to have Heroku use multiple buildpacks. Currently, we would suggest using [DDollar's Heroku Buildpack Multi](https://github.com/ddollar/heroku-buildpack-multi).
|
|
14
|
+
|
|
15
|
+
Assuming you have downloaded and installed the Heroku command-line utility and have initialized the app, you will need to tell Heroku to use Heroku Buildpack Multi via the command-line:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Heroku will now be able to use the multiple buildpacks specified in `.buildpacks`.
|
|
22
|
+
|
|
23
|
+
Note, an alternative approach is to use the [Heroku Toolbelt to set buildpacks](https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app).
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# TODO FIX THIS FILE AFTER this is updated: https://github.com/shakacode/react-webpack-rails-tutorial
|
|
2
|
+
|
|
3
|
+
# Manual Installation
|
|
4
|
+
Follow these steps if you choose to forgo the generator:
|
|
5
|
+
|
|
6
|
+
1. Globally expose React in your webpack config like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/537c985dc82faee333d80509343ca32a3965f9dd/client/webpack.client.base.config.js#L31):
|
|
7
|
+
|
|
8
|
+
```javascript
|
|
9
|
+
module: {
|
|
10
|
+
loaders: [
|
|
11
|
+
// React is necessary for the client rendering:
|
|
12
|
+
{ test: require.resolve('react'), loader: 'expose?React' },
|
|
13
|
+
|
|
14
|
+
// For React 0.14
|
|
15
|
+
{ test: require.resolve('react-dom'), loader: 'expose?ReactDOM' }, // not in the server one
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
3. Expose your client globals like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/537c985dc82faee333d80509343ca32a3965f9dd/client/app/startup/clientRegistration.jsx#L3):
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import App from './ClientApp';
|
|
23
|
+
import ReactOnRails from 'react-on-rails';
|
|
24
|
+
ReactOnRails.register({ App });
|
|
25
|
+
```
|
|
26
|
+
4. Put your client globals file as webpack entry points like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/537c985dc82faee333d80509343ca32a3965f9dd/client/webpack.client.rails.config.js#L22). Similar pattern for server rendering.
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
config.entry.app.push('./app/startup/clientRegistration');
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Additional Steps For Server Rendering (option `prerender` shown below)
|
|
33
|
+
See the next section for a sample webpack.server.rails.config.js.
|
|
34
|
+
|
|
35
|
+
1. Expose your server globals like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/537c985dc82faee333d80509343ca32a3965f9dd/client/app/startup/serverRegistration.jsx#L7)
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
import App from './ServerApp';
|
|
39
|
+
global.App = App;
|
|
40
|
+
```
|
|
41
|
+
2. Make the server globals file an entry point in your webpack config, like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/537c985dc82faee333d80509343ca32a3965f9dd/client/webpack.server.rails.config.js#L7)
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
entry: ['./app/startup/serverRegistration'],
|
|
45
|
+
```
|
|
46
|
+
3. Ensure the name of your ouput file (shown [here](https://github.com/shakacode/react-webpack-rails-tutorial/blob/537c985dc82faee333d80509343ca32a3965f9dd/client/webpack.server.rails.config.js#L9)) of your server bundle corresponds to the configuration of the gem. The default path is `app/assets/javascripts/generated`. See below for customization of configuration variables.
|
|
47
|
+
4. Expose `React` in your webpack config, like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/webpack.server.rails.config.js#L23)
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
{ test: require.resolve('react'), loader: 'expose?React' },
|
|
51
|
+
|
|
52
|
+
// For React 0.14
|
|
53
|
+
{ test: require.resolve('react-dom/server'), loader: 'expose?ReactDOMServer' }, // not in client one, only server
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Sample webpack.server.rails.config.js (ONLY for server rendering)
|
|
57
|
+
Be sure to check out the latest example version of [client/webpack.server.rails.config.js](https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/webpack.server.rails.config.js).
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
// Common webpack configuration for server bundle
|
|
61
|
+
|
|
62
|
+
module.exports = {
|
|
63
|
+
|
|
64
|
+
// the project dir
|
|
65
|
+
context: __dirname,
|
|
66
|
+
entry: ['./app/startup/serverRegistration'],
|
|
67
|
+
output: {
|
|
68
|
+
filename: 'server-bundle.js',
|
|
69
|
+
path: '../app/assets/javascripts/generated',
|
|
70
|
+
|
|
71
|
+
// CRITICAL to set libraryTarget: 'this' for enabling Rails to find the exposed modules IF you
|
|
72
|
+
// use the "expose" webpackfunctionality. See startup/serverRegistration.jsx.
|
|
73
|
+
// NOTE: This is NOT necessary if you use the syntax of global.MyComponent = MyComponent syntax.
|
|
74
|
+
// See http://webpack.github.io/docs/configuration.html#externals for documentation of this option
|
|
75
|
+
//libraryTarget: 'this',
|
|
76
|
+
},
|
|
77
|
+
resolve: {
|
|
78
|
+
extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx', 'config.js'],
|
|
79
|
+
},
|
|
80
|
+
module: {
|
|
81
|
+
loaders: [
|
|
82
|
+
{test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/},
|
|
83
|
+
|
|
84
|
+
// React is necessary for the client rendering:
|
|
85
|
+
{ test: require.resolve('react'), loader: 'expose?React' },
|
|
86
|
+
{ test: require.resolve('react-dom/server'), loader: 'expose?ReactDOMServer' },
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## What Happens?
|
|
93
|
+
|
|
94
|
+
Here's what the browser will render with a call to the `react_component` helper.
|
|
95
|
+

|
|
96
|
+
|
|
97
|
+
If you're curious as to what the gem generates for the server and client rendering, see [`spec/dummy/client/app/startup/serverRegistration.jsx`](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/spec/sample_generated_js/server-generated.js)
|
|
98
|
+
and [`spec/dummy/client/app/startup/ClientReduxApp.jsx`](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/spec/sample_generated_js/client-generated.js) for examples of this. Note, this is not the code that you are providing. You can see the client code by viewing the page source.
|
|
99
|
+
|
|
100
|
+
* **props**: [hash | string of json] Properties to pass to the react object. See this example if you're using Jbuilder: [react-webpack-rails-tutorial view rendering props using jBuilder](https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/app/views/pages/index.html.erb#L20)
|
|
101
|
+
|
|
102
|
+
```erb
|
|
103
|
+
<%= react_component('App', render(template: "/comments/index.json.jbuilder"), prerender: true) %>
|
|
104
|
+
```
|
|
105
|
+
* **options:** [hash]
|
|
106
|
+
* **prerender**: <true/false> set to false when debugging!
|
|
107
|
+
* **trace**: <true/false> set to true to print additional debugging information in the browser default is true for development, off otherwise
|
|
108
|
+
* **replay_console**: <true/false> Default is true. False will disable echoing server rendering logs, which can make troubleshooting server rendering difficult.
|
|
109
|
+
* Any other options are passed to the content tag, including the id.
|
|
110
|
+
|
|
111
|
+
# JavaScript
|
|
112
|
+
|
|
113
|
+
1. Configure your webpack configuration to create the file used for server rendering if you plan to do server rendering.
|
|
114
|
+
2. Follow the examples in `spec/dummy/client/app/startup/clientRegistration.jsx` to expose your react components for client side rendering.
|
|
115
|
+
3. Follow the examples in `spec/dummy/client/app/startup/serverRegistration.jsx` to expose your react components for server side rendering. The code is the same, but you might be importing from a file specialized for server rendering.
|
|
116
|
+
|
|
117
|
+
## React 0.13 vs. React 0.14
|
|
118
|
+
We no longer support React 0.13.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Node Dependencies and NPM
|
|
2
|
+
## Updating
|
|
3
|
+
After installing the files, you may want to update the node dependencies. This is analogous to updating gem versions:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
cd client
|
|
7
|
+
npm install -g npm-check-updates
|
|
8
|
+
rm npm-shrinkwrap.json
|
|
9
|
+
npm-check-updates -u
|
|
10
|
+
npm install
|
|
11
|
+
npm prune
|
|
12
|
+
npm shrinkwrap
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Confirm that the hot replacement dev server and the Rails server both work. You may have to delete `node_modules` and `npm-shrinkwrap.json` and then run `npm shrinkwrap`.
|
|
16
|
+
|
|
17
|
+
*Note: `npm prune` is required before running `npm shrinkwrap` to remove dependencies that are no longer needed after doing updates.*
|
|
18
|
+
|
|
19
|
+
## Adding New Dependencies
|
|
20
|
+
Typically, you can add your Node dependencies as you normally would. Occasionally, adding a new dependency may require removing and re-running `npm shrinkwrap`:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
cd client
|
|
24
|
+
npm install --save module_name@version
|
|
25
|
+
# or
|
|
26
|
+
# npm install --save_dev module_name@version
|
|
27
|
+
rm npm-shrinkwrap.json
|
|
28
|
+
npm shrinkwrap
|
|
29
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Optional Configuration
|
|
2
|
+
|
|
3
|
+
Create a file `config/react_on_rails.rb` to override any defaults (this file is automatically created for you when using the generator). If you don't specify this file, the default options are below.
|
|
4
|
+
|
|
5
|
+
The `server_bundle_js_file` must correspond to the bundle you want to use for server rendering.
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
# Shown below are the defaults for configuration
|
|
9
|
+
ReactOnRails.configure do |config|
|
|
10
|
+
# Client bundles are configured in application.js
|
|
11
|
+
# Server bundle is a single file for all server rendering of components.
|
|
12
|
+
# Set the server_bundle_js_file to "" if you know that you will not be server rendering.
|
|
13
|
+
config.server_bundle_js_file = "app/assets/javascripts/generated/server.js" # This is the default
|
|
14
|
+
|
|
15
|
+
# Below options can be overriden by passing to the helper method.
|
|
16
|
+
config.prerender = false # default is false
|
|
17
|
+
config.trace = Rails.env.development? # default is true for development, off otherwise
|
|
18
|
+
|
|
19
|
+
# For server rendering. This can be set to false so that server side messages are discarded.
|
|
20
|
+
config.replay_console = true # Default is true. Be cautious about turning this off.
|
|
21
|
+
config.logging_on_server = true # Default is true. Logs server rendering messags to Rails.logger.info
|
|
22
|
+
|
|
23
|
+
# Settings for the pool of renderers:
|
|
24
|
+
config.server_renderer_pool_size ||= 1 # ExecJS doesn't allow more than one on MRI
|
|
25
|
+
config.server_renderer_timeout ||= 20 # seconds
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
You can configure your pool of JS virtual machines and specify where it should load code:
|
|
30
|
+
|
|
31
|
+
- On MRI, use `therubyracer` for the best performance (see [discussion](https://github.com/reactjs/react-rails/pull/290))
|
|
32
|
+
- On MRI, you'll get a deadlock with `pool_size` > 1
|
|
33
|
+
- If you're using JRuby, you can increase `pool_size` to have real multi-threaded rendering.
|