Skip to content

Commit 19da4aa

Browse files
committed
Move react dependencies to a docs.
1 parent 9d55518 commit 19da4aa

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,9 @@ Webpacker out of the box supports JS and static assets (fonts, images etc.) comp
321321

322322
#### React
323323

324-
```bash
325-
yarn add react react-dom @babel/preset-react
326-
```
324+
See customization example the [Customizing Babel Config](./docs/customizing_babel_config.md)
327325

326+
#### Typescript
328327
...if you are using typescript, update your `tsconfig.json`
329328

330329
```json

docs/customizing_babel_config.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Customizing Babel Config
2+
3+
## Default Configuration
4+
The default configuration of babel is done by using `package.json` to use the file within the `@rails/webpacker` package.
5+
6+
```json
7+
{
8+
"babel": {
9+
"presets": [
10+
"./node_modules/@rails/webpacker/package/babel/preset.js"
11+
]
12+
}
13+
}
14+
```
15+
16+
## Customizing the Babel Config
17+
This example shows how you can create an object and apply _additional_ presets and plugins on top of the default.
18+
19+
### React Configuration
20+
To use this example file,
21+
22+
```
23+
yarn add react react-dom @babel/preset-react
24+
yarn add --dev @pmmmwh/react-refresh-webpack-plugin react-refresh
25+
```
26+
27+
```js
28+
// babel.config.js
29+
module.exports = function (api) {
30+
const defaultConfigFunc = require('@rails/webpacker/package/babel/preset.js')
31+
const resultConfig = defaultConfigFunc(api)
32+
const isProductionEnv = api.env('production')
33+
34+
const changesOnDefault = {
35+
presets: [
36+
[
37+
'@babel/preset-react',
38+
{
39+
development: isDevelopmentEnv || isTestEnv,
40+
useBuiltIns: true
41+
}
42+
],
43+
isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
44+
{
45+
removeImport: true
46+
}
47+
]
48+
].filter(Boolean),
49+
plugins: [
50+
process.env.WEBPACK_SERVE && 'react-refresh/babel'
51+
].filter(Boolean),
52+
}
53+
54+
resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
55+
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]
56+
57+
return resultConfig
58+
}
59+
```

package/babel/preset.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,11 @@ module.exports = function config(api) {
3232
moduleExists('@babel/preset-typescript') && [
3333
'@babel/preset-typescript',
3434
{ allExtensions: true, isTSX: true }
35-
],
36-
moduleExists('@babel/preset-react') && [
37-
'@babel/preset-react',
38-
{
39-
development: isDevelopmentEnv || isTestEnv,
40-
useBuiltIns: true
41-
}
4235
]
4336
].filter(Boolean),
4437
plugins: [
4538
['@babel/plugin-proposal-class-properties', { loose: true }],
46-
['@babel/plugin-transform-runtime', { helpers: false }],
47-
isProductionEnv &&
48-
moduleExists('babel-plugin-transform-react-remove-prop-types') && [
49-
'babel-plugin-transform-react-remove-prop-types',
50-
{ removeImport: true }
51-
]
39+
['@babel/plugin-transform-runtime', { helpers: false }]
5240
].filter(Boolean)
5341
}
5442
}

0 commit comments

Comments
 (0)