react_on_rails 12.0.0.pre.beta.1 → 12.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/.travis.yml +6 -4
- data/CHANGELOG.md +27 -17
- data/README.md +77 -79
- data/SUMMARY.md +1 -1
- data/docs/additional-reading/converting-from-custom-webpack-config-to-rails-webpacker-config.md +10 -0
- data/docs/additional-reading/react-router.md +1 -1
- data/docs/additional-reading/server-rendering-tips.md +4 -1
- data/docs/api/javascript-api.md +3 -3
- data/docs/api/redux-store-api.md +2 -2
- data/docs/api/view-helpers-api.md +4 -4
- data/docs/basics/configuration.md +28 -17
- data/docs/basics/deployment.md +2 -3
- data/docs/basics/heroku-deployment.md +24 -0
- data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +49 -0
- data/docs/basics/i18n.md +3 -3
- data/docs/basics/installation-into-an-existing-rails-app.md +2 -7
- data/docs/basics/recommended-project-structure.md +5 -22
- data/docs/basics/render-functions-and-railscontext.md +1 -1
- data/docs/basics/rspec-configuration.md +27 -16
- data/docs/basics/upgrading-react-on-rails.md +35 -13
- data/docs/basics/webpack-configuration.md +3 -7
- data/docs/misc/doctrine.md +1 -1
- data/docs/outdated/code-splitting.md +3 -3
- data/docs/outdated/how-react-on-rails-works.md +8 -4
- data/docs/outdated/manual-installation-overview.md +1 -1
- data/docs/outdated/rails-assets.md +0 -7
- data/docs/tutorial.md +40 -30
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-hmr +17 -9
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +4 -1
- data/lib/react_on_rails/configuration.rb +45 -6
- data/lib/react_on_rails/helper.rb +8 -8
- data/lib/react_on_rails/locales/base.rb +8 -0
- data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +1 -1
- data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +17 -0
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/webpacker_utils.rb +6 -0
- data/lib/tasks/assets.rake +47 -8
- data/lib/tasks/locale.rake +1 -5
- data/package.json +1 -1
- data/rakelib/examples.rake +1 -1
- data/rakelib/lint.rake +1 -1
- data/rakelib/release.rake +1 -3
- data/react_on_rails.gemspec +1 -0
- data/yarn.lock +260 -109
- metadata +21 -5
- data/docs/outdated/heroku-deployment.md +0 -86
data/docs/api/javascript-api.md
CHANGED
@@ -25,10 +25,10 @@ The best source of docs is the main [ReactOnRails.js](https://github.com/shakaco
|
|
25
25
|
/**
|
26
26
|
* Main entry point to using the react-on-rails npm package. This is how Rails will be able to
|
27
27
|
* find you components for rendering. Components get called with props, or you may use a
|
28
|
-
* "
|
28
|
+
* "Render-Function" to return a React component or an object with the following shape:
|
29
29
|
* { renderedHtml, redirectLocation, error }.
|
30
|
-
* For server rendering, if you wish to return multiple HTML strings from a
|
31
|
-
* you may return an Object from your
|
30
|
+
* For server rendering, if you wish to return multiple HTML strings from a Render-Function,
|
31
|
+
* you may return an Object from your Render-Function with a single top level property of
|
32
32
|
* renderedHtml. Inside this Object, place a key called componentHtml, along with any other
|
33
33
|
* needed keys. This is useful when you using side effects libraries like react helmet.
|
34
34
|
* Your Ruby code with get this Object as a Hash containing keys componentHtml and any other
|
data/docs/api/redux-store-api.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Redux Store
|
2
2
|
|
3
|
-
_This redux API is no longer recommended as it prevents dynamic code splitting for performance. Instead, you should use the standard react_component view helper passing in a "
|
3
|
+
_This redux API is no longer recommended as it prevents dynamic code splitting for performance. Instead, you should use the standard react_component view helper passing in a "Render-Function."_
|
4
4
|
|
5
5
|
You don't need to use the `redux_store` api to use redux. This api was setup to support multiple calls to `react_component` on one page that all talk to the same redux store.
|
6
6
|
|
7
|
-
If you are only rendering one react component on a page, as is typical to do a "Single Page App" in React, then you should _probably_ pass the props to your React component in a "
|
7
|
+
If you are only rendering one react component on a page, as is typical to do a "Single Page App" in React, then you should _probably_ pass the props to your React component in a "Render-Function."
|
8
8
|
|
9
9
|
Consider using the `redux_store` helper for the two following use cases:
|
10
10
|
|
@@ -22,7 +22,7 @@ Uncommonly used options:
|
|
22
22
|
id: nil,
|
23
23
|
```
|
24
24
|
|
25
|
-
- **component_name:** Can be a React component, created using an ES6 class or a
|
25
|
+
- **component_name:** Can be a React component, created using a React Function Component, an ES6 class or a Render-Function that returns a React component (or, only on the server side, an object with shape { redirectLocation, error, renderedHtml }), or a "renderer function" that manually renders a React component to the dom (client side only). Note, a "renderer function" is a special type of "Render-Function." A "renderer function" takes a 3rd param of a DOM ID.
|
26
26
|
All options except `props, id, html_options` will inherit from your `react_on_rails.rb` initializer, as described [here](../basics/configuration.md).
|
27
27
|
- **general options:**
|
28
28
|
- **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.
|
@@ -45,7 +45,7 @@ adding meta-tags to a page. It is exactly like react_component except for the fo
|
|
45
45
|
|
46
46
|
1. `prerender: true` is automatically added to options, as this method doesn't make sense for
|
47
47
|
client only rendering.
|
48
|
-
2. Your JavaScript
|
48
|
+
2. Your JavaScript Render-Function for server rendering must return an Object rather than a React Component.
|
49
49
|
3. Your view code must expect an object and not a string.
|
50
50
|
|
51
51
|
Here is an example of ERB view code:
|
@@ -98,11 +98,11 @@ You can call `rails_context` or `rails_context(server_side: true|false)` from yo
|
|
98
98
|
|
99
99
|
### Renderer Functions (function that will call ReactDOM.render or ReactDOM.hydrate)
|
100
100
|
|
101
|
-
A "renderer function" is a
|
101
|
+
A "renderer function" is a Render-Function that accepts three arguments (rather than 2): `(props, railsContext, domNodeId) => { ... }`. Instead of returning a React component, a renderer is responsible for installing a callback that will call `ReactDOM.render` (in React 16+, `ReactDOM.hydrate`) to render a React component into the DOM. The "renderer function" is called at the same time the document ready event would instantate the React components into the DOM.
|
102
102
|
|
103
103
|
Why would you want to call `ReactDOM.hydrate` yourself? One possible use case is [code splitting](docs/outdated/code-splitting.md). In a nutshell, you don't want to load the React component on the DOM node yet. So you want to install some handler that will call `ReactDOM.hydrate` at a later time. In the case of code splitting with server rendering, the server rendered code has any async code loaded and used to server render. Thus, the client code must also fully load any asynch code before server rendering. Otherwise, the client code would first render partially, not matching the server rendering, and then a second later, the full code would render, resulting in an unpleasant flashing on the screen.
|
104
104
|
|
105
|
-
Renderer functions are not meant to be used on the server since there's no DOM on the server. Instead, use a
|
105
|
+
Renderer functions are not meant to be used on the server since there's no DOM on the server. Instead, use a Render-Function. Attempting to server render with a renderer function will throw an error.
|
106
106
|
|
107
107
|
------------
|
108
108
|
|
@@ -12,21 +12,27 @@ default: &default
|
|
12
12
|
# public_output_path folder
|
13
13
|
manifest: manifest.json
|
14
14
|
cache_manifest: false
|
15
|
+
|
16
|
+
# Source path is used to check if webpack compilation needs to be run for `compile: true`
|
15
17
|
source_path: client/app
|
16
18
|
|
17
19
|
development:
|
18
20
|
<<: *default
|
19
|
-
#
|
21
|
+
# Generated files for development, in /public/webpack/dev
|
20
22
|
public_output_path: webpack/dev
|
21
23
|
|
22
24
|
test:
|
23
25
|
<<: *default
|
24
|
-
#
|
26
|
+
# Ensure that webpacker invokes webpack to build files for tests if not using the
|
27
|
+
# ReactOnRails rspec helper.
|
28
|
+
compile: true
|
29
|
+
|
30
|
+
# Generated files for tests, in /public/webpack/test
|
25
31
|
public_output_path: webpack/test
|
26
32
|
|
27
33
|
production:
|
28
34
|
<<: *default
|
29
|
-
#
|
35
|
+
# Generated files for production, in /public/webpack/production
|
30
36
|
public_output_path: webpack/production
|
31
37
|
cache_manifest: true
|
32
38
|
```
|
@@ -45,7 +51,7 @@ ReactOnRails.configure do |config|
|
|
45
51
|
# The default is true for development, off otherwise.
|
46
52
|
# With true, you get detailed logs of rendering and stack traces if you call setTimout,
|
47
53
|
# setInterval, clearTimout when server rendering.
|
48
|
-
config.trace = Rails.env.development?
|
54
|
+
config.trace = Rails.env.development? # default
|
49
55
|
|
50
56
|
# Configure if default DOM IDs have a random value or are fixed.
|
51
57
|
# false ==> Sets the dom id to "#{react_component_name}-react-component"
|
@@ -54,15 +60,17 @@ ReactOnRails.configure do |config|
|
|
54
60
|
# it is convenient to set this to true or else you have to either manually set the ids to
|
55
61
|
# avoid collisions. Most newer apps will have only one instance of a component on a page,
|
56
62
|
# so this should be false in most cases.
|
57
|
-
# This value can be
|
58
|
-
config.random_dom_id =
|
59
|
-
|
60
|
-
# defaults to "" (top level)
|
61
|
-
#
|
62
|
-
config.node_modules_location = "client" # If using webpacker you should use "".
|
63
|
+
# This value can be overridden for a given call to react_component
|
64
|
+
config.random_dom_id = true # default
|
63
65
|
|
64
|
-
#
|
65
|
-
|
66
|
+
# defaults to "" (top level)
|
67
|
+
config.node_modules_location = "client" # If using webpacker you should use "".
|
68
|
+
|
69
|
+
# This configures the script to run to build the production assets by webpack . Set this to nil
|
70
|
+
# if you don't want react_on_rails building this file for you.
|
71
|
+
# Note, if you want to use this command then you should remove the file
|
72
|
+
# config/webpack/production.js
|
73
|
+
# If that file exists, React on Rails thinks that you'll use the rails/webpacker bin/webpack compiler.
|
66
74
|
config.build_production_command = "RAILS_ENV=production bin/webpack"
|
67
75
|
|
68
76
|
################################################################################
|
@@ -89,7 +97,9 @@ ReactOnRails.configure do |config|
|
|
89
97
|
# Normally, you have different bundles for client and server, thus, the default is false.
|
90
98
|
# Furthermore, if you are not hashing the server bundle (not in the manifest.json), then React on Rails
|
91
99
|
# will only look for the server bundle to be created in the typical file location, typically by
|
92
|
-
# a `webpack --watch` process.
|
100
|
+
# a `webpack --watch` process.
|
101
|
+
# If true, ensure that in config/webpacker.yml that you have both dev_server.hmr and
|
102
|
+
# dev_server.inline set to false.
|
93
103
|
config.same_bundle_for_client_and_server = false
|
94
104
|
|
95
105
|
# If set to true, this forces Rails to reload the server bundle if it is modified
|
@@ -134,6 +144,7 @@ ReactOnRails.configure do |config|
|
|
134
144
|
# Replace the following line to the location where you keep translation.js & default.js for use
|
135
145
|
# by the npm packages react-intl. Be sure this directory exists!
|
136
146
|
# config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n")
|
147
|
+
#
|
137
148
|
# If not using the i18n feature, then leave this section commented out or set the value
|
138
149
|
# of config.i18n_dir to nil.
|
139
150
|
#
|
@@ -141,11 +152,11 @@ ReactOnRails.configure do |config|
|
|
141
152
|
# that will source for automatic generation on translations.js & default.js
|
142
153
|
# By default(without this option) all yaml files from Rails.root.join("config", "locales")
|
143
154
|
# and installed gems are loaded
|
144
|
-
config.i18n_yml_dir = Rails.root.join("config", "locales"
|
155
|
+
config.i18n_yml_dir = Rails.root.join("config", "locales")
|
145
156
|
|
146
157
|
# Possible output formats are js and json
|
147
158
|
# The default format is json
|
148
|
-
config.i18n_output_format = '
|
159
|
+
config.i18n_output_format = 'json'
|
149
160
|
|
150
161
|
################################################################################
|
151
162
|
################################################################################
|
@@ -186,7 +197,7 @@ ReactOnRails.configure do |config|
|
|
186
197
|
# CONFIGURE YOUR SOURCE FILES
|
187
198
|
# The test helper needs to know where your JavaScript files exist. The value is configured
|
188
199
|
# by your config/webpacker.yml source_path:
|
189
|
-
# source_path: client/app
|
200
|
+
# source_path: client/app # if using recommended /client directory
|
190
201
|
#
|
191
202
|
# Define the files we need to check for webpack compilation when running tests.
|
192
203
|
# The default is `%w( manifest.json )` as will be sufficient for most webpacker builds.
|
@@ -204,7 +215,7 @@ Example of a RenderingExtension for custom values in the `rails_context`:
|
|
204
215
|
module RenderingExtension
|
205
216
|
|
206
217
|
# Return a Hash that contains custom values from the view context that will get merged with
|
207
|
-
# the standard rails_context values and passed to all calls to
|
218
|
+
# the standard rails_context values and passed to all calls to Render-Functions used by the
|
208
219
|
# react_component and redux_store view helpers
|
209
220
|
def self.custom_context(view_context)
|
210
221
|
{
|
data/docs/basics/deployment.md
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# Deployment
|
2
2
|
|
3
|
-
-
|
4
|
-
|
5
|
-
- See the [Heroku Deployment](docs/outdated/heroku-deployment.md) doc for specifics regarding Heroku. The information for Heroku may apply to other deployments.
|
3
|
+
- `rails/webpacker` puts the necessary precompile steps automatically in the rake precompile step.
|
4
|
+
- See the [Heroku Deployment](docs/basics/heroku-deployment.md) doc for specifics regarding Heroku. The information for Heroku may apply to other deployments.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Heroku Deployment
|
2
|
+
## Heroku buildpacks
|
3
|
+
|
4
|
+
React on Rails requires both a ruby environment (for Rails) and a Node environment (for Webpack), so you will need to have Heroku use multiple buildpacks.
|
5
|
+
|
6
|
+
Assuming you have downloaded and installed the Heroku command-line utility and have initialized the app, you will need to tell Heroku to use both buildpacks via the command-line:
|
7
|
+
|
8
|
+
```
|
9
|
+
heroku buildpacks:set heroku/ruby
|
10
|
+
heroku buildpacks:add --index 1 heroku/nodejs
|
11
|
+
```
|
12
|
+
|
13
|
+
For more information, see [Using Multiple Buildpacks for an App](https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app)
|
14
|
+
|
15
|
+
## assets:precompile
|
16
|
+
|
17
|
+
### rails/webpacker webpack configuration
|
18
|
+
If you're using the standard rails/webpacker configuration of webpack, then rails/webpacker
|
19
|
+
will automatically modify or create an assets:precompile task to build your assets.
|
20
|
+
|
21
|
+
### custom webpack configuration
|
22
|
+
If you're a custom webpack configuration, and you **do not have the default
|
23
|
+
`config/webpack/production.js`** file, then the `config/initializers/react_on_rails.rb`
|
24
|
+
configuration `config.build_production_command` will be used.
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# HMR and Hot Reloading with the webpack-dev-server
|
2
|
+
|
3
|
+
The webpack-dev-server provides:
|
4
|
+
|
5
|
+
1. Speedy compilation of client side assets
|
6
|
+
2. Optional HMR which means that the page will reload automatically when after
|
7
|
+
compilation completes. Note, some developers do not like this, as you'll
|
8
|
+
abruptly lose any tweaks within the Chrome development tools.
|
9
|
+
3. Optional hot-reloading. The older react-hot-loader has been deprecated in
|
10
|
+
favor of [fast-refresh](https://reactnative.dev/docs/fast-refresh).
|
11
|
+
For use with webpack, see [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin).
|
12
|
+
|
13
|
+
If you are ***not*** using server-side rendering (***not*** using `prerender: true`),
|
14
|
+
then you can follow all the regular docs for using the `bin/webpack-dev-server`
|
15
|
+
during development.
|
16
|
+
|
17
|
+
|
18
|
+
# Server Side Rendering with the Default rails/webpacker bin/webpack-dev-server
|
19
|
+
|
20
|
+
If you are using server-side rendering, then you have a couple options. The
|
21
|
+
recommended technique is to have a different webpack configuration for server
|
22
|
+
rendering.
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
## If you use the same Webpack setup for your server and client bundles
|
28
|
+
If you do use the webpack-dev-server for prerendering, be sure to set the
|
29
|
+
`config/initializers/react_on_rails.rb` setting of
|
30
|
+
|
31
|
+
```
|
32
|
+
config.same_bundle_for_client_and_server = true
|
33
|
+
```
|
34
|
+
|
35
|
+
`dev_server.hmr` maps to [devServer.hot](https://webpack.js.org/configuration/dev-server/#devserverhot).
|
36
|
+
This must be false if you're using the webpack-dev-server for client and server bundles.
|
37
|
+
|
38
|
+
`dev_server.inline` maps to [devServer.inline](https://webpack.js.org/configuration/dev-server/#devserverinline).
|
39
|
+
This must also be false.
|
40
|
+
|
41
|
+
If you don't configure these two to false, you'll see errors like:
|
42
|
+
|
43
|
+
* "ReferenceError: window is not defined" (if hmr is true)
|
44
|
+
* "TypeError: Cannot read property 'prototype' of undefined" (if inline is true)
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
data/docs/basics/i18n.md
CHANGED
@@ -25,7 +25,7 @@ Here's a summary of adding the I18n functionality.
|
|
25
25
|
|
26
26
|
3. Javascript locale files must be generated before `yarn build`.
|
27
27
|
|
28
|
-
Once you setup `config.i18n_dir` as in the previous step,
|
28
|
+
Once you setup `config.i18n_dir` as in the previous step, you will need to make sure `rake react_on_rails:locale` runs before webpack.
|
29
29
|
|
30
30
|
For development, you should adjust your startup scripts (Procfiles) so that they run **`bundle exec rake react_on_rails:locale`** before running any webpack watch process (`yarn run build:development`).
|
31
31
|
|
@@ -48,6 +48,7 @@ with `react-intl` supported via js files:
|
|
48
48
|
```
|
49
49
|
|
50
50
|
2. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle && yarn install`.
|
51
|
+
Versions should be newer than these:
|
51
52
|
|
52
53
|
```js
|
53
54
|
"dependencies": {
|
@@ -93,7 +94,6 @@ with `react-intl` supported via js files:
|
|
93
94
|
```
|
94
95
|
|
95
96
|
# Notes
|
96
|
-
|
97
97
|
* See why using JSON could be better compare to JS if amount of data is hure [ https://v8.dev/blog/cost-of-javascript-2019#json]( https://v8.dev/blog/cost-of-javascript-2019#json).
|
98
98
|
* See [Support for Rails' i18n pluralization #1000](https://github.com/shakacode/react_on_rails/issues/1000) for a discussion of issues around pluralization.
|
99
|
-
*
|
99
|
+
* *Outdated:* You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) and [PR #340](https://github.com/shakacode/react-webpack-rails-tutorial/pull/340), [commmited](https://github.com/shakacode/react-webpack-rails-tutorial/commit/ef369ed9d922aea5116ca7e50208169fd7831389) for a complete example.
|
@@ -7,8 +7,8 @@
|
|
7
7
|
1. Add the following to your Gemfile and `bundle install`. We recommend fixing the version of React on Rails, as you will need to keep the exact version in sync with the version in your `client/package.json` file.
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem "react_on_rails", "
|
11
|
-
gem "webpacker", "~>
|
10
|
+
gem "react_on_rails", "12.0.0" # Update to the current version
|
11
|
+
gem "webpacker", "~> 5"
|
12
12
|
```
|
13
13
|
|
14
14
|
2. Run the following 2 commands to install Webpacker with React. Note, if you are using an older version of Rails than 5.1, you'll need to install webpacker with React per the instructions [here](https://github.com/rails/webpacker).
|
@@ -57,8 +57,3 @@ $ yarn add react-on-rails --exact
|
|
57
57
|
```
|
58
58
|
|
59
59
|
That will install the latest version and update your package.json. **NOTE:** the `--exact` flag will ensure that you do not have a "~" or "^" for your react-on-rails version in your package.json.
|
60
|
-
|
61
|
-
## Webpacker Configuration
|
62
|
-
|
63
|
-
React on Rails users should set configuration value `compile` to false, as React on Rails handles compilation for test and production environments.
|
64
|
-
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# Recommended Project structure
|
2
2
|
|
3
|
-
While React On Rails does not *enforce* a specific project structure, we do *recommend* a standard organization. The more we follow standards as a community, the easier it will be for all of us to move between various Rails projects that include React On Rails.
|
4
|
-
|
5
3
|
The React on Rails generator uses the standard `rails/webpacker` convention of this structure:
|
6
4
|
|
7
5
|
```yml
|
@@ -14,21 +12,9 @@ app/javascript:
|
|
14
12
|
│ └── hello-world-bundle.js
|
15
13
|
```
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
1. No support for different entry points for server rendering.
|
20
|
-
2. Webpacker adds an extra layer of abstraction over Webpack, which you probably don't want.
|
21
|
-
|
22
|
-
This default rails/webpacker configuration is used for the generator because:
|
23
|
-
|
24
|
-
1. Minimizes the amount of generated code to get up and running with React on Rails.
|
25
|
-
2. Good enough for very simple projects.
|
26
|
-
3. Configuration of Webpack is not the goal of this library, React on Rails.
|
15
|
+
However, you may wish to move all your client side files to a single directory called something like `/client`.
|
27
16
|
|
28
|
-
|
29
|
-
Thus, the generator structure and using rails/webpacker for Webpack configuration **is not recommended** for any commercial projects, especially those that will use server rendering. Instead, the recommended structure is shown in this example app: [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) and described below.
|
30
|
-
|
31
|
-
## Steps to convert from the generator defaults to use the recommended `/client` directory structure.
|
17
|
+
## Steps to convert from the generator defaults to use a `/client` directory structure.
|
32
18
|
|
33
19
|
1. Move the directory:
|
34
20
|
|
@@ -44,7 +30,7 @@ mv app/javascript client
|
|
44
30
|
|
45
31
|
## Moving node_modules from `/` to `/client` with a custom webpack setup.
|
46
32
|
|
47
|
-
`rails/webpacker` probably doesn't support having your main node_modules directory under `/client`, so only follow these steps if you want to use your own webpack configuration
|
33
|
+
`rails/webpacker` probably doesn't support having your main node_modules directory under `/client`, so only follow these steps if you want to use your own webpack configuration.
|
48
34
|
|
49
35
|
1. Move the `/package.json` to `/client/package.json`
|
50
36
|
2. Create a `/package.json` that delegates to `/client/package.json`. See the example in [spec/dummy/package.json](../../spec/dummy/package.json).
|
@@ -57,7 +43,7 @@ mv app/javascript client
|
|
57
43
|
1. `/client/app/bundles`: Top level of different app domains. Use a name within this directory for you app domains. For example, if you had a domain called `widget-editing`, then you would have: `/client/app/bundles/widget-editing`
|
58
44
|
1. `/client/app/lib`: Common code for bundles
|
59
45
|
1. Within each bundle directory (or the lib directory), such as a domain named "comments"
|
60
|
-
`/client/app/bundles/comments`, use following directory structure:
|
46
|
+
`/client/app/bundles/comments`, use following directory structure, if you're using redux. However, with React hooks, this will probably be a bit different:
|
61
47
|
|
62
48
|
* `/actions`: Redux actions.
|
63
49
|
* `/components`: "dumb" components (no connections to Redux or Ajax). These get props and can render themselves and children.
|
@@ -79,7 +65,7 @@ This isn't really any technique, as you keep handling all your styling assets us
|
|
79
65
|
1. Much simpler! There's no changes really from your current processes.
|
80
66
|
|
81
67
|
### Using Webpack to Manage Styling Assets
|
82
|
-
This technique involves customization of the webpack config files to generate CSS, image, and font assets.
|
68
|
+
This technique involves customization of the webpack config files to generate CSS, image, and font assets.
|
83
69
|
|
84
70
|
#### Directory structure
|
85
71
|
1. `/client/app/assets`: Assets for CSS for client app.
|
@@ -89,6 +75,3 @@ This technique involves customization of the webpack config files to generate CS
|
|
89
75
|
1. You can use [CSS modules](https://github.com/css-modules/css-modules), which is super compelling once you seen the benefits.
|
90
76
|
1. You can do hot reloading of your assets. Thus, you do not have to refresh your web page to see asset change, including changing styles.
|
91
77
|
1. You can run your client code on a mocked out express server for super fast prototyping. In other words, your client application can somewhat more easily be move to a different application server.
|
92
|
-
|
93
|
-
#### Updates 2017-03-04 Regarding CSS handled by Webpack
|
94
|
-
* See article [Best practices for CSS and CSS Modules using Webpack](https://forum.shakacode.com/t/best-practices-for-css-and-css-modules-using-webpack/799).
|
@@ -15,7 +15,7 @@ side rendering, except for the key `serverSide` based on whether or not you are
|
|
15
15
|
|
16
16
|
While you could manually configure your Rails code to pass the "`railsContext` information" with
|
17
17
|
the rest of your "props", the `railsContext` is a convenience because it's passed consistently to
|
18
|
-
all invocations of
|
18
|
+
all invocations of Render-Functions.
|
19
19
|
|
20
20
|
For example, suppose you create a "render-function" called MyAppComponent.
|
21
21
|
|
@@ -1,9 +1,31 @@
|
|
1
1
|
# RSpec Configuration
|
2
2
|
_Click [here for minitest](./minitest-configuration.md)_
|
3
3
|
|
4
|
+
# If your webpack configurations correspond to rails/webpacker's default setup
|
5
|
+
If you're able to configure your webpack configuration to be run by having your webpack configuration
|
6
|
+
returned by the files in `/config/webpack`, then you have 2 options to ensure that your files are
|
7
|
+
compiled by webpack before running tests and during production deployment:
|
8
|
+
|
9
|
+
1. **Use rails/webpacker's compile option**: Configure your `config/webpacker.yml` so that `compile: true` is for `test` and `production`
|
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).
|
13
|
+
|
14
|
+
```yml
|
15
|
+
config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/webpack"
|
16
|
+
```
|
17
|
+
|
18
|
+
Which should you use? If you're already using the `rails/webpacker` way to configure webpack, then
|
19
|
+
you can keep things simple and use the `rails/webpacker` options.
|
20
|
+
|
21
|
+
# Checking for stale assets using React on Rails
|
22
|
+
|
4
23
|
Because you will probably want to run RSpec tests that rely on compiled webpack assets (typically, your integration/feature specs where `js: true`), you will want to ensure you don't accidentally run tests on missing or stale webpack assets. If you did use stale Webpack assets, you will get invalid test results as your tests do not use the very latest JavaScript code.
|
5
24
|
|
6
|
-
|
25
|
+
As mentioned above, you can configure `compile: true` in `config/webpacker.yml` _if_ you've got configuration for
|
26
|
+
your webpack in the standard `rails/webpacker` spot of `config/webpack/<NODE_ENV>.js`
|
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`.
|
7
29
|
|
8
30
|
```ruby
|
9
31
|
RSpec.configure do |config|
|
@@ -22,40 +44,29 @@ If you are using Webpack to build CSS assets, you should do something like this
|
|
22
44
|
```
|
23
45
|
|
24
46
|
Please take note of the following:
|
25
|
-
- 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.
|
26
48
|
|
27
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`
|
28
50
|
|
29
|
-
- 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.
|
30
52
|
|
31
|
-
- By default, the webpack processes look
|
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.
|
32
54
|
|
33
55
|
The following `config/react_on_rails.rb` settings **must** match your setup:
|
34
56
|
```ruby
|
35
|
-
# Directory where your generated assets go. All generated assets must go to the same directory.
|
36
|
-
# Configure this in your webpack config files. This relative to your Rails root directory.
|
37
|
-
# We recommend having different generated assets dirs per Rails env.
|
38
|
-
config.generated_assets_dir = File.join(%w[public webpack], Rails.env)
|
39
|
-
|
40
57
|
# Define the files we need to check for webpack compilation when running tests.
|
41
|
-
# Generally, the manifest.json is good enough for this check if using webpacker
|
42
58
|
config.webpack_generated_files = %w( manifest.json )
|
43
59
|
|
44
60
|
# OR if you're not hashing the server-bundle.js, then you should include your server-bundle.js in the list.
|
45
61
|
# config.webpack_generated_files = %w( server-bundle.js manifest.json )
|
46
62
|
|
47
|
-
# OR if you're not using webpacker, your setup might look like.
|
48
|
-
# config.webpack_generated_files = %w( client-bundle.js server-bundle.js )
|
49
|
-
|
50
63
|
# If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
|
51
64
|
# with rspec then this controls what yarn command is run
|
52
65
|
# to automatically refresh your webpack assets on every test run.
|
53
66
|
config.build_test_command = "yarn run build:test"
|
54
67
|
```
|
55
68
|
|
56
|
-
If you want to speed up the re-compiling process so you don't wait to run your tests to build the files, you can run your test compilation with the "watch" flags.
|
57
|
-
|
58
|
-
[spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy) contains examples of how to set the proc files for this purpose.
|
69
|
+
If you want to speed up the re-compiling process so you don't wait to run your tests to build the files, you can run your test compilation with the "watch" flags. For example, `yarn run build:test --watch`
|
59
70
|
|
60
71
|
![2016-01-27_02-36-43](https://cloud.githubusercontent.com/assets/1118459/12611951/7c56d070-c4a4-11e5-8a80-9615f99960d9.png)
|
61
72
|
|