Skip to content

Commit 6444eab

Browse files
authored
Merge pull request #1214 from reactjs/update-readme-to-refer-to-shakapacker
Update readme to refer to shakapacker
2 parents db28720 + 30614f3 commit 6444eab

File tree

3 files changed

+101
-43
lines changed

3 files changed

+101
-43
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Tell us what should happen
1212
Tell us what happens instead
1313

1414
### System configuration
15-
**Sprockets or Webpacker version**:
16-
**React-Rails version**:
17-
**Rect_UJS version**:
18-
**Rails version**:
19-
**Ruby version**:
15+
- **Shakapacker or Sprockets version**:
16+
- **React-Rails version**:
17+
- **Rect_UJS version**:
18+
- **Rails version**:
19+
- **Ruby version**:
2020

2121

2222
-------

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Include details about your configuration and environment, React-Rails integrates
5757

5858
* **Which version of React-Rails are you using?**
5959
* **Which version of React_UJS are you using?**
60-
* **Which version of Webpacker/Sprockets are you using?**
60+
* **Which version of Shakapacker/Sprockets are you using?**
6161
* **Which version of Rails are you using?**
6262

6363
### Your First Code Contribution

README.md

Lines changed: 95 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ React-Rails is a flexible tool to use [React](http://facebook.github.io/react/)
2020
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
2121
## Contents
2222

23-
- [Get started with Webpacker](#get-started-with-webpacker)
23+
- [Get started with Shakapacker](#get-started-with-shakapacker)
2424
- [File naming](#file-naming)
25+
- [Typescript support](#typescript-support)
2526
- [Use with Asset Pipeline](#use-with-asset-pipeline)
2627
- [Custom JSX Transformer](#custom-jsx-transformer)
2728
- [React.js versions](#reactjs-versions)
@@ -57,31 +58,45 @@ https://github.com/reactjs/React-Rails/wiki
5758
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.
5859

5960

60-
## Get started with Webpacker
61+
## Get started with Shakapacker
6162

62-
[Alternatively, get started with Sprockets](#use-with-asset-pipeline)
63+
_Alternatively, get started with [Sprockets](#use-with-asset-pipeline)_
6364

64-
[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:
65+
#### 1) Create a new Rails app:
66+
Prevent installing default javascript dependencies by using `--skip-javascript` option:
6567

66-
##### 1) Create a new Rails app:
67-
```
68-
$ rails new my-app
68+
```bash
69+
$ rails new my-app --skip-javascript
6970
$ cd my-app
7071
```
7172

72-
##### 2) Add `react-rails` to your Gemfile:
73-
```ruby
74-
gem 'react-rails'
73+
#### 2) Install `shakapacker`:
74+
```bash
75+
$ bundle add shakapacker --strict
76+
$ rails webpacker:install
77+
```
78+
79+
#### 3) Install `react` and some other required npm packages:
80+
```bash
81+
$ yarn add react react-dom @babel/preset-react prop-types \
82+
css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
7583
```
76-
Note: On rails versions < 6.0, You need to add `gem 'webpacker'` to your Gemfile in step 2 above.
7784

78-
##### 3) Now run the installers:
85+
Also update the Babel configuration in the `package.json` file:
7986

80-
###### Rails 6.x and 5.x:
87+
```diff
88+
"babel": {
89+
"presets": [
90+
- "./node_modules/shakapacker/package/babel/preset.js"
91+
+ "./node_modules/shakapacker/package/babel/preset.js",
92+
+ "@babel/preset-react"
93+
]
94+
},
8195
```
82-
$ bundle install
83-
$ rails webpacker:install # OR (on rails version < 5.0) rake webpacker:install
84-
$ rails webpacker:install:react # OR (on rails version < 5.0) rake webpacker:install:react
96+
97+
#### 4) Install `react-rails`:
98+
```bash
99+
$ bundle add 'react-rails' --strict
85100
$ rails generate react:install
86101
```
87102

@@ -91,43 +106,48 @@ This gives you:
91106
- [`ReactRailsUJS`](#ujs) setup in `app/javascript/packs/application.js`
92107
- `app/javascript/packs/server_rendering.js` for [server-side rendering](#server-side-rendering)
93108

94-
Note: On rails versions < 6.0, link the JavaScript pack in Rails view using `javascript_pack_tag` [helper](https://github.com/rails/webpacker#usage):
95-
```erb
96-
<!-- application.html.erb in Head tag below turbolinks -->
97-
<%= javascript_pack_tag 'application' %>
98-
```
99-
100-
##### 4) Generate your first component:
101-
```
109+
#### 5) Generate your first component:
110+
```bash
102111
$ rails g react:component HelloWorld greeting:string
103112
```
104113

105-
##### 5) You can also generate your component in a subdirectory:
106-
```
114+
You can also generate your component in a subdirectory:
115+
116+
```bash
107117
$ rails g react:component my_subdirectory/HelloWorld greeting:string
108118
```
119+
109120
Note: Your component is added to `app/javascript/components/` by default.
110121

111122
Note: If your component is in a subdirectory you will append the directory path to your erb component call.
112123

113124
Example:
114-
```
125+
```erb
115126
<%= react_component("my_subdirectory/HelloWorld", { greeting: "Hello from react-rails." }) %>
116127
```
117128

118-
##### 6) [Render it in a Rails view](#view-helper):
129+
#### 6) [Render it in a Rails view](#view-helper):
119130

120131
```erb
121132
<!-- erb: paste this in view -->
122133
<%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>
123134
```
124135

125136
##### 7) Lets Start the app:
126-
```
137+
```bash
127138
$ rails s
128139
```
129140
Output: greeting: Hello from react-rails", inspect webpage in your browser to see the change in tag props.
130141

142+
##### 7) Run dev server (optional)
143+
In order to run dev server with HMR feature you need to parallely run:
144+
145+
```bash
146+
$ ./bin/webpacker-dev-server
147+
```
148+
149+
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.
150+
131151
### Component name
132152

133153
The component name tells `react-rails` where to load the component. For example:
@@ -166,24 +186,62 @@ Component File Name | `react_component` call
166186

167187
### Typescript support
168188

169-
If you want to use React-Rails with Typescript, simply run the installer and add @types:
189+
```bash
190+
yarn add typescript @babel/preset-typescript
170191
```
171-
$ bundle exec rails webpacker:install:typescript
172-
$ yarn add @types/react @types/react-dom
192+
193+
Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
194+
195+
```bash
196+
yarn add fork-ts-checker-webpack-plugin
173197
```
174198

175-
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:
199+
Add `tsconfig.json` with the following content:
200+
201+
```json
202+
{
203+
"compilerOptions": {
204+
"declaration": false,
205+
"emitDecoratorMetadata": true,
206+
"experimentalDecorators": true,
207+
"lib": ["es6", "dom"],
208+
"module": "es6",
209+
"moduleResolution": "node",
210+
"sourceMap": true,
211+
"target": "es5",
212+
"jsx": "react",
213+
"noEmit": true
214+
},
215+
"exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
216+
"compileOnSave": false
217+
}
176218
```
219+
220+
Then modify the webpack config to use it as a plugin:
221+
222+
```js
223+
// config/webpack/webpack.config.js
224+
const { webpackConfig, merge } = require("shakapacker");
225+
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
226+
227+
module.exports = merge(webpackConfig, {
228+
plugins: [new ForkTSCheckerWebpackPlugin()],
229+
});
230+
```
231+
232+
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:
233+
234+
```ruby
177235
config.react.server_renderer_extensions = ["jsx", "js", "tsx", "ts"]
178236
```
179237

180238
### Test component
181239

182240
You can use `assert_react_component` to test component render:
183241

184-
app/views/welcome/index.html.erb
185-
186242
```erb
243+
<!-- app/views/welcome/index.html.erb -->
244+
187245
<%= react_component("HelloWorld", { greeting: "Hello from react-rails.", info: { name: "react-rails" } }, { class: "hello-world" }) %>
188246
```
189247

@@ -406,10 +464,10 @@ delete window.Turbolinks;
406464

407465
### `getConstructor`
408466

409-
Components are loaded with `ReactRailsUJS.getConstructor(className)`. This function has two default implementations, depending on if you're using the asset pipeline or Webpacker:
467+
Components are loaded with `ReactRailsUJS.getConstructor(className)`. This function has two default implementations, depending on if you're using the asset pipeline or Shakapacker:
410468

411469
- On the asset pipeline, it looks up `className` in the global namespace (`ReactUJS.constructorFromGlobal`).
412-
- On Webpacker, it `require`s files and accesses named exports, as described in [Get started with Webpacker](#get-started-with-webpacker), falling back to the global namespace (`ReactUJS.constructorFromRequireContextWithGlobalFallback`).
470+
- 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`).
413471

414472
You can override this function to customize the mapping of name-to-constructor. [Server-side rendering](#server-side-rendering) also uses this function.
415473

0 commit comments

Comments
 (0)