react-rails 2.6.2 → 2.7.0.rc.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: 62aac1b130be2ccd36d0412fb3078560c38daccdb08dcb88fb85a55f0abe4e45
4
+ data.tar.gz: a047ab347feba6ed50cebab0b08e0b2f2e717f87e05ca9041eb8a47f8671fa06
5
5
  SHA512:
6
- metadata.gz: aac97afcf29aa71177ddbac2e11f117cb64fb524cd435701c3c6f70fb81775f870becfd1ba04f5167830b72808b8e8afbcf18c802477335c28d78ee5b0ec6d2e
7
- data.tar.gz: 8988ff16c6619dc0ee8df11b060b49107e63524641b90ba497199d1cbbff96b696be61b3b8eb2237ab6b121e636352c07f5a7066426ff5c8c80608d85a7c32fb
6
+ metadata.gz: a581433841efb1b1fc11d11c928eb87c5d86e52837ce0076fe75fbdae37f5bf5943b4cdd935a94a1207f463cad7ab36edaa98dc6b6e5ee7897359da1fba07664
7
+ data.tar.gz: ae92fb8ae8d1a84204ef0cc6b61141032479bd6562f3658fdf44efbfda91dea8eac09528a7f3d58af4814050172a831abb87c101175774b1073dadc006933116
data/CHANGELOG.md CHANGED
@@ -1,12 +1,40 @@
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
10
38
 
11
39
  ## 2.6.2
12
40
 
@@ -526,3 +554,6 @@
526
554
  - View helper and UJS for mounting components
527
555
  - Server rendering with `prerender: true`
