react_on_rails 12.0.1 → 13.0.0

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.
Files changed (152) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +27 -13
  3. data/.eslintrc +2 -1
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +23 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. data/.github/workflows/lint-js-and-ruby.yml +53 -0
  7. data/.github/workflows/main.yml +178 -0
  8. data/.github/workflows/package-js-tests.yml +35 -0
  9. data/.github/workflows/rspec-package-specs.yml +45 -0
  10. data/.gitignore +0 -3
  11. data/.prettierignore +2 -0
  12. data/.prettierrc +0 -3
  13. data/.rubocop.yml +48 -39
  14. data/.travis.yml +3 -1
  15. data/CHANGELOG.md +112 -25
  16. data/CONTRIBUTING.md +4 -3
  17. data/Gemfile +1 -33
  18. data/Gemfile.development_dependencies +52 -0
  19. data/NEWS.md +14 -4
  20. data/REACT-ON-RAILS-PRO-LICENSE +1 -1
  21. data/README.md +42 -324
  22. data/Rakefile +1 -1
  23. data/SUMMARY.md +22 -13
  24. data/docs/{basics → additional-details}/generator-details.md +1 -6
  25. data/docs/{outdated → additional-details}/manual-installation-overview.md +6 -6
  26. data/docs/additional-details/recommended-project-structure.md +69 -0
  27. data/docs/additional-details/upgrade-webpacker-v3-to-v4.md +10 -0
  28. data/docs/api/javascript-api.md +2 -2
  29. data/docs/api/redux-store-api.md +3 -3
  30. data/docs/api/view-helpers-api.md +7 -8
  31. data/docs/contributor-info/linters.md +5 -6
  32. data/docs/contributor-info/pull-requests.md +2 -4
  33. data/docs/contributor-info/releasing.md +1 -1
  34. data/docs/{basics → deployment}/heroku-deployment.md +16 -5
  35. data/docs/{basics → guides}/client-vs-server-rendering.md +4 -2
  36. data/docs/{basics → guides}/configuration.md +65 -31
  37. data/docs/guides/deployment.md +4 -0
  38. data/docs/guides/getting-started.md +183 -0
  39. data/docs/guides/hmr-and-hot-reloading-with-the-webpack-dev-server.md +104 -0
  40. data/docs/{outdated → guides}/how-react-on-rails-works.md +10 -10
  41. data/docs/guides/how-to-conditionally-server-render-based-on-device-type.md +39 -0
  42. data/docs/guides/how-to-use-different-files-for-client-and-server-rendering.md +98 -0
  43. data/docs/{basics → guides}/installation-into-an-existing-rails-app.md +14 -7
  44. data/docs/guides/rails-webpacker-react-integration-options.md +182 -0
  45. data/docs/guides/react-on-rails-overview.md +30 -0
  46. data/docs/{basics → guides}/react-server-rendering.md +8 -5
  47. data/docs/{basics → guides}/rspec-configuration.md +11 -11
  48. data/docs/{tutorial.md → guides/tutorial.md} +46 -50
  49. data/docs/{basics → guides}/upgrading-react-on-rails.md +56 -18
  50. data/docs/guides/webpack-configuration.md +42 -0
  51. data/docs/home.md +23 -0
  52. data/docs/{outdated → javascript}/code-splitting.md +5 -5
  53. data/docs/{additional-reading → javascript}/converting-from-custom-webpack-config-to-rails-webpacker-config.md +4 -4
  54. data/docs/{additional-reading → javascript}/images.md +5 -6
  55. data/docs/{articles.md → misc/articles.md} +1 -1
  56. data/docs/misc/doctrine.md +5 -6
  57. data/docs/outdated/deferred-rendering.md +39 -0
  58. data/docs/outdated/rails-assets-relative-paths.md +3 -3
  59. data/docs/outdated/rails-assets.md +8 -8
  60. data/docs/outdated/rails3.md +2 -2
  61. data/docs/{additional-reading → rails}/convert-rails-5-api-only-app.md +1 -1
  62. data/docs/{additional-reading → rails}/rails-engine-integration.md +3 -12
  63. data/docs/{additional-reading → rails}/turbolinks.md +13 -1
  64. data/docs/react-on-rails-pro/react-on-rails-pro.md +43 -0
  65. data/docs/testimonials/testimonials.md +6 -6
  66. data/lib/generators/react_on_rails/base_generator.rb +64 -12
  67. data/lib/generators/react_on_rails/dev_tests_generator.rb +1 -1
  68. data/lib/generators/react_on_rails/generator_helper.rb +4 -0
  69. data/lib/generators/react_on_rails/templates/.eslintrc +3 -1
  70. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +4 -8
  71. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-static +9 -0
  72. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +2 -1
  73. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.module.css +4 -0
  74. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorldServer.js +5 -0
  75. data/lib/generators/react_on_rails/templates/base/base/app/javascript/packs/server-bundle.js +8 -0
  76. data/lib/generators/react_on_rails/templates/base/base/app/views/layouts/hello_world.html.erb +1 -0
  77. data/lib/generators/react_on_rails/templates/base/base/babel.config.js.tt +32 -0
  78. data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +3 -3
  79. data/lib/generators/react_on_rails/templates/base/base/config/webpack/clientWebpackConfig.js.tt +17 -0
  80. data/lib/generators/react_on_rails/templates/base/base/config/webpack/commonWebpackConfig.js.tt +15 -0
  81. data/lib/generators/react_on_rails/templates/base/base/config/webpack/development.js.tt +25 -0
  82. data/lib/generators/react_on_rails/templates/base/base/config/webpack/production.js.tt +9 -0
  83. data/lib/generators/react_on_rails/templates/base/base/config/webpack/serverWebpackConfig.js.tt +117 -0
  84. data/lib/generators/react_on_rails/templates/base/base/config/webpack/test.js.tt +6 -0
  85. data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpack.config.js.tt +15 -0
  86. data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpackConfig.js.tt +36 -0
  87. data/lib/generators/react_on_rails/templates/base/base/config/webpacker.yml +62 -0
  88. data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +8 -2
  89. data/lib/generators/react_on_rails/templates/dev_tests/spec/simplecov_helper.rb +1 -1
  90. data/lib/generators/react_on_rails/templates/dev_tests/spec/{features → system}/hello_world_spec.rb +2 -2
  91. data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +2 -1
  92. data/lib/react_on_rails/configuration.rb +42 -41
  93. data/lib/react_on_rails/git_utils.rb +3 -3
  94. data/lib/react_on_rails/helper.rb +54 -28
  95. data/lib/react_on_rails/json_parse_error.rb +1 -1
  96. data/lib/react_on_rails/locales/base.rb +11 -15
  97. data/lib/react_on_rails/locales/to_js.rb +0 -4
  98. data/lib/react_on_rails/locales/to_json.rb +0 -4
  99. data/lib/react_on_rails/prerender_error.rb +6 -3
  100. data/lib/react_on_rails/react_component/render_options.rb +30 -7
  101. data/lib/react_on_rails/server_rendering_js_code.rb +13 -0
  102. data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +41 -13
  103. data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +1 -1
  104. data/lib/react_on_rails/test_helper/webpack_assets_status_checker.rb +3 -3
  105. data/lib/react_on_rails/test_helper.rb +4 -2
  106. data/lib/react_on_rails/utils.rb +24 -4
  107. data/lib/react_on_rails/version.rb +1 -1
  108. data/lib/react_on_rails/webpacker_utils.rb +9 -10
  109. data/lib/tasks/assets.rake +19 -50
  110. data/lib/tasks/locale.rake +1 -2
  111. data/package.json +24 -29
  112. data/rakelib/docker.rake +0 -5
  113. data/rakelib/example_type.rb +10 -3
  114. data/rakelib/examples.rake +3 -5
  115. data/rakelib/lint.rake +3 -9
  116. data/rakelib/release.rake +35 -15
  117. data/rakelib/run_rspec.rake +10 -11
  118. data/rakelib/task_helpers.rb +16 -4
  119. data/react_on_rails.gemspec +5 -20
  120. data/yarn.lock +2616 -5190
  121. metadata +86 -256
  122. data/.release-it.json +0 -3
  123. data/docs/additional-reading/upgrade-webpacker-v3-to-v4.md +0 -10
  124. data/docs/additional-reading/webpack-dev-server.md +0 -15
  125. data/docs/basics/deployment.md +0 -4
  126. data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +0 -49
  127. data/docs/basics/recommended-project-structure.md +0 -77
  128. data/docs/basics/webpack-configuration.md +0 -50
  129. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-hmr +0 -26
  130. data/ruby-lint.yml +0 -25
  131. /data/docs/{basics → additional-details}/migrating-from-react-rails.md +0 -0
  132. /data/docs/{additional-reading → additional-details}/updating-dependencies.md +0 -0
  133. /data/docs/{additional-reading → deployment}/elastic-beanstalk.md +0 -0
  134. /data/docs/{basics → guides}/i18n.md +0 -0
  135. /data/docs/{basics → guides}/minitest-configuration.md +0 -0
  136. /data/docs/{basics → guides}/render-functions-and-railscontext.md +0 -0
  137. /data/docs/{additional-reading → javascript}/angular-js-integration-migration.md +0 -0
  138. /data/docs/{additional-reading → javascript}/asset-pipeline.md +0 -0
  139. /data/docs/{additional-reading → javascript}/capistrano-deployment.md +0 -0
  140. /data/docs/{additional-reading → javascript}/credits.md +0 -0
  141. /data/docs/{additional-reading → javascript}/foreman-issues.md +0 -0
  142. /data/docs/{additional-reading → javascript}/node-dependencies-and-npm.md +0 -0
  143. /data/docs/{additional-reading → javascript}/react-and-redux.md +0 -0
  144. /data/docs/{additional-reading → javascript}/react-helmet.md +0 -0
  145. /data/docs/{additional-reading → javascript}/react-router.md +0 -0
  146. /data/docs/{additional-reading → javascript}/server-rendering-tips.md +0 -0
  147. /data/docs/{additional-reading → javascript}/troubleshooting-when-using-webpacker.md +0 -0
  148. /data/docs/{additional-reading → javascript}/webpack-v1-notes.md +0 -0
  149. /data/docs/{additional-reading → javascript}/webpack.md +0 -0
  150. /data/docs/{coding-style → misc}/style.md +0 -0
  151. /data/docs/{additional-reading → misc}/tips.md +0 -0
  152. /data/docs/{additional-reading → rails}/rails_view_rendering_from_inline_javascript.md +0 -0
