Skip to content

feat(code-splitting): support inlining of shared deps in dynamic entries - #10526

Merged
IWANABETHATGUY merged 6 commits into
rolldown:mainfrom
nicolo-ribaudo:issue-10263
Jul 31, 2026
Merged

IWANABETHATGUY merged 6 commits into
rolldown:mainfrom
nicolo-ribaudo:issue-10263

Conversation

@nicolo-ribaudo

Copy link
Copy Markdown
Contributor

This PR fixes #10263 by aligning Rolldown's behavior with Rollup's: modules can be inlined into dynamic entry chunks, but when doing so we must generate a fake namespace object that we use at the dynamic import site, to avoid exposing the extra exports.

This is my first time contributing to Rolldown. I split this PR into three commits hoping that it makes things easier to review:

  • the first one adds a test showing the current behavior (wrong)
  • the second one moves some existing code into a shared helper because I need to re-use it for the third commit (it's the code to generate the fake namespace objects). I used an LLM to help extracting this code.
  • the third commit implements the actual fix.

I also used an LLM to figure out where to even make this change, and to generate the extra tests included in the last commit.

@codspeed

codspeed Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing nicolo-ribaudo:issue-10263 (39e1945) with main (7221254)2

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

  2. No successful run was found on main (1f32178) during the generation of this report, so 7221254 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@nicolo-ribaudo
nicolo-ribaudo force-pushed the issue-10263 branch 2 times, most recently from f4bf47a to 13693e0 Compare July 29, 2026 13:45
@IWANABETHATGUY

This comment was marked as outdated.

@IWANABETHATGUY

Copy link
Copy Markdown
Member

I think the overall direction and implementation of #10526 are solid. There are still a few minor issues, though. For reference, #10533 takes a more incremental approach by splitting the changes up and addressing a few regressions at the same time.

@nicolo-ribaudo

Copy link
Copy Markdown
Contributor Author

wait, i'm working on the other feedback still.

@nicolo-ribaudo

nicolo-ribaudo commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Done :) in a82b388

@IWANABETHATGUY

Copy link
Copy Markdown
Member

Thanks again for the work here — I re-reviewed the branch and ran a detailed side-by-side against an alternative implementation. I don't think we should land it in its current shape. One blocking finding, one secondary, and a proposal for how to converge.

Blocking: external star re-exports are dropped from the extracted namespace.

For a dynamic entry with an entry-level export * from '<external>', the merge is still applied and the call site rewrites to .then((n) => n.t), but t is built by __exportAll({...}) from statically-known exports only — everything the external supplies at runtime silently vanishes from the import() result:

// app.js (dynamic entry whose shared dep gets inlined)
export * from 'node:path';
export const done = import('./lazy.js').then((m) => m.value);

// main.js
const app = await import('./app.js');
typeof app.sep; // expected 'string' — actual: 'undefined' on this branch

The chunk still emits export * from "node:path" at the chunk level, but the extraction callback discards the chunk namespace (where those names live) and returns the synthetic one. The same shape also breaks an external-supplied callable then: promise assimilation should settle with the value then forwards, but the callback reads .t off the assimilated value and observes undefined. Both fail deterministically on this branch. Rollup handles these by building the extracted namespace with _mergeNamespaces(...) — a runtime merge — and by not emitting the chunk-level star for extracted entries, so the chunk namespace never becomes thenable.

Secondary: extraction cost on the partial-usage path.

For this issue's own repro the import() usage is statically known (.then((app) => app.done)), so no namespace hiding is needed at all — the reads provably can't observe the extra exports. This branch pays the machinery anyway: the namespace object, the __exportAll/__defProp helpers in the chunk, and an extra .then((n) => n.t) microtask hop. Measured on the repro: 871 B total vs 436 B achievable without extraction (unminified).

Proposal.

I've put up a stacked alternative that addresses both: #10532 (merge when partial usage proves the extra exports unobservable — no extraction machinery, leaner than Rollup on this graph), #10560 (extraction for opaque usage via the existing common_chunk_exported_facade_chunk_namespace + rewrite_dynamic_import_for_merged_entry path — output byte-identical to this branch where both act, and it passes this PR's already_loaded_namespace_extraction and already_loaded_then_export_no_extraction fixtures unchanged), and #10559 (external stars forwarded through a runtime __reExport merge, verified against Rollup 4.62.3 on these exact graphs; executable reproductions of the cases above live there as already_loaded_external_star{,_then}_extraction).

To be clear, the direction here was right — the two implementations converge on identical output wherever this branch is correct. And the simulated_facade_inclusion.rs refactor is genuinely valuable; I'd love to land that as a follow-up on top of the stack rather than folded into the behavior change. Would appreciate your eyes on the stack.

