Splitting Shared Dependencies
What you'll learn: How bundlers automatically extract common code into separate chunks and when you need manual configuration.
When you code-split multiple routes or components, they often import the same libraries—React itself, lodash, date-fns, etc. Without optimization, each chunk would include its own copy of these dependencies, bloating your bundle size dramatically.
Automatic Extraction
Modern bundlers like Webpack and Vite automatically detect shared dependencies across your code-split chunks and extract them into separate "vendor" or "common" chunks. This happens behind the scenes when you use React.lazy() and dynamic import().
For example, if Route A and Route B both import date-fns, the bundler creates three chunks: routeA.js, routeB.js, and vendors-date-fns.js. The browser downloads the shared chunk once and reuses it.
When Manual Configuration Helps
The automatic extraction works well most of the time, but you might need manual control when:
- A dependency is only shared by two small chunks—extracting it creates an extra HTTP request that hurts performance more than duplication
- You want specific libraries in their own chunk (like React itself) for better caching
- You need to control the size threshold for extraction (e.g., "only extract if shared code exceeds 30KB")
In Webpack, you configure this with optimization.splitChunks. In Vite/Rollup, you use manualChunks. These settings let you define rules like "extract React separately" or "only split if shared by 3+ chunks."
Key Takeaway: Bundlers automatically extract shared dependencies into common chunks to avoid duplication, but manual splitChunks configuration gives you fine-grained control over extraction thresholds and caching strategies.