-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: New package-spec "dual"
#6209
Comments
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 |
(Note that ReScript already supports this, btw. It just doesn't support both to output in-source) |
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. |
CommonJS is specific to Node.js, and Node.js allows dynamic imports within CommonJS modules. Probably no changes are needed. |
Do we really need "dual"?
|
Right. I considered that too. Then it need to fuse the
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. |
The suffix is already there according to
|
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. |
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:
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 runtsc
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
toes6
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
modedual
to support dual-package libraries.Which means ReScript should emit both CommonJS and ESM. To adjust the extension of the outputs, the
suffix
option must also be expanded.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 hasA.cjs
andA.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.The text was updated successfully, but these errors were encountered: