react-rails 2.6.2 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96a680594edd90c4c4b7956ca981b98c39c0be650c2570bcac0640fe4641ba0f
4
- data.tar.gz: 0c8153bde6bfaaddb6c86c8450729d9ac2aa336ea976dbc959fe94c0a66907a1
3
+ metadata.gz: 2fb970443961197a3c1c24fda2fa12f4072dd4f596d7254adac40d11ac13a9cb
4
+ data.tar.gz: d85049088bb5d4166894641cca4bd4599f967fe43f504e501a3ce7232f38faff
5
5
  SHA512:
6
- metadata.gz: aac97afcf29aa71177ddbac2e11f117cb64fb524cd435701c3c6f70fb81775f870becfd1ba04f5167830b72808b8e8afbcf18c802477335c28d78ee5b0ec6d2e
7
- data.tar.gz: 8988ff16c6619dc0ee8df11b060b49107e63524641b90ba497199d1cbbff96b696be61b3b8eb2237ab6b121e636352c07f5a7066426ff5c8c80608d85a7c32fb
6
+ metadata.gz: 608ab0b73247bc4310f6a78f525e73ecaeb89d1bdb1b2be5170b6d5054e356e9bc6abaa5f54bf2b403fa727b1828542ce347e922fba49bbe7e0a91d938728e23
7
+ data.tar.gz: 35c53e0d3e63a16ad7c505a83e7d77255f87715e4ebca890e3103eb4e14101993f3d25c0b1bea56eaff3071e0e83d397292036bc4162169cecfee7e5843ac84e
data/CHANGELOG.md CHANGED
@@ -1,12 +1,31 @@
1
1
  # react-rails
2
2
 
