Skip to content

[Bug]: Excessive automatic code splitting when importing a module both statically and dynamically #9269

@nicolo-ribaudo

Description

@nicolo-ribaudo

Reproduction link or steps

Consider these two examples:

They both contain these modules:

// app.js
import "./shared.js";
console.log("app");
import("./sidebar.js");
// shared.js
console.log("shared");
// sidebar.js
import "./shared.js"
console.log("sidebar");

In addition, REPL B contains this module:

// entry.js
console.log("entry");
import("./app.js");

The entry point of REPL A is app.js, while the entry point of REPL B is entry.js.

What is expected?

Rolldown bundles REPL A with two chunks:

  • app, which contains app.js and shared.js
  • sidebar, which contains sidebar.js

app has a dynamic import for sidebar (for the app.js->sidebar.js dynamic import), and sidebar has a static import for app (for the sidebar.js->shared.js static import). Rolldown correctly detects that it does not need to split shared.js into its own chunk, because the only way sidebar.js is ever loaded is through app.js, so it's always app.js causing first shared.js to be loaded.

I would expect REPL B to be similar, given that the only difference is that app.js is not directly the entrypoint but it's dynamically imported by entry.js:

  • entry, which contains entry.js, and dynamically imports app
  • app, which contains app.js and shared.js, and dynamically imports sidebar
  • sidebar, which contains sidebar.js, and statically imports app

Rolldown should not extract shared.js into its own chunk for the same reason as above.

What is actually happening?

In REPL B, Rolldown is extracting shared.js into its own chunk:

  • entry, which contains entry.js, and dynamically imports app
  • app, which contains app.js, statically imports shared, and dynamically imports sidebar
  • shared, which contains shared.js
  • sidebar, which contains sidebar.js, and statically imports app

System Info

This `envinfo` command doesn't print the rolldown version, but it would be 1.0.0-rc.16.

Any additional comments?

I noticed this because Vite 8 (Rolldown) is producing many more chunks than Vite 7 (Rollup), going from about 50 to about 80.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions