Closed
Description
What version of OCaml are you using?
This is using bs-platform@3.0.
It would be nice if bucklescript supported code splitting via dynamic imports. Even though dynamic import is stage 3, Webpack has supported a version of it for quite some time code splitting.
/* Foo.re */
let x = 5;
/* Importer.re */
import(Foo /* maybe just a relative path? */)
|> Js.Promise.then_(/* hand waving */);
would generate the following code
import ('./Foo.js').then(foo => foo.x);
Activity
xtuc commentedon Apr 22, 2018
If you have a webpack loader for buckelscript you can just compile it down to the JavaScript
import()
and Webpack will handle the compilation.kennetpostigo commentedon Apr 23, 2018
@xtuc this is not ideal. Not everyone uses webpack, nor should they be forced to if they are using bucklescript. I think this is more of, "Please support this within bucklescript platform itself" Just like bucklescript can support outputting ES Modules and CommonJS.
gilbert commentedon May 8, 2018
Supporting dynamic import like this might be tricky. In a browser environment, I don't know how BuckleScript would know what path to render, as it doesn't know the asset urls your server responds to. Also, what happens when you dynamically load an npm module?.
My personal feature wish is for BuckleScript to support
import : string => Js.Promise.t(???)
where we can, somehow, typecast the???
to a module of our choice.bloodyowl commentedon Jun 6, 2018
right now I think things could unlock pretty easily:
outputs
It'd only require BuckleScript to understand that in case
X
is a file, accessing its exports (herex
) is throughimportedX##x
and notimportedX[0]
cortopy commentedon Aug 19, 2018
Bumped into this https://github.com/kMeillet/bs-dynamic-import
cortopy commentedon Aug 19, 2018
and this (recently updated) https://github.com/thangngoc89/reason-loadable
outkine commentedon Dec 4, 2018
My use case is more simple - I just need to import a json dynamically using webpack, but because
[%%bs.raw]
doesn't support dynamic strings, I can't follow the advice presented in the bsb docs. Is there a different way?wegry commentedon Dec 4, 2018
@outkine Webpack's dynamic import can't accept arbitrary strings at runtime either. It only works with hard-coded strings.
yawaramin commentedon Jan 17, 2019
Here's a hacky workaround that relies on BuckleScript's nice feature of not mangling names.
This generates the output:
BlueHotDog commentedon Jul 9, 2020
@bobzhang maybe close this issue and consolidate discussion to:
#3919
module type of
rescript-lang/rescript-lang.org#74wegry commentedon Jun 16, 2022
Closing as I think #3919 probably supersedes this. I've been out of the Reason/Rescript game for quite some time now.