react-rails 2.6.1 → 2.7.0.rc.0

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: 3ca305d1d6e644b9a6626812de984d9f7df96d230c6d7544906228e5d266a008
4
- data.tar.gz: 0adaef4fa3452878dcb2d113f7e383333c297dd4fb245ce669fd0616c2bf4060
3
+ metadata.gz: 34e2141d97fafdc9f052046cca1b328162c2e629c5a766ee446280db9ce2ca76
4
+ data.tar.gz: 42f9d30392a3c215f2e9e97cd837437ad5051b9de694b557ff73a508143c0204
5
5
  SHA512:
6
- metadata.gz: 9d0584ef2799b453aa22f965594dde71dc3f4895ca0747108bd96b99494df68c2603b27f7146a93aed9c8ef02f632142d408c457beb4eaf7932a714f2ee6ab75
7
- data.tar.gz: 400040ce79b9f881197e5bc9f60378427972d3e75804e8683f226eefe8e0af76e71737c236796daf27d977ebfa81d528047b7e5949744f5371e74b118c3c2977
6
+ metadata.gz: 8da70ab5914716f3f5679aaaa2881085232537b35ab76fa1a0a8c3f540738730e55820f3e94ff8d91dd53005395c7b307ca6ad152fd3f9b5b6fdf823b400f0d1
7
+ data.tar.gz: 9dab395f855aae1468910bc91dd8c8edbd23fd5e508ade3623f5b71a82c88d81abe9c60aa1b8516241bbe823f651f215d94edb527b522c07e0b79193d9a1cba3
data/CHANGELOG.md CHANGED
@@ -1,12 +1,53 @@
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
+
5
+ [#1209 2.7 Release Discussion](https://github.com/reactjs/react-rails/issues/1209)
6
+
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.0.rc.0] - TBD
4
13
 
5
14
  #### New Features
15
+ - Camelizes keys with primitive values, in addition to hashes #946
16
+ - Expose alternative implementations for `ReactUJS.getConstructor` #1050
17
+ - Include turbolinks in dev and update webdrivers #1174
18
+ - Add support for multiple `require.context` with addition of `useContexts` #1144
19
+ - Update dependencies
20
+
21
+ #### Update dependencies
22
+ - react to 17.0.2 #1218
23
+ - webpack to 5.74.0
24
+ - webpack-dev-server to 2.11.5 #1150
25
+ - Node-sass to 7.0.0 #1158
26
+ - url-parse to 1.5.10 #1167
27
+ - minimist to 1.2.6 #1169
28
+ - async to 2.6.4 #1178
29
+ - rack to 2.2.3.1 #1182
30
+ - eventsource to 1.1.1 #1183
31
+ - rails-html-sanitizer to 1.4.3 #1188
32
+ - tzinfo to 1.2.10 #1190
6
33
 
7
34
  #### Deprecation
8
35
 
9
36
  #### Bug Fixes
37
+ - Fix installation crash caused by absolute path for `source_entry_path` in default `config/webpacker.yml` coming from `shakapacker` version 6.x - #1216
38
+
39
+ ## 2.6.2
40
+
41
+ #### New Features
42
+
43
+ - React 16.14
44
+ - Support for ShakaPacker
45
+ - Preparation for React 18 #1151
46
+
47
+ #### Bug Fixes
48
+
49
+ - URI.open instead of open #1099
50
+ - No longer unmount components on Turbolinks navigation #1135
10
51
 
11
52
  ## 2.6.1
12
53
 
@@ -112,6 +153,7 @@
112
153
  - Call ReactDOM.render() when react_component :prerender option is falsy, instead of ReactDOM.hydrate() #844, #842
113
154
 
114
155
  ## 2.4.2
156
+
115
157
  #### Bug Fixes
116
158
 
117
159
  - ReactDOM.hydrate() may not be defined for everyone, it will now use hydrate if it is defined or fallback to render #832
@@ -224,7 +266,6 @@
224
266
  // = require components
