Skip to content

Commit 51092c3

Browse files
committed
Move react dependencies to a docs
Also, provide a simple example of customizing the base babel config.
1 parent 79718d9 commit 51092c3

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,17 @@ By default, you will find the Webpacker preset in your `package.json`.
318318
},
319319
```
320320

321-
Optionally, you can change your Babel configuration by removing these lines in your `package.json` and add [a Babel configuration file](https://babeljs.io/docs/en/config-files) in your project.
322-
321+
Optionally, you can change your Babel configuration by removing these lines in your `package.json` and add [a Babel configuration file](https://babeljs.io/docs/en/config-files) in your project. For an example customization based on the original, see [Customizing Babel Config](./docs/customizing_babel_config.md).
323322

324323
### Integrations
325324

326325
Webpacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:
327326

328327
#### React
329328

330-
```bash
331-
yarn add react react-dom @babel/preset-react
332-
```
329+
See customization example the [Customizing Babel Config](./docs/customizing_babel_config.md) for React configuration.
333330

331+
#### Typescript
334332
...if you are using typescript, update your `tsconfig.json`
335333

336334
```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+
```

docs/v6_upgrade.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Example going to a specific version:
8484
]
8585
}
8686
```
87+
See customization example the [Customizing Babel Config](./docs/customizing_babel_config.md) for React configuration.
8788

8889
12. `extensions` was removed from the `webpacker.yml` file. Move custom extensions to your configuration by merging an object like this. For more details, see docs for [Webpack Configuration](https://github.com/rails/webpacker/blob/master/README.md#webpack-configuration)
8990

package/babel/preset.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,10 @@ 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: [
45-
['@babel/plugin-transform-runtime', { helpers: false }],
46-
isProductionEnv &&
47-
moduleExists('babel-plugin-transform-react-remove-prop-types') && [
48-
'babel-plugin-transform-react-remove-prop-types',
49-
{ removeImport: true }
50-
]
38+
['@babel/plugin-transform-runtime', { helpers: false }]
5139
].filter(Boolean)
5240
}
5341
}

0 commit comments

Comments
 (0)