Skip to content

Commit 2917b52

Browse files
committed
Update installation guide for Shakapacker
Replaced the installation guide to use Shakapacker instead of Webpacker. Also updated Typescript support based on the guideline in Shakapacker project.
1 parent a430521 commit 2917b52

File tree

1 file changed

+97
-37
lines changed

1 file changed

+97
-37
lines changed

README.md

Lines changed: 97 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,47 @@ 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+
Update `config/webpacker.yml` and set the `source_entry_path` to `packs`.
80+
81+
#### 3) Install `react` and some other required npm packages:
82+
```bash
83+
$ yarn add react react-dom @babel/preset-react prop-types \
84+
css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
7585
```
76-
Note: On rails versions < 6.0, You need to add `gem 'webpacker'` to your Gemfile in step 2 above.
7786

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

80-
###### Rails 6.x and 5.x:
89+
```diff
90+
"babel": {
91+
"presets": [
92+
- "./node_modules/shakapacker/package/babel/preset.js"
93+
+ "./node_modules/shakapacker/package/babel/preset.js",
94+
+ "@babel/preset-react"
95+
]
96+
},
8197
```
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
98+
99+
#### 4) Install `react-rails`:
100+
```bash
101+
$ bundle add 'react-rails' --strict
85102
$ rails generate react:install
86103
```
87104

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

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-
```
111+
#### 5) Generate your first component:
112+
```bash
102113
$ rails g react:component HelloWorld greeting:string
103114
```
104115

105-
##### 5) You can also generate your component in a subdirectory:
106-
```
116+
You can also generate your component in a subdirectory:
117+
118+
```bash
107119
$ rails g react:component my_subdirectory/HelloWorld greeting:string
108120
```
121+
109122
Note: Your component is added to `app/javascript/components/` by default.
110123

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

113126
Example:
114-
```
127+
```erb
115128
<%= react_component("my_subdirectory/HelloWorld", { greeting: "Hello from react-rails." }) %>
116129
```
117130

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

120133
```erb
121134
<!-- erb: paste this in view -->
122135
<%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>
123136
```
124137

125138
##### 7) Lets Start the app:
126-
```
139+
```bash
127140
$ rails s
128141
```
129142
Output: greeting: Hello from react-rails", inspect webpage in your browser to see the change in tag props.
130143

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

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

167189
### Typescript support
168190

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

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

180240
### Test component
181241

182242
You can use `assert_react_component` to test component render:
183243

184-
app/views/welcome/index.html.erb
185-
186244
```erb
245+
<!-- app/views/welcome/index.html.erb -->
246+
187247
<%= react_component("HelloWorld", { greeting: "Hello from react-rails.", info: { name: "react-rails" } }, { class: "hello-world" }) %>
188248
```
189249

@@ -406,10 +466,10 @@ delete window.Turbolinks;
406466

407467
### `getConstructor`
408468

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:
469+
Components are loaded with `ReactRailsUJS.getConstructor(className)`. This function has two default implementations, depending on if you're using the asset pipeline or Shakapacker:
410470

411471
- 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`).
472+
- 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`).
413473

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

0 commit comments

Comments
 (0)