Repository navigation
fix(commonjs): pass exports to the wrapper when only __esModule is defined on it - #2046
ItsRenjiii wants to merge 1 commit into
Conversation
…fined on it A top-level `Object.defineProperty(exports, '__esModule', ...)` is skipped during analysis, so it did not mark `exports` as used. When the module was wrapped for another reason (such as a `module` reference), the wrapper only received `module`, and the kept `defineProperty` call referenced a free `exports`. In ES output this throws `ReferenceError: exports is not defined`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
[Medium impact] The change affects a bounded set of modules processed by the CommonJS plugin. Fix the conflicting wrapper parameters before merging. Findings
Reviews (2) · Last reviewed commit: "fix(commonjs): pass exports to the wrapp..." · Reviewed by Greptile |
| for (const { type } of topLevelDefineCompiledEsmExpressions) { | ||
| uses[type] = true; |
There was a problem hiding this comment.
This loop also marks locally declared names as wrapper arguments. For example, const exports = {}; Object.defineProperty(exports, '__esModule', { value: true }); module.exports = exports; previously got a wrapper with only a module parameter. The new flag adds an exports parameter alongside const exports, so the transformed file fails to parse.
The call recorder does not check whether the target is locally declared. Exclude those targets before marking uses[type], and add a regression test.
Rollup Plugin Name:
commonjsThis PR contains:
Are tests included?
Breaking Changes?
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:
Reported in discussion #2044 (no issue filed).
Description
A top-level
Object.defineProperty(exports, '__esModule', { value: true })is recorded intopLevelDefineCompiledEsmExpressions, and itsexportsargument goes intoskippedNodes, so it never setsuses.exports. When the module is wrapped for another reason (here, amodulereference),wrapCodeonly passes the names marked as used. ThedefinePropertycall is kept as written, so the wrapper ends up referencing a freeexports:In ES output this throws
ReferenceError: exports is not defined in ES module scope. In CJS output it doesn't throw, becauseexportsresolves to the output file's own binding, but__esModulenever reaches the module's exports. The new test checks for exactly that.This PR marks
uses.exports(oruses.module, forObject.defineProperty(module.exports, ...)) for those calls whenever the module is wrapped. The wrapper then receives what the kept calls reference:Modules that aren't wrapped are unchanged.
Tests
wrapped-define-compiled-esm. It fails onmasterbecause the dependency's exports lack__esModule, and passes with this change.format: 'es') throws theReferenceErroronmasterand printsobjectwith this change.validate.ymlall pass locally:prettier:check,pnpm --recursive build,lint:jsandci:coveragefor...[origin/master]. The commonjs suite (292 tests) passes on Node 20 and 24.This is independent of #2045, which changes a different part of
transform-commonjs.js.🤖 Generated with Claude Code