vitejs / vite-plugin-vue

Vite Vue Plugins
MIT License
498 stars 153 forks source link

preserve state on HMR when multiple components exists in a single file #107

Open wxs77577 opened 1 year ago

wxs77577 commented 1 year ago

Related plugins

Describe the bug

import { defineComponent } from "vue";

export const Test = defineComponent({
  render() {
    return <b>Then change me...</b>;
  },
});

export default defineComponent({
  render() {
    return (
      <div>
        <div>
          <input type="text" placeholder="Type fist..." />
        </div>
        <Test></Test>
      </div>
    );
  },
});

Reproduction

https://stackblitz.com/edit/vitejs-vite-uda5gz?file=src/main.ts

Steps to reproduce

No response

System Info

System:
    OS: macOS 13.1
    CPU: (8) arm64 Apple M1
    Memory: 125.39 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.18.0 - /opt/homebrew/bin/npm
  Browsers:
    Safari: 16.2
  npmPackages:
    @vitejs/plugin-vue: ^4.0.0 => 4.0.0 
    @vitejs/plugin-vue-jsx: ^3.0.0 => 3.0.0 
    vite: ^4.0.0 => 4.1.1

Used Package Manager

npm

Logs

No response

Validations

sapphi-red commented 1 year ago

I wasn't able to reproduce. Would you write a more detailed steps?

wxs77577 commented 1 year ago

I wasn't able to reproduce. Would you write a more detailed steps?

The problem is when I change the jsx template of Test component and save the App.tsx, the msg in the default component has been reseted.

I've made some changes in App.tsx:

https://stackblitz.com/edit/vitejs-vite-uda5gz?file=src%2FApp.tsx,src%2Fstyle.css,src%2Fmain.ts

sapphi-red commented 1 year ago

I see.

To achieve that, we somehow need to skip calling __VUE_HMR_RUNTIME__.reload for the default component. I'm not sure how we can do that though.

edison1105 commented 1 month ago

it should call __VUE_HMR_RUNTIME__.rerender instead of reload.