eslint-plugin-next: add ESM named exports via conditional exports field#94132
Open
12122J wants to merge 2 commits into
Open
eslint-plugin-next: add ESM named exports via conditional exports field#9413212122J wants to merge 2 commits into
12122J wants to merge 2 commits into
Conversation
SWC compiles TypeScript named exports to CJS getter-based properties using
Object.defineProperty(). Node.js only creates synthetic ESM named exports
from own *data* properties on module.exports, ignoring getters. This means
`import { configs } from '@next/eslint-plugin-next'` throws:
SyntaxError: Named export 'configs' not found.
Fix: add a dual-export setup using the package.json "exports" field.
- "require" (CJS callers): existing dist/index.js, unchanged.
- "import" (ESM callers): new dist/index.mjs, a thin wrapper that imports
the CJS default and re-exports the named bindings as data properties.
The dist/index.mjs file is generated by scripts/generate-esm.cjs which runs
as part of the build step after SWC compilation.
Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
0e94f72 to
e17f8f2
Compare
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.
What?
Add a dual CJS/ESM export setup to
@next/eslint-plugin-nextso that named ESM imports likeimport { configs } from '@next/eslint-plugin-next'work correctly.Fixes #86504.
Why?
SWC compiles TypeScript named exports to CJS using
Object.defineProperty()with getter functions:Node.js only creates synthetic ESM named exports from own data properties on
module.exports— not accessor (getter) properties. So importing the package via ESM named imports throws:How?
Added a dual-export setup via the
package.json"exports"field:"require"condition → existingdist/index.js(CJS, no change)"import"condition → newdist/index.mjs, a thin ESM wrapper that imports the CJS default and explicitly re-exports the named bindings as data properties:dist/index.mjsis generated byscripts/generate-esm.cjswhich runs as part of the existingbuildscript after SWC compilation.After this change: