Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/plugin-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Add `refreshInclude` and `refreshExclude` options ([#1516](https://github.com/vitejs/vite-plugin-react/pull/1516))

Add `refreshInclude` and `refreshExclude` options to skip fast refresh for certain files that won't run in the browser.

## 6.1.2 (2026-10-05)

### Fix HMR for compound components ([#1484](https://github.com/vitejs/vite-plugin-react/pull/1484))
Expand Down
48 changes: 37 additions & 11 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
silenceUseClientWarning,
virtualPreamblePlugin,
} from '@vitejs/react-common'
import type { Plugin, ServerOptions } from 'vite'
import { createFilter, type Plugin, type ServerOptions } from 'vite'
import { reactRefreshWrapperPlugin } from 'vite/internal'
import type { ReactCompilerOptions } from '#optionalTypes'
import { defaultCodeFilter, reactCompilerPreset } from './reactCompilerPreset'
Expand Down Expand Up @@ -44,6 +44,16 @@ export interface Options {
* @default /\/node_modules\//
*/
exclude?: string | RegExp | Array<string | RegExp>
/**
* Can be used to skip fast refresh for certain files that won't run in the browser.
* @example options.include
*/
refreshInclude?: string | RegExp | Array<string | RegExp>
/**
* Can be used to skip fast refresh for certain files that won't run in the browser.
* @default options.exclude
*/
refreshExclude?: string | RegExp | Array<string | RegExp>
/**
* Control where the JSX factory is imported from.
* https://oxc.rs/docs/guide/usage/transformer/jsx.html#import-source
Expand Down Expand Up @@ -78,6 +88,12 @@ const defaultExcludeRE = /\/node_modules\//
function viteReact(opts: Options = {}): Plugin[] {
const include = opts.include ?? defaultIncludeRE
const exclude = opts.exclude ?? defaultExcludeRE
const jsxRefreshInclude = makeIdFiltersToMatchWithQuery(
opts.refreshInclude ?? include,
)
const jsxRefreshExclude = makeIdFiltersToMatchWithQuery(
opts.refreshExclude ?? exclude,
)

const jsxImportSource = opts.jsxImportSource ?? 'react'
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
Expand Down Expand Up @@ -108,8 +124,8 @@ function viteReact(opts: Options = {}): Plugin[] {
runtime: 'classic',
refresh,
},
jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude),
jsxRefreshInclude,
jsxRefreshExclude,
},
}
} else {
Expand All @@ -120,8 +136,8 @@ function viteReact(opts: Options = {}): Plugin[] {
importSource: opts.jsxImportSource,
refresh,
},
jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude),
jsxRefreshInclude,
jsxRefreshExclude,
},
optimizeDeps: {
rolldownOptions: { transform: { jsx: { runtime: 'automatic' } } },
Expand Down Expand Up @@ -170,8 +186,8 @@ function viteReact(opts: Options = {}): Plugin[] {

return reactRefreshWrapperPlugin({
cwd: process.cwd(),
include: makeIdFiltersToMatchWithQuery(include),
exclude: makeIdFiltersToMatchWithQuery(exclude),
include: jsxRefreshInclude,
exclude: jsxRefreshExclude,
jsxImportSource,
reactRefreshHost: opts.reactRefreshHost ?? '',
}) as unknown as boolean
Expand Down Expand Up @@ -301,7 +317,10 @@ function createReactCompilerPlugin(
{ logDiagnostics, ...reactCompilerOptions }: ReactCompilerPluginOptions,
include: NonNullable<Options['include']>,
exclude: NonNullable<Options['exclude']>,
reactOptions: Pick<Options, 'jsxRuntime' | 'jsxImportSource'>,
reactOptions: Pick<
Options,
'jsxRuntime' | 'jsxImportSource' | 'refreshInclude' | 'refreshExclude'
>,
isFastRefreshEnabled: () => boolean,
): Plugin {
let jsxDevelopment = false
Expand All @@ -313,6 +332,10 @@ function createReactCompilerPlugin(
const jsxImportSource = reactOptions.jsxImportSource ?? 'react'
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`
const jsxRefreshFilter =
reactOptions.refreshInclude || reactOptions.refreshExclude
? createFilter(reactOptions.refreshInclude, reactOptions.refreshExclude)
: undefined

const loadCompiler = async (
onError: (message: string) => never,
Expand Down Expand Up @@ -359,9 +382,12 @@ function createReactCompilerPlugin(
const refreshEnabled =
isClient &&
isFastRefreshEnabled() &&
(isJSX ||
code.includes(jsxImportRuntime) ||
code.includes(jsxImportDevRuntime))
((jsxRefreshFilter && !jsxRefreshFilter(id)) ||
!(
isJSX ||
code.includes(jsxImportRuntime) ||
code.includes(jsxImportDevRuntime)
))

const shouldCompile =
isClient &&
Expand Down
Loading