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

react-fast-refresh from scratch #166

Open
nojaf opened this issue Jan 10, 2022 · 0 comments
Open

react-fast-refresh from scratch #166

nojaf opened this issue Jan 10, 2022 · 0 comments

Comments

@nojaf
Copy link

nojaf commented Jan 10, 2022

Hello Dan,

After reading facebook/react#16604 (comment), I'm interested in what it would take to get this working without an existing bundler.

I'm asking in regards to the https://github.com/AngelMunoz/Perla project. This is a .NET devServer that replace imports from package to CDN URLs (from Skypack). Perla will create an import map so the browser understands everything but that is about it.
Basically in this setup, I have two givens:

  • I can manipulate any requested JS module
  • The server detects file changes and communicates these via SSE to a web worker.

Currently, the full page is reloaded when the web worker receives a changed event.
I'm wondering if it is possible to initiate the react refresh.

Imagine I have an app.js with:

import React from 'https://cdn.skypack.dev/react';

function App() {
    return React.createElement('div', null, `App component`);
}

export default App;

At some point, I know it was changed and wish to refresh it.

So my main question here is: can I pull this off?
The original guide speaks about:

try {

  // !!!
  // ...ACTUAL MODULE SOURCE CODE...
  // !!!
  ==> I cannot just put the source code here, the browser doesn't like the `import React from 'https://cdn.skypack.dev/react';` part
           The import and export should be top-level I guess?

} finally {
  window.$RefreshReg$ = prevRefreshReg;
  window.$RefreshSig$ = prevRefreshSig;
}

If the component changed, how exactly do I reload it in the browser? Can I just dynamically import it again?

And lastly, I believe I'll need to call let enqueueUpdate = debounce(runtime.performReactRefresh, 30); to trigger the refresh.

Thoughts?

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

1 participant