Repository navigation
fix(commonjs): pass exports to the wrapper when only __esModule is defined on it - #2046
Open
ItsRenjiii wants to merge 1 commit into
Open
ItsRenjiii wants to merge 1 commit into
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 PR appears safe to merge. Reviews (1) · Last reviewed commit: "fix(commonjs): pass exports to the wrapp..." · Reviewed by Greptile |
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.
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