Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Following up on my offer from #5018 (comment), here is an ESM loader for CoffeeScript that (once published) enables the following:
For example, I've tested with the
main.coffee
/scream.coffee
examples from https://nodejs.org/api/esm.html#esm_transpiler_loader (on which this code is based). You can test too vianpm link
.Potential issues for discussion
Currently the loader assumes that, if you're using an ESM loader, the desire is to load code in ESM mode. I think this is often what people would want, but it's possible that the user is writing ESM code that
import
s a CJS module written in CoffeeScript. In that case, the user should ideally use CoffeeScript'sregister
. But if the user has a mix of CoffeeScript ESM and CJS code, the ESM loader will probably breakimport
of CJS code.By contrast, https://nodejs.org/api/esm.html#esm_transpiler_loader uses
package.json
'stype
field. But given the lack of an.mjs
-style extension, this is not a very flexible system. I also worry about the performance hit of traversing up the filesystem (which is often very expensive on Windows).Another alternative would be to look for
import
/export
statements in the output, and use that to turn on ESM mode. Top-levelawait
is maybe trickier to detect though. (Maybe via the AST.)Another thing I've not yet tested is combining with another loader such as babel-register-esm.
If any of the above are priorities before an initial release, I'd be happy to work on them given a desired direction. But hopefully this is a useful first step toward good ESM support.