Provides React Refresh support for Vite development server with hot module replacement capabilities
npx @tessl/cli install tessl/npm-vitejs--plugin-react-refresh@1.3.0@vitejs/plugin-react-refresh is a Vite plugin that provides React Refresh (Fast Refresh) support for React applications during development. It integrates with Vite's development server to enable hot module replacement (HMR) that preserves React component state while reloading code changes, providing instant feedback during development with minimal configuration.
npm install @vitejs/plugin-react-refreshimport reactRefresh from '@vitejs/plugin-react-refresh';For CommonJS:
const reactRefresh = require('@vitejs/plugin-react-refresh');// vite.config.js
import reactRefresh from '@vitejs/plugin-react-refresh';
export default {
plugins: [reactRefresh()]
};With TypeScript:
// vite.config.ts
import { defineConfig } from 'vite';
import reactRefresh from '@vitejs/plugin-react-refresh';
export default defineConfig({
plugins: [reactRefresh()]
});The plugin operates at build time as part of Vite's plugin system:
Creates a Vite plugin with React Refresh support.
/**
* Creates a Vite plugin that enables React Refresh for development
* @param options - Optional configuration for the plugin
* @returns Vite plugin object with React Refresh capabilities
*/
function reactRefresh(options?: Options): import('vite').Plugin;Usage Examples:
// Basic usage
import reactRefresh from '@vitejs/plugin-react-refresh';
export default {
plugins: [reactRefresh()]
};
// With custom parser plugins for experimental syntax
export default {
plugins: [
reactRefresh({
parserPlugins: ['classProperties', 'classPrivateProperties']
})
]
};
// With custom include/exclude patterns
export default {
plugins: [
reactRefresh({
include: '**/*.tsx',
exclude: [/\.stories\.(t|j)sx?$/, /node_modules/]
})
]
};Access to the preamble code used for React Refresh runtime setup.
/**
* Preamble code injected into HTML for React Refresh runtime initialization
* @type {string}
*/
reactRefresh.preambleCode: string;Usage Example:
// Access preamble code for custom HTML injection
import reactRefresh from '@vitejs/plugin-react-refresh';
console.log(reactRefresh.preambleCode);
// Contains the runtime setup code for React RefreshConfiguration options for the React Refresh plugin.
interface Options {
/**
* Additional Babel parser plugins to enable for experimental syntax
* @example ['classProperties', 'decorators-legacy']
*/
parserPlugins?: ParserOptions['plugins'];
/**
* Files to include for React Refresh processing
* @default /\.(t|j)sx?$/
* @example '**/*.tsx' or [/\.tsx$/, /\.jsx$/]
*/
include?: string | RegExp | Array<string | RegExp>;
/**
* Files to exclude from React Refresh processing
* @default /node_modules/
* @example [/\.stories\.(t|j)sx?$/, /node_modules/]
*/
exclude?: string | RegExp | Array<string | RegExp>;
}Vite plugin object returned by the factory function. The plugin conforms to Vite's Plugin interface and implements the following hooks:
// The plugin returns a standard Vite Plugin object with these key properties:
interface ReactRefreshPlugin extends import('vite').Plugin {
/** Plugin identifier */
name: 'react-refresh';
/** Plugin execution order - runs before other plugins */
enforce: 'pre';
}Extended from @babel/core for parser plugin configuration.
type ParserOptions = {
/**
* Babel parser plugins to enable
* @example ['jsx', 'typescript', 'decorators-legacy']
*/
plugins?: string[];
};The plugin handles several error scenarios:
When using Vite in middleware mode, you may encounter this error:
@vitejs/plugin-react-refresh can't detect preamble. Something is wrong.
See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201This occurs when your entry index.html file is not transformed with ViteDevServer.transformIndexHtml.
Solution: Explicitly transform your index.html in your server setup:
app.get('/', async (req, res, next) => {
try {
let html = fs.readFileSync(path.resolve(root, 'index.html'), 'utf-8')
html = await viteServer.transformIndexHtml(req.url, html)
res.send(html)
} catch (e) {
return next(e)
}
}).js, .jsx, .ts, and .tsx files.bs.js files