Skip to content

Commit c6e938c

Browse files
committed
docs: update README
1 parent c00d407 commit c6e938c

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# React Refresh Webpack Plugin
22

3+
[![NPM Version](https://img.shields.io/npm/v/@pmmmwh/react-refresh-webpack-plugin)](https://www.npmjs.com/package/pmmmwh/react-refresh-webpack-plugin)
4+
[![License](https://img.shields.io/github/license/pmmmwh/react-refresh-webpack-plugin)](./LICENSE)
5+
36
An **EXPERIMENTAL** Webpack plugin to enable "Fast Refresh" (also previously known as _Hot Reloading_) for React components.
47

58
## Installation
@@ -26,16 +29,24 @@ First, apply the plugin in your Webpack configuration as follows:
2629
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
2730
// ... your other imports
2831

32+
// You can tie this to whatever mechanisms you are using to detect a development environment.
33+
// For example, as shown here, is to tie that to `NODE_ENV` -
34+
// Then if you run `NODE_ENV=production webpack`, the constant will be set to false.
35+
const isDevelopment = process.env.NODE_ENV !== 'production';
36+
2937
module.exports = {
3038
// It is suggested to run the plugin in development mode only
31-
mode: 'development',
39+
// If you are an advanced user and would like to setup Webpack yourselves,
40+
// you can also use the `none` mode,
41+
// but you will need to set `forceEnable: true` in the plugin options.
42+
mode: isDevelopment ? 'development' : 'production',
3243
// ... other configurations
3344
plugins: [
3445
// ... other plugins
3546
// You could also keep the plugin in your production config,
3647
// It will simply do nothing.
37-
new ReactRefreshWebpackPlugin(),
38-
],
48+
isDevelopment && new ReactRefreshWebpackPlugin(),
49+
].filter(Boolean),
3950
};
4051
```
4152

@@ -45,9 +56,11 @@ This can either be done in your Webpack config (via options of `babel-loader`),
4556
**webpack.config.js** (if you choose to inline the config)
4657

4758
```js
59+
const isDevelopment = process.env.NODE_ENV !== 'production';
60+
4861
module.exports = {
4962
// DO NOT apply the plugin in production mode!
50-
mode: 'development',
63+
mode: isDevelopment ? 'development' : 'production',
5164
module: {
5265
rules: [
5366
// ... other rules
@@ -62,7 +75,7 @@ module.exports = {
6275
options: {
6376
// ... other options
6477
// DO NOT apply the Babel plugin in production mode!
65-
plugins: [require.resolve('react-refresh/babel')],
78+
plugins: [isDevelopment && require.resolve('react-refresh/babel')].filter(Boolean),
6679
},
6780
},
6881
],
@@ -76,15 +89,15 @@ module.exports = {
7689

7790
```js
7891
module.exports = api => {
79-
// This caches the Babel config.
92+
// This caches the Babel config by environment.
8093
api.cache.using(() => process.env.NODE_ENV);
8194
return {
8295
// ... other options
8396
plugins: [
8497
// ... other plugins
8598
// Applies the react-refresh Babel plugin on development modes only
86-
...(api.env('development') ? ['react-refresh/babel'] : []),
87-
],
99+
api.env('development') && 'react-refresh/babel',
100+
].filter(Boolean),
88101
};
89102
};
90103
```
@@ -97,7 +110,7 @@ The allowed values are as follows:
97110
| Name | Type | Default | Description |
98111
| :-----------------------: | :-------: | :-----: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
99112
| **`disableRefreshCheck`** | `boolean` | `false` | Disables detection of react-refresh's Babel plugin. Useful if you do not parse JS files within `node_modules`, or if you have a Babel setup not entirely controlled by Webpack. |
100-
| **`forceEnable`** | `boolean` | `false` | Enables the plugin forcefully. Useful if you want to use the plugin in production, for example. |
113+
| **`forceEnable`** | `boolean` | `false` | Enables the plugin forcefully. Useful if you want to use the plugin in production, or if you are using Webpack's `none` mode without `NODE_ENV`, for example. |
101114

102115
## Related Work
103116

0 commit comments

Comments
 (0)