-
-
Notifications
You must be signed in to change notification settings - Fork 700
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
add Node.js ESM entry point with named and default exports #1340
Conversation
@sebamarynissen your solution is emphatically not is-promise problem proof. DO NOT MERGE PR #1317. If you do, you will bar people from deep-importing into This is why in this PR I added the line:
In the This PR also adds testing this feature, which was not trivial given that it can only be tested in Node versions that support ESM. |
The better way to do this is to define the documented entry points in the |
Yes, that was what I was thinking of as well now: wait for a semver major and leave it up to the core team to decide what entry points to export. |
@sebamarynissen - not necessarily. You can publish this PR now with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it'd be fully backwards compatible I don't see any problems with having this in.
Just for information, are there plans to release a new version on npm with this feature anytime soon? Given that Node 14 becomes LTS in October I think that being able to use named imports with chai will speed up the adoption of ES modules on Node. |
ESM import now canonical: github.com/chaijs/chai/pull/1340.
Now that Mocha supports ESM test files, I found that importig Chai in ESM files is awkward. So, if we used to do this in CJS:
You'd like to do this in ESM:
But you can't, because Chai doesn't have an ESM entry point, and relies on the fact that CJS files can be imported, but only using default import. So you have to do this:
This PR exports an ESM entry point so that you can use named exports in a natural manner when importing Chai itself.
The way it does this is to add an
exports
section to thepackage.json
, as described in the Node.js documentation for exports. This exports has a "conditional export" that defines separate entry points for ESM and CJS.Note that
exports
affects both ESM and CJS, so we should be careful (see ramifications below): based on the case ofis-promise
, theoretically this can break anybody using Mocha, because addingexports: ...
topackage.json
affects CJS too! It means that the only entrypoints allowed (in CJS and ESM) are the ones defined in theexports
.Which is why I added
"./": "./"
to theexports
, which says to Node.js: allow any deep importing such asrequire("mocha/foo/bar")
in the case that somebody, somewhere, is using that. This was also the solution used byis-promise
.