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

Adds a config flag to add React Refresh code #15

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Xenoveritas
Copy link

#11 is caused by a chunk of code that the vitejs/react-plugin plugin adds missing. I'm not sure what the best way to add this is. This PR almost certainly isn't it, but I was able to confirm that it does work with the React code I'm using. (It would probably be possible to pull the "correct" code out of the installed plugin itself, and then only add it if the plugin can be resolved. Or maybe that would be worse.) This is a "simple" fix, though.

This makes it possible to use the plugin via a GitHub URL
package.json Outdated Show resolved Hide resolved
index.ts Outdated Show resolved Hide resolved
@Dan-Shields
Copy link
Member

I thought I submitted this review but never pressed confirm - apologies!

@Xenoveritas
Copy link
Author

Finally got a chance to get back to this - Node.js releasing v22 as LTS cooked my existing NodeCG install.

@Dan-Shields
Copy link
Member

I don't use react, so not in the best place to review this PR. @Hoishin do you have capacity to help?

@Hoishin
Copy link
Member

Hoishin commented Dec 31, 2024

I will take a look at this next year!

@Dan-Shields Dan-Shields requested a review from Hoishin December 31, 2024 20:46
// Capture resolved config for use in injectAssets
config = resolvedConfig
// Check to see if one of the plugins is vite:react-refresh
if (resolvedConfig.plugins.find((plugin) => plugin.name === 'vite:react-refresh')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.find() returns the found object but this is only used as a boolean. should use .some() instead.

// Check to see if one of the plugins is vite:react-refresh
if (resolvedConfig.plugins.find((plugin) => plugin.name === 'vite:react-refresh')) {
// If it is, import it and get the preamble code from it if possible
reactPreamble = (await import('@vitejs/plugin-react'))?.default?.preambleCode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part should handle when @vitejs/plugin-react is not installed, in which case it would throw an error

Comment on lines +92 to +98
tags.push(
`<script type="module">${reactPreamble.replace(/__BASE__/g, `${dSrvProtocol}://${path.posix.join(
dSrvHost,
'bundles',
bundleName
)}/`)}</script>`
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to my knowledge and information available online, in order to enable React Refresh, you have to add these lines in HTML:

<script type="module">
  import RefreshRuntime from 'http://localhost:5173/@react-refresh'
  RefreshRuntime.injectIntoGlobalHook(window)
  window.$RefreshReg$ = () => {}
  window.$RefreshSig$ = () => (type) => type
  window.__vite_plugin_react_preamble_installed__ = true
</script>

reference: https://vite.dev/guide/backend-integration

The script lines here is very different. Is there any official documentation about this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the default preamble that plugin-react exports is exactly that: https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/src/fast-refresh.ts#L23-L29

I think that means this is setup correctly?

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

Successfully merging this pull request may close these issues.

3 participants