melange-re.github.io icon indicating copy to clipboard operation
melange-re.github.io copied to clipboard

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

Open jchavarri opened this issue 2 years ago • 4 comments

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 avatar Mar 30 '23 08:03 jchavarri

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.

jchavarri avatar Mar 30 '23 16:03 jchavarri

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 avatar May 11 '23 18:05 anmonteiro

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

anmonteiro avatar Jun 28 '23 00:06 anmonteiro

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.

jchavarri avatar Jun 28 '23 07:06 jchavarri