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

Confilt with @vitejs/plugin-react 2.0.0 + Vite 3 and breaks HMR #22

Open
dsrkafuu opened this issue Aug 11, 2022 · 9 comments
Open

Confilt with @vitejs/plugin-react 2.0.0 + Vite 3 and breaks HMR #22

dsrkafuu opened this issue Aug 11, 2022 · 9 comments

Comments

@dsrkafuu
Copy link

dsrkafuu commented Aug 11, 2022

@vitejs/plugin-react can't detect preamble. Something is wrong.

Reproduce:

Checkout the main branch of this project: https://github.com/dsrkafuu/skyline-overlay

Simply upgrade vite to ^3.0.0 and @vitejs/plugin-react to ^2.0.0, then start the dev server & open the browser.

Removes the HtmlEnv plugin in vite.config.js, it works well.

@dsrkafuu
Copy link
Author

  • @vitejs/plugin-react: 2.0.0
  • vite: 3.0.5
  • vite-plugin-html-env: 1.2.4

Devtools

@dsrkafuu
Copy link
Author

The README of React plugin mentioned this Error:
https://github.com/vitejs/vite/tree/main/packages/plugin-react#middleware-mode

Can't confirm if this is the cause of the problem though.

@yeion7
Copy link

yeion7 commented Aug 25, 2022

I think the issue is that the react refresh script gets transform

from:

import RefreshRuntime from "/@react-refresh"
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true

to:

import RefreshRuntime from "/@react-refresh"RefreshRuntime.injectIntoGlobalHook(window)window.$RefreshReg$ = () => {}window.$RefreshSig$ = () => (type) => typewindow.__vite_plugin_react_preamble_installed__ = true

that makes window.__vite_plugin_react_preamble_installed__, convert to typewindow.__vite_plugin_react_preamble_installed__

@Bonnev
Copy link

Bonnev commented Sep 6, 2022

Hi all,

Problem is exactly what @yeion7 said. It is located in src/parse/index.js line 82 (in current master):

this.html = this.html.replace(/\n/g, '')

which removes all new lines. Since the code generated by plugin-react doesn't have semicolons, no delimiter remains between "/@react-refresh" and RefreshRuntime

Removing this line directly in node_modules/vite-plugin-html-env/lib/parse/index.js fixes the issue.

Now, depending on the reason for adding this line in the first place, the fix would be different.

@lxs24sxl FYI

@mauro-ni
Copy link

mauro-ni commented Sep 8, 2022

Hi all,
is there any information about the time it will take to fix the problem?

Many thanks,
Mauro

@lxs24sxl
Copy link
Owner

Sorry guys, busy with wedding stuff this month. I'll deal with those in the next few days.

@Bonnev
Copy link

Bonnev commented Sep 21, 2022

@lxs24sxl Reminder 😇

@lxs24sxl
Copy link
Owner

@mauro-ni @yeion7 @Bonnev @dsrkafuu

Thanks for the feedback, I have solved this problem in version 1.2.7.

The plugin incorrectly parsed and integrated the code because the new version of the plugin (@vitejs/plugin) was outputting a file without a semicolon in some of the code.

<script type="module" src="/src/index.tsx">
    import RefreshRuntime from "/@react-refresh"
    RefreshRuntime.injectIntoGlobalHook(window)
    window.$RefreshReg$ = () => {}
    window.$RefreshSig$ = () => (type) => type
    window.__vite_plugin_react_preamble_installed__ = true
 </script>

PS: Preparing for the wedding is really busy, but meaningful. 👀

@Bonnev
Copy link

Bonnev commented Oct 1, 2022

@lxs24sxl I can confirm it works!

PS: I'm sure it will be the best wedding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants