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+
36An ** 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:
2629const 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+
2937module .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+
4861module .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
7891module .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