Skip to content

Commit 2be29cc

Browse files
fix: do not import 'react-refresh/runtime'
1 parent 4d864ee commit 2be29cc

3 files changed

Lines changed: 26 additions & 41 deletions

File tree

‎.changeset/clean-tigers-rescue.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/router-plugin': patch
3+
---
4+
5+
Fix React route HMR for webpack and rspack so it no longer imports `react-refresh/runtime`, avoiding failures when that optional dependency is not installed.

‎packages/router-plugin/src/core/hmr/webpack-adapter.ts‎

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
2018
export 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
`
8169
if (import.meta.webpackHot) {
@@ -104,7 +92,5 @@ if (import.meta.webpackHot) {
10492
syntacticPlaceholders: true,
10593
},
10694
)(),
107-
)
108-
109-
return statements
95+
]
11096
}

‎packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export const Route = createFileRoute('/posts')({
1515
loader: fetchPosts,
1616
component: TSRSplitComponent
1717
});
18-
import { performReactRefresh as __tsr_performReactRefresh } from 'react-refresh/runtime';
1918
if (import.meta.webpackHot) {
2019
const hot = import.meta.webpackHot;
2120
const hotData = hot.data ??= {};
@@ -149,16 +148,11 @@ if (import.meta.webpackHot) {
149148
};
150149
}
151150
})(routeId, Route);
152-
const tsrRefreshState = globalThis.__TSR_HMR__ ??= {};
153151
try {
154-
if (!tsrRefreshState.refreshScheduled) {
155-
tsrRefreshState.refreshScheduled = true;
156-
setTimeout(() => {
157-
tsrRefreshState.refreshScheduled = false;
158-
try {
159-
__tsr_performReactRefresh();
160-
} catch (_e) {}
161-
}, 30);
152+
const tsrReactRefreshUtils = typeof __react_refresh_utils__ !== 'undefined' ? __react_refresh_utils__ : undefined;
153+
const tsrEnqueueUpdate = tsrReactRefreshUtils && typeof tsrReactRefreshUtils.enqueueUpdate === 'function' ? tsrReactRefreshUtils.enqueueUpdate : undefined;
154+
if (tsrEnqueueUpdate) {
155+
tsrEnqueueUpdate(() => {});
162156
}
163157
} catch (_err) {}
164158
}

0 commit comments

Comments
 (0)