528
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,31 @@
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.rc.0 is out on [Ruby Gems](https://rubygems.org/gems/react-rails/versions/2.7.0.rc.0) and [NPM](https://www.npmjs.com/package/react_ujs/v/2.7.0-rc.0). Please try it out!
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
+ ## Resouces
18
+ * [Click to join **React + Rails Slack**](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE). Then join the channel `#react-rails`.
19
+ * 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).
20
+ * If you are upgrading, you might consider migrating to the [react_on_rails](https://github.com/shakacode/react_on_rails) gem.
21
+ * Source code example utilizing React-Rails: https://github.com/BookOfGreg/react-rails-example-app
16
22
 
17
23
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
18
24
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
19
25
  ## Contents
20
26
 
21
- - [Get started with Webpacker](#get-started-with-webpacker)
27
+ - [Get started with Shakapacker](#get-started-with-shakapacker)
22
28
  - [File naming](#file-naming)
29
+ - [Typescript support](#typescript-support)
23
30
  - [Use with Asset Pipeline](#use-with-asset-pipeline)
24
31
  - [Custom JSX Transformer](#custom-jsx-transformer)
25
32
  - [React.js versions](#reactjs-versions)
@@ -55,31 +62,45 @@ https://github.com/reactjs/React-Rails/wiki
55
62
  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
63
 
57
64
 
58
- ## Get started with Webpacker
65
+ ## Get started with Shakapacker
59
66
 
60
- [Alternatively, get started with Sprockets](#use-with-asset-pipeline)
67
+ _Alternatively, get started with [Sprockets](#use-with-asset-pipeline)_
61
68
 
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:
69
+ #### 1) Create a new Rails app:
70
+ Prevent installing default javascript dependencies by using `--skip-javascript` option:
63
71
 
64
- ##### 1) Create a new Rails app:
65
- ```
66
- $ rails new my-app
72
+ ```bash
73
+ $ rails new my-app --skip-javascript
67
74
  $ cd my-app
68
75
  ```
69
76
 
70
- ##### 2) Add `react-rails` to your Gemfile:
71
- ```ruby
72
- gem 'react-rails'
77
+ #### 2) Install `shakapacker`:
78
+ ```bash
79
+ $ bundle add shakapacker --strict
80
+ $ rails webpacker:install
73
81
  ```
74
- Note: On rails versions < 6.0, You need to add `gem 'webpacker'` to your Gemfile in step 2 above.
75
82
 
76
- ##### 3) Now run the installers:
83
+ #### 3) Install `react` and some other required npm packages:
84
+ ```bash
85
+ $ yarn add react react-dom @babel/preset-react prop-types \
86
+ css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
87
+ ```
77
88
 
78
- ###### Rails 6.x and 5.x:
89
+ Also update the Babel configuration in the `package.json` file:
90
+
91
+ ```diff
92
+ "babel": {
93
+ "presets": [
94
+ - "./node_modules/shakapacker/package/babel/preset.js"
95
+ + "./node_modules/shakapacker/package/babel/preset.js",
96
+ + "@babel/preset-react"
97
+ ]
98
+ },
79
99
  ```
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
100
+
101
+ #### 4) Install `react-rails`:
102
+ ```bash
103
+ $ bundle add 'react-rails' --strict
83
104
  $ rails generate react:install
84
105
  ```
85
106
 
@@ -89,31 +110,27 @@ This gives you:
89
110
  - [`ReactRailsUJS`](#ujs) setup in `app/javascript/packs/application.js`
90
111
  - `app/javascript/packs/server_rendering.js` for [server-side rendering](#server-side-rendering)
91
112
 
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
- ```
113
+ #### 5) Generate your first component:
114
+ ```bash
100
115
  $ rails g react:component HelloWorld greeting:string
101
116
  ```
102
117
 
103
- ##### 5) You can also generate your component in a subdirectory:
104
- ```
118
+ You can also generate your component in a subdirectory:
119
+
120
+ ```bash
105
121
  $ rails g react:component my_subdirectory/HelloWorld greeting:string
106
122
  ```
123
+
107
124
  Note: Your component is added to `app/javascript/components/` by default.
108
125
 
109
126
  Note: If your component is in a subdirectory you will append the directory path to your erb component call.
110
127
 
111
- Example:
112
- ```
128
+ Example:
129
+ ```erb
113
130
  <%= react_component("my_subdirectory/HelloWorld", { greeting: "Hello from react-rails." }) %>
114
131
  ```
115
132
 
116
- ##### 6) [Render it in a Rails view](#view-helper):
133
+ #### 6) [Render it in a Rails view](#view-helper):
117
134
 
118
135
  ```erb
119
136
  <!-- erb: paste this in view -->
@@ -121,10 +138,19 @@ Example:
121
138
  ```
122
139
 
123
140
  ##### 7) Lets Start the app:
124
- ```
141
+ ```bash
125
142
  $ rails s
126
143
  ```
127
- output: greeting: Hello from react-rails", inspect webpage in your browser too see change in tag props.
144
+ Output: greeting: Hello from react-rails", inspect webpage in your browser to see the change in tag props.
145
+
146
+ ##### 7) Run dev server (optional)
147
+ In order to run dev server with HMR feature you need to parallely run:
148
+
149
+ ```bash
150
+ $ ./bin/webpacker-dev-server
151
+ ```
152
+
153
+ 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
154
 
129
155
  ### Component name
130
156
 
@@ -150,6 +176,24 @@ ReactRailsUJS.useContext(myCustomContext)
150
176
 
151
177
  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
178
 
179
+ In some cases, having multiple `require.context` entries may be desired. Examples of this include:
180
+
181
+ - 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.
182
+ - 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:
183
+
184
+ ```js
185
+ // server_rendering.js
186
+ var homepageRequireContext = require.context('homepage', true);
187
+ var searchRequireContext = require.context('search', true);
188
+ var checkoutRequireContext = require.context('checkout', true);
189
+
190
+ var ReactRailsUJS = require('react_ujs');
191
+ ReactRailsUJS.useContexts([
192
+ homepageRequireContext,
193
+ searchRequireContext,
194
+ checkoutRequireContext
195
+ ]);
196
+ ```
153
197
  ### File naming
154
198
 
155
199
  React-Rails supports plenty of file extensions such as: .js, .jsx.js, .js.jsx, .es6.js, .coffee, etcetera!
@@ -164,14 +208,52 @@ Component File Name | `react_component` call
164
208
 
165
209
  ### Typescript support
166
210
 
167
- If you want to use React-Rails with Typescript, simply run the installer and add @types:
211
+ ```bash
212
+ yarn add typescript @babel/preset-typescript
168
213
  ```
169
- $ bundle exec rails webpacker:install:typescript
170
- $ yarn add @types/react @types/react-dom
214
+
215
+ Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
216
+
217
+ ```bash
218
+ yarn add fork-ts-checker-webpack-plugin
171
219
  ```
172
220
 
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:
221
+ Add `tsconfig.json` with the following content:
222
+
223
+ ```json
224
+ {
225
+ "compilerOptions": {
226
+ "declaration": false,
227
+ "emitDecoratorMetadata": true,
228
+ "experimentalDecorators": true,
229
+ "lib": ["es6", "dom"],
230
+ "module": "es6",
231
+ "moduleResolution": "node",
232
+ "sourceMap": true,
233
+ "target": "es5",
234
+ "jsx": "react",
235
+ "noEmit": true
236
+ },
237
+ "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
238
+ "compileOnSave": false
239
+ }
240
+ ```
241
+
242
+ Then modify the webpack config to use it as a plugin:
243
+
244
+ ```js
245
+ // config/webpack/webpack.config.js
246
+ const { webpackConfig, merge } = require("shakapacker");
247
+ const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
248
+
249
+ module.exports = merge(webpackConfig, {
250
+ plugins: [new ForkTSCheckerWebpackPlugin()],
251
+ });
174
252
  ```
253
+
254
+ 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:
255
+
256
+ ```ruby
175
257
  config.react.server_renderer_extensions = ["jsx", "js", "tsx", "ts"]
176
258
  ```
177
259
 
@@ -179,9 +261,9 @@ config.react.server_renderer_extensions = ["jsx", "js", "tsx", "ts"]
179
261
 
180
262
  You can use `assert_react_component` to test component render:
181
263
 
182
- app/views/welcome/index.html.erb
183
-
184
264
  ```erb
265
+ <!-- app/views/welcome/index.html.erb -->
266
+
185
267
  <%= react_component("HelloWorld", { greeting: "Hello from react-rails.", info: { name: "react-rails" } }, { class: "hello-world" }) %>
186
268
  ```
187
269
 
@@ -404,13 +486,27 @@ delete window.Turbolinks;
404
486
 
405
487
  ### `getConstructor`
406
488
 
407
- Components are loaded with `ReactRailsUJS.getConstructor(className)`. This function has two built-in implementations:
489
+ 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
490
 
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).
491
+ - On the asset pipeline, it looks up `className` in the global namespace (`ReactUJS.constructorFromGlobal`).
492
+ - 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
493
 
412
494
  You can override this function to customize the mapping of name-to-constructor. [Server-side rendering](#server-side-rendering) also uses this function.
413
495
 
496
+ For example, the fallback behavior of
497
+ `ReactUJS.constructorFromRequireContextWithGlobalFallback` can sometimes make
498
+ server-side rendering errors hard to debug as it will swallow the original error
499
+ (more info
500
+ [here](https://github.com/reactjs/react-rails/issues/264#issuecomment-552326663)).
501
+ `ReactUJS.constructorFromRequireContext` is provided for this reason. You can
502
+ use it like so:
503
+
504
+ ```js
505
+ // Replaces calls to `ReactUJS.useContext`
506
+ ReactUJS.getConstructor = ReactUJS.constructorFromRequireContext(require.context('components', true));
507
+ ```
508
+
509
+
414
510
  ## Server-Side Rendering
415
511
 
416
512
  You can render React components inside your Rails server with `prerender: true`:
@@ -622,6 +718,12 @@ You can also specify this option in `react_component`:
622
718
  <%= react_component('HelloMessage', {name: 'John'}, {camelize_props: true}) %>
623
719
  ```
624
720
 
721
+ ### Changing Component Templates
722
+
723
+ To make simple changes to Component templates, copy the respective template file to your Rails project at `lib/templates/react/component/template_filename`.
724
+
725
+ 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.
726
+
625
727
  ## Upgrading
626
728
 
627
729
  ### 2.3 to 2.4
@@ -686,7 +788,7 @@ Hot Module Replacement is [possible with this gem](https://stackoverflow.com/a/5
686
788
 
687
789
  Sample repo that shows HMR working with `react-rails`: [https://github.com/edelgado/react-rails-hmr](https://github.com/edelgado/react-rails-hmr)
688
790
 
689
- One caveat is that currently you [cannot Server-Side Render along with HMR](https://github.com/reactjs/react-rails/issues/925#issuecomment-415469572).
791
+ One caveat is that currently you [cannot Server-Side Render along with HMR](https://github.com/reactjs/react-rails/issues/925#issuecomment-415469572).
690
792
 
691
793
  ## Related Projects
692
794