225
267
  ```
226
268
 
227
-
228
269
  #### New Features
229
270
 
230
271
  - Webpacker support:
@@ -471,14 +512,14 @@
471
512
 
472
513
  - Changed server rendering configuration names #253
473
514
 
474
- | Old | New |
475
- | ---- | ---- |
476
- | `config.react.timeout` | `config.react.server_renderer_timeout` |
477
- | `config.react.max_renderers` | `config.react.server_renderer_pool_size` |
478
- | `config.react.react_js` | `config.react.server_renderer_options[:files]` |
479
- | `config.react.component_filenames` | `config.react.server_renderer_options[:files]` |
480
- | `config.react.replay_console` | `config.react.server_renderer_options[:replay_console]` |
481
- | (none) | `config.react.server_renderer` |
515
+ | Old | New |
516
+ | ---------------------------------- | ------------------------------------------------------- |
517
+ | `config.react.timeout` | `config.react.server_renderer_timeout` |
518
+ | `config.react.max_renderers` | `config.react.server_renderer_pool_size` |
519
+ | `config.react.react_js` | `config.react.server_renderer_options[:files]` |
520
+ | `config.react.component_filenames` | `config.react.server_renderer_options[:files]` |
521
+ | `config.react.replay_console` | `config.react.server_renderer_options[:replay_console]` |
522
+ | (none) | `config.react.server_renderer` |
482
523
 
483
524
  - JSX is transformed by Babel, not JSTransform #295
484
525
 
@@ -504,7 +545,6 @@
504
545
  - Add `tilt` dependency #248
505
546
  - Default prerender pool size to 1 #302
506
547
 
507
-
508
548
  ## 1.0.0 (April 7, 2015)
509
549
 
510
550
  #### New Features
@@ -514,3 +554,6 @@
514
554
  - View helper and UJS for mounting components
515
555
  - Server rendering with `prerender: true`
516
556
  - Transform `.jsx` in the asset pipeline
557
+
558
+ [Unreleased]: https://github.com/reactjs/react-rails/compare/v2.7.0...master
559
+ [2.7.0]: https://github.com/shakacode/shakapacker/compare/v2.6.2...v2.7.0
data/README.md CHANGED
@@ -2,24 +2,28 @@
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)[![Maintainers Wanted](https://img.shields.io/badge/Maintainers-Wanted-red.svg?style=flat-square)]()
7
6
 
8
7
 
9
8
  React-Rails is a flexible tool to use [React](http://facebook.github.io/react/) with Rails. The benefits:
10
9
  * Automatically renders React server-side and client-side
11
- * Supports Webpacker 4.x, 3.x, 2.x, 1.1+
10
+ * Supports Webpacker 4.x, 3.x, 2.x, 1.1+ or Shakapacker v6+
12
11
  * Supports Sprockets 4.x, 3.x, 2.x
13
12
  * 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
13
 
15
- A source code example utilizing React-Rails: https://github.com/BookOfGreg/react-rails-example-app
14
+ ## Resouces
15
+ * [Click to join **React + Rails Slack**](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE). Then join the channel `#react-rails`.
16
+ * If you need help upgrading `react-rails`, `webpacker`, or JS packages, contact [justin@shakacode.com](mailto:justin@shakacode.com). The [ShakaCode.com](https://www.shakacode.com) team is helping to maintain this Ruby gem. Check out [this discussion](https://github.com/reactjs/react-rails/discussions/1200).
17
+ * If you are upgrading, you might consider migrating to the [react_on_rails](https://github.com/shakacode/react_on_rails) gem.
18
+ * Source code example utilizing React-Rails: https://github.com/BookOfGreg/react-rails-example-app
16
19
 
17
20
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
18
21
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
19
22
  ## Contents
20
23
 
21
- - [Get started with Webpacker](#get-started-with-webpacker)
24
+ - [Get started with Shakapacker](#get-started-with-shakapacker)
22
25
  - [File naming](#file-naming)
26
+ - [Typescript support](#typescript-support)
23
27
  - [Use with Asset Pipeline](#use-with-asset-pipeline)
24
28
  - [Custom JSX Transformer](#custom-jsx-transformer)
25
29
  - [React.js versions](#reactjs-versions)
@@ -55,78 +59,95 @@ https://github.com/reactjs/React-Rails/wiki
55
59
  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
60
 
57
61
 
58
- ## Get started with Webpacker
62
+ ## Get started with Shakapacker
59
63
 
60
- [Alternatively, get started with Sprockets](#use-with-asset-pipeline)
64
+ _Alternatively, get started with [Sprockets](#use-with-asset-pipeline)_
61
65
 
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:
66
+ #### 1) Create a new Rails app:
67
+ Prevent installing default javascript dependencies by using `--skip-javascript` option:
63
68
 
64
- ##### 1) Create a new Rails app:
65
- ```
66
- $ rails new my-app
69
+ ```bash
70
+ $ rails new my-app --skip-javascript
67
71
  $ cd my-app
68
72
  ```
69
73
 
70
- ##### 2) Add `webpacker` and `react-rails` to your gemfile:
71
- ```ruby
72
- gem 'webpacker'
73
- gem 'react-rails'
74
+ #### 2) Install `shakapacker`:
75
+ ```bash
76
+ $ bundle add shakapacker --strict
77
+ $ rails webpacker:install
74
78
  ```
75
79
 
76
- ##### 3) Now run the installers:
77
-
78
- ###### Rails 6.x:
79
- ```
80
- $ bundle install
81
- $ rails webpacker:install
82
- $ rails webpacker:install:react
83
- $ rails generate react:install
80
+ #### 3) Install `react` and some other required npm packages:
81
+ ```bash
82
+ $ yarn add react react-dom @babel/preset-react prop-types \
83
+ css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
84
84
  ```
85
- Note: For Rails 6, You don't need to add `javascript_pack_tag` as in Step 4. Since its already added by default.
86
85
 
87
- ###### Rails 5.x:
86
+ Also update the Babel configuration in the `package.json` file:
87
+
88
+ ```diff
89
+ "babel": {
90
+ "presets": [
91
+ - "./node_modules/shakapacker/package/babel/preset.js"
92
+ + "./node_modules/shakapacker/package/babel/preset.js",
93
+ + "@babel/preset-react"
94
+ ]
95
+ },
88
96
  ```
89
- $ bundle install
90
- $ rails webpacker:install # OR (on rails version < 5.0) rake webpacker:install
91
- $ rails webpacker:install:react # OR (on rails version < 5.0) rake webpacker:install:react
97
+
98
+ #### 4) Install `react-rails`:
99
+ ```bash
100
+ $ bundle add 'react-rails' --strict
92
101
  $ rails generate react:install
93
102
  ```
103
+
94
104
  This gives you:
95
105
 
96
106
  - `app/javascript/components/` directory for your React components
97
107
  - [`ReactRailsUJS`](#ujs) setup in `app/javascript/packs/application.js`
98
108
  - `app/javascript/packs/server_rendering.js` for [server-side rendering](#server-side-rendering)
99
109
 
100
- ##### 4) Link the JavaScript pack in Rails view using `javascript_pack_tag` [helper](https://github.com/rails/webpacker#usage):
101
- ```erb
102
- <!-- application.html.erb in Head tag below turbolinks -->
103
- <%= javascript_pack_tag 'application' %>
104
- ```
105
-
106
- ##### 5) Generate your first component:
107
- ```
110
+ #### 5) Generate your first component:
111
+ ```bash
108
112
  $ rails g react:component HelloWorld greeting:string
109
113
  ```
110
114
 
111
- ##### 6) You can also generate your component in a subdirectory:
112
- ```
115
+ You can also generate your component in a subdirectory:
116
+
117
+ ```bash
113
118
  $ rails g react:component my_subdirectory/HelloWorld greeting:string
114
119
  ```
120
+
115
121
  Note: Your component is added to `app/javascript/components/` by default.
116
122
 
123
+ Note: If your component is in a subdirectory you will append the directory path to your erb component call.
124
+
125
+ Example:
126
+ ```erb
127
+ <%= react_component("my_subdirectory/HelloWorld", { greeting: "Hello from react-rails." }) %>
128
+ ```
117
129
 
118
- ##### 7) [Render it in a Rails view](#view-helper):
130
+ #### 6) [Render it in a Rails view](#view-helper):
119
131
 
120
132
  ```erb
121
133
  <!-- erb: paste this in view -->
122
134
  <%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>
123
135
  ```
124
136
 
125
- ##### 8) Lets Start the app:
126
- ```
137
+ ##### 7) Lets Start the app:
138
+ ```bash
127
139
  $ rails s
128
140
  ```
129
- output: greeting: Hello from react-rails", inspect webpage in your browser too see change in tag props.
141
+ Output: greeting: Hello from react-rails", inspect webpage in your browser to see the change in tag props.
142
+
143
+ ##### 7) Run dev server (optional)
144
+ In order to run dev server with HMR feature you need to parallely run:
145
+
146
+ ```bash
147
+ $ ./bin/webpacker-dev-server
148
+ ```
149
+
150
+ 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.
130
151
 
131
152
  ### Component name
132
153
 
@@ -152,6 +173,24 @@ ReactRailsUJS.useContext(myCustomContext)
152
173
 
153
174
  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).
154
175
 
176
+ In some cases, having multiple `require.context` entries may be desired. Examples of this include:
177
+
178
+ - 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.
179
+ - 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:
180
+
181
+ ```js
182
+ // server_rendering.js
183
+ var homepageRequireContext = require.context('homepage', true);
184
+ var searchRequireContext = require.context('search', true);
185
+ var checkoutRequireContext = require.context('checkout', true);
186
+
187
+ var ReactRailsUJS = require('react_ujs');
188
+ ReactRailsUJS.useContexts([
189
+ homepageRequireContext,
190
+ searchRequireContext,
191
+ checkoutRequireContext
192
+ ]);
193
+ ```
155
194
  ### File naming
156
195
 
157
196
  React-Rails supports plenty of file extensions such as: .js, .jsx.js, .js.jsx, .es6.js, .coffee, etcetera!
@@ -166,21 +205,62 @@ Component File Name | `react_component` call
166
205
 
167
206
  ### Typescript support
168
207
 
169
- If you want to use React-Rails with Typescript, simply run the installer and add @types:
208
+ ```bash
209
+ yarn add typescript @babel/preset-typescript
210
+ ```
211
+
212
+ Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
213
+
214
+ ```bash
215
+ yarn add fork-ts-checker-webpack-plugin
216
+ ```
217
+
218
+ Add `tsconfig.json` with the following content:
219
+
220
+ ```json
221
+ {
222
+ "compilerOptions": {
223
+ "declaration": false,
224
+ "emitDecoratorMetadata": true,
225
+ "experimentalDecorators": true,
226
+ "lib": ["es6", "dom"],
227
+ "module": "es6",
228
+ "moduleResolution": "node",
229
+ "sourceMap": true,
230
+ "target": "es5",
231
+ "jsx": "react",
232
+ "noEmit": true
233
+ },
234
+ "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
235
+ "compileOnSave": false
236
+ }
170
237
  ```
171
- $ bundle exec rails webpacker:install:typescript
172
- $ yarn add @types/react @types/react-dom
238
+
239
+ Then modify the webpack config to use it as a plugin:
240
+
241
+ ```js
242
+ // config/webpack/webpack.config.js
243
+ const { webpackConfig, merge } = require("shakapacker");
244
+ const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
245
+
246
+ module.exports = merge(webpackConfig, {
247
+ plugins: [new ForkTSCheckerWebpackPlugin()],
248
+ });
173
249
  ```
174
250
 
175
- Doing this will allow React-Rails to support the .tsx extension.
251
+ 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:
252
+
253
+ ```ruby
254
+ config.react.server_renderer_extensions = ["jsx", "js", "tsx", "ts"]
255
+ ```
176
256
 
177
257
  ### Test component
178
258
 
179
259
  You can use `assert_react_component` to test component render:
180
260
 
181
- app/views/welcome/index.html.erb
182
-
183
261
  ```erb
262
+ <!-- app/views/welcome/index.html.erb -->
263
+
184
264
  <%= react_component("HelloWorld", { greeting: "Hello from react-rails.", info: { name: "react-rails" } }, { class: "hello-world" }) %>
185
265
  ```
186
266
 
@@ -403,13 +483,27 @@ delete window.Turbolinks;
403
483
 
404
484
  ### `getConstructor`
405
485
 
406
- Components are loaded with `ReactRailsUJS.getConstructor(className)`. This function has two built-in implementations:
486
+ Components are loaded with `ReactRailsUJS.getConstructor(className)`. This function has two default implementations, depending on if you're using the asset pipeline or Shakapacker:
407
487
 
408
- - On the asset pipeline, it looks up `className` in the global namespace.
409
- - On Webpacker, it `require`s files and accesses named exports, as described in [Get started with Webpacker](#get-started-with-webpacker).
488
+ - On the asset pipeline, it looks up `className` in the global namespace (`ReactUJS.constructorFromGlobal`).
489
+ - 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`).
410
490
 
411
491
  You can override this function to customize the mapping of name-to-constructor. [Server-side rendering](#server-side-rendering) also uses this function.
412
492
 
493
+ For example, the fallback behavior of
494
+ `ReactUJS.constructorFromRequireContextWithGlobalFallback` can sometimes make
495
+ server-side rendering errors hard to debug as it will swallow the original error
496
+ (more info
497
+ [here](https://github.com/reactjs/react-rails/issues/264#issuecomment-552326663)).
498
+ `ReactUJS.constructorFromRequireContext` is provided for this reason. You can
499
+ use it like so:
500
+
501
+ ```js
502
+ // Replaces calls to `ReactUJS.useContext`
503
+ ReactUJS.getConstructor = ReactUJS.constructorFromRequireContext(require.context('components', true));
504
+ ```
505
+
506
+
413
507
  ## Server-Side Rendering
414
508
 
415
509
  You can render React components inside your Rails server with `prerender: true`:
@@ -621,6 +715,12 @@ You can also specify this option in `react_component`:
621
715
  <%= react_component('HelloMessage', {name: 'John'}, {camelize_props: true}) %>
622
716
  ```
623
717
 
718
+ ### Changing Component Templates
719
+
720
+ To make simple changes to Component templates, copy the respective template file to your Rails project at `lib/templates/react/component/template_filename`.
721
+
722
+ 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.
723
+
624
724
  ## Upgrading
625
725
 
626
726
  ### 2.3 to 2.4
@@ -685,16 +785,16 @@ Hot Module Replacement is [possible with this gem](https://stackoverflow.com/a/5
685
785
 
686
786
  Sample repo that shows HMR working with `react-rails`: [https://github.com/edelgado/react-rails-hmr](https://github.com/edelgado/react-rails-hmr)
687
787
 
688
- One caveat is that currently you [cannot Server-Side Render along with HMR](https://github.com/reactjs/react-rails/issues/925#issuecomment-415469572).
788
+ One caveat is that currently you [cannot Server-Side Render along with HMR](https://github.com/reactjs/react-rails/issues/925#issuecomment-415469572).
689
789
 
690
790
  ## Related Projects
691
791
 
692
792
  - [webpacker-react](https://github.com/renchap/webpacker-react): Integration of React with Rails utilizing Webpack with Hot Module Replacement (HMR).
693
- - [The Free React on Rails Course](https://learnetto.com/users/hrishio/courses/the-free-react-on-rails-5-course) A free video course which teaches the basics of React and how to get started using it in Rails with `react-rails`.
793
+ - [The React on Rails Course](https://learnetto.com/users/hrishio/courses/the-free-react-on-rails-5-course) A video course which teaches the basics of React and how to get started using it in Rails with `react-rails`.
694
794
  - [react\_on\_rails](https://github.com/shakacode/react_on_rails): Integration of React with Rails utilizing Webpack, Redux, React-Router.
695
795
  - [react-rails-hot-loader](https://github.com/rmosolgo/react-rails-hot-loader) Simple live-reloader for `react-rails`.
696
796
  - [react-rails-benchmark_renderer](https://github.com/pboling/react-rails-benchmark_renderer) adds performance instrumentation to server rendering.
697
- - [Ruby Hyperloop](http://ruby-hyperloop.org/): Use Ruby to build reactive user interfaces with React.
797
+ - [Ruby Hyperstack](https://hyperstack.org/): Use Ruby to build reactive user interfaces with React.
698
798
 
699
799
  ## Contributing
700
800