Full comparison data — per-test chunk counts & correctness, emitted bytes, implementation shape, guard coverage (every cell measured by running the fixture under the named implementation)

Setup

Per-test: chunk count and correctness

# Graph shape Rollup Stack This PR
1 issues/10263 — the issue repro, partial usage (.then(m => m.done)) 3 ✅ 3 ✅ (lean) 3 ✅ (extraction)
2 already_loaded_namespace_extraction — export const app = import(...), opaque 3 ✅ 3 ✅ 3 ✅
3 already_loaded_then_export_no_extraction — entry statically exports callable then — 4 ✅ 4 ✅
4 dynamic_dominator_with_exports_no_merge — rest-destructured namespace 3 ✅ 3 ✅ 3 ✅
5 Object.keys(ns) observation 3 ✅ 3 ✅ 3 ✅
6 reads ns.missing (undefined in source semantics) 3 ✅ 3 ✅ 3 ✅
7 static then export (stack's own refusal fixture) — 4 ✅ 4 ✅
8 export * from 'node:path', reads runtime-only ns.sep 3 ✅ 3 ✅ 3 ❌ sep → undefined
9 export * from '<external>', reads a runtime-only named export 3 ✅ 3 ✅ 3 ❌ value → undefined
10 export * from '<external>' where the external supplies a callable then 3 ✅ 3 ✅ 3 ❌ hijack value → undefined
union suite 10/10 7/10

Rows 2–3 are byte-identical outputs between the stack and this PR. Rows 8–10 are one bug class: the extraction namespace enumerates only build-time-known exports, so runtime-supplied names vanish. Under the stack, #10560 alone keeps rows 8–10 at 4 chunks (correct, refused); #10559 lifts them to 3 with the runtime merge.

Emitted bytes for the issue's own graph (unminified)

Chunk Stack (tier-1 lean) This PR
app.js 210 B 628 B (incl. __defProp/__exportAll helpers hidden in snapshots as // HIDDEN [\0rolldown/runtime.js])
lazy.js 121 B 121 B
main.js 105 B 122 B (.then(n => n.t) hop)
total 436 B 871 B (2.0×)

Where extraction is genuinely needed (opaque usage), stack and PR outputs are byte-identical — the delta exists only where the stack's tier-1 proves extraction unnecessary.

Implementation shape

Stack (all 3 PRs) This PR
Rust source diff +310 / −18 across 2 files +345 / −181 across 5 files
New modules none simulated_facade_inclusion.rs (+141)
chunk_optimizer.rs untouched ~230 lines churned (helper-extraction refactor)
Machinery reuses simulated-facade subsystem as-is refactors it into a shared helper first
Known duplication ~40 lines of IncludeContext construction (unifiable later by exactly this PR's refactor) —
Snapshot churn outside intended fixtures zero (across ~2000 fixtures, every run) not measured

Guard-boundary coverage

Boundary Stack fixture This PR
Partial usage → lean merge issues/10263 — (always extracts)
Opaque namespace → extraction already_loaded_opaque_namespace_extraction already_loaded_namespace_extraction
Unexported-name read → extraction, undefined preserved already_loaded_unexported_read_extraction —
Static then export → refuse already_loaded_then_export_keeps_split already_loaded_then_export_no_extraction
CJS export * → refuse already_loaded_cjs_star_keeps_split —
External star → forward via runtime merge already_loaded_external_star_extraction — (broken, untested)
External star supplying runtime then already_loaded_external_star_then_extraction — (broken, untested)

@nicolo-ribaudo

nicolo-ribaudo commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Blocking: external star re-exports are dropped from the extracted namespace.

For a dynamic entry with an entry-level export * from '<external>', the merge is still applied and the call site rewrites to .then((n) => n.t), but t is built by __exportAll({...}) from statically-known exports only — everything the external supplies at runtime silently vanishes from the import() result:

// app.js (dynamic entry whose shared dep gets inlined)
export * from 'node:path';
export const done = import('./lazy.js').then((m) => m.value);

// main.js
const app = await import('./app.js');
typeof app.sep; // expected 'string' — actual: 'undefined' on this branch

The chunk still emits export * from "node:path" at the chunk level, but the extraction callback discards the chunk namespace (where those names live) and returns the synthetic one. The same shape also breaks an external-supplied callable then: promise assimilation should settle with the value then forwards, but the callback reads .t off the assimilated value and observes undefined. Both fail deterministically on this branch. Rollup handles these by building the extracted namespace with _mergeNamespaces(...) — a runtime merge — and by not emitting the chunk-level star for extracted entries, so the chunk namespace never becomes thenable.

I don't think this is correct? I added a test case that shows that in that case typeof app.sep is a string, in d43e580 (EDIT: 20d3cc6 now that I rebased).

@nicolo-ribaudo

nicolo-ribaudo commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Secondary: extraction cost on the partial-usage path.

For this issue's own repro the import() usage is statically known (.then((app) => app.done)), so no namespace hiding is needed at all — the reads provably can't observe the extra exports. This branch pays the machinery anyway: the namespace object, the __exportAll/__defProp helpers in the chunk, and an extra .then((n) => n.t) microtask hop. Measured on the repro: 871 B total vs 436 B achievable without extraction (unminified).

Good suggestion, done in 73d0bfd (EDIT: afcb666 now that I rebased).

@nicolo-ribaudo
nicolo-ribaudo force-pushed the issue-10263 branch 3 times, most recently from 3bd195c to afcb666 Compare July 31, 2026 11:31

@IWANABETHATGUY IWANABETHATGUY 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!

@netlify

netlify Bot commented Jul 31, 2026

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 39e1945
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6a6c9f6b1b9ead00084dc8a8

@IWANABETHATGUY
IWANABETHATGUY merged commit e273daa into rolldown:main Jul 31, 2026
34 checks passed
@rolldown-guard rolldown-guard Bot mentioned this pull request Aug 3, 2026
shulaoda added a commit that referenced this pull request Aug 3, 2026
## [1.2.2] - 2026-08-03

### 🚀 Features

- code-splitting: support inlining of shared deps in dynamic entries (#10526) by @nicolo-ribaudo
- plugin: time the user callbacks configured on the options (#10509) by @IWANABETHATGUY

### 🐛 Bug Fixes

- dev: force `cleanDir` off in dev mode (#10579) by @shulaoda
- dev: keep `?rolldown-lazy` proxy ids out of user `resolveId` hooks (#10580) by @btea
- initialize re-exported wrapped ESM modules at the entry chunk (#10567) by @hyfdev
- link: resolve circular star reexports as null (#10445) by @Nic-Polumeyv
- transform: transformer gate skips newer syntax that oxc can lower (#10564) by @sekyungk
- code-splitting: materialize consumer-local barrel routing when the order-wrap plan is empty (#10544) by @hyfdev
- plugin: measure plugin hooks in JavaScript, report them from Rust (#10508) by @IWANABETHATGUY
- plugin: include external dynamic imports in chunk metadata (#10557) by @hyfdev
- plugin: report plugin timings after closeBundle has run (#10521) by @IWANABETHATGUY
- browser: avoid using node builtin modules (#10542) by @sapphi-red

### 🚜 Refactor

- plugin: give the build's own clocks their own type (#10520) by @shulaoda

### 📚 Documentation

- correct stale `buildStart` comment in `ScanStageCache::merge` (#10577) by @shulaoda
- update outdated pluginutils references (#10574) by @dogledogle

### ⚡ Performance

- external: drop the runtime import from chunks that never wrap (#10517) by @IWANABETHATGUY
- plugin: clock the build only when it is being measured (#10558) by @IWANABETHATGUY

### 🧪 Testing

- vite-tests: drop the dead `@rolldown/pluginutils` pnpm override (#10575) by @IWANABETHATGUY
- transform: group the transform-target fixtures and say why each exists (#10569) by @IWANABETHATGUY
- dev: fix hmr-hot-update-hook-vite fixture on Windows (#10565) by @h-a-n-a
- dev: match Vite's renamed full-reload log (#10547) by @shulaoda

### ⚙️ Miscellaneous Tasks

- deps: update napi (#10576) by @renovate[bot]
- deps: bump `@napi-rs/wasm-runtime` to 1.2.2 (#10570) by @shulaoda
- deps: update npm packages (#10583) by @renovate[bot]
- deps: update dependency rolldown-plugin-dts to ^0.28.0 (#10588) by @renovate[bot]
- deps: update taiki-e/install-action action to v2.85.5 (#10587) by @renovate[bot]
- deps: update codspeedhq/action action to v5 (#10584) by @renovate[bot]
- deps: update github actions (#10582) by @renovate[bot]
- deps: update rust crates (#10581) by @renovate[bot]
- deps: update dependency vite-plus to v0.2.7 (#10568) by @renovate[bot]
- deps: update dependency rolldown-plugin-dts to v0.27.14 (#10531) by @renovate[bot]
- deps: update pnpm to v11.17.0 (#10571) by @renovate[bot]
- deps: update @napi-rs/wasm-runtime to 1.2.1 (#10545) by @sapphi-red

### ❤️ New Contributors

* @sekyungk made their first contribution in [#10564](#10564)
* @nicolo-ribaudo made their first contribution in [#10526](#10526)

Co-authored-by: shulaoda <[email protected]>
@nicolo-ribaudo
nicolo-ribaudo deleted the issue-10263 branch August 3, 2026 13:34
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]: Excessive automatic code splitting when importing a module both statically and dynamically

2 participants