react_on_rails 12.4.0 → 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.
- checksums.yaml +4 -4
- data/.circleci/config.yml +3 -1
- data/.github/ISSUE_TEMPLATE/bug_report.md +23 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- data/.github/workflows/main.yml +3 -3
- data/.gitignore +0 -3
- data/.prettierignore +2 -0
- data/CHANGELOG.md +39 -2
- data/Gemfile.development_dependencies +7 -6
- data/REACT-ON-RAILS-PRO-LICENSE +1 -1
- data/README.md +20 -13
- data/SUMMARY.md +9 -0
- data/docs/deployment/heroku-deployment.md +16 -5
- data/docs/guides/configuration.md +29 -15
- data/docs/guides/hmr-and-hot-reloading-with-the-webpack-dev-server.md +4 -4
- data/docs/guides/how-react-on-rails-works.md +1 -2
- data/docs/guides/rspec-configuration.md +1 -1
- data/docs/guides/tutorial.md +27 -27
- data/docs/guides/upgrading-react-on-rails.md +15 -0
- data/docs/guides/webpack-configuration.md +1 -1
- data/docs/javascript/converting-from-custom-webpack-config-to-rails-webpacker-config.md +2 -2
- data/lib/generators/react_on_rails/base_generator.rb +55 -10
- data/lib/generators/react_on_rails/generator_helper.rb +4 -0
- data/lib/generators/react_on_rails/templates/.eslintrc +3 -1
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +4 -8
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-static +9 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +2 -1
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.module.css +4 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorldServer.js +5 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/packs/server-bundle.js +8 -0
- data/lib/generators/react_on_rails/templates/base/base/app/views/layouts/hello_world.html.erb +1 -0
- data/lib/generators/react_on_rails/templates/base/base/babel.config.js.tt +32 -0
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +3 -3
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/clientWebpackConfig.js.tt +17 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/commonWebpackConfig.js.tt +15 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/development.js.tt +25 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/production.js.tt +9 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/serverWebpackConfig.js.tt +117 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/test.js.tt +6 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpack.config.js.tt +15 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpackConfig.js.tt +36 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpacker.yml +62 -0
- data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +2 -1
- data/lib/react_on_rails/configuration.rb +40 -41
- data/lib/react_on_rails/helper.rb +18 -17
- data/lib/react_on_rails/json_parse_error.rb +1 -1
- data/lib/react_on_rails/locales/base.rb +1 -3
- data/lib/react_on_rails/prerender_error.rb +5 -2
- data/lib/react_on_rails/react_component/render_options.rb +14 -0
- data/lib/react_on_rails/server_rendering_js_code.rb +13 -0
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +27 -10
- data/lib/react_on_rails/test_helper/webpack_assets_status_checker.rb +3 -3
- data/lib/react_on_rails/test_helper.rb +2 -2
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/webpacker_utils.rb +2 -7
- data/lib/tasks/assets.rake +5 -47
- data/lib/tasks/locale.rake +1 -2
- data/package.json +1 -1
- data/rakelib/example_type.rb +10 -3
- data/rakelib/examples.rake +3 -5
- data/rakelib/release.rake +7 -1
- data/react_on_rails.gemspec +2 -3
- data/yarn.lock +117 -71
- metadata +22 -21
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-hmr +0 -26
data/docs/guides/tutorial.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
-----
|
|
6
6
|
|
|
7
|
-
*Updated for Ruby 2.7.1, Rails 6.0.3.1,
|
|
7
|
+
*Updated for Ruby 2.7.1, Rails 6.0.3.1, React on Rails v12.5.0, and Shakapacker v6*
|
|
8
8
|
|
|
9
9
|
This tutorial guides you through setting up a new or existing Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering.
|
|
10
10
|
|
|
@@ -21,18 +21,16 @@ By the time you read this, the latest may have changed. Be sure to check the ver
|
|
|
21
21
|
* https://rubygems.org/gems/react_on_rails
|
|
22
22
|
* https://www.npmjs.com/package/react-on-rails
|
|
23
23
|
|
|
24
|
-
_Note: some of the screen images below show the "npm" command. react_on_rails 6.6.0 and greater uses `yarn`._
|
|
25
|
-
|
|
26
24
|
## Setting up your environment
|
|
27
25
|
|
|
28
|
-
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
|
|
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.
|
|
29
27
|
|
|
30
28
|
```
|
|
31
29
|
nvm install node # download and install latest stable Node
|
|
32
30
|
nvm alias default node # make it default version
|
|
33
31
|
nvm list # check
|
|
34
32
|
|
|
35
|
-
brew install yarn # you can use other installer if desired
|
|
33
|
+
brew install yarn # you can use other installer if desired, such as yvm
|
|
36
34
|
rvm install 2.7 # download and install latest stable Ruby (update to exact version)
|
|
37
35
|
rvm use 2.7 --default # use it and make it default
|
|
38
36
|
rvm list # check
|
|
@@ -42,46 +40,41 @@ gem install foreman # download and install Foreman
|
|
|
42
40
|
```
|
|
43
41
|
|
|
44
42
|
## Create a new Ruby on Rails App
|
|
45
|
-
Then we need to create a fresh Rails application
|
|
43
|
+
Then we need to create a fresh Rails application as following.
|
|
46
44
|
|
|
47
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).
|
|
48
46
|
|
|
49
|
-
```
|
|
47
|
+
```bash
|
|
50
48
|
cd <directory where you want to create your new Rails app>
|
|
51
49
|
|
|
52
50
|
# Any name you like for the rails app
|
|
53
51
|
# Skip javascript so will add that next and get the current version
|
|
54
|
-
rails new --skip-
|
|
52
|
+
rails new --skip-turbolinks --skip-javascript test-react-on-rails
|
|
55
53
|
|
|
56
54
|
cd test-react-on-rails
|
|
57
|
-
bundle
|
|
58
55
|
```
|
|
59
56
|
|
|
60
|
-
## Add the
|
|
57
|
+
## Add the shakapacker and react_on_rails gems
|
|
61
58
|
To avoid issues regarding inconsistent gem and npm versions, you should specify the exact versions
|
|
62
59
|
of both the gem and npm package. In other words, don't use the `^` or `~` in the version specifications.
|
|
63
|
-
_Use the latest version for react_on_rails
|
|
60
|
+
_Use the latest version for `react_on_rails` and `shakapacker`._
|
|
64
61
|
|
|
65
62
|
```
|
|
66
|
-
gem '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
|
+
|
|
67
66
|
```
|
|
68
67
|
|
|
69
68
|
Note: The latest released React On Rails version is considered stable. Please use the latest
|
|
70
69
|
version to ensure you get all the security patches and the best support.
|
|
71
70
|
|
|
72
|
-
|
|
73
|
-
bundle add webpacker
|
|
74
|
-
bundle add react_on_rails --version=12.0.4 --strict
|
|
75
|
-
```
|
|
71
|
+
## Run the webpacker (shakapacker) generator
|
|
76
72
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
```
|
|
73
|
+
```terminal
|
|
80
74
|
bundle exec rails webpacker:install
|
|
81
|
-
bundle exec rails webpacker:install:react
|
|
82
75
|
```
|
|
83
76
|
|
|
84
|
-
Let's commit everything before installing React on Rails
|
|
77
|
+
**Let's commit everything before installing React on Rails.**
|
|
85
78
|
|
|
86
79
|
```
|
|
87
80
|
# Here are git commands to make a new git repo and commit everything.
|
|
@@ -97,20 +90,25 @@ Install React on Rails: `rails generate react_on_rails:install`. You need to fir
|
|
|
97
90
|
Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
|
|
98
91
|
If you want the redux install: `rails generate react_on_rails:install --redux`
|
|
99
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
|
+
|
|
100
95
|
```
|
|
101
|
-
rails generate react_on_rails:install
|
|
96
|
+
bundle exec rails generate react_on_rails:install
|
|
102
97
|
```
|
|
103
98
|
|
|
104
|
-
|
|
105
|
-
|
|
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.
|
|
106
101
|
|
|
102
|
+
Then run the server with one of the following options:
|
|
103
|
+
|
|
104
|
+
## Running with HMR
|
|
107
105
|
```
|
|
108
106
|
foreman start -f Procfile.dev
|
|
109
107
|
```
|
|
110
108
|
|
|
111
|
-
##
|
|
109
|
+
## Running without HMR, statically creating the bundles
|
|
112
110
|
```
|
|
113
|
-
foreman start -f Procfile.dev-
|
|
111
|
+
foreman start -f Procfile.dev-static
|
|
114
112
|
```
|
|
115
113
|
|
|
116
114
|
Visit [http://localhost:3000/hello_world](http://localhost:3000/hello_world) and see your **React On Rails** app running!
|
|
@@ -250,6 +248,8 @@ heroku open
|
|
|
250
248
|
|
|
251
249
|
and you will see your live app and you can share this URL with your friends. Congrats!
|
|
252
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
|
+
|
|
253
253
|
## Turning on Server Rendering
|
|
254
254
|
|
|
255
255
|
You can turn on server rendering by simply changing the `prerender` option to `true`:
|
|
@@ -312,7 +312,7 @@ mv app/javascript client
|
|
|
312
312
|
|
|
313
313
|
## Using HMR with the rails/webpacker setup
|
|
314
314
|
|
|
315
|
-
Start the app using `foreman start -f Procfile.dev
|
|
315
|
+
Start the app using `foreman start -f Procfile.dev`.
|
|
316
316
|
|
|
317
317
|
When you change a JSX file and save, the browser will automatically refresh!
|
|
318
318
|
|
|
@@ -5,6 +5,21 @@ If you would like help in migrating between React on Rails versions or help with
|
|
|
5
5
|
|
|
6
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
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.
|
|
22
|
+
|
|
8
23
|
## Upgrading to v12
|
|
9
24
|
### Recent versions
|
|
10
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!
|
|
@@ -33,7 +33,7 @@ Until version 9, all React on Rails apps used the `/client` directory for config
|
|
|
33
33
|
You can access values in the `config/webpacker.yml`
|
|
34
34
|
|
|
35
35
|
```js
|
|
36
|
-
const { config, devServer } = require('
|
|
36
|
+
const { config, devServer } = require('shakapacker');
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
You will want consider using some of the same values set in these files:
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
and avoid any duplicates. We don't want different versions of the same packages.
|
|
5
5
|
We want the versions from rails/webpacker unless we specifically want to override them.
|
|
6
6
|
2. Search the rails/webpacker repo for anything you're not sure about in terms of package names.
|
|
7
|
-
3. run `bin/
|
|
7
|
+
3. run `bin/webpacker` and make sure there are zero errors
|
|
8
8
|
4. update webpack plugins and loaders to current or close to current
|
|
9
|
-
5. Make sure that your bin/
|
|
9
|
+
5. Make sure that your bin/webpacker and bin/webpacker match the latest on
|
|
10
10
|
https://github.com/rails/webpacker/tree/master/lib/install/bin
|
|
@@ -33,7 +33,42 @@ module ReactOnRails
|
|
|
33
33
|
app/views/layouts/hello_world.html.erb
|
|
34
34
|
config/initializers/react_on_rails.rb
|
|
35
35
|
Procfile.dev
|
|
36
|
-
Procfile.dev-
|
|
36
|
+
Procfile.dev-static]
|
|
37
|
+
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def copy_js_bundle_files
|
|
41
|
+
base_path = "base/base/"
|
|
42
|
+
base_files = %w[app/javascript/packs/server-bundle.js
|
|
43
|
+
app/javascript/bundles/HelloWorld/components/HelloWorldServer.js
|
|
44
|
+
app/javascript/bundles/HelloWorld/components/HelloWorld.module.css]
|
|
45
|
+
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def copy_webpack_config
|
|
49
|
+
puts "Adding Webpack config"
|
|
50
|
+
base_path = "base/base"
|
|
51
|
+
base_files = %w[babel.config.js
|
|
52
|
+
config/webpack/clientWebpackConfig.js
|
|
53
|
+
config/webpack/commonWebpackConfig.js
|
|
54
|
+
config/webpack/test.js
|
|
55
|
+
config/webpack/development.js
|
|
56
|
+
config/webpack/production.js
|
|
57
|
+
config/webpack/serverWebpackConfig.js
|
|
58
|
+
config/webpack/webpack.config.js
|
|
59
|
+
config/webpack/webpackConfig.js]
|
|
60
|
+
config = {
|
|
61
|
+
message: "// The source code including full typescript support is available at:"
|
|
62
|
+
}
|
|
63
|
+
base_files.each do |file|
|
|
64
|
+
template("#{base_path}/#{file}.tt", file, config)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def copy_webpacker_config
|
|
69
|
+
puts "Adding Webpacker v6 config"
|
|
70
|
+
base_path = "base/base/"
|
|
71
|
+
base_files = %w[config/webpacker.yml]
|
|
37
72
|
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
|
|
38
73
|
end
|
|
39
74
|
|
|
@@ -51,6 +86,16 @@ module ReactOnRails
|
|
|
51
86
|
puts "Adding the lastest react-on-rails NPM module. Double check this is correct in package.json"
|
|
52
87
|
run "yarn add react-on-rails --exact"
|
|
53
88
|
end
|
|
89
|
+
|
|
90
|
+
puts "Adding React dependencies"
|
|
91
|
+
run "yarn add react react-dom @babel/preset-react prop-types babel-plugin-transform-react-remove-prop-types \
|
|
92
|
+
babel-plugin-macros"
|
|
93
|
+
|
|
94
|
+
puts "Adding CSS handlers"
|
|
95
|
+
run "yarn add css-loader css-minimizer-webpack-plugin mini-css-extract-plugin style-loader"
|
|
96
|
+
|
|
97
|
+
puts "Adding dev dependencies"
|
|
98
|
+
run "yarn add -D @pmmmwh/react-refresh-webpack-plugin react-refresh"
|
|
54
99
|
end
|
|
55
100
|
|
|
56
101
|
def append_to_spec_rails_helper
|
|
@@ -93,28 +138,28 @@ module ReactOnRails
|
|
|
93
138
|
|
|
94
139
|
What to do next:
|
|
95
140
|
|
|
96
|
-
- See the documentation on https://github.com/
|
|
141
|
+
- See the documentation on https://github.com/shakacode/shakapacker#webpack-configuration
|
|
97
142
|
for how to customize the default webpack configuration.
|
|
98
143
|
|
|
99
144
|
- Include your webpack assets to your application layout.
|
|
100
145
|
|
|
101
146
|
<%= javascript_pack_tag 'hello-world-bundle' %>
|
|
102
147
|
|
|
103
|
-
- Run `rails s` to start the Rails server
|
|
148
|
+
- Run `rails s` to start the Rails server.
|
|
104
149
|
|
|
105
|
-
-
|
|
106
|
-
|
|
107
|
-
- Run bin/webpack-dev-server to start the Webpack dev server for compilation of Webpack
|
|
150
|
+
- Run bin/webpacker-dev-server to start the Webpack dev server for compilation of Webpack
|
|
108
151
|
assets as soon as you save. This default setup with the dev server does not work
|
|
109
152
|
for server rendering
|
|
110
153
|
|
|
111
|
-
-
|
|
112
|
-
|
|
154
|
+
- Visit http://localhost:3000/hello_world and see your React On Rails app running!
|
|
155
|
+
|
|
156
|
+
- Alternately, run the foreman command to start the rails server and run webpack#{' '}
|
|
157
|
+
in watch mode.
|
|
113
158
|
|
|
114
|
-
foreman start -f Procfile.dev
|
|
159
|
+
foreman start -f Procfile.dev-static
|
|
115
160
|
|
|
116
161
|
- To turn on HMR, edit config/webpacker.yml and set HMR to true. Restart the rails server
|
|
117
|
-
and bin/
|
|
162
|
+
and bin/webpacker-dev-server. Or use Procfile.dev.
|
|
118
163
|
|
|
119
164
|
- To server render, change this line app/views/hello_world/index.html.erb to
|
|
120
165
|
`prerender: true` to see server rendering (right click on page and select "view source").
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
+
# Procfile for development using HMR
|
|
1
2
|
# You can run these commands in separate shells
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
# When making frequent changes to client side assets, you will prefer building webpack assets
|
|
6
|
-
# upon saving rather than when you refresh your browser page.
|
|
7
|
-
# Note, if using React on Rails localization you will need to run
|
|
8
|
-
# `bundle exec rake react_on_rails:locale` before you run bin/webpack
|
|
9
|
-
client: sh -c 'rm -rf public/packs/* || true && bin/webpack -w'
|
|
3
|
+
rails: bundle exec rails s -p 3000
|
|
4
|
+
wp-client: HMR=true bin/webpacker-dev-server
|
|
5
|
+
wp-server: HMR=true SERVER_BUNDLE_ONLY=yes bin/webpacker --watch
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# You can run these commands in separate shells
|
|
2
|
+
web: rails s -p 3000
|
|
3
|
+
|
|
4
|
+
# Next line runs a watch process with webpack to compile the changed files.
|
|
5
|
+
# When making frequent changes to client side assets, you will prefer building webpack assets
|
|
6
|
+
# upon saving rather than when you refresh your browser page.
|
|
7
|
+
# Note, if using React on Rails localization you will need to run
|
|
8
|
+
# `bundle exec rake react_on_rails:locale` before you run bin/webpacker
|
|
9
|
+
webpack: sh -c 'rm -rf public/packs/* || true && bin/webpacker -w'
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import PropTypes from 'prop-types';
|
|
2
2
|
import React, { useState } from 'react';
|
|
3
|
+
import style from './HelloWorld.module.css';
|
|
3
4
|
|
|
4
5
|
const HelloWorld = (props) => {
|
|
5
6
|
const [name, setName] = useState(props.name);
|
|
@@ -9,7 +10,7 @@ const HelloWorld = (props) => {
|
|
|
9
10
|
<h3>Hello, {name}!</h3>
|
|
10
11
|
<hr />
|
|
11
12
|
<form>
|
|
12
|
-
<label htmlFor="name">
|
|
13
|
+
<label className={style.bright} htmlFor="name">
|
|
13
14
|
Say hello to:
|
|
14
15
|
<input id="name" type="text" value={name} onChange={(e) => setName(e.target.value)} />
|
|
15
16
|
</label>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<%= add_documentation_reference(config[:message], "// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/babel.config.js") %>
|
|
2
|
+
|
|
3
|
+
module.exports = function (api) {
|
|
4
|
+
const defaultConfigFunc = require('shakapacker/package/babel/preset.js')
|
|
5
|
+
const resultConfig = defaultConfigFunc(api)
|
|
6
|
+
const isProductionEnv = api.env('production')
|
|
7
|
+
|
|
8
|
+
const changesOnDefault = {
|
|
9
|
+
presets: [
|
|
10
|
+
[
|
|
11
|
+
'@babel/preset-react',
|
|
12
|
+
{
|
|
13
|
+
development: !isProductionEnv,
|
|
14
|
+
useBuiltIns: true
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
].filter(Boolean),
|
|
18
|
+
plugins: [
|
|
19
|
+
process.env.WEBPACK_SERVE && 'react-refresh/babel',
|
|
20
|
+
isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
|
|
21
|
+
{
|
|
22
|
+
removeImport: true
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
].filter(Boolean),
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
|
|
29
|
+
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]
|
|
30
|
+
|
|
31
|
+
return resultConfig
|
|
32
|
+
}
|
data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
ReactOnRails.configure do |config|
|
|
7
7
|
# This configures the script to run to build the production assets by webpack. Set this to nil
|
|
8
8
|
# if you don't want react_on_rails building this file for you.
|
|
9
|
-
# If nil, then the standard
|
|
9
|
+
# If nil, then the standard shakacode/shakapacker assets:precompile will run
|
|
10
10
|
# config.build_production_command = nil
|
|
11
11
|
|
|
12
12
|
################################################################################
|
|
@@ -25,7 +25,7 @@ ReactOnRails.configure do |config|
|
|
|
25
25
|
#
|
|
26
26
|
# Alternately, you can remove the `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`
|
|
27
27
|
# and set the config/webpacker.yml option for test to true.
|
|
28
|
-
config.build_test_command = "RAILS_ENV=test bin/
|
|
28
|
+
config.build_test_command = "RAILS_ENV=test bin/webpacker"
|
|
29
29
|
|
|
30
30
|
################################################################################
|
|
31
31
|
################################################################################
|
|
@@ -41,5 +41,5 @@ ReactOnRails.configure do |config|
|
|
|
41
41
|
# different. You should have ONE server bundle which can create all of your server rendered
|
|
42
42
|
# React components.
|
|
43
43
|
#
|
|
44
|
-
config.server_bundle_js_file = "
|
|
44
|
+
config.server_bundle_js_file = "server-bundle.js"
|
|
45
45
|
end
|
data/lib/generators/react_on_rails/templates/base/base/config/webpack/clientWebpackConfig.js.tt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<%= add_documentation_reference(config[:message], "// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/clientWebpackConfig.js") %>
|
|
2
|
+
|
|
3
|
+
const commonWebpackConfig = require('./commonWebpackConfig');
|
|
4
|
+
|
|
5
|
+
const configureClient = () => {
|
|
6
|
+
const clientConfig = commonWebpackConfig();
|
|
7
|
+
|
|
8
|
+
// server-bundle is special and should ONLY be built by the serverConfig
|
|
9
|
+
// In case this entry is not deleted, a very strange "window" not found
|
|
10
|
+
// error shows referring to window["webpackJsonp"]. That is because the
|
|
11
|
+
// client config is going to try to load chunks.
|
|
12
|
+
delete clientConfig.entry['server-bundle'];
|
|
13
|
+
|
|
14
|
+
return clientConfig;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
module.exports = configureClient;
|
data/lib/generators/react_on_rails/templates/base/base/config/webpack/commonWebpackConfig.js.tt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<%= add_documentation_reference(config[:message], "// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/commonWebpackConfig.js") %>
|
|
2
|
+
|
|
3
|
+
// Common configuration applying to client and server configuration
|
|
4
|
+
const { webpackConfig: baseClientWebpackConfig, merge } = require('shakapacker');
|
|
5
|
+
|
|
6
|
+
const commonOptions = {
|
|
7
|
+
resolve: {
|
|
8
|
+
extensions: ['.css', '.ts', '.tsx'],
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// Copy the object using merge b/c the baseClientWebpackConfig and commonOptions are mutable globals
|
|
13
|
+
const commonWebpackConfig = () => merge({}, baseClientWebpackConfig, commonOptions);
|
|
14
|
+
|
|
15
|
+
module.exports = commonWebpackConfig;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<%= add_documentation_reference(config[:message], "// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/development.js") %>
|
|
2
|
+
|
|
3
|
+
const { devServer, inliningCss } = require('shakapacker');
|
|
4
|
+
|
|
5
|
+
const webpackConfig = require('./webpackConfig');
|
|
6
|
+
|
|
7
|
+
const developmentEnvOnly = (clientWebpackConfig, _serverWebpackConfig) => {
|
|
8
|
+
// plugins
|
|
9
|
+
if (inliningCss) {
|
|
10
|
+
// Note, when this is run, we're building the server and client bundles in separate processes.
|
|
11
|
+
// Thus, this plugin is not applied to the server bundle.
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line global-require
|
|
14
|
+
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
|
|
15
|
+
clientWebpackConfig.plugins.push(
|
|
16
|
+
new ReactRefreshWebpackPlugin({
|
|
17
|
+
overlay: {
|
|
18
|
+
sockPort: devServer.port,
|
|
19
|
+
},
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
module.exports = webpackConfig(developmentEnvOnly);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<%= add_documentation_reference(config[:message], "// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/production.js") %>
|
|
2
|
+
|
|
3
|
+
const webpackConfig = require('./webpackConfig');
|
|
4
|
+
|
|
5
|
+
const productionEnvOnly = (_clientWebpackConfig, _serverWebpackConfig) => {
|
|
6
|
+
// place any code here that is for production only
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
module.exports = webpackConfig(productionEnvOnly);
|
data/lib/generators/react_on_rails/templates/base/base/config/webpack/serverWebpackConfig.js.tt
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
<%= add_documentation_reference(config[:message], "// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/serverWebpackConfig.js") %>
|
|
2
|
+
|
|
3
|
+
const { merge, config } = require('shakapacker');
|
|
4
|
+
const commonWebpackConfig = require('./commonWebpackConfig');
|
|
5
|
+
|
|
6
|
+
const webpack = require('webpack');
|
|
7
|
+
|
|
8
|
+
const configureServer = () => {
|
|
9
|
+
// We need to use "merge" because the clientConfigObject, EVEN after running
|
|
10
|
+
// toWebpackConfig() is a mutable GLOBAL. Thus any changes, like modifying the
|
|
11
|
+
// entry value will result in changing the client config!
|
|
12
|
+
// Using webpack-merge into an empty object avoids this issue.
|
|
13
|
+
const serverWebpackConfig = commonWebpackConfig();
|
|
14
|
+
|
|
15
|
+
// We just want the single server bundle entry
|
|
16
|
+
const serverEntry = {
|
|
17
|
+
'server-bundle': serverWebpackConfig.entry['server-bundle'],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
if (!serverEntry['server-bundle']) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
"Create a pack with the file name 'server-bundle.js' containing all the server rendering files",
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
serverWebpackConfig.entry = serverEntry;
|
|
27
|
+
|
|
28
|
+
// Remove the mini-css-extract-plugin from the style loaders because
|
|
29
|
+
// the client build will handle exporting CSS.
|
|
30
|
+
// replace file-loader with null-loader
|
|
31
|
+
serverWebpackConfig.module.rules.forEach((loader) => {
|
|
32
|
+
if (loader.use && loader.use.filter) {
|
|
33
|
+
loader.use = loader.use.filter(
|
|
34
|
+
(item) => !(typeof item === 'string' && item.match(/mini-css-extract-plugin/)),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// No splitting of chunks for a server bundle
|
|
40
|
+
serverWebpackConfig.optimization = {
|
|
41
|
+
minimize: false,
|
|
42
|
+
};
|
|
43
|
+
serverWebpackConfig.plugins.unshift(new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }));
|
|
44
|
+
|
|
45
|
+
// Custom output for the server-bundle that matches the config in
|
|
46
|
+
// config/initializers/react_on_rails.rb
|
|
47
|
+
serverWebpackConfig.output = {
|
|
48
|
+
filename: 'server-bundle.js',
|
|
49
|
+
globalObject: 'this',
|
|
50
|
+
// If using the React on Rails Pro node server renderer, uncomment the next line
|
|
51
|
+
// libraryTarget: 'commonjs2',
|
|
52
|
+
path: config.outputPath,
|
|
53
|
+
publicPath: config.publicPath,
|
|
54
|
+
// https://webpack.js.org/configuration/output/#outputglobalobject
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Don't hash the server bundle b/c would conflict with the client manifest
|
|
58
|
+
// And no need for the MiniCssExtractPlugin
|
|
59
|
+
serverWebpackConfig.plugins = serverWebpackConfig.plugins.filter(
|
|
60
|
+
(plugin) =>
|
|
61
|
+
plugin.constructor.name !== 'WebpackAssetsManifest' &&
|
|
62
|
+
plugin.constructor.name !== 'MiniCssExtractPlugin' &&
|
|
63
|
+
plugin.constructor.name !== 'ForkTsCheckerWebpackPlugin',
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// Configure loader rules for SSR
|
|
67
|
+
// Remove the mini-css-extract-plugin from the style loaders because
|
|
68
|
+
// the client build will handle exporting CSS.
|
|
69
|
+
// replace file-loader with null-loader
|
|
70
|
+
const rules = serverWebpackConfig.module.rules;
|
|
71
|
+
rules.forEach((rule) => {
|
|
72
|
+
if (Array.isArray(rule.use)) {
|
|
73
|
+
// remove the mini-css-extract-plugin and style-loader
|
|
74
|
+
rule.use = rule.use.filter((item) => {
|
|
75
|
+
let testValue;
|
|
76
|
+
if (typeof item === 'string') {
|
|
77
|
+
testValue = item;
|
|
78
|
+
} else if (typeof item.loader === 'string') {
|
|
79
|
+
testValue = item.loader;
|
|
80
|
+
}
|
|
81
|
+
return !(testValue.match(/mini-css-extract-plugin/) || testValue === 'style-loader');
|
|
82
|
+
});
|
|
83
|
+
const cssLoader = rule.use.find((item) => {
|
|
84
|
+
let testValue;
|
|
85
|
+
|
|
86
|
+
if (typeof item === 'string') {
|
|
87
|
+
testValue = item;
|
|
88
|
+
} else if (typeof item.loader === 'string') {
|
|
89
|
+
testValue = item.loader;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return testValue.includes('css-loader');
|
|
93
|
+
});
|
|
94
|
+
if (cssLoader && cssLoader.options) {
|
|
95
|
+
cssLoader.options.modules = { exportOnlyLocals: true };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Skip writing image files during SSR by setting emitFile to false
|
|
99
|
+
} else if (rule.use && (rule.use.loader === 'url-loader' || rule.use.loader === 'file-loader')) {
|
|
100
|
+
rule.use.options.emitFile = false;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// eval works well for the SSR bundle because it's the fastest and shows
|
|
105
|
+
// lines in the server bundle which is good for debugging SSR
|
|
106
|
+
// The default of cheap-module-source-map is slow and provides poor info.
|
|
107
|
+
serverWebpackConfig.devtool = 'eval';
|
|
108
|
+
|
|
109
|
+
// If using the default 'web', then libraries like Emotion and loadable-components
|
|
110
|
+
// break with SSR. The fix is to use a node renderer and change the target.
|
|
111
|
+
// If using the React on Rails Pro node server renderer, uncomment the next line
|
|
112
|
+
// serverWebpackConfig.target = 'node'
|
|
113
|
+
|
|
114
|
+
return serverWebpackConfig;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
module.exports = configureServer;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<%= add_documentation_reference(config[:message], "// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/development.js") %>
|
|
2
|
+
|
|
3
|
+
// Use dev
|
|
4
|
+
const config = require('./development');
|
|
5
|
+
|
|
6
|
+
module.exports = config;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const { env } = require('shakapacker')
|
|
2
|
+
const { existsSync } = require('fs')
|
|
3
|
+
const { resolve } = require('path')
|
|
4
|
+
|
|
5
|
+
const envSpecificConfig = () => {
|
|
6
|
+
const path = resolve(__dirname, `${env.nodeEnv}.js`)
|
|
7
|
+
if (existsSync(path)) {
|
|
8
|
+
console.log(`Loading ENV specific webpack configuration file ${path}`)
|
|
9
|
+
return require(path)
|
|
10
|
+
} else {
|
|
11
|
+
throw new Error(`Could not find file to load ${path}, based on NODE_ENV`)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = envSpecificConfig()
|