Repository navigation
fix(code-splitting): only code-less entries keep side-effect-free static import edges - #11199
Conversation
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
0c45b1b to
18d668a
Compare
|
The result looks somewhat positive in tree-shaking, but it also increases the generated chunks. I would leave the decision to @hyfdev to have a look. Also, in your case, you could use |
|
Thanks for looking! The extra chunk is intentional: it's the minimum split that stops A manual group doesn't scale as a workaround: you have to know which module of which barrel to pull out, and in a real app (we hit this with default chunking on a large Angular app, ~0.4 MB brotli of unreachable code on several routes through one barrel imported for an enum) there are thousands of barrels and nothing flags which ones overfetch. It's also the only case I found where rolldown's default chunking makes a static importer load code it doesn't reach. The only existing snapshot that changes ( |
18d668a to
2399188
Compare
2399188 to
41bdd79
Compare
…tic import edges The chunking walk keeps a side-effect-free import edge into an entry module so a code-less re-export barrel that is also a dynamic entry collapses onto the chunk holding its exports (Rollup's `entry-without-code-dynamic`). Applied to every entry, it also kept edges into barrels with code of their own, marking everything that code imports as loaded by each static importer: a module that only read one re-exported binding shared a chunk with the barrel's own code and statically loaded its whole dependency tree. Rollup keeps the binding separate. Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_0119HdSDLEi9Hu4J1DE2fDts
41bdd79 to
6e09d3c
Compare
hyfdev
left a comment
There was a problem hiding this comment.
I'm OK with the extra chunk.
Rollup emits 6 chunks for your fixture, one more than this PR.
The common-chunk merge can still undo the split when the dynamic entries import the barrel directly:
graph LR
main -.->|"import()"| r1
main -.->|"import()"| r2
main -.->|"import()"| lib["lib/index.js"]
r1 -->|"{ A }"| lib
r2 -->|"{ B }"| lib
lib --> a["lib/a.js"]
lib --> b["lib/b.js"]
// lib/index.js (sideEffects: false)
import { A } from './a.js';
import { B } from './b.js';
export { A, B };
export const all = [A, B];a.js and b.js both end up in the chunk of lib/index.js, so r1 loads B.
The output is the same as on main.
find_merge_target still counts a static import of the barrel as a load.
Could it also require the barrel to be in the importer's load_dependencies?
With that check and the one below, your fixture and this case both match Rollup's chunks.
The two checks change no other snapshots.
A declaration that contains `import()` or `require()` has import records, so the code-less check took it for an import or re-export and kept the barrel's static importers in its bit pattern. Only `ImportKind::Import` records count now. The common-chunk merge picked a dynamic entry's chunk as the target whenever every other entry imported that entry. An importer that only reads a re-exported binding does not load a side-effect-free entry with code of its own, so the merge put the module it shares with the entry into a chunk it otherwise never fetches, along with everything else merged there. The target's entry module must now also be in each importer's `load_dependencies`. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…r a common chunk `m.js` was a dynamic entry with code that `x.js` imports without using. With only code-less entries keeping that edge, `x.js` no longer shares a chunk with it, so `m.js` became a plain dynamic entry chunk and the fixture stopped covering a common chunk that no file imports. `m.js` is now a code-less barrel over `impl.js`, which keeps the common chunk and the kept-as-file decision. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Thanks, both cases now match Rollup, and the Could you update the description and two comments before we merge?
|
…mport edges Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks! Updated both comments in e6af925. |
✅ Deploy Preview for rolldown-rs canceled.
|
Since #11199, when a module imports a binding that an entry re-exports, the module declaring the binding can run before the importer's earlier imports. This PR restores import order when that entry is listed in `input` or emitted by a plugin. #11199 is not released yet. ```js // later.js import './init.js'; import { value, dummy } from './lib/index.js'; export const result = [value, dummy()]; // early.js import { value } from './lib/index.js'; import './init.js'; export function first() { return value; } // lib/index.js export { value } from './read.js'; export function dummy() { return 'dummy'; } // lib/read.js export const value = typeof rolldownReproValue === 'object' ? 'ready' : 'unset'; // init.js globalThis.rolldownReproValue = {}; ``` With `early.js`, `later.js` and `lib/index.js` as inputs, `result` is `['unset', 'dummy']` on `main`. Node, Rollup 4.68.7, esbuild 0.28.2 and webpack 5.112.2 give `['ready', 'dummy']`. The emitted `later.js` imports the chunk with `read.js` before `init.js`: ```js import { t as value } from "./read.js"; import "./init.js"; ``` #11199 drops a side-effect-free static import edge into an entry that has code of its own. Without that edge, a lazy route that reads one re-exported binding loads only the chunk that declares it. This PR gives the edge back to entries from `input` and to plugin-emitted entries. If the re-exporting entry is dynamic and has code of its own, the bug remains, as on `main`. That is the trade-off #11199 makes for lazy routes. The only existing snapshot change is in `optimization/inline_const/cross_chunk_value_type`: `main.js` imports the `input` entry `./test.js`, as it did before #11199. These cases run like Node on `main` but not with this PR. With #11199 reverted, each differs from Node in the same way: - In two randomly generated module graphs, a module reads a global at a different time than in Node. Rollup 4.68.7 and webpack 5.112.2 match Node on both, and esbuild 0.28.2 throws. - In one, the module already sees a value that Node sets only after it runs. - In the other, it reads the global before its setup runs. - If `preserveEntrySignatures` is `'allow-extension'` and two entries from `input` import each other through a shared module, the shared code moves into one entry's chunk. That chunk gets an extra export. - A manual group can hold a module A and a module B that A loads with `import()`. If B statically imports an entry from `input` or a plugin-emitted entry but uses none of the entry's bindings, the problems below can happen. v1.3.3 has both. - If A uses top-level `await import()`, the `await` never settles. `main` hangs the same way when B instead imports a dynamic entry that only re-exports. - With `entriesAware: true`, A and B can land in one chunk. `main` puts them in separate chunks. B can then run before setup code that runs ahead of it on `main`. Loading a different entry can also run A's code.
## [1.3.4] - 2026-11-26 ### 🚀 Features - add builtin `injectPlugin` with `include`/`exclude` (#11290) by @onmax - magic-string: support explicit sourcemap locations (#11884) by @IWANABETHATGUY - introduce `declaredModuleSize` to allow plugins to declare a module's real size (#11844) by @hyfdev - binding: add dropInner() to 12 binding classes and extract release_arc helper (#11855) by @Brooooooklyn - inline-common-chunks: add `maxDuplicatedSize` to keep widely copied chunks as files (#11845) by @mattlewis92 - cli: report when a watch round finishes (#11820) by @hyfdev - rolldown_plugin_vite_build_import_analysis: avoid stripping import attributes when `experimental.importAttributes` is enabled (#11802) by @sapphi-red - import-attributes: warn when a non-ESM output drops the import attributes of an external (#11797) by @shulaoda - warn when the `watch` option is passed to `build()` (#11781) by @shulaoda ### 🐛 Bug Fixes - keep_names: restore hoisted function names before the chunk's modules run (#11962) by @hyfdev - external: don't drop `_/` from relative external imports (#11968) by @hyfdev - external: don't panic on empty output filename with relative externals (#11963) by @hyfdev - use the same path as direct imports for externals re-exported with `export *` (#11964) by @hyfdev - sourcemap: don't panic on empty output filename (#11966) by @hyfdev - sourcemap: treat empty `sourcemapFileNames` as unset (#11967) by @hyfdev - rename clashing names in execution order with `chunkModulesOrder: 'module-id'` (#11943) by @hyfdev - code-splitting: stop counting JSON modules as 0 bytes (#11939) by @hyfdev - fetch changed modules from their creating resolution in incremental rebuilds (#11837) by @h-a-n-a - code-splitting: keep import order through entry re-exports (#11906) by @hyfdev - cjs: keep CommonJS named exports in `export *` of a dynamically imported module (#11835) by @hyfdev - plugin: remove internal keys from plugin and code splitting group contexts (#11914) by @hyfdev - remove the leftover `watch.notify` from the option validator (#11918) by @shulaoda - return the group chunk file from `getFileName()` for a merged emitted entry (#11858) by @IWANABETHATGUY - dev: let `this.load` of an unchanged module finish during a rebuild (#11911) by @hyfdev - code-splitting: avoid an extra shared chunk when an entry only re-exports another entry (#11847) by @hyfdev - treeshake: remove unused objects containing identity function calls (#11852) by @hyfdev - cjs: avoid `ReferenceError` when re-exporting from an external `export *` in another chunk (#11848) by @hyfdev - binding: remove stale WASI workaround in to_binding_error (#11890) by @Brooooooklyn - inline-const: respect `inlineConst` options for default exports (#11851) by @hyfdev - magic-string: keep trailing lines in decoded maps (#11882) by @IWANABETHATGUY - magic-string: preserve column mappings through trim (#11881) by @IWANABETHATGUY - magic-string: map each line of multiline replacements (#11880) by @IWANABETHATGUY - follow Rollup for module meta and moduleSideEffects set by plugins (#11792) by @h-a-n-a - code-splitting: import the bindings a CommonJS entry re-exports from an ES module (#11839) by @hyfdev - cjs: stop overwriting `module.exports` of a CommonJS entry that re-exports an ES module (#11846) by @hyfdev - sourcemap: accept undefined source entries from plugins (#11850) by @fengmk2 - code-splitting: unblock top-level awaited dynamic imports in manual groups (#11819) by @hyfdev - runtime: resolve `require()` from the calling chunk in Node ESM output (#11834) by @hyfdev - plugin: throw instead of panicking when a saved transform context calls `getCombinedSourcemap` (#11838) by @hyfdev - code-splitting: only code-less entries keep side-effect-free static import edges (#11199) by @mattlewis92 - make `getCombinedSourcemap` work with `experimental.nativeMagicString` (#11107) by @IWANABETHATGUY - plugin: avoid hangs when resolveId awaits this.load() (#11824) by @shulaoda - keep template chunk names from overwriting explicit file names (#11831) by @hyfdev - code-splitting: skip redundant re-export chunks (#11817) by @hyfdev - preserve build errors when `buildEnd` fails (#11828) by @sapphi-red - preserve top-level using with topLevelVar (#11804) by @akshathayadav378-boop - treeshake: keep array destructuring that calls an unknown iterator (#11812) by @IWANABETHATGUY - preserve `export` names starting with `*` in ModuleInfo.exports (#11825) by @sapphi-red - treeshake: keep calls to functions that direct `eval` can replace (#11811) by @IWANABETHATGUY - keep each source's content in a multi-source transform sourcemap (#11246) by @IWANABETHATGUY - treeshake: keep calls to empty functions with a rest parameter default (#11810) by @IWANABETHATGUY - plugin: allow this.load() while resolving entries (#11816) by @hyfdev - cli: avoid a WASI hang when watch mode exits normally (#11815) by @shulaoda - report effective moduleSideEffects in getModuleInfo and this.resolve (#11422) by @cestercian - code-splitting: avoid an import cycle through an entry's re-exports (#11803) by @hyfdev - treeshake: apply @__NO_SIDE_EFFECTS__ to functions exported after declaration (#11222) by @araishi - code-splitting: keep unused entry re-exports from loading lazy chunks (#11753) by @hyfdev - code-splitting: keep re-exported bindings out of dynamic import namespaces (#11799) by @hyfdev - perf-kit: Node mode times out under the inspector with `--ready exit` or an anchored `stdout:` pattern (#11416) by @IWANABETHATGUY - treeshaking: drop dead dynamic imports that form a cycle (#11768) by @hyfdev - binding: release the WASI runtime when a callable builtin plugin hook rejects (#11786) by @shulaoda - perf-kit: print real imports in the why chain (#11500) by @IWANABETHATGUY - chunk: name emitted chunks before shared chunks (#11790) by @hyfdev - cjs: forward exports from duplicate CommonJS entries (#11756) by @hyfdev - code-splitting: keep chained emitted entries from becoming empty facades (#11090) by @Huzaifa-Asif ### 💼 Other - wasi: bundle self-contained WASI loaders, staging script, and reactor checks (#11843) by @Brooooooklyn ### 🚜 Refactor - scan: remove an unreachable branch when collecting dynamic import entries (#11919) by @shulaoda - cli: replace the unreachable prototype pollution guard with a test (#11925) by @shulaoda - remove the unused `DeterminedSideEffects::to_module_side_effects` (#11926) by @shulaoda - remove more unused types, functions and re-exports (#11922) by @shulaoda - link_stage: handle required modules only in the pre-pass (#11920) by @shulaoda - cli: remove the leftover `inject` from the CLI options type (#11924) by @shulaoda - rolldown_plugin_vite_build_import_analysis: remove the unused `optimizeModulePreloadRelativePaths` option (#11923) by @shulaoda - remove experimental `freeExternalMemory` (#11886) by @shulaoda - rust: drop direct tokio dependencies from core library crates (#11841) by @Brooooooklyn - rolldown_plugin: move `normalize_transform_sourcemap` to utils (#11247) by @IWANABETHATGUY - remove the unused `plugins_are_slow` method (#11800) by @shulaoda - remove parallel plugin support (#11789) by @h-a-n-a - remove the unused `is_css` sourcemap flag (#11782) by @shulaoda ### 📚 Documentation - rename javascript to JavaScript (#11958) by @mdong1909 - say that resolveId fails the build when the same id is external and not external (#11910) by @h-a-n-a - explain which resolution sets meta and moduleSideEffects in resolveId (#11907) by @h-a-n-a - resolve the import in the `moduleSideEffects` plugin example (#11860) by @hyfdev - clarify side-effect-free module behavior (#11856) by @IWANABETHATGUY - clarify the behavior when `buildEnd` / `renderError` itself errors (#11829) by @sapphi-red - add docs for `code` / `plugin` / `pluginCode` of `RolldownLog` (#11827) by @sapphi-red - explain that a module in an entry chunk runs after the common chunks it imports (#11822) by @hyfdev - magic-string: document `generateDecodedMap` and the segments of `hires: false` maps (#11791) by @IWANABETHATGUY ### ⚡ Performance - binding: return memory to the OS once rolldown is idle (#11655) by @shulaoda - scan: reserve symbol capacity for each module's facade symbols (#11854) by @magurotuna - runtime: stop using bound functions as `__copyProps` getters (#11826) by @magurotuna - replace `IndexVec<ModuleIdx, bool>` with `IndexBitSet` (#11805) by @IWANABETHATGUY - rolldown_plugin_vite_manifest: look up chunk names by file name once per build (#11697) by @Boshen - drop module sourcemaps after the chunk is joined (#11773) by @shulaoda ### 🧪 Testing - magic-string: re-enable two passing compatibility tests (#11879) by @IWANABETHATGUY - node: add standalone runtime/proxy/retained-context tests and vitest binding staging (#11842) by @Brooooooklyn - cli: stop watch CLI tests from hanging on Windows (#11821) by @shulaoda ### ⚙️ Miscellaneous Tasks - delete cancelled pkg.pr.new runs on main and debounce for 15 mins (#11928) by @shulaoda - remove the unused `serde` features of rolldown, rolldown_plugin and rolldown_common (#11927) by @shulaoda - deps: update vite-plus to v1.8.0 in the workspace and CI (#11896) by @camc314 - deps: update rollup submodule for tests to v4.68.8 (#11901) by @rolldown-guard[bot] - deps: update test262 submodule for tests (#11902) by @rolldown-guard[bot] - remove the graphite merge-when-ready label check (#11904) by @shulaoda - deps: pin walkdir to 2.5.0 (#11897) by @hyfdev - deps: remove remove-unused-vars (#11895) by @camc314 - deps: update rust crate testing_macros to v3 (#11876) by @renovate[bot] - deps: update rust crates (#11873) by @renovate[bot] - deps: update npm packages (#11872) by @renovate[bot] - deps: upgrade oxc to 0.160.0 (#11885) by @camc314 - deps: update github actions (#11874) by @renovate[bot] - rust: standalone runtime prep (devtools sync writer, TestDir, 1ms debounce, Wasm sourcemap inline, parking_lot_core patch) (#11840) by @Brooooooklyn - run setup steps one by one on Windows (#11849) by @shulaoda ###◀️ Revert - "feat(binding): add dropInner() to 12 binding classes and extract release_arc helper (#11855)" (#11898) by @shulaoda - "fix(code-splitting): keep chained emitted entries from becoming empty facades (#11090)" (#11891) by @hyfdev ### ❤️ New Contributors * @fengmk2 made their first contribution in [#11850](#11850) * @akshathayadav378-boop made their first contribution in [#11804](#11804) * @araishi made their first contribution in [#11222](#11222) * @Huzaifa-Asif made their first contribution in [#11090](#11090) Co-authored-by: shulaoda <[email protected]>
What this fixes
A side-effect-free barrel that is both a dynamic entry and statically imported makes every static importer load the barrel's own code and its whole dependency tree, even an importer that only reads one re-exported binding.
patch_module_dependencieskeeps side-effect-free import edges into entry modules, so that a code-less re-export barrel that is also a dynamic entry collapses onto the chunk holding its exports (Rollup'sentry-without-code-dynamic). The exception covers every entry, including barrels that declare code of their own. For those, keeping the edge marks everything the barrel's code imports as loaded by each static importer.Before this change,
a.jsstatically imports the chunk holdingbar.jsandheavy.js. Rollup putsfooin its own chunk, and so does this branch:This change limits the exception to code-less entries: those whose included statements only import or re-export, and declare or evaluate nothing of their own. Code-less barrels keep the existing behaviour. A declaration that contains
import()orrequire()counts as code of the entry's own.The common-chunk merge follows the same rule. An entry's chunk is a merge target only when every other entry both imports that entry and loads it (it is in the importer's
load_dependencies). An importer that only reads a re-exported binding does not load the barrel, so the module it shares with the barrel no longer moves into the barrel's chunk.Impact
In a large production app that relies on default chunking (no manual groups), one lazily loaded page read a single constant through a component's barrel. Because of that it statically loaded about 2 MB of unrelated component code it never runs. With this change that page loads only the constant.
This isn't a recent regression. A fixture for this case also fails at #10111, which introduced
load_dependencies, and at its parent. Before #10111 every side-effect-free edge counted, so the overfetch was larger.Tests
Against current
main, the PR adds three fixtures, changes three existing snapshots and rewrites one existing fixture:code_splitting/static_import_of_dynamic_entry_with_code: the counterpart ofstatic_import_of_code_less_dynamic_entry. It walksa.js's static imports, asserts none of them holdsheavy.js, and runs all three dynamic imports. Onmainit fails witha.js statically loads lib.js, which holds heavy.js.code_splitting/static_import_of_dynamic_entry_with_dynamic_import: the same barrel with animport()inside its own declaration.a.jsmust still not loadheavy.js.code_splitting/dynamic_entry_with_code_not_merge_target: two dynamic entries each read one binding re-exported by a barrel that is also a dynamic entry. Neither may statically load the module the other one reads.function/advanced_chunks/dynamic_entry_shared_module_not_merged:shared.jsis a dynamic entry with code, so it now exportsfuncdirectly instead of going through a namespace object (.then((n) => n.t)).issues/11392:data.jsonstays in thedata.jsentry chunk instead of a separatedata2.jsthat the entry re-exported.optimization/inline_const/cross_chunk_value_type: a bareimport "./test.js"of a side-effect-free entry is gone.function/inline_common_chunks/kept_as_file/import_target_without_importer. Itsm.jswas a dynamic entry with code thatx.jsimports without using, so with this change it became a plain dynamic entry chunk and the fixture no longer covered a common chunk that no file imports.m.jsis now a code-less barrel overimpl.js, which keeps the common chunk and the kept-as-file decision.cargo test --workspace --exclude rolldown_bindingpasses, apart fromtest262, which needs the submodule that is missing from my local checkout.main(1,220 passing and no new failures) and therolldown-testsmain suite passed, apart from the dev-server test files, which need@rolldown/test-dev-serverbuilt. I have not rerun these two suites locally for the later commits.AI disclosure
I wrote this with Claude (Claude Code), after tracking down the overfetch in a production build.
🤖 Generated with Claude Code
https://claude.ai/code/session_01GQUsQxBGnchKGCa2tNqrC2