Skip to content

Explore how to implement the ergonomic dynamic import #5698

Closed
@mununki

Description

@mununki

Related #5593

  • Emit the inlined js module path instead of import statement in the header

Thanks to JSX v4, now we can write the dynamic import for react component:

// LazyComp.res
@react.component
let make = (~x) => React.string(x)

// App.res
@val external import: 'a => Js.Promise.t<'a> = "import"
type option = {ssr?: bool}
@val external dynamic: (unit => Js.Promise.t<'a>, option) => 'a = "dynamic"

module L = {
  let make = dynamic(() => import(LazyComp.make), {ssr: false})
}

<L x="lazy" />

This example passes the type checker as we intended, but the import statement in js output should be emitted differently.

As-is:

import * as LazyComp from "./LazyComp.bs.js";

var make = dynamic((function (param) {
        return import(LazyComp.make);
      }), {
      ssr: false
    });

To-be:

// omit the import statement
var make = dynamic((function (param) {
        return import("./LazyComp.bs.js"); // <-- inlined
      }), {
      ssr: false
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions