Rspack split chunks

From rstackjs/agent-skills · View interactive version

Rspack split chunks is an AI agent skill from the GitHub repository rstackjs/agent-skills. Diagnose or tune Rspack splitChunks for duplicate modules, route over-fetching, cache groups, caching, or oversized chunks.

How to use this skill

Skill file

Rspack SplitChunks optimization

Use this skill when the task is to recommend, review, or debug optimization.splitChunks. If you are using ESM library, it's not the same algorithm of this skill.

Default stance

optimization: {
  splitChunks: {
    chunks: "all",
  },
}

Read CODE0 when you need the source-backed rationale.

What to optimize for

First identify which problem the user actually has:

Do not optimize all of these at once. Pick the primary goal and keep the rest as constraints.

Workflow

1. Start from the safest production baseline

Unless the user already has a measured problem that requires custom grouping, prefer:

optimization: {
  splitChunks: {
    chunks: "all",
  },
}

Why:

If the existing config disables default or defaultVendors, assume that is suspicious until proven necessary.

2. Audit the config for high-risk knobs

Check these first:

3. Interpret `name` correctly

Use this rule:

That means a fixed name: "vendors" or name: "common" is often the real reason a page starts fetching modules from unrelated dependency chains.

Prefer these alternatives before adding name:

Use a fixed name only when the user explicitly wants one shared asset across multiple entries/routes and accepts the extra coupling.

4. Preserve the built-in cache groups by default

Rspack's built-in production-oriented behavior depends heavily on these two groups:

These defaults are usually the best balance between dedupe and "only fetch what this page needs".

If you customize cacheGroups, do not casually replace these with one manually named vendor bucket.

5. Use `chunks: "all"` without fear of breaking execution order

When a module group is split out, Rspack connects the new chunk back to the original chunk groups. That preserves JavaScript loading semantics.

So:

6. Use `maxSize` as a refinement tool

Use maxSize, maxAsyncSize, or maxInitialSize when the problem is "this shared chunk is too large", not when the problem is "I need a stable vendor chunk name".

Important behavior:

This is usually safer than forcing one giant named vendor chunk, because it keeps chunk graph semantics while subdividing hot spots.

7. Use `usedExports` deliberately

If the user has multiple runtimes/entries and wants leaner shared chunks per runtime, prefer keeping usedExports enabled.

If they set usedExports: false, expect broader sharing and potentially larger common chunks.

This is still not tree shaking. It only changes how splitChunks groups modules across runtimes.

8. Treat `enforce: true` as an escape hatch

enforce: true bypasses several normal guardrails. Use it only when the user intentionally wants a split regardless of minSize, minChunks, and request limits.

If a config looks aggressive and hard to explain, check enforce before changing anything else.

Recommendations by goal

Better default production chunking

Recommend:

optimization: {
  splitChunks: {
    chunks: "all",
  },
}

Avoid:

Avoid fetching non-essential modules

Recommend:

Avoid:

cacheGroups: {
  vendors: {
    test: /[\\/]node_modules[\\/]/,
    chunks: "all",
    name: "vendors",
    enforce: true
  }
}

That pattern often creates one over-shared chunk that many pages must fetch.

Improve caching without over-merging

Recommend:

Use a fixed name only if the user explicitly prefers cache reuse over route isolation.

Split a large shared chunk

Recommend:

optimization: {
  splitChunks: {
    chunks: "all",
    maxSize: 200000,
  },
}

Then tune:

Keep an intentionally shared chunk

Recommend a named chunk only when the user says something like:

Even then, call out the tradeoff explicitly:

Review checklist

When reviewing a user's config, explicitly answer:

  1. Is the goal dedupe, cache stability, request count, or route isolation?
  2. Is chunks: "all" a better baseline than the current config?
  3. Did name accidentally turn multiple candidates into one forced shared chunk?
  4. Were default or defaultVendors disabled without a strong reason?
  5. Would idHint satisfy the naming goal without changing grouping?
  6. Is maxSize a better fit than a broad manual vendor/common bucket?
  7. Does the result still keep each page fetching only reachable chunks?

Minimal stats setup

When the task includes diagnosis, ask for or generate stats that expose chunk relations:

stats: {
  chunks: true,
  chunkRelations: true,
  chunkOrigins: true,
  entrypoints: true,
  modules: false
}

Then compare:

FAQ

Why do I still see duplicate modules?

Common reasons:

If the duplicate module is tiny, do not assume this is a bug. Rspack may intentionally keep it in place because splitting it out would create a worse chunk.

Does splitChunks affect JS execution order?

No.

Does splitChunks affect tree shaking?

No.

Can splitChunks affect CSS order?

Yes, potentially.

See web-infra-dev discussion #12.

Quick conclusions to reuse

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.