Skip to content

RFC: New package-spec "dual" #6209

Closed as not planned
Closed as not planned

Description

@cometkim

Motivation

Deciding on the module format is one of the most important parts of getting a JavaScript library published on NPM.

A leaf project usually uses only one format, but a library does not. There are several ways:

  • CommonJS-only: CJS is still the most common module format in Node.js. It is also compatible with projects using ESM. But the problem is that it is not standard and therefore breaks interoperability with other JS platforms like Deno, Bun, Browsers.
  • ESM-only: ESM is standard. Adoption rates are growing, but there are still many gaps. One major problem is that it is not compatible with projects using CJS.
  • Dual-package publish: To resolve the problems, Node.js has adopted a relaxed module resolution mechanism since v16+. Libraries can support both CJS/ESM projects with that in mind.

I've introduced the dual-package publish method using bundler on a forum: Bundle ReScript libraries with dual-package exports

However, the ReScript compiler today only supports one package-spec at a time and doesn't offer enough customization options, so we have to rely on external tools to do that.

Babel has env options for transpile multiple target once, TypeScript can customize project config file, so users can do that by just run tsc twice.

But it's still difficult, even with external tools. I've automated it to some extent, but there are tricky tasks like fixing the bs package-spec to es6 for compatibility, adjusting the module resolution in the output, and linking the correct libraries, etc.

I want ReScript to be a good option for libraries as well. It will eventually lead to ReScript-first projects and increased adoption.

Design

Introduce a new package-spec mode dual to support dual-package libraries.

{
  "package-specs": {
    "module": "dual",
    "in-source": true
  }
}

Which means ReScript should emit both CommonJS and ESM. To adjust the extension of the outputs, the suffix option must also be expanded.

{
  "suffix": {
    "commonjs": ".cjs",
    "esmodule": ".mjs"
  }
}

To use dual, suffix must be specified as an object, a unique and valid extension for each format must be specified.

As a result, the A.res module has A.cjs and A.mjs outputs at the same time. This is the minimum requirement for publishing a dual package and should be sufficient to use ReScript as the only toolchain for a library.

Activity

  1. cristianoc commented on Apr 26, 2023

    @cristianoc
    Collaborator

    Screenshot 2023-04-26 at 12 11 01

    Sounds like a thumbs up to me.

  2. anmonteiro commented on Apr 26, 2023

    @anmonteiro
    Contributor

    This isn't very hard to implement. You need to be careful about detecting conflicts (2 specs shouldn't have the same extension, etc.).

    I explored this in melange but never ended up merging it. melange-re/melange#158

    It's now, however, trivially supported with our dune integration. Check out module_systems here if you're curious: https://dune.readthedocs.io/en/latest/melange.html#melange-emit.

  3. anmonteiro commented on Apr 26, 2023

    @anmonteiro
    Contributor

    (Note that ReScript already supports this, btw. It just doesn't support both to output in-source)

  4. mununki commented on Apr 26, 2023

    @mununki
    Member

    Once this feature is implemented in the compiler, it seems that it will also affect the module_systems for Dynamic import and should be taken into consideration.

  5. added this to the v12 milestone on Apr 26, 2023
  6. cometkim commented on Apr 26, 2023

    @cometkim
    MemberAuthor

    it seems that it will also affect the module_systems for Dynamic import and should be taken into consideration.

    The easy way is not to support it. Dynamic import is semantic of ESM and there is no way to do it properly in CommonJS.

    CommonJS is specific to Node.js, and Node.js allows dynamic imports within CommonJS modules. Probably no changes are needed.

  7. changed the title [-]RFC: New package-spec `"dual"`[/-] [+][RFC] New package-spec `"dual"`[/+] on Jan 3, 2024
  8. changed the title [-][RFC] New package-spec `"dual"`[/-] [+]RFC: New package-spec `"dual"`[/+] on Jan 25, 2024
  9. cknitt commented on Aug 11, 2024

    @cknitt
    Member

    Do we really need "dual"?

    1. I think package-specs can already be an array today, and we could just explicitly list commonjs and esmodule there?
    2. What if we need to add a third format in the future for some reason? Then "dual" doesn't really make sense anymore.
  10. cometkim commented on Aug 11, 2024

    @cometkim
    MemberAuthor

    I think package-specs can already be an array today, and we could just explicitly list commonjs and esmodule there?

    Right. I considered that too. Then it need to fuse the suffix (and others.. maybe genType config too) into the package-specs

    What if we need to add a third format in the future for some reason? Then "dual" doesn't really make sense anymore.

    Yes, for example, TypeScript could be added as an new backend (why not?) The point of dual is that we should always be able to support multiple targets simultaneously. It is important for multi target libraries.

  11. cknitt commented on Aug 11, 2024

    @cknitt
    Member

    Right. I considered that too. Then it need to fuse the suffix (and others.. maybe genType config too) into the package-specs

    The suffix is already there according to build-schema.json (module-format-object):

    https://github.com/rescript-lang/rescript-compiler/blob/8a28dafe71082da9d40eb53ea182dc2eb85fc5f8/docs/docson/build-schema.json#L22

  12. DZakh commented on Aug 12, 2024

    @DZakh
    Member

    Also, I think for the library mode there should be some sort of an idiomatic suffix (or default one), so all libraries use the same one to allow depending on them without bundlers.

  13. moved this to Todo in RFCson Nov 28, 2024
  14. cometkim commented on Nov 28, 2024

    @cometkim
    MemberAuthor

    Note: Parent directories like current js/ and es6/ are important for interoperability with import-map. Unfortunately, in-source: true we don't have a guarantee like this. Using Node.js resolution, users can capture it as a *.ext pattern, but that is non-standard and will not work in web browsers.

    That means, if the user wants to serve directly to the browser, they should turn off in-source mod, or we need to do it simultaneously.

  15. cometkim commented on Nov 28, 2024

    @cometkim
    MemberAuthor

    Another one:

    For dependencies, the compiler should use "dual" as the only fixed mode, so (re-)compilation isn't needed for dependencies

  16. removed this from the v12 milestone on May 5, 2025
  17. github-actions commented on May 1, 2026

    @github-actions

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  18. added
    stale
    on May 1, 2026
  19. moved this from Todo to Done in RFCson May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stale

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions