Closed
Description
This is a follow up of the forum post
When it comes to dynamic import of modules in ReScript, it looks verbose. For example, given a React component, we can make its dynamic counterpart like this:
module DynamicWidget = {
let makeProps = () => Js.Obj.empty() // <- 1
let make = {
// The example uses NextJS but it does not matter much
// The focus is on `import`
open Next.Dynamic
dynamic(
() =>
import_("./Widgets/Widget.bs.js") // <- 2
->PromiseX.map(x => x["make"]), // <- 3
options(~loading=() => <MaterialUi.CircularProgress />, ~ssr=false, ()),
)
}
}
// Later use <DynamicWidget /> as a regular component
Three issues inconveniences:
- Since we don’t use @react.component here, we have to define
makeProps
for myself. It should go away when JSX V4 will be released. - Here we should know the exact path to
.bs.js
. Perhaps some magic possible to just mention a module name and the path comes out automatically - We have to tweak the import result since ReScript components don’t provide the
default
export (aren’t they?).
The dynamic import problem is not specific to JSX / React components only. import
might be used for regular modules as well. I wonder if ReScript can make dynamic imports a little more ergonomic.