React Snowpack HMR

Has anyone built a React-Rescript Snowpack application? I don’t have any problems with getting it working, just interested in getting the Hot Module Reload to work in ReScript.

Basically, you need this snippet in your index.js:

if (import.meta.hot) {
  import.meta.hot.accept();
}

And the HMR works when I add that in index.bs.js, but I obviously need to re-add that every time my index.res recompiles. I’m not too sure what import.meta is or how to bind to it. Any advice?

Adding it in manually is not the worst experience, but definitely not ideal :slight_smile:

Another solution I’ve thought of: is there some way to hook into the compiler to add it in at compile time?

Otherwise I’m also happy to move to ViteJS if anyone can tell me that it works there? Thanks!

Wait, I can probably add that as raw js hey? Let me try that first :thinking:

Getting an FFI warning on the import. syntax when embedding raw js…like I said, I’m not even sure what that code snippet does, so any help would be appreciated :slight_smile:

using raw is the right solution. it is okay to ignore that warning

1 Like

Any way to suppress a warning in Rescript? :slight_smile:

Hm, warning went away when I restarted the compiler :thinking: thanks :slight_smile:

I’m using:

@scope(("import", "meta")) @val external hot: bool = "hot"
@scope(("import", "meta", "hot")) @val
external accept: unit => unit = "accept"

switch ReactDOM.querySelector("#root") {
| None => Js.Console.log("Could not find #root element")
| Some(v) => ReactDOM.render(<React.StrictMode> <App /> </React.StrictMode>, v)
}

if hot {
  accept()
}

Which is very close to the raw solution.

1 Like

Fantastic thanks! Works perfectly :slight_smile: