Skip to content

fix(commonjs): recognize named export#1962

Draft
rtritto wants to merge 1 commit intorollup:masterfrom
rtritto:analyze-exports-lexer
Draft

fix(commonjs): recognize named export#1962
rtritto wants to merge 1 commit intorollup:masterfrom
rtritto:analyze-exports-lexer

Conversation

@rtritto
Copy link

@rtritto rtritto commented Feb 14, 2026

Rollup Plugin Name: @rollup/plugin-commonjs

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers: #1678

Description

Add the analyze export lexer for named export that allows to recognize exports like:

// foo/index.js
Object.defineProperty(exports, "foo", { enumerable: true, get: function () { return 123; } });

@shellscape
Copy link
Collaborator

@rtritto looks like CI is failing on prettier

@charliecreates
Copy link
Contributor

I reviewed the diff + CI + the context in #1678. Using cjs-module-lexer to augment named export detection (e.g. Object.defineProperty(exports, "foo", ...)) makes sense and matches what was discussed in the issue, but I don’t think this is merge-ready yet.

MUST fix (before merge)

  1. CI is currently failing prettier:check (it flags packages/commonjs/src/analyze-exports-lexer.js). Please run Prettier and push the formatted result until checks are green.
  2. Please add tests for the new named-export detection. At minimum, cover Object.defineProperty(exports, "foo", { ... }) and assert import { foo } from ./cjs works. (The PR template calls out that bugfixes/features won’t be merged without tests.)
  3. Avoid console.warn(...) in this code path. If the lexer fails on a file, this could spam user builds. Prefer routing warnings through Rollup’s plugin context (e.g. pass this.warn into analyzeExports) or otherwise make it opt-in.
  4. Revisit the lexer failure fallback: returning hasDefaultExport: true when parsing fails can flip meta.commonjs.hasDefaultExport and change interop semantics. Prefer returning hasDefaultExport: false/undefined on failure, and only updating commonjsMeta.hasDefaultExport when the lexer succeeded.

IMPROVEMENTS (non-blocking)
5. lexerReexports / resolveReexports(...) are parsed/stored but don’t appear to be consumed yet. Either wire this into the actual export generation logic, or drop it for now to keep the change focused.
6. Perf: this adds an additional parse pass for each CJS module (AST scan + lexer parse). If it ends up showing up in real-world graphs, consider only running the lexer when the AST analysis can’t confidently determine exports, or cache results more aggressively.

@rtritto rtritto marked this pull request as draft February 16, 2026 17:54
@rtritto
Copy link
Author

rtritto commented Feb 16, 2026

@shellscape thanks for reply
Can you continue this PR?

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