Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hmr work not correctly with jsx plugin for vue3 #1486

Closed
Jokcy opened this issue Jan 11, 2021 · 8 comments · Fixed by #1495
Closed

hmr work not correctly with jsx plugin for vue3 #1486

Jokcy opened this issue Jan 11, 2021 · 8 comments · Fixed by #1495

Comments

@Jokcy
Copy link
Contributor

Jokcy commented Jan 11, 2021

Describe the bug

When using jsx-plugin.

function getText(text){
  return `hello ${text}`
}

export default defineComponent({
  setup() {
    return () => <div>{getText('world')}</div>
  }
})

In this example, if I update the default exported component, it will hot update successfully. But if I edit the getText function for example change hello to Hi. The component will not hot update.

It mainly because jsx-plugin only detect component update, but in file written in jsx, people may split some logic into functions or some others.

Since we can't hold component state when hmr in jsx, maybe just not accept, let all component rerender is a better choice?

I will be happy to make a pr to help after we determine the best way to handle hmr in jsx.

Reproduction

I guess it's clear enough...

System Info

  • vite version: 2.0.0-beta.7
  • Operating System: macos big sur
  • Node version: 14
  • Package manager (npm/yarn/pnpm) and version: yarn
@yyx990803
Copy link
Member

Yeah, it looks like to be safe we should just update all components exported from the file. PR welcome.

@Jokcy
Copy link
Contributor Author

Jokcy commented Jan 12, 2021

Yeah, it looks like to be safe we should just update all components exported from the file. PR welcome.

Ok I will do it this week

@Jokcy
Copy link
Contributor Author

Jokcy commented Jan 12, 2021

@yyx990803 I found these seems no way to fully reload a module without refresh, not like webpack, vite use native esmodule system and there is no module proxy. Emm... so if I find same code other then component changed I can only refresh page to reload right? If I miss something please give me some advice.

@yyx990803
Copy link
Member

The reason it's not updating now is because the component's own code did not change, so it didn't even trigger an update. You need to remove all the hash checks comparing prev/next components in the plugin codegen so it always updates every exported component.

@Jokcy
Copy link
Contributor Author

Jokcy commented Jan 12, 2021

Yeah I know this will work for components. But what if this module export something not component, do we have a method to tell other module which imported this module? or maybe we don't need to care about this.

@Jokcy Jokcy mentioned this issue Jan 12, 2021
@yyx990803
Copy link
Member

yyx990803 commented Jan 12, 2021

If not every export is a component, then the module cannot be HMRed (no import.meta.hot injection). I think the fast refresh plugin has similar heuristics - it checks if every export has uppercase name.

@NoraH1to
Copy link

If i use tsx/jsx in a .vue file like this:

<script lang="tsx">
export default defineComponent({
  setup: (props) => {
    return () => (
      <>
        <h1>hello</h1>
        <h1>world</h1>
      </>
    );
  },
});
</script>

<style scoped>
...
</style>

When i change JSXElement like <h1>new hello</h1>, DOM will not update.


If i just use in .tsx file:

export default defineComponent({
  setup: (props) => {
    return () => (
      <>
        <h1>hello</h1>
        <h1>world</h1>
      </>
    );
  },
});

It work.

Is this a bug?

@jiangmaniu
Copy link

@SterbenJ The problem I encountered is the same as yours. Both the syntax of the .tsx/.jsx file and the template syntax of the .vue file vite will be hmr. Only when the .vue file uses the tsx/jsx syntax, vite will not hmr.

我遇到的问题与你一致,使用 .tsx/.jsx 文件的语法和 .vue 文件的 template 语法 vite 都会 hmr。唯独在 .vue 文件使用 tsx/jsx语法时,vite 不会 hmr 。

@github-actions github-actions bot locked and limited conversation to collaborators Jul 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants