Describe the bug
Currently, fast refresh is working for a file only if either
- the file extension ends with an "x" (e.g. "jsx", "tsx") or
- the code in the file imports
React in some way
Now when using a compile-to-JS language like ReScript with the new JSX runtime enabled, one can get .js or .mjs files with imports from "react/jsx-runtime" or "react/jsx-dev-runtime", but no React import, and fast refresh will not work for these files.
Reproduction
https://github.com/cknitt/vite-plugin-react-fast-refresh-issue
Steps to reproduce
- Install dependencies and start vite:
- Increment counter
- Change the text in
TestComponent.bs.js (alternatively change it in TestComponent.res and run npm run res:build to recompile). Observe that the counter value is reset to 0.
Workaround
- Run
npx patch-package to apply the patch from patches/@vitejs+plugin-react+3.0.1.patch.
- Restart vite and repeat the above test steps.
- Observe that TestComponent now refreshs correctly and the counter value remains unchanged.
System Info
System:
OS: macOS 13.1
CPU: (10) arm64 Apple M1 Pro
Memory: 893.44 MB / 32.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.17.1 - ~/Library/Caches/fnm_multishells/60384_1673351682301/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.15.0 - ~/Library/Caches/fnm_multishells/60384_1673351682301/bin/npm
Watchman: 2023.01.02.00 - /opt/homebrew/bin/watchman
Browsers:
Brave Browser: 108.1.46.153
Firefox: 107.0.1
Safari: 16.2
npmPackages:
@vitejs/plugin-react: ^3.0.1 => 3.0.1
vite: ^4.0.4 => 4.0.4
Used Package Manager
npm
Logs
No response
Validations
Describe the bug
Currently, fast refresh is working for a file only if either
Reactin some wayNow when using a compile-to-JS language like ReScript with the new JSX runtime enabled, one can get
.jsor.mjsfiles with imports from "react/jsx-runtime" or "react/jsx-dev-runtime", but noReactimport, and fast refresh will not work for these files.Reproduction
https://github.com/cknitt/vite-plugin-react-fast-refresh-issue
Steps to reproduce
TestComponent.bs.js(alternatively change it in TestComponent.res and runnpm run res:buildto recompile). Observe that the counter value is reset to 0.Workaround
npx patch-packageto apply the patch frompatches/@vitejs+plugin-react+3.0.1.patch.System Info
System: OS: macOS 13.1 CPU: (10) arm64 Apple M1 Pro Memory: 893.44 MB / 32.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.17.1 - ~/Library/Caches/fnm_multishells/60384_1673351682301/bin/node Yarn: 1.22.19 - /opt/homebrew/bin/yarn npm: 8.15.0 - ~/Library/Caches/fnm_multishells/60384_1673351682301/bin/npm Watchman: 2023.01.02.00 - /opt/homebrew/bin/watchman Browsers: Brave Browser: 108.1.46.153 Firefox: 107.0.1 Safari: 16.2 npmPackages: @vitejs/plugin-react: ^3.0.1 => 3.0.1 vite: ^4.0.4 => 4.0.4Used Package Manager
npm
Logs
No response
Validations