react_on_rails 2.0.0.beta.1 → 2.0.0.beta.2
Sign up to get free protection for your applications and to get access to all the features.
- 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/CHANGELOG.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file. Items under `Unreleased` is upcoming features that will be out in next version.
|
3
|
+
|
4
|
+
## v1.2.0
|
5
|
+
### Added
|
6
|
+
- Support `--skip-bootstrap` or `-b` option for generator.
|
7
|
+
- Create examples tasks to test generated example apps.
|
8
|
+
|
9
|
+
### Fixed
|
10
|
+
- Fix non-server rendering configuration issues.
|
11
|
+
- Fix application.js incorrect overwritten issue.
|
12
|
+
- Fix Gemfile dependencies.
|
13
|
+
- Fix several generator issues.
|
14
|
+
|
15
|
+
### Removed
|
16
|
+
- Remove templates/client folder.
|
17
|
+
|
18
|
+
## [1.1.1] - 2015-11-28
|
19
|
+
### Added
|
20
|
+
- Support for React Router.
|
21
|
+
- Error and redirect handling.
|
22
|
+
- Turbolinks support.
|
23
|
+
|
24
|
+
### Fixed
|
25
|
+
- Fix several generator related issues.
|
26
|
+
|
27
|
+
### Deprecated
|
28
|
+
- Nothing.
|
29
|
+
|
30
|
+
### Removed
|
31
|
+
- Nothing.
|
32
|
+
|
33
|
+
[Unreleased]: https://github.com/shakacode/react_on_rails/compare/v1.0.0...HEAD
|
34
|
+
[1.1.1]: https://github.com/shakacode/react_on_rails/compare/v1.0.0...v1.1.1
|
data/Dockerfile_tests
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
FROM dylangrafmyre/docker-ci
|
2
|
+
|
3
|
+
WORKDIR /app/
|
4
|
+
|
5
|
+
COPY ["/lib/react_on_rails/version.rb", "/app/lib/react_on_rails/"]
|
6
|
+
COPY ["Gemfile", "Gemfile.lock", "react_on_rails.gemspec", "rakelib/", "/app/"]
|
7
|
+
COPY ["/spec/dummy/Gemfile", "/spec/dummy/Gemfile.lock", "/app/spec/dummy/"]
|
8
|
+
RUN bundle install --gemfile=spec/dummy/Gemfile
|
9
|
+
|
10
|
+
ENV DISPLAY :99
|
11
|
+
ENTRYPOINT service xvfd start \
|
12
|
+
&& rake
|
data/Gemfile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem"s dependencies in react_on_rails.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
# The following gems are dependencies of the gem's dummy/example apps, not the gem itself.
|
7
|
+
# They must be defined here because of the way Travis CI works, in that it will only
|
8
|
+
# bundle install from a single Gemfile. Therefore, all gems that we will need for any dummy/example
|
9
|
+
# app have to be manually added to this file.
|
10
|
+
gem "awesome_print"
|
11
|
+
gem "bootstrap-sass"
|
12
|
+
gem "capybara"
|
13
|
+
gem "capybara-screenshot"
|
14
|
+
gem "chromedriver-helper"
|
15
|
+
gem "coffee-rails", "~> 4.1.0"
|
16
|
+
gem "jbuilder", "~> 2.0"
|
17
|
+
gem "jquery-rails"
|
18
|
+
gem "launchy"
|
19
|
+
gem "pry"
|
20
|
+
gem "pry-byebug"
|
21
|
+
gem "pry-doc"
|
22
|
+
gem "pry-rails"
|
23
|
+
gem "pry-rescue"
|
24
|
+
gem "pry-stack_explorer"
|
25
|
+
gem "puma"
|
26
|
+
gem "rails", "4.2.5"
|
27
|
+
gem "rails_12factor"
|
28
|
+
gem "rspec-rails"
|
29
|
+
gem "rubocop", require: false
|
30
|
+
gem "ruby-lint", require: false
|
31
|
+
gem "sass-rails", "~> 5.0"
|
32
|
+
gem "scss_lint", require: false
|
33
|
+
gem "sdoc", "~> 0.4.0", group: :doc
|
34
|
+
gem "selenium-webdriver"
|
35
|
+
gem "spring"
|
36
|
+
gem "sqlite3"
|
37
|
+
gem "therubyracer"
|
38
|
+
gem "turbolinks"
|
39
|
+
gem "uglifier", ">= 2.7.2"
|
40
|
+
gem "web-console", "~> 2.0"
|
data/README.md
ADDED
@@ -0,0 +1,365 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/shakacode/react_on_rails.svg?branch=master)](https://travis-ci.org/shakacode/react_on_rails) [![Coverage Status](https://coveralls.io/repos/shakacode/react_on_rails/badge.svg?branch=master&service=github)](https://coveralls.io/github/shakacode/react_on_rails?branch=master) [![Dependency Status](https://gemnasium.com/shakacode/react_on_rails.svg)](https://gemnasium.com/shakacode/react_on_rails) [![Gem Version](https://badge.fury.io/rb/react_on_rails.svg)](https://badge.fury.io/rb/react_on_rails)
|
2
|
+
|
3
|
+
# React on Rails
|
4
|
+
React on Rails integrates Facebook's [React](https://github.com/facebook/react) front-end framework with Rails. Currently, both React v0.14 and v0.13 are supported, with server rendering. [Redux](https://github.com/rackt/redux) and [React-Router](https://github.com/rackt/react-redux) are supported as well. See the Rails on Maui [blog post](http://www.railsonmaui.com/blog/2014/10/03/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/) that started it all!
|
5
|
+
|
6
|
+
Be sure to see the [React Webpack Rails Tutorial Code](https://github.com/shakacode/react-webpack-rails-tutorial) along with the live example at [www.reactrails.com](http://www.reactrails.com).
|
7
|
+
|
8
|
+
## Including your React Component in your Rails Views
|
9
|
+
Please see [Getting Started](#getting-started) for how to set up your Rails project for React on Rails to understand how `react_on_rails` can see your ReactComponents.
|
10
|
+
|
11
|
+
+ *Normal Mode (React component will be rendered on client):*
|
12
|
+
|
13
|
+
```erb
|
14
|
+
<%= react_component("HelloWorldApp", @some_props) %>
|
15
|
+
```
|
16
|
+
+ *Server-Side Rendering (React component is first rendered into HTML on the server):*
|
17
|
+
|
18
|
+
```erb
|
19
|
+
<%= react_component("HelloWorldApp", @some_props, prerender: true) %>
|
20
|
+
```
|
21
|
+
|
22
|
+
+ The `component_name` parameter is a string matching the name you used to globally expose your React component. So, in the above examples, if you had a React component named "HelloWorldApp," you would register it with the following lines:
|
23
|
+
|
24
|
+
```js
|
25
|
+
import ReactOnRails from 'react-on-rails';
|
26
|
+
import HelloWorldApp from './HelloWorldApp';
|
27
|
+
ReactOnRails.register({ HelloWorldApp });
|
28
|
+
```
|
29
|
+
|
30
|
+
Exposing your component in this way is how React on Rails is able to reference your component from a Rails view. You can expose as many components as you like, as long as their names do not collide. See below for the details of how you expose your components via the react_on_rails webpack configuration.
|
31
|
+
|
32
|
+
+ `@some_props` can be either a hash or JSON string. This is an optional argument assuming you do not need to pass any options (if you want to pass options, such as `prerender: true`, but you do not want to pass any properties, simply pass an empty hash `{}`). This will make the data available in your component:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# Rails View
|
36
|
+
<%= react_component("HelloWorldApp", { name: "Stranger" })
|
37
|
+
```
|
38
|
+
|
39
|
+
```javascript
|
40
|
+
// inside your React component
|
41
|
+
this.props.name // "Stranger"
|
42
|
+
```
|
43
|
+
|
44
|
+
## Documentation
|
45
|
+
|
46
|
+
+ [Features](#features)
|
47
|
+
+ [Why Webpack?](#why-webpack)
|
48
|
+
+ [Getting Started](#getting-started)
|
49
|
+
+ [How it Works](#how-it-works)
|
50
|
+
- [Client-Side Rendering vs. Server-Side Rendering](#client-side-rendering-vs-server-side-rendering)
|
51
|
+
- [Building the Bundles](#building-the-bundles)
|
52
|
+
- [Globally Exposing Your React Components](#globally-exposing-your-react-components)
|
53
|
+
- [Rails View Helpers In-Depth](#rails-view-helpers-in-depth)
|
54
|
+
- [Redux](#redux)
|
55
|
+
- [React-Router](#react-router)
|
56
|
+
+ [Generator](#generator)
|
57
|
+
- [Understanding the Organization of the Generated Client Code](#understanding-the-organization-of-the-generated-client-code)
|
58
|
+
- [Redux](#redux)
|
59
|
+
- [Using Images and Fonts](#using-images-and-fonts)
|
60
|
+
- [Bootstrap Integration](#bootstrap-integration)
|
61
|
+
+ [Bootstrap via Rails Server](#bootstrap-via-rails-server)
|
62
|
+
+ [Bootstrap via Webpack HMR Dev Server](#bootstrap-via-webpack-hmr-dev-server)
|
63
|
+
+ [Keeping Custom Bootstrap Configurations Synced](#keeping-custom-bootstrap-configurations-synced)
|
64
|
+
+ [Skip Bootstrap Integration](#skip-bootstrap-integration)
|
65
|
+
- [Linters](#linters)
|
66
|
+
+ [JavaScript Linters](#javascript-linters)
|
67
|
+
+ [Ruby Linters](#ruby-linters)
|
68
|
+
+ [Running the Linters](#running-the-linters)
|
69
|
+
+ [Developing with the Webpack Dev Server](#developing-with-the-webpack-dev-server)
|
70
|
+
+ [Adding Additional Routes for the Dev Server](#adding-additional-routes-for-the-dev-server)
|
71
|
+
+ [Additional Reading](#additional-reading)
|
72
|
+
+ [Contributing](#contributing)
|
73
|
+
+ [License](#license)
|
74
|
+
+ [Authors](#authors)
|
75
|
+
+ [About ShakaCode](#about-shakacode)
|
76
|
+
|
77
|
+
---
|
78
|
+
|
79
|
+
## Features
|
80
|
+
Like the [react-rails](https://github.com/reactjs/react-rails) gem, React on Rails is capable of server-side rendering with fragment caching and is compatible with [turbolinks](https://github.com/rails/turbolinks). Unlike react-rails, which depends heavily on sprockets and jquery-ujs, React on Rails uses [webpack](http://webpack.github.io/) and does not depend on jQuery. While the initial setup is slightly more involved, it allows for advanced functionality such as:
|
81
|
+
|
82
|
+
+ [Redux](https://github.com/rackt/redux)
|
83
|
+
+ [Webpack dev server](https://webpack.github.io/docs/webpack-dev-server.html) with [hot module replacement](https://webpack.github.io/docs/hot-module-replacement-with-webpack.html)
|
84
|
+
+ [Webpack optimization functionality](https://github.com/webpack/docs/wiki/optimization)
|
85
|
+
+ _*(Coming Soon)*_ [React Router](https://github.com/rackt/react-router)
|
86
|
+
+ *([Gem *Pull Request*](https://github.com/shakacode/react_on_rails/pull/68))*
|
87
|
+
+ *([Tutorial Pull Request](https://github.com/shakacode/react-webpack-rails-tutorial/pull/128))*
|
88
|
+
|
89
|
+
See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code.
|
90
|
+
|
91
|
+
## Why Webpack?
|
92
|
+
|
93
|
+
Webpack is used for 2 purposes:
|
94
|
+
|
95
|
+
1. Generate several JavaScript "bundles" for inclusion in `application.js`.
|
96
|
+
2. Providing the Webpack Dev Server for quick prototyping of components without needing to refresh your browser to see updates.
|
97
|
+
|
98
|
+
This usage of webpack fits neatly and simply into the existing Rails sprockets system and you can include React components on a Rails view with a simple helper.
|
99
|
+
|
100
|
+
Compare this to some alternative approaches for SPAs (Single Page Apps) that utilize Webpack and Rails. They will use a separate node server to distribute web pages, JavaScript assets, CSS, etc., and will still use Rails as an API server. A good example of this is our ShakaCode team member Alex's article [
|
101
|
+
Universal React with Rails: Part I](https://medium.com/@alexfedoseev/isomorphic-react-with-rails-part-i-440754e82a59).
|
102
|
+
|
103
|
+
We're definitely not doing that. With react_on_rails, webpack is mainly generating a nice JavaScript file for inclusion into `application.js`. We're going to KISS. And that's all relative given how much there is to get right in an enterprise class web application.
|
104
|
+
|
105
|
+
## Getting Started
|
106
|
+
1. Add the following to your Gemfile and bundle install:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
gem "react_on_rails"
|
110
|
+
```
|
111
|
+
|
112
|
+
2. Run the generator with a simple "Hello World" example:
|
113
|
+
|
114
|
+
```bash
|
115
|
+
rails generate react_on_rails:install
|
116
|
+
```
|
117
|
+
|
118
|
+
3. NPM install. Make sure you are on a recent version of node, preferably using nvm.
|
119
|
+
|
120
|
+
```bash
|
121
|
+
npm install
|
122
|
+
```
|
123
|
+
|
124
|
+
4. Start your Rails server:
|
125
|
+
|
126
|
+
```bash
|
127
|
+
foreman start -f Procfile.dev
|
128
|
+
```
|
129
|
+
|
130
|
+
5. Visit [localhost:3000/hello_world](http://localhost:3000/hello_world)
|
131
|
+
|
132
|
+
## How it Works
|
133
|
+
The generator installs your webpack files in the `client` folder. Foreman uses webpack to compile your code and output the bundled results to `app/assets/javascripts/generated`, which are then loaded by sprockets. These generated bundle files have been added to your `.gitignore` for your convenience.
|
134
|
+
|
135
|
+
Inside your Rails views, you can now use the `react_component` helper method provided by React on Rails.
|
136
|
+
|
137
|
+
### Client-Side Rendering vs. Server-Side Rendering
|
138
|
+
In most cases, you should use the `prerender: false` (default behavior) with the provided helper method to render the React component from your Rails views. In some cases, such as when SEO is vital or many users will not have JavaScript enabled, you can enable server-rendering by passing `prerender: true` to your helper, or you can simply change the default in `config/initializers/react_on_rails`.
|
139
|
+
|
140
|
+
Now the server will interpret your JavaScript using [ExecJS](https://github.com/rails/execjs) and pass the resulting HTML to the client. We recommend using [therubyracer](https://github.com/cowboyd/therubyracer) as ExecJS's runtime. The generator will automatically add it to your Gemfile for you.
|
141
|
+
|
142
|
+
Note that **server-rendering requires globally exposing your components by setting them to `global`, not `window`** (as is the case with client-rendering). If using the generator, you can pass the `--server-rendering` option to configure your application for server-side rendering.
|
143
|
+
|
144
|
+
In the following screenshot you can see the 3 parts of react_on_rails rendering:
|
145
|
+
|
146
|
+
1. Inline JavaScript to "client-side" render the React component.
|
147
|
+
2. The wrapper div `<div id="HelloWorld-react-component-0">` enclosing the server-rendered HTML for the React component
|
148
|
+
3. Additional JavaScript to console log any messages, such as server rendering errors.
|
149
|
+
|
150
|
+
**Note**: If server rendering is not used (prerender: false), then the major difference is that the HTML rendered contains *only* the outer div: `<div id="HelloWorld-react-component-0"/>`
|
151
|
+
|
152
|
+
![Comparison of a normal React Component with its server-rendered version](https://cloud.githubusercontent.com/assets/1118459/10157268/41435186-6624-11e5-9341-6fc4cf35ee90.png)
|
153
|
+
|
154
|
+
### Building the Bundles
|
155
|
+
Each time you change your client code, you will need to re-generate the bundles (the webpack-created JavaScript files included in application.js). The included Foreman `Procfile.dev` will take care of this for you by watching your JavaScript code files for changes. Simply run `foreman start -f Procfile.dev`.
|
156
|
+
|
157
|
+
On Heroku deploys, the `lib/assets.rake` file takes care of running webpack during deployment. If you have used the provided generator, these bundles will automatically be added to your `.gitignore` in order to prevent extraneous noise from re-generated code in your pull requests. You will want to do this manually if you do not use the provided generator.
|
158
|
+
|
159
|
+
### Globally Exposing Your React Components
|
160
|
+
Place your JavaScript code inside of the provided `client/app` folder. Use modules just as you would when using webpack alone. The difference here is that instead of mounting React components directly to an element using `React.render`, you **expose your components globally and then mount them with helpers inside of your Rails views**.
|
161
|
+
|
162
|
+
+ *Normal Mode (JavaScript is Rendered on client):*
|
163
|
+
|
164
|
+
If you are not server rendering, `clientRegistration.jsx` will have
|
165
|
+
|
166
|
+
```javascript
|
167
|
+
import HelloWorld from '../components/HelloWorld';
|
168
|
+
import ReactOnRails from 'react-on-rails';
|
169
|
+
ReactOnRails.register({ HelloWorld });
|
170
|
+
```
|
171
|
+
+ *Server-Side Rendering:*
|
172
|
+
|
173
|
+
If you are server rendering, `serverRegistration.jsx` will have this. Note, you might be initializing HelloWorld with version specialized for server rendering.
|
174
|
+
|
175
|
+
```javascript
|
176
|
+
import HelloWorld from '../components/HelloWorld';
|
177
|
+
import ReactOnRails from 'react-on-rails';
|
178
|
+
ReactOnRails.register({ HelloWorld });
|
179
|
+
```
|
180
|
+
|
181
|
+
In general, you may want different initialization for your server rendered components.
|
182
|
+
|
183
|
+
### Rails View Helpers In-Depth
|
184
|
+
Once the bundled files have been generated in your `app/assets/javascripts/generated` folder and you have exposed your components globally, you will want to run your code in your Rails views using the included helper method.
|
185
|
+
|
186
|
+
This is how you actually render the React components you exposed to `window` inside of `clientRegistration` (and `global` inside of `serverRegistration` if you are server rendering).
|
187
|
+
|
188
|
+
#### react_component
|
189
|
+
`react_component(component_name, props = {}, options = {})`
|
190
|
+
|
191
|
+
+ **react_component_name:** Can be a React component, created using a ES6 class, or `React.createClass`, or a generator function that returns a React component.
|
192
|
+
+ **props:** Ruby Hash which contains the properties to pass to the react object, or a JSON string. If you pass a string, we'll escape it for you.
|
193
|
+
+ **options:**
|
194
|
+
+ **prerender:** enable server-side rendering of component. Set to false when debugging!
|
195
|
+
+ **router_redirect_callback:** Use this option if you want to provide a custom handler for redirects on server rendering. If you don't specify this, we'll simply change the rendered output to a script that sets window.location to the new route. Set this up exactly like a generator_function. Your function will will take one parameter, containing all the values that react-router gives on a redirect request, such as pathname, search, etc.
|
196
|
+
+ **trace:** set to true to print additional debugging information in the browser. Defaults to true for development, off otherwise.
|
197
|
+
+ **replay_console:** Default is true. False will disable echoing server-rendering logs to the browser. While this can make troubleshooting server rendering difficult, so long as you have the default configuration of logging_on_server set to true, you'll still see the errors on the server.
|
198
|
+
+ **raise_on_prerender_error:** Default is false. True will throw an error on the server side rendering. Your controller will have to handle the error.
|
199
|
+
+ Any other options are passed to the content tag, including the id
|
200
|
+
|
201
|
+
#### Generator Functions
|
202
|
+
Why would you create a function that returns a React compnent? For example, you may want the ability to use the passed-in props to initialize a redux store or setup react-router. Or you may want to return different components depending on what's in the props. ReactOnRails will automatically detect a registered generator function.
|
203
|
+
|
204
|
+
#### server_render_js
|
205
|
+
`server_render_js(js_expression, options = {})`
|
206
|
+
|
207
|
+
+ js_expression, like 2 + 3, and not a block of js code. If you have more than one line that needs to be executed, wrap it in an [IIFE](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression). JS exceptions will be caught and console messages handled properly
|
208
|
+
+ Currently the only option you may pass is `replay_console` (boolean)
|
209
|
+
|
210
|
+
This is a helper method that takes any JavaScript expression and returns the output from evaluating it. If you have more than one line that needs to be executed, wrap it in an IIFE. JS exceptions will be caught and console messages handled properly.
|
211
|
+
|
212
|
+
## Generator
|
213
|
+
The `react_on_rails:install` generator combined with the example pull requests of generator runs will get you up and running efficiently. There's a fair bit of setup with integrating Webpack with Rails. Defaults for options are such that the default is for the flag to be off. For example, the default for `-R` is that `redux` is off, and the default of `-b` means that `skip-bootstrap` is off.
|
214
|
+
|
215
|
+
Run `rails generate react_on_rails:install --help` for descriptions of all available options:
|
216
|
+
|
217
|
+
```
|
218
|
+
Usage:
|
219
|
+
rails generate react_on_rails:install [options]
|
220
|
+
|
221
|
+
Options:
|
222
|
+
-R, [--redux], [--no-redux] # Install Redux gems and Redux version of Hello World Example
|
223
|
+
-S, [--server-rendering], [--no-server-rendering] # Add necessary files and configurations for server-side rendering
|
224
|
+
-j, [--skip-js-linters], [--no-skip-js-linters] # Skip installing JavaScript linting files
|
225
|
+
-L, [--ruby-linters], [--no-ruby-linters] # Install ruby linting files, tasks, and configs
|
226
|
+
-H, [--heroku-deployment], [--no-heroku-deployment] # Install files necessary for deploying to Heroku
|
227
|
+
-b, [--skip-bootstrap], [--no-skip-bootstrap] # Skip installing files for bootstrap support
|
228
|
+
|
229
|
+
Runtime options:
|
230
|
+
-f, [--force] # Overwrite files that already exist
|
231
|
+
-p, [--pretend], [--no-pretend] # Run but do not make any changes
|
232
|
+
-q, [--quiet], [--no-quiet] # Suppress status output
|
233
|
+
-s, [--skip], [--no-skip] # Skip files that already exist
|
234
|
+
|
235
|
+
Description:
|
236
|
+
Create react on rails files for install generator.
|
237
|
+
```
|
238
|
+
|
239
|
+
For a clear example of what each generator option will do, see our generator results repo: [Generator Results](https://github.com/shakacode/react_on_rails-generator-results/blob/master/README.md). Each pull request shows a git "diff" that highlights the changes that the generator has made.
|
240
|
+
|
241
|
+
### Understanding the Organization of the Generated Client Code
|
242
|
+
The generated client code follows our organization scheme. Each unique set of functionality, is given its own folder inside of `client/app/bundles`. This encourages for modularity of DOMAINS.
|
243
|
+
|
244
|
+
Inside of the generated "HelloWorld" domain you will find the following folders:
|
245
|
+
|
246
|
+
+ `startup`: two types of files, one that return a container component and implement any code that differs between client and server code (if using server-rendering), and a `clientRegistration` file that exposes the aforementioned files (as well as a `serverRegistration` file if using server rendering). These registration files are what webpack is using as an entry point.
|
247
|
+
+ `containers`: "smart components" (components that have functionality and logic that is passed to child "dumb components").
|
248
|
+
+ `components`: includes "dumb components", or components that simply render their properties and call functions given to them as properties by a parent component. Ultimately, at least one of these dumb components will have a parent container component.
|
249
|
+
|
250
|
+
You may also notice the `app/lib` folder. This is for any code that is common between bundles and therefore needs to be shared (for example, middleware).
|
251
|
+
|
252
|
+
### Redux
|
253
|
+
If you have used the `--redux` generator option, you will notice the familiar additional redux folders in addition to the aforementioned folders. The Hello World example has also been modified to use Redux.
|
254
|
+
|
255
|
+
Note the organizational paradigm of "bundles". These are like application domains and are used for grouping your code into webpack bundles, in case you decide to create different bundles for deployment. This is also useful for separating out logical parts of your application. The concept is that each bundle will have it's own Redux store. If you have code that you want to reuse across bundles, including components and reducers, place them under `/client/app/lib`.
|
256
|
+
|
257
|
+
### Using Images and Fonts
|
258
|
+
The generator has amended the folders created in `client/assets/` to Rails's asset path. We recommend that if you have any existing assets that you want to use with your client code, you should move them to these folders and use webpack as normal. This allows webpack's development server to have access to your assets, as it will not be able to see any assets in the default Rails directories which are above the `/client` directory.
|
259
|
+
|
260
|
+
Alternatively, if you have many existing assets and don't wish to move them, you could consider creating symlinks from client/assets that point to your Rails assets folders inside of `app/assets/`. The assets there will then be visible to both Rails and webpack.
|
261
|
+
|
262
|
+
### Bootstrap Integration
|
263
|
+
React on Rails ships with Twitter Bootstrap already integrated into the build. Note that the generator removes `require_tree` in both the application.js and application.css.scss files. This is to ensure the correct load order for the bootstrap integration, and is usually a good idea in general. You will therefore need to explicitly require your files.
|
264
|
+
|
265
|
+
How the Bootstrap library is loaded depends upon whether one is using the Rails server or the HMR development server.
|
266
|
+
|
267
|
+
#### Bootstrap via Rails Server
|
268
|
+
In the former case, the Rails server loads `bootstrap-sprockets`, provided by the `bootstrap-sass` ruby gem (added automatically to your Gemfile by the generator) via the `app/assets/stylesheets/_bootstrap-custom.scss` partial.
|
269
|
+
|
270
|
+
This allows for using Bootstrap in your regular Rails stylesheets. If you wish to customize any of the Bootstrap variables, you can do so via the `client/assets/stylesheets/_pre-bootstrap.scss` partial.
|
271
|
+
|
272
|
+
#### Bootstrap via Webpack Dev Server
|
273
|
+
When using the webpack dev server, which does not go through Rails, bootstrap is loaded via the [bootstrap-sass-loader](https://github.com/shakacode/bootstrap-sass-loader) which uses the `client/bootstrap-sass-config.js` file.
|
274
|
+
|
275
|
+
#### Keeping Custom Bootstrap Configurations Synced
|
276
|
+
Because the webpack dev server and Rails each load Bootstrap via a different file (explained in the two sections immediately above), any changes to the way components are loaded in one file must also be made to the other file in order to keep styling consistent between the two. For example, if an import is excluded in `_bootstrap-custom.scss`, the same import should be excluded in `bootstrap-sass-config.js` so that styling in the Rails server and the webpack dev server will be the same.
|
277
|
+
|
278
|
+
#### Skip Bootstrap Integration
|
279
|
+
Bootstrap integration is enabled by default, but can be disabled by passing the `--skip-bootstrap` flag (alias `-b`). When you don't need Bootstrap in your existing project, just skip it as needed.
|
280
|
+
|
281
|
+
### Linters
|
282
|
+
The React on Rails generator can add linters and their recommended accompanying configurations to your project. There are two classes of linters: ruby linters and JavaScript linters.
|
283
|
+
|
284
|
+
##### JavaScript Linters
|
285
|
+
JavaScript linters are **enabled by default**, but can be disabled by passing the `--skip-js-linters` flag (alias `j`) , and those that run in Node have been add to `client/package.json` under `devDependencies`.
|
286
|
+
|
287
|
+
##### Ruby Linters
|
288
|
+
Ruby linters are **disabled by default**, but can be enabled by passing the `--ruby-linters` flag when generating. These linters have been added to your Gemfile in addition to the the appropriate Rake tasks.
|
289
|
+
|
290
|
+
We really love using all the linters! Give them a try.
|
291
|
+
|
292
|
+
#### Running the Linters
|
293
|
+
To run the linters (runs all linters you have installed, even if you installed both Ruby and Node):
|
294
|
+
|
295
|
+
```bash
|
296
|
+
rake lint
|
297
|
+
```
|
298
|
+
|
299
|
+
Run this command to see all the linters available
|
300
|
+
|
301
|
+
```bash
|
302
|
+
rake -T lint
|
303
|
+
```
|
304
|
+
|
305
|
+
**Here's the list:**
|
306
|
+
```bash
|
307
|
+
rake lint # Runs all linters
|
308
|
+
rake lint:eslint # eslint
|
309
|
+
rake lint:js # JS Linting
|
310
|
+
rake lint:jscs # jscs
|
311
|
+
rake lint:rubocop[fix] # Run Rubocop lint in shell
|
312
|
+
rake lint:ruby # Run ruby-lint as shell
|
313
|
+
rake lint:scss # See docs for task 'scss_lint'
|
314
|
+
```
|
315
|
+
|
316
|
+
## Developing with the Webpack Dev Server
|
317
|
+
One of the benefits of using webpack is access to [webpack's dev server](https://webpack.github.io/docs/webpack-dev-server.html) and its [hot module replacement](https://webpack.github.io/docs/hot-module-replacement-with-webpack.html) functionality.
|
318
|
+
|
319
|
+
The webpack dev server with HMR will apply changes from the code (or styles!) to the browser as soon as you save whatever file you're working on. You won't need to reload the page, and your data will still be there. Start foreman as normal (it boots up the Rails server *and* the webpack HMR dev server at the same time).
|
320
|
+
|
321
|
+
```bash
|
322
|
+
foreman start -f Procfile.dev
|
323
|
+
```
|
324
|
+
|
325
|
+
Open your browser to [localhost:4000](http://localhost:4000). Whenever you make changes to your JavaScript code in the `client` folder, they will automatically show up in the browser. Hot module replacement is already enabled by default.
|
326
|
+
|
327
|
+
Note that **React-related error messages are typically significantly more helpful when encountered in the dev server** than the Rails server as they do not include noise added by the React on Rails gem.
|
328
|
+
|
329
|
+
### Adding Additional Routes for the Dev Server
|
330
|
+
As you add more routes to your front-end application, you will need to make the corresponding API for the dev server in `client/server.js`. See our example `server.js` from our [tutorial](https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/server.js).
|
331
|
+
|
332
|
+
## Additional Reading
|
333
|
+
+ [Generated Client Code](docs/additional_reading/generated_client_code.md)
|
334
|
+
+ [Heroku Deployment](docs/additional_reading/heroku_deployment.md)
|
335
|
+
+ [Manual Installation](docs/additional_reading/manual_installation.md)
|
336
|
+
+ [Node Dependencies and NPM](docs/additional_reading/node_dependencies_and_npm.md)
|
337
|
+
+ [Optional Configuration](docs/additional_reading/optional_configuration.md)
|
338
|
+
+ [React Router](docs/additional_reading/react_router.md)
|
339
|
+
+ [Server Rendering Tips](docs/additional_reading/server_rendering_tips.md)
|
340
|
+
+ [Tips](docs/additional_reading/tips.md)
|
341
|
+
+ [Webpack Configuration](docs/additional_reading/webpack.md)
|
342
|
+
+ [Webpack Cookbook](https://christianalfoni.github.io/react-webpack-cookbook/index.html)
|
343
|
+
|
344
|
+
## Contributing
|
345
|
+
Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to our version of the [Contributor Covenant](contributor-covenant.org) code of conduct (see [CODE OF CONDUCT](docs/code_of_conduct.md)).
|
346
|
+
|
347
|
+
See [Contributing](docs/contributing.md) to get started.
|
348
|
+
|
349
|
+
## License
|
350
|
+
The gem is available as open source under the terms of the [MIT License](docs/LICENSE).
|
351
|
+
|
352
|
+
## Authors
|
353
|
+
[The Shaka Code team!](http://www.shakacode.com/about/)
|
354
|
+
|
355
|
+
The project started with [Justin Gordon](https://github.com/justin808/) pairing with [Samnang Chhun](https://github.com/samnang) to figure out how to do server rendering with Webpack plus Rails. [Alex Fedoseev](https://github.com/alexfedoseev) then joined in. [Rob Wise](https://github.com/robwise), [Aaron Van Bokhoven](https://github.com/aaronvb), and [Andy Wang](https://github.com/yorzi) did the bulk of the generators.
|
356
|
+
|
357
|
+
We owe much gratitude to the work of the [react-rails gem](https://github.com/reactjs/react-rails). We've also been inspired by the [react_webpack_rails gem](https://github.com/netguru/react_webpack_rails).
|
358
|
+
|
359
|
+
## About [ShakaCode](http://www.shakacode.com/)
|
360
|
+
|
361
|
+
Visit [our forums!](http://forum.shakacode.com). We've got a [category dedicated to react_on_rails](http://forum.shakacode.com/c/rails/reactonrails).
|
362
|
+
|
363
|
+
If you're looking for consulting on a project using React and Rails, email us ([contact@shakacode.com](mailto: contact@shakacode.com))! You can also join our slack room for some free advice.
|
364
|
+
|
365
|
+
We're looking for great developers that want to work with Rails + React with a distributed, worldwide team, for our own products, client work, and open source. [More info here](http://www.shakacode.com/about/index.html#work-with-us).
|