Rescript-compiler: Provide .mjs support for bsconfig.json suffix

Created on 21 Feb 2019  路  10Comments  路  Source: rescript-lang/rescript-compiler

Relevant to #2037 -- I think it might be a good idea to add .mjs support now

Most helpful comment

Hmm actually I don't think emitting both commonjs and es6 would solve my problem, because the JS that my project depends on uses ES6 import syntax to optimize for tree shaking. I don't think that would work in general unless the modules are written in commonjs, in which case it's a non issue anyway.

My 2c is that we should include mjs now, it'll ease some pain points for people who want tree shaking and dual browser-nodejs uses as well. Anyone who tries to use node to run mjs files will know that it's experimental because they have to pass the flag to node, or they'll use something like esm.

All 10 comments

so does nodejs settle on .mjs for es module support?

Yeah I think so, at least for the foreseeable future. I don't think they have much of a choice since they can't make commonjs backward compatible:
https://nodejs.org/api/esm.html#esm_enabling

My use-case right now is that I'm writing a library to be used for both frontend and backend, but it's a large library and needs to really make use of tree-shaking to be practical on the frontend.

But in order to get the best tree-shaking I need to use es6 modules, which would mean that the backend would need to create its own bundle unless it can use nodes mjs support or something like https://github.com/standard-things/esm to load the JS. I tried both loaders but it looks like they require the files to be mjs files for either of them to really work properly

@Risto-Stevcev are you aware that you can emit both es6 and commonjs at the same time?
I am okay to add mjs support, but would be better to wait for NodeJS to remove experimental flag

How do you emit both at the same time? I tried adding both but I couldn't get it to work for some reason

"package-specs" : ["commonjs", "es6"]

should work

Ah thanks. No wonder it wasn't working, I was doing this

  "package-specs": {
    "module": ["commonjs", "es6"],
    "in-source": false
  },

Hmm actually I don't think emitting both commonjs and es6 would solve my problem, because the JS that my project depends on uses ES6 import syntax to optimize for tree shaking. I don't think that would work in general unless the modules are written in commonjs, in which case it's a non issue anyway.

My 2c is that we should include mjs now, it'll ease some pain points for people who want tree shaking and dual browser-nodejs uses as well. Anyone who tries to use node to run mjs files will know that it's experimental because they have to pass the flag to node, or they'll use something like esm.

Why to restrict suffix option to certain values? Compiled files are for external usage. Just some warning would be enough if the option is set to something different. In my case, I rename all .bs.js to .bs.mjs, so they can be used as modules.

As the above notification points out, I've started taking a stab at this in #4116. (My time isn't unconstrained right now, so I'm not sure how quickly the work will proceed; feel free to snag this out from under me if you think you can complete it faster. (= )

will be fixed in #4687

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bobzhang picture bobzhang  路  3Comments

chenglou picture chenglou  路  4Comments

cknitt picture cknitt  路  3Comments

bobzhang picture bobzhang  路  3Comments

alexfedoseev picture alexfedoseev  路  5Comments