3
- #### Breaking Changes
3
+ If you need help upgrading `react-rails`, `webpacker` to `shakapacker`, or JS packages, contact justin@shakacode.com. The ShakaCode.com team is helping to maintain this Ruby gem. You also might want to consider the [react_on_rails](https://github.com/shakacode/react_on_rails) gem.
4
4
 
5
- #### New Features
5
+ [#1209 2.7 Release Discussion](https://github.com/reactjs/react-rails/issues/1209)
6
6
 
7
- #### Deprecation
7
+ ## Unreleased
8
+ Changes since last non-beta release.
9
+
10
+ _Please add entries here for your pull requests that are not yet released._
11
+
12
+ ## [2.7.1] - 2023-05-19
8
13
 
9
14
  #### Bug Fixes
15
+ - Fix ReactDomClient initialization error during SSR. #1278
16
+
17
+ ## [2.7.0] - 2023-05-06
18
+
19
+ #### New Features
20
+ - Camelizes keys with primitive values, in addition to hashes #946
21
+ - Expose alternative implementations for `ReactUJS.getConstructor` #1050
22
+ - Include turbolinks in dev and update webdrivers #1174
23
+ - Add support for multiple `require.context` with addition of `useContexts` #1144
24
+ - Update many dependencies
25
+
26
+ #### Bug Fixes
27
+ - Fix installation crash caused by absolute path for `source_entry_path` in default `config/webpacker.yml` coming from `shakapacker` version 6.x - #1216
28
+ - Fix warning for loading `react-dom` in React 18 - #1269
10
29
 
11
30
  ## 2.6.2
12
31
 
@@ -526,3 +545,7 @@
526
545
  - View helper and UJS for mounting components
527
546
  - Server rendering with `prerender: true`
528
547
  - Transform `.jsx` in the asset pipeline
548
+
549
+ [Unreleased]: https://github.com/reactjs/react-rails/compare/v2.7.1...master
550
+ [2.7.1]: https://github.com/shakacode/shakapacker/compare/v2.7.0...v2.7.1
551
+ [2.7.0]: https://github.com/shakacode/shakapacker/compare/v2.6.2...v2.7.0
data/README.md CHANGED
@@ -2,26 +2,51 @@
2
2
 
3
3
  [![Gem](https://img.shields.io/gem/v/react-rails.svg?style=flat-square)](http://rubygems.org/gems/react-rails)
4
4
  [![npm](https://img.shields.io/npm/v/react_ujs.svg?style=flat-square)](https://www.npmjs.com/package/react_ujs)
5
- [![Build Status](https://img.shields.io/travis/reactjs/react-rails/master.svg?style=flat-square)](https://travis-ci.org/reactjs/react-rails)
6
- [![Maintainers Wanted](https://img.shields.io/badge/Maintainers-Wanted-red.svg?style=flat-square)]()
5
+ [![Ruby](https://github.com/reactjs/react-rails/actions/workflows/ruby.yml/badge.svg)](https://github.com/reactjs/react-rails/actions/workflows/ruby.yml)
7
6
 
7
+ ## News
8
+ v2.7.0 is released. Please try it out and report any issues. We'll try to address any critical issues ASAP. We're going shortly release 3.0 which improves support for SSR, but it's a breaking change because it requires updating dependencies, such as going from `webpacker` to `shakapacker`.
8
9
 
10
+ ## Summary
9
11
  React-Rails is a flexible tool to use [React](http://facebook.github.io/react/) with Rails. The benefits:
10
12
  * Automatically renders React server-side and client-side
11
- * Supports Webpacker 4.x, 3.x, 2.x, 1.1+
13
+ * Supports Webpacker 4.x, 3.x, 2.x, 1.1+ or Shakapacker v6+
12
14
  * Supports Sprockets 4.x, 3.x, 2.x
13
15
  * Lets you use [JSX](http://facebook.github.io/react/docs/jsx-in-depth.html), [ES6](http://es6-features.org/), [TypeScript](https://www.typescriptlang.org/), [CoffeeScript](http://coffeescript.org/)
14
16
 
15
- A source code example utilizing React-Rails: https://github.com/BookOfGreg/react-rails-example-app
17
+ ---
18
+ ## ShakaCode Support
19
+
20
+ [ShakaCode](https://www.shakacode.com) offers support for upgrading this gem, and related gems such as Webpacker and using Shakapacker. If interested, contact Justin Gordon, [justin@shakacode.com](mailto:justin@shakacode.com). We're also [hiring](https://jobs.lever.co/shakacode/3bdbfdb3-4495-4611-a279-01dddb351abe)!
21
+
22
+ Here's a testimonial of how ShakaCode can help, from [Florian Gößler](https://github.com/FGoessler) of [Blinkist](https://www.blinkist.com/), January 2, 2023:
23
+ > Hey Justin 👋
24
+ >
25
+ > I just wanted to let you know that we today shipped the webpacker to shakapacker upgrades and it all seems to be running smoothly! Thanks again for all your support and your teams work! 😍
26
+ >
27
+ > On top of your work, it was now also very easy for me to upgrade Tailwind and include our external node_module based web component library which we were using for our other (more modern) apps already. That work is going to be shipped later this week though as we are polishing the last bits of it. 😉
28
+ >
29
+ > Have a great 2023 and maybe we get to work together again later in the year! 🙌
30
+
31
+ Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=date_DESC#review-2118154). Here's [another review of a Shakapacker migration that led to more work](https://clutch.co/profile/shakacode#reviews?sort_by=date_DESC#review-2096078).
32
+
33
+ ## Resouces
34
+ * [Click to join **React + Rails Slack**](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE). Then join the channel `#react-rails`.
35
+ * If you are upgrading, you might consider migrating to the [react_on_rails](https://github.com/shakacode/react_on_rails) gem.
36
+ * Source code example utilizing React-Rails: https://github.com/BookOfGreg/react-rails-example-app
16
37
 
17
38
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
18
39
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
19
40
  ## Contents
20
41
 
21
- - [Get started with Webpacker](#get-started-with-webpacker)
42
+ - [Get started with Shakapacker](#get-started-with-shakapacker)
43
+ - [Component name](#component-name)
22
44
  - [File naming](#file-naming)
45
+ - [Typescript support](#typescript-support)
46
+ - [Test component](#test-component)
23
47
  - [Use with Asset Pipeline](#use-with-asset-pipeline)
24
48
  - [Custom JSX Transformer](#custom-jsx-transformer)
49
+ - [Transform Plugin Options](#transform-plugin-options)
25
50
  - [React.js versions](#reactjs-versions)
26
51
  - [View Helper](#view-helper)
27
52
  - [Custom View Helper](#custom-view-helper)
@@ -37,9 +62,11 @@ A source code example utilizing React-Rails: https://github.com/BookOfGreg/react
37
62
  - [Component Generator](#component-generator)
38
63
  - [Use with JBuilder](#use-with-jbuilder)
39
64
  - [Camelize Props](#camelize-props)
65
+ - [Changing Component Templates](#changing-component-templates)
40
66
  - [Upgrading](#upgrading)
41
67
  - [2.3 to 2.4](#23-to-24)
42
68
  - [Common Errors](#common-errors)
69
+ - [Getting warning for `Can't resolve 'react-dom/client'` in React < 18](#getting-warning-for-cant-resolve-react-domclient-in-react--18)
43
70
  - [During installation](#during-installation)
44
71
  - [Undefined Set](#undefined-set)
45
72
  - [Using TheRubyRacer](#using-therubyracer)
@@ -55,31 +82,45 @@ https://github.com/reactjs/React-Rails/wiki
55
82
  The Wiki page features a significant amount of additional information about React-Rails which includes instructional articles and answers to the most frequently asked questions.
56
83
 
57
84
 
58
- ## Get started with Webpacker
85
+ ## Get started with Shakapacker
59
86
 
60
- [Alternatively, get started with Sprockets](#use-with-asset-pipeline)
87
+ _Alternatively, get started with [Sprockets](#use-with-asset-pipeline)_
61
88
 
62
- [Webpacker](https://github.com/rails/webpacker) provides modern JS tooling for Rails. Here are the listed steps for integrating Webpacker and Rails-React with Rails:
89
+ 1. Create a new Rails app:
90
+ Prevent installing default javascript dependencies by using `--skip-javascript` option:
63
91
 
64
- ##### 1) Create a new Rails app:
65
- ```
66
- $ rails new my-app
92
+ ```bash
93
+ $ rails new my-app --skip-javascript
67
94
  $ cd my-app
68
95
  ```
69
96
 
70
- ##### 2) Add `react-rails` to your Gemfile:
71
- ```ruby
72
- gem 'react-rails'
97
+ 2. Install `shakapacker`:
98
+ ```bash
99
+ $ bundle add shakapacker --strict
100
+ $ rails webpacker:install
73
101
  ```
74
- Note: On rails versions < 6.0, You need to add `gem 'webpacker'` to your Gemfile in step 2 above.
75
102
 
76
- ##### 3) Now run the installers:
103
+ 3. Install `react` and some other required npm packages:
104
+ ```bash
105
+ $ yarn add react react-dom @babel/preset-react prop-types \
106
+ css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
107
+ ```
77
108
 
78
- ###### Rails 6.x and 5.x:
109
+ Also update the Babel configuration in the `package.json` file:
110
+
111
+ ```diff
112
+ "babel": {
113
+ "presets": [
114
+ - "./node_modules/shakapacker/package/babel/preset.js"
115
+ + "./node_modules/shakapacker/package/babel/preset.js",
116
+ + "@babel/preset-react"
117
+ ]
118
+ },
79
119
  ```
80
- $ bundle install
81
- $ rails webpacker:install # OR (on rails version < 5.0) rake webpacker:install
82
- $ rails webpacker:install:react # OR (on rails version < 5.0) rake webpacker:install:react
120
+
121
+ 4. Install `react-rails`:
122
+ ```bash
123
+ $ bundle add 'react-rails' --strict
83
124
  $ rails generate react:install
84
125
  ```
85
126
 
@@ -89,42 +130,47 @@ This gives you:
89
130
  - [`ReactRailsUJS`](#ujs) setup in `app/javascript/packs/application.js`
90
131
  - `app/javascript/packs/server_rendering.js` for [server-side rendering](#server-side-rendering)
91
132
 
92
- Note: On rails versions < 6.0, link the JavaScript pack in Rails view using `javascript_pack_tag` [helper](https://github.com/rails/webpacker#usage):
93
- ```erb
94
- <!-- application.html.erb in Head tag below turbolinks -->
95
- <%= javascript_pack_tag 'application' %>
96
- ```
97
-
98
- ##### 4) Generate your first component:
99
- ```
133
+ 5. Generate your first component:
134
+ ```bash
100
135
  $ rails g react:component HelloWorld greeting:string
101
136
  ```
102
137
 
103
- ##### 5) You can also generate your component in a subdirectory:
104
- ```
138
+ You can also generate your component in a subdirectory:
139
+
140
+ ```bash
105
141
  $ rails g react:component my_subdirectory/HelloWorld greeting:string
106
142
  ```
143
+
107
144
  Note: Your component is added to `app/javascript/components/` by default.
108
145
 
109
146
  Note: If your component is in a subdirectory you will append the directory path to your erb component call.
110
147
 
111
- Example:
112
- ```
148
+ Example:
149
+ ```erb
113
150
  <%= react_component("my_subdirectory/HelloWorld", { greeting: "Hello from react-rails." }) %>
114
151
  ```
115
152
 
116
- ##### 6) [Render it in a Rails view](#view-helper):
153
+ 6. [Render it in a Rails view](#view-helper):
117
154
 
118
155
  ```erb
119
156
  <!-- erb: paste this in view -->
120
157
  <%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>
121
158
  ```
122
159
 
123
- ##### 7) Lets Start the app:
124
- ```
160
+ 7. Lets Start the app:
161
+ ```bash
125
162
  $ rails s
126
163
  ```
127
- output: greeting: Hello from react-rails", inspect webpage in your browser too see change in tag props.
164
+ Output: greeting: Hello from react-rails", inspect webpage in your browser to see the change in tag props.
165
+
166
+ 8. Run dev server (optional)
167
+ In order to run dev server with HMR feature you need to parallely run:
168
+
169
+ ```bash
170
+ $ ./bin/webpacker-dev-server
171
+ ```
172
+
173
+ Note: On Rails 6 you need to specify `webpack-dev-server` host. To this end, update `config/initializers/content_security_policy.rb` and uncomment relevant lines.
128
174
 
129
175
  ### Component name
130
176
 
@@ -150,6 +196,24 @@ ReactRailsUJS.useContext(myCustomContext)
150
196
 
151
197
  If `require` fails to find your component, [`ReactRailsUJS`](#ujs) falls back to the global namespace, described in [Use with Asset Pipeline](#use-with-asset-pipeline).
152
198
 
199
+ In some cases, having multiple `require.context` entries may be desired. Examples of this include:
200
+
201
+ - Refactoring a typical Rails application into a Rails API with an (eventually) separate Single Page Application (SPA). For this use case, one can add a separate pack in addition to the typical `application` one. React components can be shared between the packs but the new pack can use a minimal Rails view layout, different default styling, etc.
202
+ - In a larger application, you might find it helpful to split your JavaScript by routes/controllers to avoid serving unused components and improve your site performance by keeping bundles smaller. For example, you might have separate bundles for homepage, search, and checkout routes. In that scenario, you can add an array of `require.context` component directory paths via `useContexts` to `server_rendering.js`, to allow for [Server-Side Rendering](#server-side-rendering) across your application:
203
+
204
+ ```js
205
+ // server_rendering.js
206
+ var homepageRequireContext = require.context('homepage', true);
207
+ var searchRequireContext = require.context('search', true);
208
+ var checkoutRequireContext = require.context('checkout', true);
209
+
210
+ var ReactRailsUJS = require('react_ujs');
211
+ ReactRailsUJS.useContexts([
212
+ homepageRequireContext,
213
+ searchRequireContext,
214
+ checkoutRequireContext
215
+ ]);
216
+ ```
153
217
  ### File naming
154
218
 
155
219
  React-Rails supports plenty of file extensions such as: .js, .jsx.js, .js.jsx, .es6.js, .coffee, etcetera!
@@ -164,14 +228,52 @@ Component File Name | `react_component` call
164
228
 
165
229
  ### Typescript support
166
230
 
167
- If you want to use React-Rails with Typescript, simply run the installer and add @types:
231
+ ```bash
232
+ yarn add typescript @babel/preset-typescript
168
233
  ```
169
- $ bundle exec rails webpacker:install:typescript
170
- $ yarn add @types/react @types/react-dom
234
+
235
+ Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
236
+
237
+ ```bash
238
+ yarn add fork-ts-checker-webpack-plugin
171
239
  ```
172
240
 
173
- Doing this will allow React-Rails to support the .tsx extension. Additionally, it is recommended to add `ts` and `tsx` to the `server_renderer_extensions` in your application configuration:
241
+ Add `tsconfig.json` with the following content:
242
+
243
+ ```json
244
+ {
245
+ "compilerOptions": {
246
+ "declaration": false,
247
+ "emitDecoratorMetadata": true,
248
+ "experimentalDecorators": true,
249
+ "lib": ["es6", "dom"],
250
+ "module": "es6",
251
+ "moduleResolution": "node",
252
+ "sourceMap": true,
253
+ "target": "es5",
254
+ "jsx": "react",
255
+ "noEmit": true
256
+ },
257
+ "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
258
+ "compileOnSave": false
259
+ }
174
260
  ```
261
+
262
+ Then modify the webpack config to use it as a plugin:
263
+
264
+ ```js
265
+ // config/webpack/webpack.config.js
266
+ const { webpackConfig, merge } = require("shakapacker");
267
+ const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
268
+
269
+ module.exports = merge(webpackConfig, {
270
+ plugins: [new ForkTSCheckerWebpackPlugin()],
271
+ });
272
+ ```
273
+
274
+ Doing this will allow React-Rails to support the .tsx extension. Additionally, it is recommended to add `ts` and `tsx` to the `server_renderer_extensions` in your application configuration:
275
+
276
+ ```ruby
175
277
  config.react.server_renderer_extensions = ["jsx", "js", "tsx", "ts"]
176
278
  ```
177
279
 
@@ -179,9 +281,9 @@ config.react.server_renderer_extensions = ["jsx", "js", "tsx", "ts"]
179
281
 
180
282
  You can use `assert_react_component` to test component render:
181
283
 
182
- app/views/welcome/index.html.erb
183
-
184
284
  ```erb
285
+ <!-- app/views/welcome/index.html.erb -->
286
+
185
287
  <%= react_component("HelloWorld", { greeting: "Hello from react-rails.", info: { name: "react-rails" } }, { class: "hello-world" }) %>
186
288
  ```
187
289
 
@@ -404,13 +506,27 @@ delete window.Turbolinks;
404
506
 
405
507
  ### `getConstructor`
406
508
 
407
- Components are loaded with `ReactRailsUJS.getConstructor(className)`. This function has two built-in implementations:
509
+ Components are loaded with `ReactRailsUJS.getConstructor(className)`. This function has two default implementations, depending on if you're using the asset pipeline or Shakapacker:
408
510
 
409
- - On the asset pipeline, it looks up `className` in the global namespace.
410
- - On Webpacker, it `require`s files and accesses named exports, as described in [Get started with Webpacker](#get-started-with-webpacker).
511
+ - On the asset pipeline, it looks up `className` in the global namespace (`ReactUJS.constructorFromGlobal`).
512
+ - On Shakapacker, it `require`s files and accesses named exports, as described in [Get started with Shakapacker](#get-started-with-shakapacker), falling back to the global namespace (`ReactUJS.constructorFromRequireContextWithGlobalFallback`).
411
513
 
412
514
  You can override this function to customize the mapping of name-to-constructor. [Server-side rendering](#server-side-rendering) also uses this function.
413
515
 
516
+ For example, the fallback behavior of
517
+ `ReactUJS.constructorFromRequireContextWithGlobalFallback` can sometimes make
518
+ server-side rendering errors hard to debug as it will swallow the original error
519
+ (more info
520
+ [here](https://github.com/reactjs/react-rails/issues/264#issuecomment-552326663)).
521
+ `ReactUJS.constructorFromRequireContext` is provided for this reason. You can
522
+ use it like so:
523
+
524
+ ```js
525
+ // Replaces calls to `ReactUJS.useContext`
526
+ ReactUJS.getConstructor = ReactUJS.constructorFromRequireContext(require.context('components', true));
527
+ ```
528
+
529
+
414
530
  ## Server-Side Rendering
415
531
 
416
532
  You can render React components inside your Rails server with `prerender: true`:
@@ -622,6 +738,12 @@ You can also specify this option in `react_component`:
622
738
  <%= react_component('HelloMessage', {name: 'John'}, {camelize_props: true}) %>
623
739
  ```
624
740
 
741
+ ### Changing Component Templates
742
+
743
+ To make simple changes to Component templates, copy the respective template file to your Rails project at `lib/templates/react/component/template_filename`.
744
+
745
+ For example, to change the [ES6 Component template](https://github.com/reactjs/react-rails/blob/master/lib/generators/templates/component.es6.jsx), copy it to `lib/templates/react/component/component.es6.jsx` and modify it.
746
+
625
747
  ## Upgrading
626
748
 
627
749
  ### 2.3 to 2.4
@@ -643,6 +765,22 @@ For the vast majority of cases this will get you most of the migration:
643
765
  - re-run `bundle exec rails webpacker:install:react` to update npm packages (Webpacker only)
644
766
 
645
767
  ## Common Errors
768
+ ### Getting warning for `Can't resolve 'react-dom/client'` in React < 18
769
+
770
+ You may see a warning like this when building a Webpack bundle using any version of React below 18. This warning can be safely [suppressed](https://webpack.js.org/configuration/other-options/#ignorewarnings) in your Webpack configuration. The following is an example of this suppression in `config/webpack/webpack.config.js`:
771
+
772
+ ```diff
773
+ - const { webpackConfig } = require('shakapacker')
774
+ + const { webpackConfig, merge } = require('shakapacker')
775
+
776
+ +const ignoreWarningsConfig = {
777
+ + ignoreWarnings: [/Module not found: Error: Can't resolve 'react-dom\/client'/],
778
+ +};
779
+
780
+ - module.exports = webpackConfig
781
+ + module.exports = merge({}, webpackConfig, ignoreWarningsConfig)
782
+ ```
783
+
646
784
  ### During installation
647
785
  1) While using installers.(rails webpacker:install:react && rails webpacker:install)
648
786
  Error:
@@ -686,7 +824,7 @@ Hot Module Replacement is [possible with this gem](https://stackoverflow.com/a/5
686
824
 
687
825
  Sample repo that shows HMR working with `react-rails`: [https://github.com/edelgado/react-rails-hmr](https://github.com/edelgado/react-rails-hmr)
688
826
 
689
- One caveat is that currently you [cannot Server-Side Render along with HMR](https://github.com/reactjs/react-rails/issues/925#issuecomment-415469572).
827
+ One caveat is that currently you [cannot Server-Side Render along with HMR](https://github.com/reactjs/react-rails/issues/925#issuecomment-415469572).
690
828
 
691
829
  ## Related Projects
692
830