-
Notifications
You must be signed in to change notification settings - Fork 2k
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
ESM loader #5447
base: main
Are you sure you want to change the base?
ESM loader #5447
Conversation
I think we might want to wait a bit to see what happens to nodejs/node#47880. Once there’s a clear way to handle both ESM and CJS code through a loader, it should be much easier to handle realistic use cases. For example I don’t know if it’s plausible to assume an app has no CommonJS in its module graph, when you take As for the Anyway I’m not trying to add blockers; if you’ve thought through the various CommonJS use cases and think they’re all handled, please just explain them and I’m happy to trust you. |
Node 20.6.0 had the ESM hooks refactor I was waiting for. The module customization hooks in Node are now in their likely final shape, and we can refactor this PR to provide a way to transpile and run ESM CoffeeScript. First off, don’t use node --import coffeescript/register app.coffee Inside That other file, Getting the |
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.