Skip to content

HMR always reloading script in SFC when just style is changed if using another vite plugin that transforms the script #211

Closed
@rashfael

Description

@rashfael

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

  1. Change something in the styles of App.vue
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions