Skip to content

Dynamic import ergonomics #5593

Closed
Closed
@nkrkv

Description

@nkrkv

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:

  1. 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.
  2. 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
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions