melange-re / melange-re.github.io

Documentation site for Melange
https://melange.re
25 stars 6 forks source link

Include package.json while installing es6 stdlib (or any es6 package?) #89

Open jchavarri opened 1 year ago

jchavarri commented 1 year ago

Rescript es6 version of stdlib included a package.json with "type": "module"

https://github.com/rescript-lang/rescript-compiler/blob/77b6a3bcd63989d2fb015c8a2543d87f3cb45007/lib/es6/package.json

This comes useful when running nodejs apps with es6 bundling, as otherwise one has to resort to workarounds like custom node loaders, using mjs extensions and such.

There is a workaround using dune rules:

(subdir
 target
 (subdir
  node_modules
  (subdir
   melange
   (rule
    (alias my_melange_alias)
    (action
     (with-stdout-to
      package.json
      (run echo "{\"type\": \"module\"}")))))
  (subdir
   melange.belt
   (rule
    (alias my_melange_alias)
    (action
     (with-stdout-to
      package.json
      (run echo "{\"type\": \"module\"}")))))
  (subdir
   melange.runtime
   (rule
    (alias my_melange_alias)
    (action
     (with-stdout-to
      package.json
      (run echo "{\"type\": \"module\"}")))))))

But it has to be defined for the 3 libraries exposed by Melange, and also replicated for every melange.emit stanza used.

I wonder if Melange could include package.json files similarly. The problem applies to any library really, so there might be some generic solution out there. Most probably it needs some coordination with Dune? 🤔

cc @denis-ok

jchavarri commented 1 year ago

An idea by @anmonteiro

we could expose a variable %{melange:module_system}

The goal of this would be to have the library itself have the package.json as runtime_dep but only for the case where there's es6 being emitted.

anmonteiro commented 1 year ago

I'm not sure how actionable this is. We can't include a static package.json because the module system could be commonjs.

I think the action item here is: document it and offer the dune rules in the website.

anmonteiro commented 1 year ago

@jchavarri how do you feel about this one now that you have had some more production experience?

jchavarri commented 1 year ago

As we ended up with this convention of using as few melange emit stanzas as possible, I think it's not too bad to add the rules manually when needed. Maybe we can move the issue to melange-re.github.io repo to document it.