Skip to content

Library chunks make lazy routes' dependencies load at startup #4966

Closed

Description

@onmax

Describe the feature

libChunksGroup() puts each node_modules package into its own chunk. Rolldown's includeDependenciesRecursively defaults to true, so that chunk also includes everything the package imports. When startup code imports one small module from a package, the server loads the whole chunk. That can include code that only lazy routes use.

In my Nuxt app, a shared file imports mrmime to look up MIME types. mrmime ended up in the same chunk as Realtime, so Tiptap, ProseMirror and yjs all loaded at startup.

Setting entriesAware: true on the group fixes it. Rolldown then splits each package chunk by the entries that use it. On the app's Node build, startup modules went from 9.4 MB to 6.4 MB. The first request loaded 6.0 MB of extra modules instead of 11.8 MB. Responses didn't change.

const group = {
  test: NODE_MODULES_RE,
  name: (id: string) => libChunkName(id),
  entriesAware: true,
};

Rolldown names the split chunks <group>~<entries>~<hash>. To keep file names the same as today, getChunkName() drops everything from the first ~.

I have a patch with tests, and Nitro's unit tests, typecheck and lint pass. Would you accept this as the default, or would you rather have it behind an option? I can open the PR.

Additional information

  • Would you be willing to help implement this feature?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions