@@ -13,9 +13,7 @@ import type * as t from '@babel/types'
1313 * `routeId` out of `hot.data`. `hot.dispose` stashes it for the next run, and
1414 * `hot.accept()` (no callback) enrolls us as a self-accepting boundary.
1515 *
16- * Returns an array of statements so that for React we can prepend an
17- * `import { performReactRefresh } from 'react-refresh/runtime'` hoisted to the
18- * top of the module.
16+ * Returns an array of statements that patches route definitions during HMR.
1917 */
2018export function createWebpackHmrStatement (
2119 stableRouteOptionKeys : Array < string > ,
@@ -30,21 +28,16 @@ export function createWebpackHmrStatement(
3028 ? JSON . stringify ( opts . routeId )
3129 : 'undefined'
3230
33- const statements : Array < t . Statement > = [ ]
34-
3531 // React-only: route modules aren't React Refresh "boundaries" (they export
3632 // a non-component `Route`), so the bundler's react-refresh runtime won't
3733 // call `performReactRefresh` for us. We kick it manually after swapping
3834 // route options so newly-registered component bodies get patched into live
3935 // fibers.
4036 //
41- // We import `performReactRefresh` directly from `react-refresh/runtime` —
42- // the canonical public API — rather than relying on the ProvidePlugin-
43- // injected `__react_refresh_utils__` global, whose name is an internal
44- // detail of `@rspack/plugin-react-refresh`. The rspack plugin aliases
45- // `react-refresh` → its bundled runtime (getRefreshRuntimeDirPath), so this
46- // resolves to the same singleton the plugin itself uses and shares the
47- // registry React was patched against.
37+ // Webpack and Rspack refresh plugins inject `__react_refresh_utils__` via
38+ // ProvidePlugin. Use it when present instead of importing
39+ // `react-refresh/runtime`, because Rsbuild apps may use React without the
40+ // React plugin and therefore may not have that optional dependency installed.
4841 //
4942 // Use the same delayed refresh style as Rspack's React Refresh runtime.
5043 // Route modules and their split component chunks can arrive in separate HMR
@@ -55,27 +48,22 @@ export function createWebpackHmrStatement(
5548 const reactRefreshCall =
5649 opts . targetFramework === 'react'
5750 ? `
58- const tsrRefreshState = globalThis.__TSR_HMR__ ??= {}
5951 try {
60- if (!tsrRefreshState.refreshScheduled) {
61- tsrRefreshState.refreshScheduled = true
62- setTimeout(() => {
63- tsrRefreshState.refreshScheduled = false
64- try { __tsr_performReactRefresh() } catch (_e) { /* noop */ }
65- }, 30)
52+ const tsrReactRefreshUtils =
53+ typeof __react_refresh_utils__ !== 'undefined'
54+ ? __react_refresh_utils__
55+ : undefined
56+ const tsrEnqueueUpdate =
57+ tsrReactRefreshUtils && typeof tsrReactRefreshUtils.enqueueUpdate === 'function'
58+ ? tsrReactRefreshUtils.enqueueUpdate
59+ : undefined
60+ if (tsrEnqueueUpdate) {
61+ tsrEnqueueUpdate(() => {})
6662 }
6763 } catch (_err) { /* noop */ }`
6864 : ''
6965
70- if ( opts . targetFramework === 'react' ) {
71- statements . push (
72- template . statement (
73- `import { performReactRefresh as __tsr_performReactRefresh } from 'react-refresh/runtime'` ,
74- ) ( ) ,
75- )
76- }
77-
78- statements . push (
66+ return [
7967 template . statement (
8068 `
8169if (import.meta.webpackHot) {
@@ -104,7 +92,5 @@ if (import.meta.webpackHot) {
10492 syntacticPlaceholders : true ,
10593 } ,
10694 ) ( ) ,
107- )
108-
109- return statements
95+ ]
11096}
0 commit comments