Skip to content

Commit

Permalink
Move react dependencies to a docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
justin808 committed Nov 15, 2021
1 parent 9d55518 commit 19da4aa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,9 @@ Webpacker out of the box supports JS and static assets (fonts, images etc.) comp

#### React

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

#### Typescript
...if you are using typescript, update your `tsconfig.json`

```json
Expand Down
59 changes: 59 additions & 0 deletions docs/customizing_babel_config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Customizing Babel Config

## Default Configuration
The default configuration of babel is done by using `package.json` to use the file within the `@rails/webpacker` package.

```json
{
"babel": {
"presets": [
"./node_modules/@rails/webpacker/package/babel/preset.js"
]
}
}
```

## Customizing the Babel Config
This example shows how you can create an object and apply _additional_ presets and plugins on top of the default.

### React Configuration
To use this example file,

```
yarn add react react-dom @babel/preset-react
yarn add --dev @pmmmwh/react-refresh-webpack-plugin react-refresh
```

```js
// babel.config.js
module.exports = function (api) {
const defaultConfigFunc = require('@rails/webpacker/package/babel/preset.js')
const resultConfig = defaultConfigFunc(api)
const isProductionEnv = api.env('production')

const changesOnDefault = {
presets: [
[
'@babel/preset-react',
{
development: isDevelopmentEnv || isTestEnv,
useBuiltIns: true
}
],
isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
{
removeImport: true
}
]
].filter(Boolean),
plugins: [
process.env.WEBPACK_SERVE && 'react-refresh/babel'
].filter(Boolean),
}

resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]

return resultConfig
}
```
14 changes: 1 addition & 13 deletions package/babel/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,11 @@ module.exports = function config(api) {
moduleExists('@babel/preset-typescript') && [
'@babel/preset-typescript',
{ allExtensions: true, isTSX: true }
],
moduleExists('@babel/preset-react') && [
'@babel/preset-react',
{
development: isDevelopmentEnv || isTestEnv,
useBuiltIns: true
}
]
].filter(Boolean),
plugins: [
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-transform-runtime', { helpers: false }],
isProductionEnv &&
moduleExists('babel-plugin-transform-react-remove-prop-types') && [
'babel-plugin-transform-react-remove-prop-types',
{ removeImport: true }
]
['@babel/plugin-transform-runtime', { helpers: false }]
].filter(Boolean)
}
}

0 comments on commit 19da4aa

Please sign in to comment.