-
-
Notifications
You must be signed in to change notification settings - Fork 702
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
Export chai as ES module #1317
Export chai as ES module #1317
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1317 +/- ##
=======================================
Coverage 94.61% 94.61%
=======================================
Files 33 33
Lines 1708 1708
Branches 416 416
=======================================
Hits 1616 1616
Misses 92 92 Continue to review full report at Codecov.
|
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.
This looks somewhat sensible, as an interim measure. Someone else from @chaijs/core should take a second look at this, with a view to merge.
I think it should indeed be considered as an interim measure until chai has been fully migrated to ES modules - which I suppose will eventually be done once ES modules are no longer experimental in Node. |
This is exactly the pattern we recommend.while transitioning from CJS to ESM https://nodejs.org/dist/latest-v13.x/docs/api/esm.html#esm_approach_1_use_an_es_module_wrapper |
Thanks for this work – it would be good to see it merged & released. |
I would expect this could also potentially allow browsers to use chai via ESM without transpiling (using a direct |
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.
This LGTM. @lucasfcosta or other @chaijs/core members want to approve+merge?
DO NOT MERGE THIS PR, as you will disable deep-linking into chai. See #1340 (comment) for an explanation. |
I'm closing this following #1340. I verified that indeed it does not work if you want to deep import chai: const chai = require('chai/lib/chai'); @MylesBorins: Is this something that the modules team is aware of? I fear that a lot of modules will suffer from this problem when trying to migrate to ESM with conditional exports. |
Yes, this is by design. With
The docs go into greater detail, but basically you're encouraged to define what the package's actual supported entry points are; or if you truly want to allow access to any file, you can add |
Allright, so if I understand correctly the best way to go forward with this is to only publish it in a semver major, perhaps with some explicit additional entry points. Correct? |
@sebamarynissen if you included |
"exports": { | ||
"import": "./lib/chai.mjs", | ||
"default": "./index.js" | ||
}, |
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.
"exports": { | |
"import": "./lib/chai.mjs", | |
"default": "./index.js" | |
}, | |
"exports": { | |
".": { | |
"import": "./lib/chai.mjs", | |
"default": "./index.js" | |
}, | |
"./": "./" | |
}, |
Ok, but perhaps that we should go with #1340 then as it adds the same functionality, handles the deep imports problem and contains tests for it. |
Since Node 13.2
--experimental-modules
was unflagged. Since then mocha has enabled support for tests written as an ES module in v7.0.0-esm1.This means that from now on it is possible to write your tests as
Unfortunately it is not possible to write
because Node doesn't support named exports from CommonJS modules.
This PR changes this by exporting chai as an ES module and by making use of conditional exports, which was unflagged - but is still experimental - in Node 13.7.
I am aware that conditional exports is still experimental, and hence you'll still get a warning, both for
I therefore propose to release the conditional exports as an experimental version - similar to mocha
v7.0.0-esm1
- but to include thechai.mjs
file in a semver minor so that people can doif they want.