Repository navigation
fix: treat top-level this of a required module without module syntax as its exports - #11271
Conversation
…x as its exports A module without module syntax is scanned as ExportsKind::None, so its top-level `this` is mapped to `void 0`. When the link stage promotes it to CommonJS because it is reached through require() (or a dynamic import with code splitting off), switch that `this` to the module's exports object, as Node does, and give the CJS closure its `this` binding. Closes rolldown#11269
✅ Deploy Preview for rolldown-rs canceled.
|
There was a problem hiding this comment.
🟡 Changes recommended
A dynamic-import-only .js target changes top-level this when code splitting is disabled; preserve the context replacements and add a regression confirming consistent behavior across splitting modes.
1 open issue
What changed in this PR
This PR addresses #11269 by updating Rolldown’s linker so syntax-free modules loaded through require() use their exports object for top-level this, fixing crashes in UMD-style dependencies.
Changes:
- Adds a CommonJS promotion helper that updates top-level
thisbindings, used by require and no-splitting dynamic-import paths. - Adds regression fixtures for UMD-style initialization and direct
thisassignments. - Documents the linker’s new binding and symbol updates.
| File | Description |
|---|---|
internal-docs/linking/determine-module-exports-kind/implementation.md |
Documents promotion behavior and affected state. |
crates/rolldown/tests/rolldown/issues/11269/main.js |
Asserts both required modules expose expected values. |
crates/rolldown/tests/rolldown/issues/11269/lib/thing.js |
Reproduces UMD-style top-level this usage. |
crates/rolldown/tests/rolldown/issues/11269/lib/index.js |
Loads syntax-free targets through require(). |
crates/rolldown/tests/rolldown/issues/11269/lib/direct.js |
Covers direct assignment through top-level this. |
crates/rolldown/tests/rolldown/issues/11269/artifacts.snap |
Records generated CommonJS wrappers and bindings. |
crates/rolldown/tests/rolldown/issues/11269/_config.json |
Configures the regression fixture entry. |
crates/rolldown/src/stages/link_stage/determine_module_exports_kind.rs |
Adds and invokes the CommonJS promotion helper. |
Review effort: Balanced
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
…t code splitting Also update the `cjs_this_ref` field doc and point `promote_to_commonjs` at the internal doc.
## [1.3.1] - 2026-11-09 ### 🚀 Features - inline-common-chunks: inline chunks using `import.meta.ROLLDOWN_FILE_URL_*` (#11543) by @mattlewis92 ### 🐛 Bug Fixes - cli: apply --environment before loading the binding (#11553) by @Brooooooklyn - rolldown_plugin_vite_resolve: escape module names in generated error stubs (#11584) by @sapphi-red - add the code location to transform logs and errors at position 0 (#11568) by @chinmayajha - match sticky regexes only at the start of the string (#11569) by @chinmayajha - iife/umd: skip namespace markers when the entry has no exports (#11567) by @lazerg - keep a declaration's name when it clashes with an external import (#11052) by @Nic-Polumeyv - drop top-level statements made dead by imported constants before chunk linking (#11053) by @elliott-with-the-longest-name-on-github - keep `this.resolve` custom options apart for overlapping calls (#11505) by @wan9chi - transform: reuse TsconfigCache's answer when no tsconfig owns the file (#11265) by @sarankumar1325 - treat top-level `this` of a required module without module syntax as its exports (#11271) by @sarankumar1325 - keep side effects of a module whose exported constant is inlined (#11440) by @hyfdev - leave same-chunk dynamic imports out of `dynamicImports` (#11156) by @kkkhs ### 🚜 Refactor - prepend transferred imports as AST fragments (#11464) by @Dunqing ### 📚 Documentation - show `vp` first in setup commands (#11503) by @IWANABETHATGUY ### ⚡ Performance - incremental: reuse link-time facade symbols across rebuilds (#11542) by @mattlewis92 - incremental: share module sourcemap chains across cache clones (#11540) by @mattlewis92 - chunk_optimizer: answer runtime-merge reachability from one chunk-level graph (#11541) by @mattlewis92 - send only changed chunk properties back to Rust from output hooks (#11488) by @anonrig ### 🧪 Testing - remove review fixtures committed by mistake in #11052 (#11574) by @hyfdev - wasi: cover raw context destruction (#11561) by @Brooooooklyn ### ⚙️ Miscellaneous Tasks - deps: update rust crates (#11573) by @renovate[bot] - deps: update mimalloc-safe to 0.1.70 (#11589) by @shulaoda - renovate: update GitHub Actions that are pinned to a commit (#11583) by @hyfdev - skip Playwright browser downloads in Prepare Release (#11579) by @shulaoda - pin the rolldown crates to exact versions of each other on release (#11577) by @shulaoda - update `CodSpeedHQ/action` to v5.6.3 for Ubuntu 26.04 (#11575) by @hyfdev - remove stale entries from `.typos.toml` (#11571) by @iiio2 - pick the Prepare Release target branch from the version (#11525) by @shulaoda - run every OS on pushes to maintenance branches (#11520) by @shulaoda - rebase maintenance Vite branches onto their Vite minor (#11515) by @shulaoda - run CI on pushes to maintenance branches (#11516) by @shulaoda - support patch releases from maintenance branches (#11393) by @shulaoda ### ❤️ New Contributors * @chinmayajha made their first contribution in [#11568](#11568) * @lazerg made their first contribution in [#11567](#11567) * @elliott-with-the-longest-name-on-github made their first contribution in [#11053](#11053) * @sarankumar1325 made their first contribution in [#11265](#11265) * @anonrig made their first contribution in [#11488](#11488) * @kkkhs made their first contribution in [#11156](#11156) Co-authored-by: shulaoda <[email protected]>

Closes #11269.
What
A
.jsfile without module syntax (noimport/export, norequire/module/exports) is scanned asExportsKind::None, so the scanner maps its top-levelthistoThisExprReplaceKind::Contextand prints it asvoid 0. When such a file is only reached throughrequire(), the link stage promotes it toCommonJsand wraps it with__commonJSMin, but itsthisstaysvoid 0. UMD-style files like(function (global) { ... })(this)then crash withTypeError: Cannot set properties of undefined. Node runs the file as CommonJS, where top-levelthisismodule.exports.Change
In
determine_module_exports_kind, theRequireand no-code-splittingDynamicImportarms that promote aNonemodule toCommonJsnow call a newpromote_to_commonjs. When the module has top-levelthis, it switches those entries toThisExprReplaceKind::Exports, setsEcmaModuleAstUsage::ExportsRefand createscjs_this_ref, as the scanner already does for a module with CommonJS syntax. The finalizer then printsthisas thethisparameter of the CJS closure:Modules promoted on other paths (for example a
Nonemodule imported by animportfrom a CommonJS module) keep their current behaviour. I also updatedinternal-docs/linking/determine-module-exports-kind/implementation.md.Tests
crates/rolldown/tests/rolldown/issues/11269:thing.js(the issue's UMD file) anddirect.js(this.direct = ...) are only reached throughrequire(). Onmainthe bundle fails withTypeError: Cannot set properties of undefined (setting 'Thing'). With this change it passes.cargo test -p rolldown --test integration: 2156 passed. 5 fail in my environment (test262_module_code, threecjs_module_lexer_compatfixtures,npm_packages/util_deprecate), and the same 5 fail onmain, so they aren't related to this change. No existing snapshot changed.cargo fmt --check,cargo clippy -p rolldown --all-targets -- -D warningsand the pre-commitvp checkare clean.AI disclosure: I used an AI coding assistant to investigate this and write the fix and the test. I reviewed the change and ran the tests locally.