Description
Related plugins
Describe the bug
When using another vite plugin like @vue-macros/reactivity-transform that transforms the source code, HMR will always update the script part of an SFC even if it's not changed, for example when editing just css. This leads to the whole script being executed again and state being lost.
When changing css I expect this in the console:
[vite] hmr update /src/App.vue?vue&type=style&index=0&scoped=7a7a37b1&lang.css
but with another plugin present I get:
[vite] hmr update /src/App.vue, /src/App.vue?vue&type=style&index=0&scoped=7a7a37b1&lang.css
This most likely happens because handleHotUpdate reads the raw file, parses it, and compares it with the previous version from the cache populated by transformMain, which gets the already transformed code directly from vite. Since the untransformed and transformed source is always different, even if no edits happen, vite-plugin-vue assumes an edit happened and adds the script to the list of affected modules.
I looked a bit into a solution, but neither always caching with the raw source nor always transforming seems straight forward.
Applying the same plugins to the source in the handleHotUpdate
hook so it's based on the same code that the transform
hook gets is probably preferable, but I couldn't find anything to do that in the vite API.
Reproduction
https://stackblitz.com/edit/vitejs-vite-tzll9a?file=src%2FApp.vue&terminal=dev
Steps to reproduce
- Change something in the styles of
App.vue
- Save file
System Info
System:
OS: Linux 6.4 Arch Linux
CPU: (16) x64 AMD Ryzen 7 PRO 6850H with Radeon Graphics
Memory: 19.42 GB / 30.62 GB
Container: Yes
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.4.0 - /usr/bin/node
Yarn: 1.22.19 - /usr/bin/yarn
npm: 9.8.1 - /usr/bin/npm
pnpm: 8.6.9 - /usr/bin/pnpm
Browsers:
Chromium: 115.0.5790.102
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.