Repository navigation
feat: add module.exports named export for require(esm) compatibility - #700
Merged
Merged
Conversation
3 of 5 tasks
Contributor
Author
|
ugh and of course arbitrary module namespace identifiers are an es2022 thing... |
antfu
approved these changes
Feb 26, 2025
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #700 +/- ##
=======================================
Coverage 95.72% 95.72%
=======================================
Files 122 122
Lines 20377 20377
Branches 4814 4814
=======================================
Hits 19506 19506
Misses 871 871 ☔ View full report in Codecov by Sentry. |
13 of 25 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This makes it possible to use v4+ in CJS-contexts with
require(esm)without any downstream code changes, by adding a special named export thatrequire(esm)will favor instead of the usual{ __esModule: true, default: ... }result.Linked Issues
Relates to #661
Additional context
Because of how ESM works compared to CJS, by default
require(esm)results in an object with thedefault exportmapped to adefaultproperty rather than returning thedefaultdirectly in order to allow access to named exports.This means that while technically we can now use ESM-only packages in CJS contexts, because
requireis usually expected to be equivalent to importing thedefaultit's not quite a "drop-in" because therequire'er needs to account for the ESM/CJS interop to actually get thatdefaultvalue.To account for this,
require(esm)has a preference for amodule.exportsexport which it will return if found, allowing upstream packages to explicitly define that named export to enable "drop-in" usage; this comes at the cost of preventing access to other named exports, but there are multiple easy ways to handle that including ensuring those properties are available on themodule.exportsvalue and/or making the named exports available via another file - I also don't think it's likely that these packages specifically will ever have named exports (even if there were some utilities or such, I'd expect they'd end up in a dedicated package since this is a monorepo).Also see:
And as a fun little side fact, this code is actually pretty close to what
rollupgenerates anyway: