Skip to content

Fast Refresh not working with automatic runtime and file extension without "x" #83

@cknitt

Description

@cknitt

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

  1. Install dependencies and start vite:
npm i
npm run dev
  1. Increment counter
  2. 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

  1. Run npx patch-package to apply the patch from patches/@vitejs+plugin-react+3.0.1.patch.
  2. Restart vite and repeat the above test steps.
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions