Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@pmmmwh/react-refresh-webpack-plugin
Advanced tools
An **EXPERIMENTAL** Webpack plugin to enable "Fast Refresh" (also previously known as _Hot Reloading_) for React components.
The @pmmmwh/react-refresh-webpack-plugin npm package enables hot reloading of React components in webpack without losing their state. This is particularly useful during development as it allows developers to see changes in real-time without a full page reload.
Hot Module Replacement (HMR) for React components
This feature allows React components to be updated in the browser without a full page reload, preserving their state. The code sample shows how to include the plugin in a webpack configuration.
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
module.exports = {
// ... other webpack config settings ...
plugins: [
// ... other plugins ...
new ReactRefreshWebpackPlugin(),
],
};
React Hot Loader is a plugin that allows React components to be live reloaded without the loss of state. It is similar to @pmmmwh/react-refresh-webpack-plugin but has been largely superseded by the newer React Fast Refresh feature, which @pmmmwh/react-refresh-webpack-plugin leverages.
Webpack Hot Middleware is a middleware for webpack that allows for hot reloading of modules in development mode. It is similar to @pmmmwh/react-refresh-webpack-plugin in that it supports HMR, but it is more general-purpose and not specifically tailored to React.
An EXPERIMENTAL Webpack plugin to enable "Fast Refresh" (also previously known as Hot Reloading) for React components.
First and foremost, this plugin is not 100% stable. We're working towards a stable v1 release, and we've been testing the plugin quite extensively; but since it is still pretty young, there might still be some unknown edge cases.
There are no breaking changes planned for the v1 release, but they might still happen if we hit some significant road blockers.
Also, ensure that you are using the minimum supported versions of the plugin's peer dependencies - older versions unfortunately do not contain code to orchestrate "Fast Refresh", and thus cannot be made compatible.
Dependency | Minimum | Best |
---|---|---|
react | 16.9.0 | 16.13.0 + |
react-dom | 16.9.0 | 16.13.0 + |
react-reconciler | 0.22.0 | 0.25.0 + |
webpack | 4.0.0 (for 0.3.x )4.43.0 / 5.0.0 (for 0.4.x +) | 4.43.0 +5.2.0 + |
You only need
react-dom
if you're rendering to the DOM.
You only need to check for
react-reconciler
if you use custom reconcilers likereact-three-fiber
. You should check theirpackage.json
to make sure they use a compatible version instead of installingreact-reconciler
yourself. If the reconcilers are not compatible, please create an issue at their repository.
With all prerequisites met, you can install the plugin with one of the commands below:
# if you prefer npm
npm install -D @pmmmwh/react-refresh-webpack-plugin react-refresh
# if you prefer yarn
yarn add -D @pmmmwh/react-refresh-webpack-plugin react-refresh
# if you prefer pnpm
pnpm add -D @pmmmwh/react-refresh-webpack-plugin react-refresh
The plugin depends on a package from the React team - react-refresh
,
so you will have to install and configure it separately as demonstrated in the Usage section.
TypeScript support is available out-of-the-box for those who use webpack.config.ts
:tada:!
For that you will have to install type-fest
as a development peer dependency with one of the commands below:
# if you prefer npm
npm install -D type-fest
# if you prefer yarn
yarn add -D type-fest
# if you prefer pnpm
pnpm add -D type-fest
The most basic setup to enable "Fast Refresh" is to update your webpack.config.js
(or .ts
) as follows:
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const webpack = require('webpack');
// ... your other imports
const isDevelopment = process.env.NODE_ENV !== 'production';
module.exports = {
// It is suggested to run both `react-refresh/babel` and the plugin in the `development` mode only,
// even though both of them have optimisations in place to do nothing in the `production` mode.
// If you would like to override Webpack's defaults for modes, you can also use the `none` mode -
// you then will need to set `forceEnable: true` in the plugin's options.
mode: isDevelopment ? 'development' : 'production',
module: {
rules: [
// ... other rules
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: [
// ... other loaders
{
loader: require.resolve('babel-loader'),
options: {
// ... other options
plugins: [
// ... other plugins
isDevelopment && require.resolve('react-refresh/babel'),
].filter(Boolean),
},
},
],
},
],
},
plugins: [
// ... other plugins
isDevelopment && new webpack.HotModuleReplacementPlugin(),
isDevelopment && new ReactRefreshWebpackPlugin(),
].filter(Boolean),
// ... other configuration options
};
You need to install react-refresh-typescript and your TypeScript must be at least 4.0.
Emit module must be ESModule
not CommonJS
. You can overwrite it in ts-loader
and still use CommonJS
in your tsconfig file.
⚠ This package is maintained by the community not by Facebook.
# if you prefer npm
npm install -D react-refresh-typescript
# if you prefer yarn
yarn add -D react-refresh-typescript
# if you prefer pnpm
pnpm add -D react-refresh-typescript
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const webpack = require('webpack');
const ReactRefreshTypeScript = require('react-refresh-typescript');
// ... your other imports
const isDevelopment = process.env.NODE_ENV !== 'production';
module.exports = {
// It is suggested to run both `react-refresh-typescript` and the plugin in the `development` mode only,
// even though both of them have optimisations in place to do nothing in the `production` mode.
// If you would like to override Webpack's defaults for modes, you can also use the `none` mode -
// you then will need to set `forceEnable: true` in the plugin's options.
mode: isDevelopment ? 'development' : 'production',
module: {
rules: [
// ... other rules
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: [
// ... other loaders
{
loader: require.resolve('ts-loader'),
options: {
getCustomTransformers: () => ({
before: isDevelopment ? [ReactRefreshTypeScript()] : [],
}),
// `ts-loader` does not work with HMR unless `transpileOnly` is used.
// If you need type checking, `ForkTsCheckerWebpackPlugin` is an alternative.
transpileOnly: isDevelopment,
},
},
],
},
],
},
plugins: [
// ... other plugins
isDevelopment && new webpack.HotModuleReplacementPlugin(),
isDevelopment && new ReactRefreshWebpackPlugin(),
].filter(Boolean),
// ... other configuration options
};
Your @swc/core
version must be at least 1.2.52 and swc-loader
version greater than 0.1.13 is recommended.
⚠
@swc/core
andswc-loader
are maintained by the community not by Facebook.
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const webpack = require('webpack');
// ... your other imports
const isDevelopment = process.env.NODE_ENV !== 'production';
module.exports = {
mode: isDevelopment ? 'development' : 'production',
module: {
rules: [
// ... other rules
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: [
// ... other loaders
{
loader: require.resolve('swc-loader'),
options: {
jsc: {
// ... other options
transform: {
react: {
// swc-loader will check whether webpack mode is 'development'
// and set this automatically starting from 0.1.13. You could also set it yourself.
// swc won't enable fast refresh when development is false
development: isDevelopment,
refresh: isDevelopment,
},
},
},
},
},
],
},
],
},
plugins: [
// ... other plugins
isDevelopment && new webpack.HotModuleReplacementPlugin(),
isDevelopment && new ReactRefreshWebpackPlugin(),
].filter(Boolean),
// ... other configuration options
};
You might want to further tweak the configuration to accommodate your setup:
isDevelopment
In this example we've shown the simple way of splitting up development
and production
builds with the NODE_ENV
environment variable.
It will default to true
(i.e. development
mode) when NODE_ENV
is not available from the environment.
In practice though, you might want to wire this up differently, like using a function config or using multiple configuration files.
webpack.HotModuleReplacementPlugin
If you use webpack-dev-server
or webpack-plugin-serve
,
you can set devServer.hot
/devServer.hotOnly
and hmr
to true
respectively,
instead of adding the HMR plugin to your plugin list.
Officially, webpack-dev-server
, webpack-hot-middleware
and webpack-plugin-serve
are supported out of the box -
you just have to set the overlay.sockIntegration
option to match what you're using.
For each of the integrations listed above, you can also take a look at the corresponding sample projects for a better understanding of how things should be wired up.
Please refer to the API docs for all available options.
Please refer to the Troubleshooting guide for FAQs and resolutions to common issues.
This project is licensed under the terms of the MIT License.
FAQs
An **EXPERIMENTAL** Webpack plugin to enable "Fast Refresh" (also previously known as _Hot Reloading_) for React components.
The npm package @pmmmwh/react-refresh-webpack-plugin receives a total of 6,027,039 weekly downloads. As such, @pmmmwh/react-refresh-webpack-plugin popularity was classified as popular.
We found that @pmmmwh/react-refresh-webpack-plugin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.