-
-
Notifications
You must be signed in to change notification settings - Fork 153
fix(hmr): skip HMR for JSX files with hooks #480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this only remove warnings for non component files? It looks like the test case here and the repor in #354 is working in terms of HMR without this change.
Another interesting case is that the module exports only hook, but internally there's a non-exported components. Transform injects $RefreshReg$(_c, "Button")
for this case, so refreshContentRE
matches and thus warning still appears (but HMR still works).
// useButtonHook.tsx
export function useButtonHook() {
return <Button />
}
function Button() {
const [count, setCount] = useState(0)
return (
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
)
}
The change seems good, but I just want to confirm what this change is fixing.
Current HMR of hooks is "always working", but it as a strange behaviour that makes the DX not great:
This is because we only apply React refresh transform on JSX files & the current logic says that if the file contains I updated the code to have files with only hooks do the refresh self update, but without success, it lead to some stale updates. Plus this would requires extra complexity and running the react refresh transformation on all .js/.ts files to be consistent, which would be a big change. This change is safe and makes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file name seems to be inconsistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oups bad copy paste ahah
Thanks for clarifying it, but I'm not sure I got the answer 😅 My question was, in terms of user visible difference, does it only remove warnings for hooks exports only jsx/tsx files? |
37c7762
to
01f1e68
Compare
@hi-ogawa Yes you are right this was too much and too technical information, I changed to only tell the user facing change and people can read the comment above to better understand the change if needed |
Thanks! What's left is just renaming |
| datasource | package | from | to | | ---------- | -------------------- | ----- | ----- | | npm | @vitejs/plugin-react | 4.4.1 | 4.5.0 | ## [v4.5.0](https://github.com/vitejs/vite-plugin-react/blob/HEAD/packages/plugin-react/CHANGELOG.md#450-2025-05-23) ##### Add `filter` for rolldown-vite [#470](vitejs/vite-plugin-react#470) Added `filter` so that it is more performant when running this plugin with rolldown-powered version of Vite. ##### Skip HMR for JSX files with hooks [#480](vitejs/vite-plugin-react#480) This removes the HMR warning for hooks with JSX.
| datasource | package | from | to | | ---------- | -------------------- | ----- | ----- | | npm | @vitejs/plugin-react | 4.4.1 | 4.5.0 | ## [v4.5.0](https://github.com/vitejs/vite-plugin-react/blob/HEAD/packages/plugin-react/CHANGELOG.md#450-2025-05-23) ##### Add `filter` for rolldown-vite [#470](vitejs/vite-plugin-react#470) Added `filter` so that it is more performant when running this plugin with rolldown-powered version of Vite. ##### Skip HMR for JSX files with hooks [#480](vitejs/vite-plugin-react#480) This removes the HMR warning for hooks with JSX.
| datasource | package | from | to | | ---------- | -------------------- | ----- | ----- | | npm | @vitejs/plugin-react | 4.4.1 | 4.5.0 | ## [v4.5.0](https://github.com/vitejs/vite-plugin-react/blob/HEAD/packages/plugin-react/CHANGELOG.md#450-2025-05-23) ##### Add `filter` for rolldown-vite [#470](vitejs/vite-plugin-react#470) Added `filter` so that it is more performant when running this plugin with rolldown-powered version of Vite. ##### Skip HMR for JSX files with hooks [#480](vitejs/vite-plugin-react#480) This removes the HMR warning for hooks with JSX.
The current HMR implementation was trying to all HMR files that contains either hooks or components, but this was working only for components and lead to HMR invalidation for JSX files containing hooks.
The best solution would have been to support HMR for hooks, but in my testing it was sometimes leading to stale updates. So this simple and reliable solution is to skip HMR for these files and have the components handle the updates, like any other hooks file.
Fixes #289
Fixes #354
Fixes #411