Skip to content
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

Named exports for Node ESM for coffeescript package #5376

Merged

Conversation

GeoffreyBooth
Copy link
Collaborator

When writing ES module code in Node, the following doesn’t currently work:

import { compile } from 'coffeescript';

This is because coffeescript is a CommonJS package that exports a single object (module.exports = CoffeeScript) and Node’s “best guess” algorithm for trying to detect named exports of CommonJS packages can’t find CoffeeScript’s exports. Currently only import CoffeeScript from 'coffeescript' works.

This PR simply adds several lines that explicitly define the named exports, which enables code like the first example to work. cc @guybedford

There are no tests for this, because the only way to currently run ESM code in Node from a CommonJS context is to either use an experimental API that’s moribund and likely to significantly change whenever it gets picked up again, or to spawn a child process of Node which is a bit too resource-intensive for our test suite. This code is extremely low risk, though, and doesn’t strike me as needing tests.

Copy link
Contributor

@edemaine edemaine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I just tested this locally, with npm link and the following code:

import CS, {compile} from 'coffeescript'
console.log(CS, compile, CS.compile === compile)

Confirmed that this additional functionality doesn't seem to mess up regular import, or require. Funny that it's necessary, but x.y = x.y shouldn't hurt. 🙂

@GeoffreyBooth GeoffreyBooth merged commit e82de9c into jashkenas:master Sep 21, 2021
@GeoffreyBooth GeoffreyBooth deleted the node-esm-named-exports branch September 21, 2021 05:05
@GeoffreyBooth GeoffreyBooth mentioned this pull request Oct 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants