mergeCommonChunks is visit-order dependent: a leaf common chunk rejected by the cycle check is never retried after its importer is merged
Reproduction
Rolldown 1.2.11 (also 1.2.8), no plugins.
// src/main.js — the only user entry, no exports
import { a } from "./a.js";
console.log("main", a);
globalThis.load = [() => import("./d1.js"), () => import("./d2.js"), () => import("./d3.js")];
// src/a.js — shared by main + d1
import { c } from "./c.js";
console.log("a");
export const a = { c };
// src/c.js — shared by main (via a) + d2 + d3
export const c = { name: "c" };
// src/d1.js
import { a } from "./a.js";
export default a;
// src/d2.js / src/d3.js
import { c } from "./c.js";
export default [c, 2]; // [c, 3]// build.mjs
import { rolldown } from "rolldown";
const bundle = await rolldown({
input: "src/main.js",
experimental: { chunkOptimization: { mergeCommonChunks: true, avoidRedundantChunkLoads: false } },
});
const { output } = await bundle.generate({ dir: "dist", format: "es" });
for (const c of output) console.log(c.fileName, Object.keys(c.modules).map((m) => m.split("/").pop()));Actual
main.js modules=[a.js, main.js] imports=[c-*.js]
c-*.js (common) modules=[c.js]
d1-*.js imports=[main.js]
d2-*.js imports=[c-*.js]
d3-*.js imports=[c-*.js]Expected
c.js is merged into main.js as well, as it is with the default options:
main.js modules=[c.js, a.js, main.js]
d1/d2/d3 imports=[main.js]Once a is in main, merging c into main creates no cycle: d2/d3 would just import main, which is already loaded before any of them. The only thing that prevents it is that c's common chunk is visited before a's.
Analysis
As far as I can tell from crates/rolldown/src/chunk_graph/chunk_optimizer.rs (line numbers from v1.2.8):
- Pending common chunks are visited in insertion order. That order comes from
init_module_assignmentwalkingsorted_modules(execution order), so a leaf likeccomes before its importera(code_splitting.rs~1016/1050 →chunk_optimizer.rs~146; loops at ~398/424). - When
cis visited,ais still a separate pending chunk.would_create_circular_dependency(~217-251) walkssource.deps ∪ target.depsand treats "depends on source" as pointing to the target. It findsmain → a → c, so it reports a cycle and rejects the merge (~428). ais then merged intomainon its own turn, butcis never re-evaluated. The final graph has no cycle, andcships as its own chunk.
This looks like the same family of problem as #9963 / #10905 (merge attempts that give up on a temporary cycle), but in mergeCommonChunks instead of dynamic_already_loaded: the repro above has avoidRedundantChunkLoads turned off.
Why it matters in practice
In a real TanStack Start app (a single client entry, ~1k chunks), this is what decides whether small shared modules end up in the entry. Two tiny constant modules (0.7 KB and 1.3 KB) and Vite's preload-helper stay as separate chunks, so every page loads 2–3 more files. Unrelated changes flip the outcome: after a refactor removed some modules that happened to be ordered earlier, these files went from merged to not merged, with no change to the modules themselves. avoidRedundantChunkLoads did not strip the dynamic-entry bits for these modules, which we confirmed with attachDebugInfo: 'full', so mergeCommonChunks is the only pass that could merge them.
Possible fix
- Retry chunks rejected by the cycle check after the other merges are done, until no merge happens (like the
loop {}in #10905), or - visit pending common chunks in reverse execution order, so importers are merged before their dependencies.
Source: rolldown/rolldown