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

[Question] Will this work with React? #11

Closed
zeljkomarinkovic opened this issue Aug 30, 2022 · 5 comments
Closed

[Question] Will this work with React? #11

zeljkomarinkovic opened this issue Aug 30, 2022 · 5 comments

Comments

@zeljkomarinkovic
Copy link

zeljkomarinkovic commented Aug 30, 2022

I have tested it with semi success. Works only with legacy codebase not with functional components.
Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong. See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201 at Button.jsx:1:1
Have someone had similar problems?

@DitaXoXo
Copy link

DitaXoXo commented Sep 19, 2022

Hello !

The solution is actually hinted by the backend integration doc from vite, you also have solutions vitejs/vite#341 (comment) here and vitejs/vite#1984 here

I edited the vite() function like this :

function vite(string $entry)
{
    $vite_host = VITE_HOST;
    if (isDev()) {
        return <<<HTML
            <script type="module">
                import RefreshRuntime from "{$vite_host}/@react-refresh"
                window.\$RefreshReg$ = () => {}
                window.\$RefreshSig$ = () => (type) => type
                RefreshRuntime.injectIntoGlobalHook(window)
                window.__vite_plugin_react_preamble_installed__ = true
            </script>
            <script type="module" src="{$vite_host}/@vite/client"></script>
            <script type="module" src="{$vite_host}/assets/js/website.js"></script>
            <script src="{$vite_host}/{$entry}" type="module" defer></script>
        HTML;
    } else {
        return "\n" . jsTag($entry)
        . "\n" . jsPreloadImports($entry)
        . "\n" . cssTag($entry);
    }
}

@andrefelipe
Copy link
Owner

Sorry, I don't use React. Will leave this open in case someone finds the problem. Thanks all

@zeljkomarinkovic
Copy link
Author

@DitaXoXo Sorry for late reply. Yeah, the solution was like you said in Vite docs, glad you worked that out.

@andrefelipe
Copy link
Owner

Thanks guys. Added this information to the README. Best regards

@ainaarawaida
Copy link

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

4 participants