vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.66k stars 6.2k forks source link

unexpected full reloads for modules that are not referenced #17806

Open mmmmmmmgo opened 3 months ago

mmmmmmmgo commented 3 months ago

Describe the bug

  1. First, App.tsx import the Foo method of a.ts
  2. Then I removed the Foo method, and now a.ts is not imported by any file
  3. Modify the a.ts file at this time. After saving, vite reload the entire page, HMR does not work.

https://github.com/user-attachments/assets/c90c59f2-99f1-4492-983d-27b83f035089

Reproduction

https://stackblitz.com/edit/vitejs-vite-d4lgyd?file=src%2Fa.ts,src%2FApp.tsx&terminal=dev

Steps to reproduce

No response

System Info

...

Used Package Manager

pnpm

Logs

No response

Validations

stackblitz[bot] commented 3 months ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

mmmmmmmgo commented 3 months ago

This may be the key code

https://github.com/vitejs/vite/blob/720447ee725046323387f661341d44e2ad390f41/packages/vite/src/node/server/hmr.ts#L399-L401

hi-ogawa commented 3 months ago

The behavior might not be entirely ideal, but it doesn't look so bothering either. Can you maybe explain if you have any concrete use cases where this scenario shouldn't full reload?

bluwy commented 3 months ago

Yeah this may be hard to fix because the no-importers check is used to assume a module is a root, e.g. if you ssrLoadModule a .js file, that file would be the root so it assumes a full reload.

If we want to fix this, we need an alternate way to track the root, which is similar to https://github.com/vitejs/vite/issues/16664 that is the inverse of the problem, where a circular dep causes the root to not be detected and miss a reload.