Unable to run dependency in es6 mode

I’ve created a small repro here: GitHub - dusty-phillips/rescript-repro: Is this a bug?

The readme covers the details, but the short summary is that when I set my bsconfig to es6 module format with mjs suffix, an import fails in the generated code of a dependency. It’s possible the dependency is “broken” as it’s quite an old buckle script library. However I have had it working in commonjs format, so I think it should be working in module format as well.

Am I doing something wrong / how can I work around this (without dropping module mode) / did I find a bug in Rescript?

hi having a look af the readme, my intuition is that the cause of default import.
Cjs and es6 have different semantics for default import

But should it not be mapped correctly either way?

This was discussed here es6 default import support by bobzhang · Pull Request #4641 · rescript-lang/rescript-compiler (github.com).

The root cause is that es6 module and commonjs is not 1:1.
There are some features in es6 can not be translated into commonjs, for example, toplevel await, default imports/exports.

For default import in es6, in babel, it was transpiled into

var val = require('module').default

In node, it was emulated as:

var val = require('module')

Neither of them is faithful to es6 import semantics, currently we followed the babel approach

1 Like

rescript-express by bloodyowl even made two different apis for es6 and commonjs to circumvent this problem:

Not a huge fan of this approach, but at least it’s telling the user that module imports are different.

1 Like

Would it be possible to have an annotation to compile conditionally one binding or another depending on the js output mode? That would solve it right?

Ah, I thought it would be possible because it’s rescript all the way down, but of course, it’s not. It’s rescript all the way to the js bindings and There lies the problem.

I guess ideally the js community will soon settle on not having common js syntax, but it’s going to be a while before rescript can stop supporting it.

1 Like

I made a proposal to address this issue, let me know what you think
make default import less painful · Issue #5149 · rescript-lang/rescript-compiler (github.com)

Wow, I think that would work!

This would be perfect! :ok_hand: