@@ -12,7 +12,7 @@ import {
1212 silenceUseClientWarning ,
1313 virtualPreamblePlugin ,
1414} from '@vitejs/react-common'
15- import type { Plugin } from 'vite'
15+ import type { Plugin , ServerOptions } from 'vite'
1616import { reactRefreshWrapperPlugin } from 'vite/internal'
1717import { reactCompilerPreset } from './reactCompilerPreset'
1818
@@ -66,11 +66,18 @@ export default function viteReact(opts: Options = {}): Plugin[] {
6666 const jsxImportDevRuntime = `${ jsxImportSource } /jsx-dev-runtime`
6767
6868 let runningInVite = false
69- let isProduction = true
7069 let skipFastRefresh = true
7170 let base : string
7271 let isBundledDev = false
7372
73+ function calculateSkipFastRefresh (
74+ isProduction : boolean ,
75+ command : 'serve' | 'build' ,
76+ hmr : ServerOptions [ 'hmr' ] ,
77+ ) {
78+ return isProduction || command === 'build' || hmr === false
79+ }
80+
7481 const viteBabel : Plugin = {
7582 name : 'vite:react-babel' ,
7683 enforce : 'pre' ,
@@ -109,11 +116,18 @@ export default function viteReact(opts: Options = {}): Plugin[] {
109116 if ( config . experimental . bundledDev ) {
110117 isBundledDev = true
111118 }
112- isProduction = config . isProduction
113- skipFastRefresh =
114- isProduction ||
115- config . command === 'build' ||
116- config . server . hmr === false
119+ if (
120+ skipFastRefresh !==
121+ calculateSkipFastRefresh (
122+ config . isProduction ,
123+ config . command ,
124+ config . server ?. hmr ,
125+ )
126+ ) {
127+ this . warn (
128+ `NODE_ENV (${ JSON . stringify ( process . env . NODE_ENV ) } ) or server.hmr was changed by plugins after the react plugin read the config. This may cause unexpected behavior.` ,
129+ )
130+ }
117131 } ,
118132 options ( options ) {
119133 if ( ! runningInVite ) {
@@ -148,8 +162,14 @@ export default function viteReact(opts: Options = {}): Plugin[] {
148162 const viteConfigPost : Plugin = {
149163 name : 'vite:react:config-post' ,
150164 enforce : 'post' ,
151- config ( userConfig ) {
152- if ( userConfig . server ?. hmr === false ) {
165+ config ( userConfig , { command } ) {
166+ skipFastRefresh = calculateSkipFastRefresh (
167+ // same with ResolvedConfig.isProduction
168+ process . env . NODE_ENV === 'production' ,
169+ command ,
170+ userConfig . server ?. hmr ,
171+ )
172+ if ( skipFastRefresh ) {
153173 return {
154174 oxc : {
155175 jsx : {
0 commit comments