vite-plugin-html-env icon indicating copy to clipboard operation
vite-plugin-html-env copied to clipboard

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

Open dsrkafuu opened this issue 2 years ago • 9 comments

@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 avatar Aug 11 '22 16:08 dsrkafuu

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

Devtools

dsrkafuu avatar Aug 11 '22 16:08 dsrkafuu

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.

dsrkafuu avatar Aug 11 '22 16:08 dsrkafuu

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__

yeion7 avatar Aug 25 '22 15:08 yeion7

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

Bonnev avatar Sep 06 '22 19:09 Bonnev

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

Many thanks, Mauro

mauro-ni avatar Sep 08 '22 09:09 mauro-ni

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

lxs24sxl avatar Sep 12 '22 15:09 lxs24sxl

@lxs24sxl Reminder 😇

Bonnev avatar Sep 21 '22 17:09 Bonnev

@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. 👀

lxs24sxl avatar Sep 29 '22 04:09 lxs24sxl

@lxs24sxl I can confirm it works!

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

Bonnev avatar Oct 01 '22 14:10 Bonnev