Skip to content

fix: emit separate __toESM bindings for mixed ESM/CJS external imports - #8987

Merged
IWANABETHATGUY merged 1 commit into
mainfrom
04-01-fix_8843
Apr 6, 2026
Merged

IWANABETHATGUY merged 1 commit into
mainfrom
04-01-fix_8843

Conversation

@IWANABETHATGUY

@IWANABETHATGUY IWANABETHATGUY commented Apr 1, 2026 •

Copy link
Copy Markdown
Member

Summary

  • When a CJS/IIFE/UMD chunk has both ESM and non-ESM importers of the same external module, emits two __toESM bindings — one with node-mode (__toESM(x, 1)) for ESM importers and one without for non-ESM importers
  • Previously, all importers shared a single binding, which broke interop when the external has __esModule: true
  • Also fixes IIFE/UMD format which never passed the node-mode flag at all

Fixes #8842

Test plan

  • Added 8842 test (single-mode: ESM importer of external in CJS output)
  • Added 8842_mixed_chunk test (mixed-mode: both .mjs and .js importers of same external)
  • All 1665 integration tests pass

Copy link
Copy Markdown
Member Author

How to use the Graphite Merge Queue

Add the label graphite: merge-when-ready to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@netlify

netlify Bot commented Apr 1, 2026

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs ready!

Name Link
🔨 Latest commit 0b95cc3
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/69cd4da433ebce0008a74efd
😎 Deploy Preview https://deploy-preview-8987--rolldown-rs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Apr 1, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs ready!

Name Link
🔨 Latest commit e6fd33f
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/69d32cc4f768f5000868bc3e
😎 Deploy Preview https://deploy-preview-8987--rolldown-rs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@IWANABETHATGUY IWANABETHATGUY changed the title fix: 8843 fix: emit separate __toESM bindings for mixed ESM/CJS external imports Apr 1, 2026
@IWANABETHATGUY
IWANABETHATGUY force-pushed the 04-01-fix_8843 branch 2 times, most recently from 4abf0b6 to 5b95891 Compare April 2, 2026 02:23
@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedoxc-walker@​0.5.210010010086100
Addedoxfmt@​0.43.0901008996100
Addedoxc-transform@​0.123.0941009696100

View full report

@IWANABETHATGUY
IWANABETHATGUY marked this pull request as ready for review April 6, 2026 03:48
@IWANABETHATGUY
IWANABETHATGUY requested a review from h-a-n-a April 6, 2026 03:48
@codspeed

codspeed Bot commented Apr 6, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 4 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing 04-01-fix_8843 (e6fd33f) with main (a48ca34)

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. ↩

@IWANABETHATGUY
IWANABETHATGUY merged commit 1a12fe7 into main Apr 6, 2026
32 checks passed
@IWANABETHATGUY
IWANABETHATGUY deleted the 04-01-fix_8843 branch April 6, 2026 04:02
This was referenced Apr 8, 2026
shulaoda added a commit that referenced this pull request Apr 8, 2026
## [1.0.0-rc.14] - 2026-04-08

### 🚀 Features

- rust: add `disable_panic_hook` feature to disable the panic hook (#9023) by @sapphi-red
- support inlineConst for CJS exports accessed through module.exports (#8976) by @h-a-n-a

### 🐛 Bug Fixes

- rolldown_plugin_vite_import_glob: normalize resolved alias path to prevent double slashes (#9032) by @shulaoda
- rolldown_plugin_vite_import_glob: follow symlinks in file scanning (#9000) by @Copilot
- wrap CJS entry modules for IIFE/UMD when using exports/module (#8999) by @IWANABETHATGUY
- emit separate __toESM bindings for mixed ESM/CJS external imports (#8987) by @IWANABETHATGUY
- tree-shake dead dynamic imports to side-effect-free CJS modules (#8529) by @sapphi-red
- skip inlining stale CJS export constants on module.exports reassignment (#8990) by @IWANABETHATGUY

### 🚜 Refactor

- generator: migrate ecma formatting from npx oxfmt to vp fmt (#9022) by @shulaoda
- generator: replace npx oxfmt with vp fmt for ecma formatting (#9021) by @shulaoda

### 📚 Documentation

- contrib-guide: mention that running tests on older Node.js version will have different stat results (#8996) by @claude

### ⚙️ Miscellaneous Tasks

- deps: update npm packages (#9002) by @renovate[bot]
- deps: update dependency @napi-rs/cli to v3.6.1 (#9034) by @renovate[bot]
- deps: upgrade oxc to 0.124.0 (#9018) by @shulaoda
- deps: update test262 submodule for tests (#9010) by @sapphi-red
- deps: update dependency oxfmt to ^0.44.0 (#9012) by @renovate[bot]
- deps: update dependency vite to v8.0.5 [security] (#9009) by @renovate[bot]
- deps: update dependency vite-plus to v0.1.16 (#9008) by @renovate[bot]
- deps: update rust crates (#9003) by @renovate[bot]
- deps: update github-actions (#9004) by @renovate[bot]
- deps: update dependency lodash-es to v4.18.1 [security] (#8992) by @renovate[bot]
- deps: update crate-ci/typos action to v1.45.0 (#8988) by @renovate[bot]
- upgrade oxc npm packages to 0.123.0 (#8985) by @shulaoda

### ◀️ Revert

- "chore(deps): update dependency oxfmt to ^0.44.0 (#9012)" (#9019) by @shulaoda

Co-authored-by: shulaoda <[email protected]>
SliverKeigo added a commit to SliverKeigo/rolldown that referenced this pull request Jul 16, 2026
…e mode

When the importer is in node ESM mode ("type": "module" / .mjs), the
CJS output requires externals and passes the result through
__toESM(mod, 1), which unconditionally overrides "default" with the
whole module.exports. Since Node supports require(esm), requiring an
ESM-only external returns a genuine module namespace whose "default"
is already correct; re-wrapping it diverges from the semantics the
source had under real Node ESM:

    import greet from 'esm-pkg'  // ESM-only external
    // real ESM:   greet is the default export
    // CJS output: greet is the whole namespace -> TypeError on call

Teach the node-mode branch of __toESM to leave genuine namespaces
alone. A genuine namespace is identified by Symbol.toStringTag being
'Module' together with a null prototype and being non-extensible, so
marker-only emulations (e.g. exports objects tagged by webpack's
runtime, which real Node ESM treats as plain CJS) keep the existing
node-mode behavior.

Emission is unchanged: node-mode __toESM(mod, 1) is still emitted for
ESM importers (rolldown#8842, rolldown#8987); only the runtime discrimination is new.

Fixes the failure mode of rolldown#9076 (closed as fixed by rolldown#8987, but rolldown#8987
addressed per-importer bindings for CJS externals; the require(esm)
namespace case still reproduced on 1.1.5). Real-world breakage:
rolldown/tsdown#1006.
SliverKeigo added a commit to SliverKeigo/rolldown that referenced this pull request Aug 7, 2026
…e mode

When the importer is in node ESM mode ("type": "module" / .mjs), the
CJS output requires externals and passes the result through
__toESM(mod, 1), which unconditionally overrides "default" with the
whole module.exports. Since Node supports require(esm), requiring an
ESM-only external returns a genuine module namespace whose "default"
is already correct; re-wrapping it diverges from the semantics the
source had under real Node ESM:

    import greet from 'esm-pkg'  // ESM-only external
    // real ESM:   greet is the default export
    // CJS output: greet is the whole namespace -> TypeError on call

Teach the node-mode branch of __toESM to leave genuine namespaces
alone. A genuine namespace is identified by Symbol.toStringTag being
'Module' together with a null prototype and being non-extensible, so
marker-only emulations (e.g. exports objects tagged by webpack's
runtime, which real Node ESM treats as plain CJS) keep the existing
node-mode behavior.

Emission is unchanged: node-mode __toESM(mod, 1) is still emitted for
ESM importers (rolldown#8842, rolldown#8987); only the runtime discrimination is new.

Fixes the failure mode of rolldown#9076 (closed as fixed by rolldown#8987, but rolldown#8987
addressed per-importer bindings for CJS externals; the require(esm)
namespace case still reproduced on 1.1.5). Real-world breakage:
rolldown/tsdown#1006.
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.

[Bug]: cjs build: __toESM fails to resolve import default jsonc-parser correctly

2 participants