Skip to content

feat(rolldown_plugin_esm_external_require): prefer 'module.exports' over a namespace spread - #10901

Merged
shulaoda merged 4 commits into
rolldown:mainfrom
isker:fix/esm-external-require-module-exports
Sep 23, 2026
Merged

shulaoda merged 4 commits into
rolldown:mainfrom
isker:fix/esm-external-require-module-exports

Conversation

@isker

@isker isker commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

When esmExternalRequire handles a require of an external module that is itself CJS and that assigns a function to module.exports, the current behavior of spreading said module in the generated facade breaks: the spread makes the module not callable.

Teach esmExternalRequire to use the NodeJS 'module.exports' convention before trying to do that spread, so that any module providing it can thread such a callable value through. Update the documentation, which was already outdated (it mentions no spread at all), to indicate the current behavior.

More background about our use case: we have a large monorepo build that invokes rollup, and we are trying to convert it to rolldown. Rollup is invoked many times granularly over different parts of the source code in this repo, to incrementally generate a large number of chunks that are dynamically linked together at runtime; in each invocation, modules from other invocations are externalized. Some of these chunks are third party dependencies that are distributed only as CJS. Some of those chunks require other such externalized CJS chunks. And some of these dynamically-linked facades involve module.exports = function() ..., thus the described issue.

We are already generating facade modules for any CJS chunks that ensure that the resulting chunks export all the necessary CJS symbols. We could update this facade to include a 'module.exports' export, and have it work in concert with this new esmExternalRequire support. Without this support, we will have to roll our own version of esmExternalRequire that does not break this kind of callable interop.

AI disclosure: I implemented this change using Amp. You can see the thread that produced it here. I understand and take responsibility for this code.

That being said, I am sure you have greater expertise than I do on CJS/ESM interop, among other things. I am not sure this is the right design (I am not sure why we are even spreading in the first place), only that it would support my use case.

When `esmExternalRequire` handles a require of an external module that is
itself CJS and that assigns a function to `module.exports`, the current
behavior of spreading said module in the generated facade breaks: the spread
makes the module not callable.

Teach `esmExternalRequire` to use the NodeJS `'module.exports'` convention
before trying to do that spread, so that any module providing it can thread
such a callable value through. Update the documentation, which was already
outdated (it mentions no spread at all), to indicate the current behavior.

More background about our use case: we have a large monorepo build that
invokes rollup, and we are trying to convert it to rolldown. Rollup is
invoked many times granularly over different parts of the source code in
this repo, to incrementally generate a large number of chunks that are
dynamically linked together at runtime; in each invocation, modules from
other invocations are externalized. Some of these chunks are third party
dependencies that are distributed only as CJS. Some of those chunks require
other such externalized CJS chunks. And some of these dynamically-linked
facades involve `module.exports = function() ...`, thus the described issue.

We are already generating facade modules for any CJS chunks that ensure that
the resulting chunks export all the necessary CJS symbols. We could update
this facade to include a `'module.exports'` export, and have it work in
concert with this new `esmExternalRequire` support. Without this support,
we will have to roll our own version of `esmExternalRequire` that does not
break this kind of callable interop.
@isker isker changed the title esmExternalRequire: prefer 'module.exports' over a namespace spread feat(esmExternalRequire): prefer 'module.exports' over a namespace spread Sep 16, 2026
@isker isker changed the title feat(esmExternalRequire): prefer 'module.exports' over a namespace spread feat(rolldown_plugin_esm_external_require): prefer 'module.exports' over a namespace spread Sep 16, 2026
@isker
isker marked this pull request as ready for review September 16, 2026 19:11
@codspeed

codspeed Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing isker:fix/esm-external-require-module-exports (f03189f) with main (02d922d)

Open in CodSpeed

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@isker

isker commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

@shulaoda sorry for the ping, but I saw you were assigned. Can you take a look?

@shulaoda

Copy link
Copy Markdown
Member

@shulaoda sorry for the ping, but I saw you were assigned. Can you take a look?

Thanks for the contribution, I will take a look before today release.

@shulaoda shulaoda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

@shulaoda
shulaoda merged commit ec5bab8 into rolldown:main Sep 23, 2026
32 checks passed
@rolldown-guard rolldown-guard Bot mentioned this pull request Sep 23, 2026
shulaoda added a commit that referenced this pull request Sep 23, 2026
## [1.2.10] - 2026-09-23

### 🚀 Features

- rolldown_plugin_esm_external_require: prefer 'module.exports' over a namespace spread (#10901) by @isker
- watch: support directories in addWatchFile (#10944) by @shulaoda
- label code-splitting timing rows (#10896) by @IWANABETHATGUY
- separate code-splitting group timing rows (#10892) by @IWANABETHATGUY
- clarify callback owners in the timing report (#10890) by @IWANABETHATGUY
- add checks.bundlerTimings with deprecated pluginTimings alias (#10883) by @IWANABETHATGUY

### 🐛 Bug Fixes

- code-splitting: initialize leaves behind a re-exported namespace import (#10694) by @hyfdev
- node: record the WASI target on the WebContainer binding fallback (#10952) by @shulaoda
- dev: force incremental build on in DevEngine::new (#10958) by @o-alexandrov
- tree-shaking: propagate side effects through cycles (#10893) by @IWANABETHATGUY
- deconflict CJS locals that shadow a renamed require wrapper (#10793) by @logaretm
- include group indexes in labeled `codeSplitting` timing rows (#10916) by @IWANABETHATGUY
- docs: type-check Algolia `indices` option (#10934) by @shulaoda
- docs: restore Algolia search (#10929) by @isker
- tell code-splitting timing rows apart by position (#10903) by @IWANABETHATGUY

### 🚜 Refactor

- rolldown_fs_watcher: remove the single-path API (#10950) by @shulaoda
- dev: register watch files through FsWatcher (#10949) by @shulaoda
- watch: let FsWatcher own the watched paths (#10948) by @shulaoda
- watch: identify watch files by normalized absolute path (#10943) by @shulaoda
- plugin: build a plugin-resolved `ResolvedId` in one place (#10921) by @IWANABETHATGUY

### 📚 Documentation

- dev: add HMR design, align dev docs and engine comments (#10271) by @h-a-n-a

### ⚡ Performance

- batch `sourcemapPathTransform` calls (#10763) by @IWANABETHATGUY
- batch `sourcemapIgnoreList` calls (#10762) by @IWANABETHATGUY
- hmr: drop the unused kind argument from registerFactory (#10915) by @h-a-n-a

### 🧪 Testing

- handle Windows paths in sourcemap callback tests (#10942) by @IWANABETHATGUY

### ⚙️ Miscellaneous Tasks

- replace custom `ban-expect-assertions` JS lint with builtin `no-restricted-properties` (#10933) by @isker
- deps: update napi (#10959) by @renovate[bot]
- deps: upgrade oxc to 0.151.0 (#10939) by @camc314
- deps: update test262 submodule for tests (#10941) by @rolldown-guard[bot]
- deps: update npm packages (#10931) by @renovate[bot]
- deps: update napi (#10928) by @renovate[bot]
- deps: update dependency vite-plus to v0.3.3 (#10914) by @renovate[bot]
- deps: update rust crates (#10932) by @renovate[bot]
- deps: update github actions (#10930) by @renovate[bot]
- deps: update dependency rolldown-plugin-dts to v0.28.6 (#10924) by @renovate[bot]
- deps: update crate-ci/typos action to v1.50.2 (#10917) by @renovate[bot]
- deps: update dependency @napi-rs/cli to v3.10.4 (#10913) by @renovate[bot]
- deps: update napi (#10897) by @renovate[bot]

### ❤️ New Contributors

* @o-alexandrov made their first contribution in [#10958](#10958)

Co-authored-by: shulaoda <[email protected]>
misama-ct added a commit to commercetools/nimbus that referenced this pull request Oct 21, 2026
… shim

Since Rolldown 1.2.10 (rolldown/rolldown#10901), the facade that
esmExternalRequirePlugin generates for require("react") reads the
'module.exports' namespace key. Webpack resolves that access statically
as a named import that React does not have, so consumer builds with
strictExportPresence (mc-scripts) fail since 3.7.0.

The only bundled CJS code that requires react is use-sync-external-store/shim,
which react-aria, react-aria-components and react-stately import. It is a
polyfill for React < 18 and returns React's own hook on newer versions. Nimbus
requires React 19, so alias the shim to react. With no CJS code requiring react
left in the bundle, esmExternalRequirePlugin is no longer needed and react
becomes a plain external.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
misama-ct added a commit to commercetools/nimbus that referenced this pull request Oct 21, 2026
… shim (#2096)

* fix(build): import useSyncExternalStore from react instead of the CJS shim

Since Rolldown 1.2.10 (rolldown/rolldown#10901), the facade that
esmExternalRequirePlugin generates for require("react") reads the
'module.exports' namespace key. Webpack resolves that access statically
as a named import that React does not have, so consumer builds with
strictExportPresence (mc-scripts) fail since 3.7.0.

The only bundled CJS code that requires react is use-sync-external-store/shim,
which react-aria, react-aria-components and react-stately import. It is a
polyfill for React < 18 and returns React's own hook on newer versions. Nimbus
requires React 19, so alias the shim to react. With no CJS code requiring react
left in the bundle, esmExternalRequirePlugin is no longer needed and react
becomes a plain external.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* chore(ci): check that the nimbus dist bundles under strict webpack

Add `pnpm check:bundler-compat` and run it in build-and-test after the
package shape check. check:package-shape (attw + publint) validates
types and package.json, not the bundled code, which is how the 3.7.0
'module.exports' import reached a release.

The check bundles @commercetools/nimbus and /theme with webpack 5 the
way mc-scripts does (strictExportPresence, warnings fail), and scans the
ESM output for Rolldown's runtime require helper (#1451). The helper's
name is shortened in the output, so the scan matches its error message.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* chore(ci): scan .esm.js chunks too and fail when no ESM chunks are found

The require helper scan only matched `*.es.js`. Chunks are named by
`[format]`, which renders as `es` today, but a Vite or Rolldown upgrade
could change it and leave the scan with no chunks to read, passing
silently. Match `.es.js` and `.esm.js`, and fail if `dist/chunks/` has
no file with either extension.

Also correct the vite.config.ts comment that said `[format]` renders as
`esm`: the build emits `.es.js` / `.cjs.js` chunks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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