Skip to content
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

HMR does not work for *.js but works for *.jsx #402

Open
1 of 4 tasks
rawasaditya opened this issue Jan 7, 2025 · 1 comment
Open
1 of 4 tasks

HMR does not work for *.js but works for *.jsx #402

rawasaditya opened this issue Jan 7, 2025 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@rawasaditya
Copy link

Documentation is

  • Missing
  • Outdated
  • Confusing
  • Not sure?

Explain in Detail

See vitejs/vite#14649

I am facing this issue too, there is no support for HMR if react component in written in js file instead of jsx. Not sure if this has to be part of documentation of vite because I think its specific to library like react

Your Suggestion for Changes

Just like here https://vite.dev/guide/troubleshooting.html#a-full-reload-happens-instead-of-hmr we have documentation for cyclic deps

Also there is a github issue linked for server.watch https://vite.dev/config/server-options.html#server-watch

Similarly there has to be documentation for this as well

Reproduction

https://github.com/rawasaditya/vite-hmr-reproduction/tree/main

Steps to reproduce

clone given project

npm i
npm run dev
change App.js to see browser reload

@rawasaditya rawasaditya added the documentation Improvements or additions to documentation label Jan 7, 2025
@hi-ogawa hi-ogawa transferred this issue from vitejs/vite Jan 7, 2025
@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Jan 7, 2025

I'm not sure what's intended by the plugin, but adding import "react/jsx-dev-runtime" seems to enable fast refresh transform.

const useFastRefresh =
!skipFastRefresh &&
!ssr &&
(isJSX ||
(opts.jsxRuntime === 'classic'
? importReactRE.test(code)
: code.includes(jsxImportDevRuntime) ||
code.includes(jsxImportRuntime)))

So, something like this actually gives HMR for .js as well.

import React from 'react';
import "react/jsx-dev-runtime"; /// 👈👈👈

function App() {
   ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants