react_on_rails 1.0.0.pre → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.dockerignore +2 -0
- data/.eslintignore +2 -0
- data/.eslintrc +1 -0
- data/.gitignore +4 -0
- data/.jscsrc +2 -2
- data/.rubocop.yml +2 -0
- data/.scss-lint.yml +3 -3
- data/.travis.yml +31 -18
- data/Dockerfile_ci +12 -0
- data/Dockerfile_tests +12 -0
- data/{LICENSE.txt → LICENSE} +0 -0
- data/README.md +170 -290
- data/Rakefile +20 -7
- data/app/helpers/react_on_rails_helper.rb +5 -5
- data/docker-compose.yml +12 -0
- data/docs/Contributing.md +26 -2
- data/docs/gen-notes/react_syntax.md +3 -0
- data/docs/gen-notes/reducers.md +31 -0
- data/docs/generator_testing_script.md +50 -0
- data/docs/linters.md +8 -0
- data/docs/manual_configuration.md +202 -0
- data/docs/node_dependencies_and_npm.md +29 -0
- data/lib/generators/react_on_rails/base_generator.rb +116 -0
- data/lib/generators/react_on_rails/bootstrap_generator.rb +84 -0
- data/lib/generators/react_on_rails/generator_helper.rb +48 -0
- data/lib/generators/react_on_rails/heroku_deployment_generator.rb +22 -0
- data/lib/generators/react_on_rails/install_generator.rb +86 -0
- data/lib/generators/react_on_rails/linters_generator.rb +38 -0
- data/lib/generators/react_on_rails/react_no_redux_generator.rb +37 -0
- data/lib/generators/react_on_rails/react_with_redux_generator.rb +61 -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 +6 -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/clientGlobals.jsx +4 -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/npm-shrinkwrap.json +2907 -0
- data/lib/generators/react_on_rails/templates/base/base/client/server.js +58 -0
- data/lib/generators/react_on_rails/templates/base/base/client/webpack.client.base.config.js +58 -0
- data/lib/generators/react_on_rails/templates/base/base/client/webpack.client.hot.config.js +65 -0
- data/lib/generators/react_on_rails/templates/base/base/client/webpack.client.rails.config.js +45 -0
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +30 -0
- data/lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake +26 -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/serverGlobals.jsx +3 -0
- data/lib/generators/react_on_rails/templates/base/server_rendering/client/webpack.server.rails.config.js +37 -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/client/README.md +97 -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/linters/client/.eslintignore +1 -0
- data/lib/generators/react_on_rails/templates/linters/client/.eslintrc +17 -0
- data/lib/generators/react_on_rails/templates/linters/client/.jscsrc +7 -0
- data/lib/generators/react_on_rails/templates/linters/lib/tasks/brakeman.rake +17 -0
- data/lib/generators/react_on_rails/templates/linters/lib/tasks/ci.rake +33 -0
- data/lib/generators/react_on_rails/templates/linters/lib/tasks/linters.rake +81 -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 +11 -0
- data/lib/generators/react_on_rails/templates/no_redux/base/client/package.json.tt +75 -0
- data/lib/generators/react_on_rails/templates/no_redux/server_rendering/client/app/bundles/HelloWorld/startup/HelloWorldAppServer.jsx +11 -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 +21 -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 +20 -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 +25 -0
- data/lib/generators/react_on_rails/templates/redux/base/client/package.json.tt +82 -0
- data/lib/generators/react_on_rails/templates/redux/server_rendering/client/app/bundles/HelloWorld/startup/HelloWorldAppServer.jsx +21 -0
- data/lib/react_on_rails.rb +1 -7
- data/lib/react_on_rails/configuration.rb +14 -5
- data/lib/react_on_rails/engine.rb +7 -0
- data/lib/react_on_rails/server_rendering_pool.rb +14 -1
- data/lib/react_on_rails/version.rb +1 -1
- data/react_on_rails.gemspec +2 -2
- data/ruby-lint.yml +1 -0
- metadata +75 -11
- data/Dockerfile +0 -14
- data/docs/README.md +0 -1
data/Rakefile
CHANGED
@@ -1,29 +1,42 @@
|
|
1
1
|
require "fileutils"
|
2
|
+
require "coveralls/rake/task"
|
2
3
|
|
3
4
|
namespace :run_rspec do
|
4
5
|
desc "Run RSpec for top level only"
|
5
6
|
task :gem do
|
6
|
-
sh %( rspec spec/
|
7
|
+
sh %( COVERAGE=true rspec spec/react_on_rails )
|
7
8
|
end
|
8
9
|
|
9
10
|
desc "Run RSpec for spec/dummy only"
|
10
11
|
task :dummy do
|
11
|
-
|
12
|
+
# TEST_ENV_NUMBER is used to make SimpleCov.command_name unique in order to
|
13
|
+
# prevent a name collision
|
14
|
+
sh %( cd spec/dummy && DRIVER=selenium_firefox COVERAGE=true TEST_ENV_COMMAND_NAME="dummy-tests" rspec )
|
12
15
|
end
|
13
16
|
|
14
17
|
desc "Run RSpec for spec/dummy only"
|
15
18
|
task :dummy_react_013 do
|
16
|
-
|
19
|
+
# TEST_ENV_NUMBER is used to make SimpleCov.command_name unique in order to
|
20
|
+
# prevent a name collision
|
21
|
+
sh %( cd spec/dummy-react-013 &&
|
22
|
+
DRIVER=selenium_firefox COVERAGE=true TEST_ENV_COMMAND_NAME="dummy-react-013-tests" rspec )
|
17
23
|
end
|
18
24
|
|
19
|
-
|
25
|
+
desc "Run RSpec on spec/empty_spec in order to have SimpleCov generate a coverage report from cache"
|
26
|
+
task :empty do
|
27
|
+
sh %( COVERAGE=true rspec spec/empty_spec.rb )
|
28
|
+
end
|
29
|
+
|
30
|
+
Coveralls::RakeTask.new
|
31
|
+
|
32
|
+
task run_rspec: [:gem, :dummy, :dummy_react_013, :empty, "coveralls:push"] do
|
20
33
|
puts "Completed all RSpec tests"
|
21
34
|
end
|
22
35
|
end
|
23
36
|
desc "Runs all tests. Run `rake -D run_rspec` to see all available test options"
|
24
37
|
task run_rspec: ["run_rspec:run_rspec"]
|
25
38
|
|
26
|
-
task default: :
|
39
|
+
task default: ["run_rspec", "docker:lint"]
|
27
40
|
|
28
41
|
namespace :lint do
|
29
42
|
desc "Run Rubocop as shell"
|
@@ -38,7 +51,7 @@ namespace :lint do
|
|
38
51
|
|
39
52
|
desc "Run scss-lint as shell"
|
40
53
|
task :scss do
|
41
|
-
sh "scss-lint
|
54
|
+
sh "scss-lint spec/dummy/app/assets/stylesheets/"
|
42
55
|
end
|
43
56
|
|
44
57
|
desc "Run eslint as shell"
|
@@ -95,4 +108,4 @@ desc "Runs all linters from docker. Run `rake -D docker` to see all available li
|
|
95
108
|
task docker: ["docker:lint"]
|
96
109
|
|
97
110
|
desc "Run all tests and linting"
|
98
|
-
task ci: %w(
|
111
|
+
task ci: %w(run_rspec lint)
|
@@ -123,11 +123,8 @@ module ReactOnRailsHelper
|
|
123
123
|
|
124
124
|
result = ReactOnRails::ServerRenderingPool.server_render_js_with_console_logging(wrapper_js)
|
125
125
|
|
126
|
-
# IMPORTANT:
|
127
|
-
|
128
|
-
#{result[0]}
|
129
|
-
#{replay_console(options) ? result[1] : ''}
|
130
|
-
HTML
|
126
|
+
# IMPORTANT: To ensure that Rails doesn't auto-escape HTML tags, use the 'raw' method.
|
127
|
+
raw("#{result[0]}#{replay_console(options) ? result[1] : ''}")
|
131
128
|
end
|
132
129
|
|
133
130
|
private
|
@@ -142,6 +139,9 @@ module ReactOnRailsHelper
|
|
142
139
|
def server_rendered_react_component_html(options, props_string, react_component_name, data_variable_name, dom_id)
|
143
140
|
return ["", ""] unless prerender(options)
|
144
141
|
|
142
|
+
# Make sure that we use up-to-date server-bundle
|
143
|
+
ReactOnRails::ServerRenderingPool.reset_pool_if_server_bundle_was_modified
|
144
|
+
|
145
145
|
wrapper_js = <<-JS
|
146
146
|
(function() {
|
147
147
|
var props = #{props_string};
|
data/docker-compose.yml
CHANGED
@@ -1,5 +1,17 @@
|
|
1
|
+
ci:
|
2
|
+
build: .
|
3
|
+
dockerfile: Dockerfile_ci
|
4
|
+
working_dir: /app/
|
5
|
+
volumes:
|
6
|
+
- '.:/app/'
|
1
7
|
lint:
|
8
|
+
image: dylangrafmyre/docker-lint
|
9
|
+
working_dir: /app/
|
10
|
+
volumes:
|
11
|
+
- '.:/app/'
|
12
|
+
tests:
|
2
13
|
build: .
|
14
|
+
dockerfile: Dockerfile_tests
|
3
15
|
working_dir: /app/
|
4
16
|
volumes:
|
5
17
|
- '.:/app/'
|
data/docs/Contributing.md
CHANGED
@@ -27,12 +27,26 @@ Run `rake` for testing the gem and `spec/dummy` and `spec/dummy-react-013`. Othe
|
|
27
27
|
|
28
28
|
If you run `rspec` at the top level, you'll see this message: `require': cannot load such file -- rails_helper (LoadError)`
|
29
29
|
|
30
|
+
After running a test, you can view the coverage results SimpleCov reports by opening `coverage/index.html`.
|
31
|
+
|
30
32
|
### Debugging
|
31
33
|
Start the sample app like this for some debug printing:
|
32
34
|
```bash
|
33
35
|
TRACE_REACT_ON_RAILS=true && foreman start
|
34
36
|
```
|
35
37
|
|
38
|
+
### Install Generator
|
39
|
+
In your Rails app add this gem with a path to your fork.
|
40
|
+
|
41
|
+
```
|
42
|
+
gem 'react_on_rails', path: '/your_fork'
|
43
|
+
gem 'therubyracer'
|
44
|
+
```
|
45
|
+
|
46
|
+
The main installer can be run with ```rails generate react_on_rails:install```
|
47
|
+
|
48
|
+
### Testing the Generator
|
49
|
+
The generators are covered by generator tests using Rails's generator testing helpers, but it never hurts to do a sanity check and explore the API. See [generator_testing_script.md](generator_testing_script.md) for a script on how to run the generator on a fresh project.
|
36
50
|
|
37
51
|
## Updating New Versions of the Gem
|
38
52
|
|
@@ -60,7 +74,17 @@ the `reactonrails_lint` docker image and docker-compose `lint` container. The in
|
|
60
74
|
but after the install, startup is very quick.
|
61
75
|
|
62
76
|
### Linting Commands
|
63
|
-
Run `rake -D docker` to see all docker linting commands for rake. `rake docker` will run all linters.
|
77
|
+
Run `rake -D docker` to see all docker linting commands for rake. `rake docker:lint` will run all linters.
|
64
78
|
For individual rake linting commands please refer to `rake -D docker` for the list.
|
65
79
|
You can run specfic linting for directories or files by using `docker-compose run lint rubocop (file path or directory)`, etc.
|
66
|
-
`docker-compose run lint
|
80
|
+
`docker-compose run lint bash` sets you up to run from the container command line.
|
81
|
+
|
82
|
+
### Docker CI - Test and Linting
|
83
|
+
Docker CI and Tests containers have a xvfd server automatically started for headless browser testing with selenium and firefox.
|
84
|
+
|
85
|
+
Run `docker-compose build ci` to build the ci container. Run `docker-compose run ci` to start all
|
86
|
+
rspec test and linting. `docker-compose run --entrypoint=/bin/bash` will override the default ci action and place
|
87
|
+
you inside the ci container in a bash session. This is what is run on Travis-ci.
|
88
|
+
|
89
|
+
Run `docker-compose build tests` to build the tests container. Run `docker-compose run tests` to start all
|
90
|
+
rspec tests.
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Reducers
|
2
|
+
Documentation of generated Redux code for reducers.
|
3
|
+
|
4
|
+
## Example
|
5
|
+
The `helloWorld/reducers/index.jsx` example that results from running the generator with the Redux option may be slightly confusing because of its simplicity. For clarity, what follows is a more fleshed-out example of what a reducer might look like:
|
6
|
+
|
7
|
+
```javascript
|
8
|
+
import usersReducer from './usersReducer';
|
9
|
+
import blogPostsReducer from './blogPostsReducer';
|
10
|
+
import commentsReducer from './commentsReducer';
|
11
|
+
// ...
|
12
|
+
|
13
|
+
import { $$initialState as $$usersState } from './usersReducer';
|
14
|
+
import { $$initialState as $$blogPostsState } from './blogPostsReducer';
|
15
|
+
import { $$initialState as $$commentsState } from './commentsReducer';
|
16
|
+
// ...
|
17
|
+
|
18
|
+
export default {
|
19
|
+
$$usersStore: usersReducer,
|
20
|
+
$$blogPostsStore: blogPostsReducer,
|
21
|
+
$$commentsStore: commentsReducer,
|
22
|
+
// ...
|
23
|
+
};
|
24
|
+
|
25
|
+
export const initalStates = {
|
26
|
+
$$usersState,
|
27
|
+
$$blogPostsState,
|
28
|
+
$$commentsState,
|
29
|
+
// ...
|
30
|
+
};
|
31
|
+
```
|
@@ -0,0 +1,50 @@
|
|
1
|
+
Per running steps:
|
2
|
+
|
3
|
+
From directory of `react_on_rails`, with a test app named "react_on_rails_gen"
|
4
|
+
|
5
|
+
```bash
|
6
|
+
cd ..
|
7
|
+
rails new react_on_rails_gen
|
8
|
+
cd react_on_rails_gen
|
9
|
+
git init
|
10
|
+
git add .
|
11
|
+
git commit -m "Rails new plus react_on_rails"
|
12
|
+
```
|
13
|
+
|
14
|
+
Edit the Gemfile, adding these 2 lines:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'react_on_rails', path: '../react_on_rails'
|
18
|
+
gem 'therubyracer'
|
19
|
+
```
|
20
|
+
|
21
|
+
Note the relative path to the react_on_rails gem.
|
22
|
+
|
23
|
+
```bash
|
24
|
+
bundle
|
25
|
+
git commit -am "added react_on_rails gem"
|
26
|
+
```
|
27
|
+
|
28
|
+
You can now mess around with the generator:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
# See available options
|
32
|
+
rails generate react_on_rails:install --help
|
33
|
+
# Actual install, for example, with Redux
|
34
|
+
rails generate react_on_rails:install --redux
|
35
|
+
```
|
36
|
+
|
37
|
+
If you do actually run the generator, then you can see the changes that the generator made, ready for a commit.
|
38
|
+
|
39
|
+
Then run:
|
40
|
+
|
41
|
+
```bash
|
42
|
+
cd client
|
43
|
+
npm install
|
44
|
+
cd ..
|
45
|
+
foreman start -f Procfile.dev
|
46
|
+
```
|
47
|
+
|
48
|
+
Then visit `localhost:3000/hello_world` to see it.
|
49
|
+
|
50
|
+
That's it!
|
data/docs/linters.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Linters
|
2
|
+
The React on Rails generator automatically adds linters and their recommended accompanying configurations to your project (to disable this behavior, include the `--skip-linters` option when running the generator). Those linters that are written in Ruby have been added to your Gemfile, and those that run in Node have been add to your `package.json` under `devDependencies`.
|
3
|
+
|
4
|
+
To run the linters (runs both Ruby and Node linters):
|
5
|
+
|
6
|
+
```bash
|
7
|
+
rake lint
|
8
|
+
```
|
@@ -0,0 +1,202 @@
|
|
1
|
+
## Manual Installation
|
2
|
+
Follow these steps if you choose to forgo the generator:
|
3
|
+
|
4
|
+
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):
|
5
|
+
|
6
|
+
```javascript
|
7
|
+
module: {
|
8
|
+
loaders: [
|
9
|
+
// React is necessary for the client rendering:
|
10
|
+
{ test: require.resolve('react'), loader: 'expose?React' },
|
11
|
+
|
12
|
+
// For React 0.14
|
13
|
+
{ test: require.resolve('react-dom'), loader: 'expose?ReactDOM' }, // not in the server one
|
14
|
+
```
|
15
|
+
|
16
|
+
|
17
|
+
2. Require `react_on_rails` in your `application.js` like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/361f4338ebb39a5d3934b00cb6d6fcf494773000/app/assets/javascripts/application.js#L15). It possibly should come after you require `turbolinks`:
|
18
|
+
|
19
|
+
```
|
20
|
+
//= require react_on_rails
|
21
|
+
```
|
22
|
+
3. Expose your client globals like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/537c985dc82faee333d80509343ca32a3965f9dd/client/app/startup/clientGlobals.jsx#L3):
|
23
|
+
|
24
|
+
```javascript
|
25
|
+
import App from './ClientApp';
|
26
|
+
window.App = App;
|
27
|
+
```
|
28
|
+
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.
|
29
|
+
|
30
|
+
```javascript
|
31
|
+
config.entry.app.push('./app/startup/clientGlobals');
|
32
|
+
```
|
33
|
+
|
34
|
+
## Additional Steps For Server Rendering (option `prerender` shown below)
|
35
|
+
See the next section for a sample webpack.server.rails.config.js.
|
36
|
+
|
37
|
+
1. Expose your server globals like [this](https://github.com/shakacode/react-webpack-rails-tutorial/blob/537c985dc82faee333d80509343ca32a3965f9dd/client/app/startup/serverGlobals.jsx#L7)
|
38
|
+
|
39
|
+
```javascript
|
40
|
+
import App from './ServerApp';
|
41
|
+
global.App = App;
|
42
|
+
```
|
43
|
+
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)
|
44
|
+
|
45
|
+
```javascript
|
46
|
+
entry: ['./app/startup/serverGlobals'],
|
47
|
+
```
|
48
|
+
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.
|
49
|
+
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)
|
50
|
+
|
51
|
+
```javascript
|
52
|
+
{ test: require.resolve('react'), loader: 'expose?React' },
|
53
|
+
|
54
|
+
// For React 0.14
|
55
|
+
{ test: require.resolve('react-dom/server'), loader: 'expose?ReactDOMServer' }, // not in client one, only server
|
56
|
+
```
|
57
|
+
|
58
|
+
#### Sample webpack.server.rails.config.js (ONLY for server rendering)
|
59
|
+
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).
|
60
|
+
|
61
|
+
```javascript
|
62
|
+
// Common webpack configuration for server bundle
|
63
|
+
|
64
|
+
module.exports = {
|
65
|
+
|
66
|
+
// the project dir
|
67
|
+
context: __dirname,
|
68
|
+
entry: ['./app/startup/serverGlobals'],
|
69
|
+
output: {
|
70
|
+
filename: 'server-bundle.js',
|
71
|
+
path: '../app/assets/javascripts/generated',
|
72
|
+
|
73
|
+
// CRITICAL to set libraryTarget: 'this' for enabling Rails to find the exposed modules IF you
|
74
|
+
// use the "expose" webpackfunctionality. See startup/serverGlobals.jsx.
|
75
|
+
// NOTE: This is NOT necessary if you use the syntax of global.MyComponent = MyComponent syntax.
|
76
|
+
// See http://webpack.github.io/docs/configuration.html#externals for documentation of this option
|
77
|
+
//libraryTarget: 'this',
|
78
|
+
},
|
79
|
+
resolve: {
|
80
|
+
extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx', 'config.js'],
|
81
|
+
},
|
82
|
+
module: {
|
83
|
+
loaders: [
|
84
|
+
{test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/},
|
85
|
+
|
86
|
+
// React is necessary for the client rendering:
|
87
|
+
{ test: require.resolve('react'), loader: 'expose?React' },
|
88
|
+
{ test: require.resolve('react-dom/server'), loader: 'expose?ReactDOMServer' },
|
89
|
+
],
|
90
|
+
},
|
91
|
+
};
|
92
|
+
```
|
93
|
+
|
94
|
+
### What Happens?
|
95
|
+
|
96
|
+
Here's what the browser will render with a call to the `react_component` helper.
|
97
|
+
![2015-09-28_20-24-35](https://cloud.githubusercontent.com/assets/1118459/10157268/41435186-6624-11e5-9341-6fc4cf35ee90.png)
|
98
|
+
|
99
|
+
If you're curious as to what the gem generates for the server and client rendering, see [`spec/dummy/client/app/startup/serverGlobals.jsx`](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/spec/sample_generated_js/server-generated.js)
|
100
|
+
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.
|
101
|
+
|
102
|
+
* **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)
|
103
|
+
|
104
|
+
```erb
|
105
|
+
<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
|
106
|
+
generator_function: true, prerender: true) %>
|
107
|
+
```
|
108
|
+
* **options:** [hash]
|
109
|
+
* **generator_function**: <true/false> default is false, set to true if you want to use a generator function rather than a React Component.
|
110
|
+
* **prerender**: <true/false> set to false when debugging!
|
111
|
+
* **trace**: <true/false> set to true to print additional debugging information in the browser default is true for development, off otherwise
|
112
|
+
* **replay_console**: <true/false> Default is true. False will disable echoing server rendering logs, which can make troubleshooting server rendering difficult.
|
113
|
+
* Any other options are passed to the content tag, including the id.
|
114
|
+
|
115
|
+
## JavaScript
|
116
|
+
|
117
|
+
1. Configure your webpack configuration to create the file used for server rendering if you plan to do server rendering.
|
118
|
+
2. Follow the examples in `spec/dummy/client/app/startup/clientGlobals.jsx` to expose your react components for client side rendering.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
import HelloWorld from '../components/HelloWorld';
|
122
|
+
window.HelloWorld = HelloWorld;
|
123
|
+
```
|
124
|
+
3. Follow the examples in `spec/dummy/client/app/startup/serverGlobals.jsx` to expose your react components for server side rendering.
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
import HelloWorld from '../components/HelloWorld';
|
128
|
+
global.HelloWorld = HelloWorld;
|
129
|
+
```
|
130
|
+
|
131
|
+
## Server Rendering Tips
|
132
|
+
|
133
|
+
- Your code can't reference `document`. Server side JS execution does not have access to `document`, so jQuery and some
|
134
|
+
other libs won't work in this environment. You can debug this by putting in `console.log`
|
135
|
+
statements in your code.
|
136
|
+
- You can conditionally avoid running code that references document by passing in a boolean prop to your top level react
|
137
|
+
component. Since the passed in props Hash from the view helper applies to client and server side code, the best way to
|
138
|
+
do this is to use a generator function.
|
139
|
+
|
140
|
+
You might do something like this in some file for your top level component:
|
141
|
+
```javascript
|
142
|
+
global.App = () => <MyComponent serverSide={true} />;
|
143
|
+
```
|
144
|
+
|
145
|
+
The point is that you have separate files for top level client or server side, and you pass some extra option indicating that rendering is happening server sie.
|
146
|
+
|
147
|
+
## Optional Configuration
|
148
|
+
|
149
|
+
Create a file `config/react_on_rails.rb` to override any defaults. If you don't specify this file,
|
150
|
+
the default options are below.
|
151
|
+
|
152
|
+
The `server_bundle_js_file` must correspond to the bundle you want to use for server rendering.
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
# Shown below are the defaults for configuration
|
156
|
+
ReactOnRails.configure do |config|
|
157
|
+
# Client bundles are configured in application.js
|
158
|
+
# Server bundle is a single file for all server rendering of components.
|
159
|
+
# Set the server_bundle_js_file to "" if you know that you will not be server rendering.
|
160
|
+
config.server_bundle_js_file = "app/assets/javascripts/generated/server.js" # This is the default
|
161
|
+
|
162
|
+
# Below options can be overriden by passing to the helper method.
|
163
|
+
config.prerender = false # default is false
|
164
|
+
config.generator_function = false # default is false, meaning that you expose ReactComponents directly
|
165
|
+
config.trace = Rails.env.development? # default is true for development, off otherwise
|
166
|
+
|
167
|
+
# For server rendering. This can be set to false so that server side messages are discarded.
|
168
|
+
config.replay_console = true # Default is true. Be cautious about turning this off.
|
169
|
+
config.logging_on_server = true # Default is true. Logs server rendering messags to Rails.logger.info
|
170
|
+
|
171
|
+
# Settings for the pool of renderers:
|
172
|
+
config.server_renderer_pool_size ||= 1 # ExecJS doesn't allow more than one on MRI
|
173
|
+
config.server_renderer_timeout ||= 20 # seconds
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
You can configure your pool of JS virtual machines and specify where it should load code:
|
178
|
+
|
179
|
+
- On MRI, use `therubyracer` for the best performance (see [discussion](https://github.com/reactjs/react-rails/pull/290))
|
180
|
+
- On MRI, you'll get a deadlock with `pool_size` > 1
|
181
|
+
- If you're using JRuby, you can increase `pool_size` to have real multi-threaded rendering.
|
182
|
+
|
183
|
+
## Tips
|
184
|
+
+ **DO NOT RUN `rails s`** and instead run `foreman start -f Procfile.dev`
|
185
|
+
+ The default for rendering right now is `prerender: false`. **NOTE:** Server side rendering does not work for some components, namely react-router, that use an async setup for server rendering. You can configure the default for prerender in your config.
|
186
|
+
+ You can expose either a React component or a function that returns a React component. If you wish to create a React component via a function, rather than simply props, then you need to set the property "generator" on that function to true. When that is done, the function is invoked with a single parameter of "props", and that function should return a React element.
|
187
|
+
+ Be sure you can first render your react component client only before you try to debug server rendering!
|
188
|
+
+ Open up the HTML source and take a look at the generated HTML and the JavaScript to see what's going on under the covers. Note that when server rendering is turned on, then you'll see the server rendered react components. When server rendering is turned off, then you'll only see the `div` element where the in-line JavaScript will render the component. You might also notice how the props you pass (a Ruby Hash) becomes in-line JavaScript on the HTML page.
|
189
|
+
|
190
|
+
## React 0.13 vs. React 0.14
|
191
|
+
The main difference for using react_on_rails is that you need to add additional lines in the webpack config files:
|
192
|
+
|
193
|
+
+ Normal mode (JavaScript is rendered on the client side) webpack config file:
|
194
|
+
|
195
|
+
```javascript
|
196
|
+
{ test: require.resolve('react-dom'), loader: 'expose?ReactDOM' },
|
197
|
+
```
|
198
|
+
+ Server-side rendering webpack config file:
|
199
|
+
|
200
|
+
```javascript
|
201
|
+
{ test: require.resolve('react-dom/server'), loader: 'expose?ReactDOMServer' },
|
202
|
+
```
|