Skip to content

feat: add module.exports named export for require(esm) compatibility - #700

Merged
antfu merged 2 commits into
eslint-stylistic:mainfrom
G-Rath:add-cjs-export
Feb 26, 2025
Merged

antfu merged 2 commits into
eslint-stylistic:mainfrom
G-Rath:add-cjs-export

Conversation

@G-Rath

@G-Rath G-Rath commented Feb 25, 2025 •

Copy link
Copy Markdown
Contributor

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 that require(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 the default export mapped to a default property rather than returning the default directly in order to allow access to named exports.

This means that while technically we can now use ESM-only packages in CJS contexts, because require is usually expected to be equivalent to importing the default it's not quite a "drop-in" because the require'er needs to account for the ESM/CJS interop to actually get that default value.

To account for this, require(esm) has a preference for a module.exports export 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 the module.exports value 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 rollup generates anyway:

var index = Object.assign(plugin, { configs });

export { index as default };

@G-Rath G-Rath mentioned this pull request Feb 25, 2025
3 of 5 tasks
@G-Rath

G-Rath commented Feb 25, 2025 •

Copy link
Copy Markdown
Contributor Author

@codecov

codecov Bot commented Feb 26, 2025 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.72%. Comparing base (e6dbb9d) to head (b6616b5).
Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants