All Projects → pmmmwh → React Refresh Webpack Plugin

pmmmwh / React Refresh Webpack Plugin

Licence: mit
A Webpack plugin to enable "Fast Refresh" (also previously known as Hot Reloading) for React components.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Refresh Webpack Plugin

chunk-splitting-plugin
Arbitrarily split your Webpack chunks and bundles into smaller pieces
Stars: ✭ 15 (-99.38%)
Mutual labels:  webpack-plugin, hot-reload
Webpack Babel Multi Target Plugin
A Webpack plugin that works with Babel to allow differential loading - production deployment of ES2015 builds targeted to modern browsers, with an ES5 fallback for legacy browsers.
Stars: ✭ 150 (-93.78%)
Mutual labels:  webpack-plugin
Rollbar Sourcemap Webpack Plugin
A Webpack plugin to upload sourcemaps to Rollbar
Stars: ✭ 127 (-94.74%)
Mutual labels:  webpack-plugin
Worker Plugin
👩‍🏭 Adds native Web Worker bundling support to Webpack.
Stars: ✭ 1,840 (-23.75%)
Mutual labels:  webpack-plugin
Instapack
All-in-one TypeScript and Sass compiler for web applications! 📦 🚀
Stars: ✭ 131 (-94.57%)
Mutual labels:  hot-reload
Bundle Stats
In-depth bundle analyzer for webpack(bundle size, assets, modules, packages)
Stars: ✭ 144 (-94.03%)
Mutual labels:  webpack-plugin
Modern Monorepo Boilerplate
Modern Monorepo Boilerplate with Lerna, TypeScript, React/CRA, HMR, Jest, ESLint/TypeScript.
Stars: ✭ 127 (-94.74%)
Mutual labels:  hot-reload
Reactql
Universal React+GraphQL starter kit: React 16, Apollo 2, MobX, Emotion, Webpack 4, GraphQL Code Generator, React Router 4, PostCSS, SSR
Stars: ✭ 1,833 (-24.04%)
Mutual labels:  hot-reload
Obsolete Webpack Plugin
🌈 A Webpack plugin generates a browser-side standalone script that detects browser compatibility based on `Browserslist` and prompts website users to upgrade it.
Stars: ✭ 148 (-93.87%)
Mutual labels:  webpack-plugin
Webpack Require From
Webpack plugin that allows to configure path or URL for fetching dynamic imports
Stars: ✭ 142 (-94.12%)
Mutual labels:  webpack-plugin
Handlebars Webpack Plugin
Renders your html-template at build time
Stars: ✭ 135 (-94.41%)
Mutual labels:  webpack-plugin
Size Plugin
Track compressed Webpack asset sizes over time.
Stars: ✭ 1,665 (-31%)
Mutual labels:  webpack-plugin
Awesome Chrome Extension Boilerplate
Use react + typescript + webpack to enhance your chrome extension development experience
Stars: ✭ 146 (-93.95%)
Mutual labels:  hot-reload
Webpack Visualizer
Visualize your Webpack bundle
Stars: ✭ 1,664 (-31.04%)
Mutual labels:  webpack-plugin
Nodemon Webpack Plugin
A webpack plugin that uses Nodemon to start and reload the server.
Stars: ✭ 150 (-93.78%)
Mutual labels:  webpack-plugin
Hot Reload All The Things
Starter project for HMR with backend routes and server/client-side react.
Stars: ✭ 127 (-94.74%)
Mutual labels:  hot-reload
Svg Sprite Loader
Webpack loader for creating SVG sprites.
Stars: ✭ 1,822 (-24.49%)
Mutual labels:  webpack-plugin
React Typescript Electron Sample With Create React App And Electron Builder
React-TypeScript-Electron sample with Create React App and Electron Builder
Stars: ✭ 143 (-94.07%)
Mutual labels:  hot-reload
React Webpack4 Cook
💯The most powerful webpack4 tutorial in the universe
Stars: ✭ 152 (-93.7%)
Mutual labels:  webpack-plugin
Clean Webpack Plugin
By default, this plugin will remove all files inside webpack's output.path directory, as well as all unused webpack assets after every successful rebuild.
Stars: ✭ 1,888 (-21.76%)
Mutual labels:  webpack-plugin

React Refresh Webpack Plugin

CircleCI License Latest Version Next Version

An EXPERIMENTAL Webpack plugin to enable "Fast Refresh" (also known as Hot Reloading) for React components.

This plugin is not 100% stable. We're hoping to land a v1 release soon - please help us by reporting any issues you've encountered!

Getting Started

Prerequisites

Ensure that you are using at least the minimum supported versions of this plugin's peer dependencies - older versions unfortunately do not contain code to orchestrate "Fast Refresh", and thus cannot be made compatible.

We recommend using the following versions:

Dependency Version
react 16.13.0+ or 17.x
react-dom 16.13.0+ or 17.x
react-refresh 0.10.0+
webpack 4.46.0+ or 5.2.0+
Minimum requirements
Dependency Version
react 16.9.0
react-dom 16.9.0
react-refresh 0.10.0
webpack 4.43.0
Using custom renderers (e.g. react-three-fiber, react-pdf, ink)

To ensure full support of "Fast Refresh" with components rendered by custom renderers, you should ensure the renderer you're using depends on a recent version of react-reconciler.

We recommend version 0.25.0 or above, but any versions above 0.22.0 should work.

If the renderer is not compatible, please file them an issue instead.

Installation

With all prerequisites met, you can install this plugin using your package manager of choice:

# 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 react-refresh package (from the React team) is a required peer dependency of this plugin. We recommend using version 0.10.0 or above.

Support for TypeScript

TypeScript support is available out-of-the-box for those who use webpack.config.ts.

Our exported types however depends on type-fest, so you'll have to add it as a devDependency:

# 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

📝 Note:

[email protected] only supports Node.js v12.20 or above. If you're using an older version of Node.js, please install [email protected].

Usage

For most setups, we recommend integrate using babel-loader. It covers the most use cases and is officially supported by the React team.

The example below will assume you're using webpack-dev-server.

If you haven't done so, set up your development Webpack configuration for Hot Module Replacement (HMR).

const isDevelopment = process.env.NODE_ENV !== 'production';

module.exports = {
  mode: isDevelopment ? 'development' : 'production',
  devServer: {
    hot: true,
  },
};
Using webpack-hot-middleware
const webpack = require('webpack');

const isDevelopment = process.env.NODE_ENV !== 'production';

module.exports = {
  mode: isDevelopment ? 'development' : 'production',
  plugins: [isDevelopment && new webpack.HotModuleReplacementPlugin()].filter(Boolean),
};
Using webpack-plugin-serve
const { WebpackPluginServe } = require('webpack-plugin-serve');

const isDevelopment = process.env.NODE_ENV !== 'production';

module.exports = {
  mode: isDevelopment ? 'development' : 'production',
  plugins: [isDevelopment && new WebpackPluginServe()].filter(Boolean),
};

Then, add the react-refresh/babel plugin to your Babel configuration and this plugin to your Webpack configuration.

const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');

const isDevelopment = process.env.NODE_ENV !== 'production';

module.exports = {
  mode: isDevelopment ? 'development' : 'production',
  module: {
    rules: [
      {
        test: /\.[jt]sx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: require.resolve('babel-loader'),
            options: {
              plugins: [isDevelopment && require.resolve('react-refresh/babel')].filter(Boolean),
            },
          },
        ],
      },
    ],
  },
  plugins: [isDevelopment && new ReactRefreshWebpackPlugin()].filter(Boolean),
};

📝 Note:

Even though both the Babel transform (react-refresh/babel) and this plugin have optimisations to do nothing in production, it is suggested to only have them both enabled in development mode to prevent shipping any additional code accidentally.

Using ts-loader

⚠️ Warning: This is an un-official integration maintained by the community.

Install react-refresh-typescript. Ensure your TypeScript version is at least 4.0.

# 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

Then, instead of wiring up react-refresh/babel via babel-loader, you can wire-up react-refresh-typescript with ts-loader:

const ReactRefreshTypeScript = require('react-refresh-typescript');

const isDevelopment = process.env.NODE_ENV !== 'production';

module.exports = {
  module: {
    rules: [
      {
        test: /\.[jt]sx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: require.resolve('ts-loader'),
            options: {
              getCustomTransformers: () => ({
                before: [isDevelopment && ReactRefreshTypeScript()].filter(Boolean),
              }),
              transpileOnly: isDevelopment,
            },
          },
        ],
      },
    ],
  },
};

ts-loader won't work with HMR unless transpileOnly is set to true. You should use ForkTsCheckerWebpackPlugin if you need typechecking during development.

Using swc-loader

⚠️ Warning: This is an un-official integration maintained by the community.

Ensure your @swc/core version is at least 1.2.86. It is also recommended to use swc-loader version 0.1.13 or above.

Then, instead of wiring up react-refresh/babel via babel-loader, you can wire-up swc-loader and use the refresh transform:

module.exports = {
  module: {
    rules: [
      {
        test: /\.[jt]sx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: require.resolve('swc-loader'),
            options: {
              jsc: {
                transform: {
                  react: {
                    development: isDevelopment,
                    refresh: isDevelopment,
                  },
                },
              },
            },
          },
        ],
      },
    ],
  },
};

Starting from version 0.1.13, swc-loader will set the development option based on Webpack's mode option. swc won't enable fast refresh when development is false.

For more information on how to set up "Fast Refresh" with different integrations, please check out our examples.

Overlay Integration

This plugin integrates with the most common Webpack HMR solutions to surface errors during development - in the form of an error overlay.

By default, webpack-dev-server is used, but you can set the overlay.sockIntegration option to match what you're using.

The supported versions are as follows:

Dependency Version
webpack-dev-server 3.6.0+ or 4.x
webpack-hot-middleware 2.x
webpack-plugin-serve 0.x or 1.x

API

Please refer to the API docs for all available options.

FAQs and Troubleshooting

Please refer to the Troubleshooting guide for FAQs and resolutions to common issues.

License

This project is licensed under the terms of the MIT License.

Special Thanks

JetBrains Logo
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].