Skip to content

Feature request: Support dynamic import() for code splitting #2364

Closed
@glennsl

Description

@glennsl

Dynamic import() is a TC39 proposal currently in stage 3 and already supported by several bundlers. Details here: https://github.com/tc39/proposal-dynamic-import.

The gist of it is: a new global function, import(), that behaves (exactly?) like require() except it returns a promise so that the module can be loaded lazily. In pseudo-OCaml:

val import : string => (module type of 'Whatever) Js.Promise.t

Problem

I don't think it's possible to use this from BuckleScript today no matter how many hacks you throw at it. You'd have to convert a toplevel module into a first-class module somehow, which I don't think is possible without type information only available to the compiler.

Proposal

module type L = module type of List (* I think you have to do this. Would be nice if this could be generated too *)

[%import (List : L)]
|> Js.Promise.then_ (fun (module List) -> List.map ...)

As far as I understand extension points, the (List : L) inside %import just needs to be valid OCaml syntax and wouldn't actually load the module. The extension point could then generate:

(import('stdlib/list').then(function (List) { return [List.map, List.filter, ...] }))

cc @kennetpostigo who brought this to me

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