The chunkSplitPlugin is a Vite plugin used to manage how Rollup splits code into chunks during the build process. It allows you to define strategies for grouping dependencies (like node_modules) or specific modules into named chunks.
Configuration Options
The plugin accepts a ChunkSplit object with the following properties:
strategy: Determines the splitting logic. Supported values are:"default": (Default) Groups node_modules into chunks based on their entry names. Static imports from node_modules are treated as part of the entry chunk unless specified otherwise."unbundle": Splits node_modules into vendor (for statically imported modules) and async-vendor (for dynamically imported modules). It also attempts to create chunks for local files based on their relative path."all-in-one": Minimizes chunk splitting by effectively bypassing the default manual chunking logic, primarily relying on customSplitting and customChunk.
useEntryName: (Boolean, default: true) When using the default strategy, if true, node_modules are assigned chunks based on their entry names. If false, they are grouped into a single "vendor" chunk.customSplitting: An object used to define custom groups. Keys are chunk names, and values are arrays of strings (package paths) or Regular Expressions to match module IDs.customChunk: A function (options) => string | null that allows for fine-grained, programmatic control over chunk assignment for any module ID.
import { chunkSplitPlugin } from 'vite-plugin-chunk-split';
export default defineConfig({
plugins: [
chunkSplitPlugin({
strategy: 'unbundle',
customSplitting: {
'react-vendor': ['react', 'react-dom'],
'utils-vendor': [/lodash/, /underscore/],
},
}),
],
});