@@ -0,0 +1,182 @@
1
+ # Rails/Webpacker React Integration Options
2
+
3
+ You only _need_ props hydration if you need SSR. However, there's no good reason to
4
+ have your app make a second round trip to the Rails server to get initialization props.
5
+
6
+ **Server-Side Rendering (SSR)** results in Rails rendering HTML for your React components. The main reasons to use SSR are better SEO and pages display more quickly.
7
+
8
+ These gems provide advanced integration of React with [rails/webpacker](https://github.com/rails/webpacker):
9
+
10
+ | Gem | Props Hydration | Server-Side-Rendering (SSR) | SSR with HMR | SSR with React-Router | SSR with Code Splitting | Node SSR |
11
+ | --- | --------------- | --- | --------------------- | ----------------------| ------------------------|----|
12
+ | [shakacode/react_on_rails](https://github.com/shakacode/react_on_rails) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
13
+ | [react-rails](https://github.com/reactjs/react-rails) | ✅ | ✅ | | | | | |
14
+ | [webpacker-react](https://github.com/renchap/webpacker-react) | ✅ | | | | | | |
15
+
16
+ Note, Node SSR for React on Rails requires [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro/).
17
+
18
+ ---
19
+
20
+ As mentioned, you don't _need_ to use a gem to integrate Rails with React.
21
+
22
+ If you're not concerned with view helpers to pass props or server rendering, can do it yourself:
23
+
24
+ ```erb
25
+ <%# views/layouts/application.html.erb %>
26
+
27
+ <%= content_tag :div,
28
+ id: "hello-react",
29
+ data: {
30
+ message: 'Hello!',
31
+ name: 'David'
32
+ }.to_json do %>
33
+ <% end %>
34
+ ```
35
+
36
+ ```js
37
+ // app/javascript/packs/hello_react.js
38
+
39
+ const Hello = props => (
40
+ <div className='react-app-wrapper'>
41
+ <img src={clockIcon} alt="clock" />
42
+ <h5 className='hello-react'>
43
+ {props.message} {props.name}!
44
+ </h5>
45
+ </div>
46
+ )
47
+
48
+ // Render component with data
49
+ document.addEventListener('DOMContentLoaded', () => {
50
+ const node = document.getElementById('hello-react')
51
+ const data = JSON.parse(node.getAttribute('data'))
52
+
53
+ ReactDOM.render(<Hello {...data} />, node)
54
+ })
55
+ ```
56
+
57
+ ----
58
+
59
+ ## HMR and React Hot Reloading
60
+
61
+ Before turning HMR on, consider upgrading to the latest stable gems and packages:
62
+ https://github.com/rails/webpacker#upgrading
63
+
64
+ Configure `config/webpacker.yml` file:
65
+
66
+ ```yaml
67
+ development:
68
+ extract_css: false
69
+ dev_server:
70
+ hmr: true
71
+ inline: true
72
+ ```
73
+
74
+ This basic configuration alone will have HMR working with the default webpacker setup. However, an code saves will trigger a full page refresh each time you save a file.
75
+
76
+ Webpack's HMR allows the replacement of modules for React in-place without reloading the browser. To do this, you have two options:
77
+
78
+ 1. Steps below for the [github.com/pmmmwh/react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin).
79
+ 1. Deprecated steps below for using the [github.com/gaearon/react-hot-loader](https://github.com/gaearon/react-hot-loader).
80
+
81
+ ### React Refresh Webpack Plugin
82
+ [github.com/pmmmwh/react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin)
83
+
84
+ You can see an example commit of adding this [here](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/commit/7e53803fce7034f5ecff335db1f400a5743a87e7).
85
+
86
+ 1. Add react refresh packages:
87
+ `yarn add @pmmmwh/react-refresh-webpack-plugin react-refresh -D`
88
+ 2. Update `babel.config.js` adding
89
+ ```js
90
+ plugins: [
91
+ process.env.WEBPACK_DEV_SERVER && 'react-refresh/babel',
92
+ // other plugins
93
+ ```
94
+ 3. Update `config/webpack/development.js`, only including the plugin if running the WEBPACK_DEV_SERVER
95
+ ```js
96
+ const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
97
+ const environment = require('./environment')
98
+
99
+ const isWebpackDevServer = process.env.WEBPACK_DEV_SERVER;
100
+
101
+ //plugins
102
+ if (isWebpackDevServer) {
103
+ environment.plugins.append(
104
+ 'ReactRefreshWebpackPlugin',
105
+ new ReactRefreshWebpackPlugin({
106
+ overlay: {
107
+ sockPort: 3035
108
+ }
109
+ })
110
+ );
111
+ }
112
+ ```
113
+
114
+ ### React Hot Loader (Deprecated)
115
+
116
+ 1. Add the `react-hot-loader` and ` @hot-loader/react-dom` npm packages.
117
+ ```sh
118
+ yarn add --dev react-hot-loader @hot-loader/react-dom
119
+ ```
120
+
121
+ 2. Update your babel config, `babel.config.js`. Add the plugin `react-hot-loader/babel`
122
+ with option `"safetyNet": false`:
123
+
124
+ ```
125
+ {
126
+ "plugins": [
127
+ [
128
+ "react-hot-loader/babel",
129
+ {
130
+ "safetyNet": false
131
+ }
132
+ ]
133
+ ]
134
+ }
135
+ ```
136
+
137
+ 3. Add changes like this to your entry points:
138
+
139
+ ```diff
140
+ // app/javascript/app.jsx
141
+
142
+ import React from 'react';
143
+ + import { hot } from 'react-hot-loader/root';
144
+
145
+ const App = () => <SomeComponent(s) />
146
+
147
+ - export default App;
148
+ + export default hot(App);
149
+ ```
150
+
151
+ 4. Adjust your webpack configuration for development so that `sourceMapContents` option for the sass
152
+ loader is `false`:
153
+
154
+ ```diff
155
+ // config/webpack/development.js
156
+
157
+ process.env.NODE_ENV = process.env.NODE_ENV || 'development'
158
+
159
+ const environment = require('./environment')
160
+
161
+ // allows for editing sass/scss files directly in browser
162
+ + if (!module.hot) {
163
+ + environment.loaders.get('sass').use.find(item => item.loader === 'sass-loader').options.sourceMapContents = false
164
+ + }
165
+ +
166
+ module.exports = environment.toWebpackConfig()
167
+ ```
168
+
169
+ 5. Adjust your `config/webpack/environment.js` for a
170
+
171
+ ```diff
172
+ // config/webpack/environment.js
173
+
174
+ // ...
175
+
176
+ // Fixes: React-Hot-Loader: react-🔥-dom patch is not detected. React 16.6+ features may not work.
177
+ // https://github.com/gaearon/react-hot-loader/issues/1227#issuecomment-482139583
178
+ + environment.config.merge({ resolve: { alias: { 'react-dom': '@hot-loader/react-dom' } } });
179
+
180
+ module.exports = environment;
181
+ ```
182
+
@@ -0,0 +1,30 @@
1
+ # React on Rails
2
+
3
+ React on Rails integrates Rails with (server rendering of) Facebook's [React](https://github.com/facebook/react) front-end framework.
4
+
5
+ ---
6
+
7
+ # Project Objective
8
+
9
+ To provide a high performance framework for integrating Ruby on Rails with React via the [**Webpacker**](https://github.com/rails/webpacker) gem especially in regards to React Server-Side Rendering for better SEO and improved performance.
10
+
11
+ # Features and Why React on Rails?
12
+
13
+ Given that [`rails/webpacker`](https://github.com/rails/webpacker/) gem already provides basic React integration, why would you use "React on Rails"?
14
+
15
+ 1. Easy passing of props directly from your Rails view to your React components rather than having your Rails view load and then make a separate request to your API.
16
+ 1. Tight integration with [rails/webpacker](https://github.com/rails/webpacker).
17
+ 1. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance, is not offered by `rails/webpacker`.
18
+ 1. Support for HMR for a great developer experience.
19
+ 1. Supports latest versions of React with hooks.
20
+ 1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/ReactTraining/react-router#readme) integration including server-side-rendering.
21
+ 1. [Internationalization (I18n) and (localization)](https://www.shakacode.com/react-on-rails/docs/guides/i18n/)
22
+ 1. A supportive community. This [web search shows how live public sites are using React on Rails](https://publicwww.com/websites/%22react-on-rails%22++-undeveloped.com+depth%3Aall/).
23
+ 1. [ReScript (Reason ML) Support](https://github.com/shakacode/reason-react-on-rails-example).
24
+
25
+ See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code.
26
+ ----
27
+
28
+ ## Prerequisites
29
+
30
+ Ruby on Rails >=5 and rails/webpacker 4.2+.
@@ -1,6 +1,9 @@
1
1
  # React Server Rendering
2
2
 
3
- See also [Client vs. Server Rendering](./client-vs-server-rendering.md)
3
+ See also [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/client-vs-server-rendering/).
4
+
5
+ ## What is the easiest way to setup a webpack configuration for server-side-rendering?
6
+ See the example webpack setup here: [github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh).
4
7
 
5
8
  ## What is Server Rendering?
6
9
 
@@ -10,13 +13,13 @@ During the Rails rendering of HTML per a browser request, the Rails server will
10
13
 
11
14
  The default JavaScript interpretter is [ExecJS](https://github.com/rails/execjs). If you want to maximize the perfomance of your server rendering, then you want to use React on Rails Pro which uses NodeJS to do the server rendering. See the [docs for React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
12
15
 
13
- See [this note](docs/outdated/how-react-on-rails-works.md#client-side-rendering-vs-server-side-rendering)
14
-
16
+ See [this note](https://www.shakacode.com/react-on-rails/docs/guides/client-vs-server-rendering/).
15
17
 
16
18
  ## How do you do Server Rendering with React on Rails?
17
19
  1. The `react_component` view helper method provides the `prerender:` option to switch on or off server rendering.
18
20
  1. Configure your Webpack setup to create a different server bundle per your needs. While you may reuse the same bundle as for client rendering, this is not common in larger apps for many reasons, such as as code splitting, handling CSS and images, different code paths for React Router on the server vs. client, etc.
19
- 1. You need to configure `config.server_bundle_js_file = "my-server-bundle.js"` in your `config/initializers/react_on_rails.rb`
21
+ 1. You need to configure `config.server_bundle_js_file = "server-bundle.js"` in your `config/initializers/react_on_rails.rb`
22
+ 1. You should ***not*** put a hash on the server-bundle so that you can easily use the webpack-dev-server for client bundles and have the server bundle generated by a watch process.
20
23
 
21
24
  ## Do you need server rendering?
22
25
 
@@ -26,4 +29,4 @@ Server rendering is used for either SEO or performance reasons.
26
29
 
27
30
  1. Never access `window`. Animations, globals on window, etc. just don't make sense when you're trying to run some JavaScript code to output a string of HTML.
28
31
  2. JavaScript calls to `setTimeout`, `setInterval`, and `clearInterval` similarly don't make sense when server rendering.
29
- 3. Promises don't work when server rendering. Anything to be done in a promise will never complete. This includes concepts such as asynchronous code loading and AJAX calls. If you want to do server rendering with asynchronous calls, [get in touch](mailto:justin@shakacode.com) as we're working on a Node renderer that handles asynchronous calls.
32
+ 3. Promises and file system access don't work when server rendering with ExecJS. Instead, you can use the Node renderer or [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro/).
@@ -1,5 +1,5 @@
1
1
  # RSpec Configuration
2
- _Click [here for minitest](./minitest-configuration.md)_
2
+ _Click [here for minitest](https://www.shakacode.com/react-on-rails/docs/guides/minitest-configuration/)_
3
3
 
4
4
  # If your webpack configurations correspond to rails/webpacker's default setup
5
5
  If you're able to configure your webpack configuration to be run by having your webpack configuration
@@ -8,12 +8,12 @@ compiled by webpack before running tests and during production deployment:
8
8
 
9
9
  1. **Use rails/webpacker's compile option**: Configure your `config/webpacker.yml` so that `compile: true` is for `test` and `production`
10
10
  environments. Ensure that your `source_path` is correct, or else `rails/webpacker` won't correctly
11
- detect changes.
12
- 2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](./configuration.md).
11
+ detect changes.
12
+ 2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](https://www.shakacode.com/react-on-rails/docs/guides/configuration/).
13
13
 
14
14
  ```yml
15
- config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/webpack"
16
- ```
15
+ config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/webpacker"
16
+ ```
17
17
 
18
18
  Which should you use? If you're already using the `rails/webpacker` way to configure webpack, then
19
19
  you can keep things simple and use the `rails/webpacker` options.
@@ -25,7 +25,7 @@ Because you will probably want to run RSpec tests that rely on compiled webpack
25
25
  As mentioned above, you can configure `compile: true` in `config/webpacker.yml` _if_ you've got configuration for
26
26
  your webpack in the standard `rails/webpacker` spot of `config/webpack/<NODE_ENV>.js`
27
27
 
28
- ReactOnRails also provides a helper method called `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`. Call this method from inside of the `RSpec.configure` block in your `spec/rails_helper.rb` file, passing the config as an argument. See file [lib/react_on_rails/test_helper.rb](../../lib/react_on_rails/test_helper.rb) for more details. You can customize this to your particular needs by replacing any of the default components used by `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`.
28
+ ReactOnRails also provides a helper method called `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`. Call this method from inside of the `RSpec.configure` block in your `spec/rails_helper.rb` file, passing the config as an argument. See file [lib/react_on_rails/test_helper.rb](https://github.com/shakacode/react_on_rails/tree/master/lib/react_on_rails/test_helper.rb) for more details. You can customize this to your particular needs by replacing any of the default components used by `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`.
29
29
 
30
30
  ```ruby
31
31
  RSpec.configure do |config|
@@ -44,22 +44,22 @@ If you are using Webpack to build CSS assets, you should do something like this
44
44
  ```
45
45
 
46
46
  Please take note of the following:
47
- - If you are using Webpacker, be **SURE** to configure the `source_path` in your `config/webpacker.yml` unless you are using the defaults for webpacker.
47
+ - If you are using Webpacker, be **SURE** to configure the `source_path` in your `config/webpacker.yml` unless you are using the defaults for webpacker.
48
48
 
49
49
  - This utility uses your `build_test_command` to build the static generated files. This command **must not** include the `--watch` option. If you have different server and client bundle files, this command **must** create all the bundles. If you are using webpacker, the default value will come from the `config/webpacker.yml` value for the `public_output_path` and the `source_path`
50
50
 
51
- - If you add an older file to your source files, that is already older than the produced output files, no new recompilation is done. The solution to this issue is to clear out your directory of webpack generated files when adding new source files that may have older dates.
51
+ - If you add an older file to your source files, that is already older than the produced output files, no new recompilation is done. The solution to this issue is to clear out your directory of webpack generated files when adding new source files that may have older dates.
52
52
 
53
- - By default, the webpack processes look in the webpack generated files folder, configured via the `config/webpacker.yml` config values of `public_root_path` and `public_output_path`. If the webpack generated files folder is missing, is empty, or contains files in the `config.webpack_generated_files` list with `mtime`s older than any of the files in your `client` folder, the helper will recompile your assets.
53
+ - By default, the webpack processes look in the webpack generated files folder, configured via the `config/webpacker.yml` config values of `public_root_path` and `public_output_path`. If the webpack generated files folder is missing, is empty, or contains files in the `config.webpack_generated_files` list with `mtime`s older than any of the files in your `client` folder, the helper will recompile your assets.
54
54
 
55
55
  The following `config/react_on_rails.rb` settings **must** match your setup:
56
56
  ```ruby
57
57
  # Define the files we need to check for webpack compilation when running tests.
58
58
  config.webpack_generated_files = %w( manifest.json )
59
-
59
+
60
60
  # OR if you're not hashing the server-bundle.js, then you should include your server-bundle.js in the list.
61
61
  # config.webpack_generated_files = %w( server-bundle.js manifest.json )
62
-
62
+
63
63
  # If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
64
64
  # with rspec then this controls what yarn command is run
65
65
  # to automatically refresh your webpack assets on every test run.
@@ -1,6 +1,10 @@
1
1
  # React on Rails Basic Tutorial
2
2
 
3
- *Updated for Ruby 2.7.1, Rails 6.0.3.1, and React on Rails v12.0.0*
3
+ **November 11, 2020**: See the example repo of [React on Rails Tutorial With SSR, HMR fast refresh, and TypeScript](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh) for a new way to setup the creation of your SSR bundle with `rails/webpacker`. This file will be update shortly. Most of it is still relevant.
4
+
5
+ -----
6
+
7
+ *Updated for Ruby 2.7.1, Rails 6.0.3.1, React on Rails v12.5.0, and Shakapacker v6*
4
8
 
5
9
  This tutorial guides you through setting up a new or existing Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering.
6
10
 
@@ -17,18 +21,16 @@ By the time you read this, the latest may have changed. Be sure to check the ver
17
21
  * https://rubygems.org/gems/react_on_rails
18
22
  * https://www.npmjs.com/package/react-on-rails
19
23
 
20
- _Note: some of the screen images below show the "npm" command. react_on_rails 6.6.0 and greater uses `yarn`._
21
-
22
24
  ## Setting up your environment
23
25
 
24
- Trying out **React on Rails** is super easy, so long as you have the basic prerequisites. This includes the basics for Rails 6.x and node version 13+. I recommend `rvm` and `nvm` to install Ruby and Node, and [brew](https://brew.sh/) to install [yarn](https://yarnpkg.com/en/docs/install#mac-tab). Rails can be installed as an ordinary gem.
26
+ Trying out **React on Rails** is super easy, so long as you have the basic prerequisites. This includes the basics for Rails 6.x and node version 14+. I recommend `rvm` or `rbevn` and `nvm` to install Ruby and Node. Rails can be installed as an ordinary gem.
25
27
 
26
28
  ```
27
29
  nvm install node # download and install latest stable Node
28
30
  nvm alias default node # make it default version
29
31
  nvm list # check
30
32
 
31
- brew install yarn # you can use other installer if desired
33
+ brew install yarn # you can use other installer if desired, such as yvm
32
34
  rvm install 2.7 # download and install latest stable Ruby (update to exact version)
33
35
  rvm use 2.7 --default # use it and make it default
34
36
  rvm list # check
@@ -38,36 +40,41 @@ gem install foreman # download and install Foreman
38
40
  ```
39
41
 
40
42
  ## Create a new Ruby on Rails App
41
- Then we need to create a fresh Rails application with webpacker react support as following.
43
+ Then we need to create a fresh Rails application as following.
42
44
 
43
45
  First be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If you are using an older version of Rails, you'll need to install webpacker with react per the instructions [here](https://github.com/rails/webpacker).
44
46
 
45
- ```
47
+ ```bash
46
48
  cd <directory where you want to create your new Rails app>
47
49
 
48
50
  # Any name you like for the rails app
49
- # Skip javascript so will add that next and get the current version
50
- rails new --skip-sprockets -J --skip-turbolinks test-react-on-rails-v12-no-sprockets
51
+ # Skip javascript so will add that next and get the current version
52
+ rails new --skip-turbolinks --skip-javascript test-react-on-rails
51
53
 
52
54
  cd test-react-on-rails
53
- bundle
54
55
  ```
55
56
 
56
- ## Add the webpacker gem
57
+ ## Add the shakapacker and react_on_rails gems
58
+ To avoid issues regarding inconsistent gem and npm versions, you should specify the exact versions
59
+ of both the gem and npm package. In other words, don't use the `^` or `~` in the version specifications.
60
+ _Use the latest version for `react_on_rails` and `shakapacker`._
57
61
 
58
62
  ```
59
- bundle add webpacker
60
- bundle add react_on_rails
63
+ gem 'react_on_rails', '13.0.0' # prefer exact gem version to match npm version
64
+ gem 'shakapacker', '6.1.1' # prefer exact gem version to match npm version
65
+
61
66
  ```
62
67
 
63
- ## Run the webpacker generator
68
+ Note: The latest released React On Rails version is considered stable. Please use the latest
69
+ version to ensure you get all the security patches and the best support.
64
70
 
65
- ```
71
+ ## Run the webpacker (shakapacker) generator
72
+
73
+ ```terminal
66
74
  bundle exec rails webpacker:install
67
- bundle exec rails webpacker:install:react
68
75
  ```
69
76
 
70
- Let's commit everything before installing React on Rails.
77
+ **Let's commit everything before installing React on Rails.**
71
78
 
72
79
  ```
73
80
  # Here are git commands to make a new git repo and commit everything.
@@ -76,45 +83,32 @@ git add -A
76
83
  git commit -m "Initial commit"
77
84
  ```
78
85
 
79
- ## Add the **React On Rails** gem to your `Gemfile`:
80
-
81
- To avoid issues regarding inconsistent gem and npm versions, you should specify the exact versions
82
- of both the gem and npm package. In other words, don't use the `^` or `~` in the version specifications.
83
-
84
- ```
85
- gem 'react_on_rails', '12.0.0' # prefer exact gem version to match npm version
86
- ```
87
-
88
- Note: The latest released React On Rails version is considered stable. Please use the latest
89
- version to ensure you get all the security patches and the best support.
90
-
91
- Run `bundle` and commit the changes.
92
-
93
- ```
94
- bundle
95
-
96
- git commit -am "Added React on Rails Gem"
97
- ```
86
+ ## Run the React on Rails Generator
98
87
 
99
88
  Install React on Rails: `rails generate react_on_rails:install`. You need to first git commit your files before running the generator, or else it will generate an error.
100
89
 
101
- Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
90
+ Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
102
91
  If you want the redux install: `rails generate react_on_rails:install --redux`
103
92
 
93
+ The generator will add `mini_racer`'s latest version. If you're using linux & encounter issues installing `libv8`, here's [a common solution](https://github.com/rubyjs/mini_racer/issues/218).
94
+
104
95
  ```
105
- rails generate react_on_rails:install
96
+ bundle exec rails generate react_on_rails:install
106
97
  ```
107
98
 
108
- Then run server with a static client bundle. Static means that the bundle is saved in your
109
- public/webpack/packs directory.
99
+ Enter `a` to replace all configuration files required by the project. You can check the diffs
100
+ before you commit to see what changed.
101
+
102
+ Then run the server with one of the following options:
110
103
 
104
+ ## Running with HMR
111
105
  ```
112
106
  foreman start -f Procfile.dev
113
107
  ```
114
108
 
115
- ## To run with the webpack-dev-server:
109
+ ## Running without HMR, statically creating the bundles
116
110
  ```
117
- foreman start -f Procfile.dev-hmr
111
+ foreman start -f Procfile.dev-static
118
112
  ```
119
113
 
120
114
  Visit [http://localhost:3000/hello_world](http://localhost:3000/hello_world) and see your **React On Rails** app running!
@@ -125,7 +119,7 @@ First, check that the `hmr` and the `inline` options are `true` in your `config/
125
119
 
126
120
  The basic setup will have HMR working with the default webpacker setup. The basic
127
121
  [HMR](https://webpack.js.org/concepts/hot-module-replacement/), without a special
128
- React setup, will cause a full page refresh each time you save a file.
122
+ React setup, will cause a full page refresh each time you save a file.
129
123
 
130
124
  ## Deploying to Heroku
131
125
 
@@ -254,6 +248,8 @@ heroku open
254
248
 
255
249
  and you will see your live app and you can share this URL with your friends. Congrats!
256
250
 
251
+ A deployed version of the project `spec/dummy` which demonstrates several uses of `react_on_rails` is available on heroku [through this link](https://ror-spec-dummy.herokuapp.com/)
252
+
257
253
  ## Turning on Server Rendering
258
254
 
259
255
  You can turn on server rendering by simply changing the `prerender` option to `true`:
@@ -294,8 +290,8 @@ versus with server rendering:
294
290
 
295
291
  For more details on server rendering, see:
296
292
 
297
- + [Client vs. Server Rendering](./basics/client-vs-server-rendering.md)
298
- + [React Server Rendering](./basics/react-server-rendering.md)
293
+ + [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/client-vs-server-rendering/)
294
+ + [React Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/react-server-rendering/)
299
295
 
300
296
  ## Moving from the Rails default `/app/javascript` to the recommended `/client` structure
301
297
 
@@ -316,13 +312,13 @@ mv app/javascript client
316
312
 
317
313
  ## Using HMR with the rails/webpacker setup
318
314
 
319
- Start the app using `foreman start -f Procfile.dev-hmr`.
315
+ Start the app using `foreman start -f Procfile.dev`.
320
316
 
321
317
  When you change a JSX file and save, the browser will automatically refresh!
322
318
 
323
319
  So you get some basics from HMR with no code changes. If you want to go further, take a look at these links:
324
320
 
325
- * https://github.com/rails/webpacker/blob/master/docs/webpack-dev-server.md
321
+ * https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack-dev-server.md
326
322
  * https://webpack.js.org/configuration/dev-server/
327
323
  * https://webpack.js.org/concepts/hot-module-replacement/
328
324
 
@@ -337,7 +333,7 @@ In case you are running some custom setup with different IP or PORT you should a
337
333
  web: rails s -p 8080 -b 0.0.0.0
338
334
  ```
339
335
 
340
- Then visit https://your-shared-addr.c9users.io:8080/hello_world
336
+ Then visit https://your-shared-addr.c9users.io:8080/hello_world
341
337
 
342
338
  ## RubyMine
343
339
 
@@ -351,8 +347,8 @@ It's super important to exclude certain directories from RubyMine or else it wil
351
347
 
352
348
  ## Conclusion
353
349
 
354
- * Browse the docs either on the [gitbook](https://shakacode.gitbooks.io/react-on-rails/content/) or in the [docs directory on github](https://github.com/shakacode/react_on_rails/tree/master/docs)
350
+ * Browse the docs on [our documentation website](https://www.shakacode.com/react-on-rails/docs/)
355
351
 
356
- Feedback is greatly appreciated! As are stars on github!
352
+ Feedback is greatly appreciated! As are stars on github!
357
353
 
358
354
  If you want personalized help, don't hesitate to get in touch with us at [contact@shakacode.com](mailto:contact@shakacode.com). We offer [React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki) and consulting so you can focus on your app and not on how to make Webpack plus Rails work optimally.
@@ -1,15 +1,30 @@
1
1
  # Upgrading React on Rails
2
2
 
3
3
  ## Need Help Migrating?
4
- If you would like help in migrating between React on Rails versions or help with implementing server rendering, please contact [justin@shakacode.com](mailto:justin@shakacode.com) for information about our [ShakaCode Pro Support](https://www.shakacode.com/work/shakacode-pro-support.pdf).
4
+ If you would like help in migrating between React on Rails versions or help with implementing server rendering, please contact [justin@shakacode.com](mailto:justin@shakacode.com) for more information about our [React on Rails Pro Support](https://www.shakacode.com/react-on-rails-pro).
5
5
 
6
- We specialize in helping companies to quickly and efficiently move from versions before 9 to current. The older versions use the Rails asset pipeline to package client assets. The current and recommended way is to use Webpack 4 for asset preparation. You may also need help migrating from the `rails/webpacker`'s Webpack configuration to a better setup ready for Server Side Rendering.
6
+ We specialize in helping companies to quickly and efficiently upgrade. The older versions use the Rails asset pipeline to package client assets. The current and recommended way is to use Webpack 4+ for asset preparation. You may also need help migrating from the `rails/webpacker`'s Webpack configuration to a better setup ready for Server Side Rendering.
7
+
8
+ ## Upgrading to v13
9
+
10
+ ### Breaking Change
11
+ Previously, the gem `webpacker` was a Gem dependency.
12
+
13
+ v13 has changed slightly to switch to `shakapacker`.
14
+
15
+ For details, see see the [Shakapacker guide to upgrading](https://github.com/shakacode/shakapacker/blob/master/docs/v6_upgrade.md)
16
+
17
+ In summary:
18
+
19
+ 1. Change the gem reference from 'webpacker' to 'shakapacker'
20
+ 2. Change the npm reference from '@rails/webpacker' to 'shakapacker'
21
+ 3. Other updates, depending on what version of rails/webpacker that you had.
7
22
 
8
23
  ## Upgrading to v12
9
24
  ### Recent versions
10
- Make sure that you are on a relatively more recent version of rails and webpacker.
25
+ Make sure that you are on a relatively more recent version of rails and webpacker. Yes, the [rails/webpacker](https://github.com/rails/webpacker) gem is required!
11
26
  v12 is tested on Rails 6. It should work on Rails v5. If you're on any older version,
12
- and v12 doesn't work, please file an issue.
27
+ and v12 doesn't work, please file an issue.
13
28
 
14
29
  ### Removed Configuration config.symlink_non_digested_assets_regex
15
30
  Remove `config.symlink_non_digested_assets_regex` from your `config/initializers/react_on_rails.rb`.
@@ -25,33 +40,56 @@ If you still need that feature, please file an issue.
25
40
  In order to solve the issues regarding React Hooks compatibility, the number of parameters
26
41
  for functions is used to determine if you have a Render-Function that will get invoked to
27
42
  return a React component, or you are registering a React component defined by a function.
28
- Please see [Render-Functions and the Rails Context](./render-functions-and-railscontext.md) for
43
+ Please see [Render-Functions and the Rails Context](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) for
29
44
  more information on what a Render-Function is.
30
45
 
31
- ##### Correct
46
+ ##### Update required for registered functions taking exactly 2 params.
32
47
 
33
- Registered Objects are of the following types. Either of these will work:
34
- 1. Takes only zero or one params and you return a React Element, often JSX.
48
+ Registered Objects are of the following type:
49
+ 1. **Function that takes only zero or one params and you return a React Element**, often JSX. If the function takes zero or one params, there is **no migration needed** for that function.
35
50
  ```js
36
51
  export default (props) => <Component {...props} />;
37
- ```
38
- 2. Takes **2 params** and returns **a React function or class component**. A function
39
- component is a function that takes zero or one params and returns a React Element, like JSX.
52
+ ```
53
+
54
+ 2. **Function that takes only zero or one params and you return an Object (_not a React Element_)**. If the function takes zero or one params, **you need to add one or two unused params so you have exactly 2 params** and then that function will be treated as a render function and it can return an Object rather than a React element. If you don't do this, you'll see this obscure error message:
55
+
56
+ ```
57
+ [SERVER] message: Objects are not valid as a React child (found: object with keys {renderedHtml}). If you meant to render a collection of children, use an array instead.
58
+ in YourComponentRenderFunction
59
+ ```
60
+
61
+ So look in YourComponentRenderFunction and do this change
62
+
63
+ ```js
64
+ export default (props) => { renderedHTML: getRenderedHTML };
65
+ ```
66
+
67
+ To have exactly 2 arguments:
68
+
69
+ ```js
70
+ export default (props, _railsContext) => { renderedHTML: getRenderedHTML };
71
+ ```
72
+
73
+ 3. Function that takes **2 params** and returns **a React function or class component**. _Migration is needed as the older syntax returned a React Element._
74
+ A function component is a function that takes zero or one params and returns a React Element, like JSX. The correct syntax
75
+ looks like:
40
76
  ```js
41
- export default (props, _railsContext) => () => <Component {...props} />;
77
+ export default (props, railsContext) => () => <Component {{...props, railsContext}} />;
42
78
  ```
43
- Note, you cannot return a React Element (JSX).
44
- 3. Takes **3 params** and uses the 3rd param, `domNodeId`, to call `ReactDOM.hydrate`
45
-
79
+ Note, you cannot return a React Element (JSX). See below for the migration steps. If your function that took **two params returned
80
+ an Object**, then no migration is required.
81
+ 4. Function that takes **3 params** and uses the 3rd param, `domNodeId`, to call `ReactDOM.hydrate`. If the function takes 3 params, there is **no migration needed** for that function.
82
+ 5. ES6 or ES5 class. There is **no migration needed**.
83
+
46
84
  Previously, with case number 2, you could return a React Element.
47
85
 
48
86
  The fix is simple. Here is an example of the change you'll do:
49
87
 
50
88
  ![2020-07-07_09-43-51 (1)](https://user-images.githubusercontent.com/1118459/86927351-eff79e80-c0ce-11ea-9172-d6855c45e2bb.png)
51
-
89
+
52
90
  ##### Broken, as this function takes two params and it returns a React Element from a JSX Literal
53
91
  ```js
54
- export default (props, _railsContext) => <Component {...props} />;
92
+ export default (props, railsContext) => <Component {{...props, railsContext} />;
55
93
  ```
56
94
 
57
95
  If you make this mistake, you'll get this warning
@@ -77,7 +115,7 @@ wrapper such that you're returning a function rather than a React Element, then:
77
115
 
78
116
  ## Upgrading rails/webpacker from v3 to v4
79
117
  ### Custom Webpack build file
80
- The default value for `extract_css` is **false** in `config/webpack.yml`. Custom webpack builds should set this value to true or else no CSS link tags are generated. You have a custom webpack build if you are not using [rails/webpacker](https://github.com/rails/webpacker to setup your Webpack configuration.
118
+ The default value for `extract_css` is **false** in `config/webpack.yml`. Custom webpack builds should set this value to true or else no CSS link tags are generated. You have a custom webpack build if you are not using [rails/webpacker](https://github.com/rails/webpacker) to setup your Webpack configuration.
81
119
 
82
120
  ```yml
83
121
  default: &default