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

refactor: simplify $RefreshReg$ handling #204

Closed
wants to merge 1 commit into from

Conversation

sapphi-red
Copy link
Member

While reviewing #203 I found out that the code can be simplified.

Built on top of #203.

@sapphi-red sapphi-red changed the title refactor: simplify $RefreshReg$ handling refactor: simplify $RefreshReg$ handling May 9, 2024
Copy link
Member

@ArnaudBarre ArnaudBarre left a comment

Choose a reason for hiding this comment

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

Yeah I always wondered the need of this boilerplate. The main issue I have with that change is that the preamble is widely used and injected by frameworks and so we should also keep the window.$RefreshReg$ for the double installed check

@sapphi-red
Copy link
Member Author

The document says the following code needs to be injected.

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

So window.__vite_plugin_react_preamble_installed__ should be declared as long as they follow the document. The redundant window.$RefreshReg$/$RefreshSig$ won't be a problem.
If we want to keep compat further so that it works with users that dropped window.__vite_plugin_react_preamble_installed__ = true, we can change window.__vite_plugin_react_preamble_installed__ = true to window.$RefreshReg$ = () => {} and if (window.__vite_plugin_react_preamble_installed__) { to if (window.$RefreshReg$) {.

Base automatically changed from class-components to main May 20, 2024 09:05
@ArnaudBarre ArnaudBarre force-pushed the refactor/simplify-refresh-reg-code branch from 6a41c73 to 6cff501 Compare May 20, 2024 09:21
@ArnaudBarre
Copy link
Member

I just found that it breaks HMR on one of my utils that was manually registering the created itself with window.$RefreshReg$?.(Provider,${key}Provider); (Which could be improved on the transformation because the component is registered for hooks with $RefreshSig$);

I did a code search on GitHub but didn't find other usages, but given this is in the initial guide and that it's been used everywhere else I'm not sure it's good that when people come to Vite they have trouble because some lib they used was checking if $RefreshReg$ is in window as a proxy for dev for ex.

@sapphi-red
Copy link
Member Author

Ah, that initial guide is a good point.
I'll close this one.

@sapphi-red sapphi-red closed this May 22, 2024
@ArnaudBarre ArnaudBarre deleted the refactor/simplify-refresh-reg-code branch May 22, 2024 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants