Vite Dependency Pre-bundling
🎯 Introduction
By the end of this article, you will understand why Vite sometimes preprocesses dependencies on its first startup and how to control this process with optimizeDeps. This can help you resolve unexpected development errors.
🧱 Why Pre-bundle Dependencies?
During development, Vite relies on native browser ES modules. Many npm packages are published as CommonJS (require/module.exports), which browsers do not support directly. On its first startup, Vite converts these packages to ES modules and combines them into fewer files. This process is called dependency pre-bundling.
Imagine eating at a cafeteria:
- Without pre-bundling: a chef prepares each dish when you order it, creating a slow queue.
- With pre-bundling: popular dishes are prepared ahead of time, so you can take them immediately.
Pre-bundled files are cached in node_modules/.vite. As long as dependencies are not upgraded, they do not need to be rebuilt on the next startup.
✨ Default Behavior
The first time you run npm run dev, you may see this terminal output:
Pre-bundling dependencies: vue, element-plus
This means Vite is scanning entry files and pre-bundling the third-party dependencies they use.
node_modules/.vite is deleted or dependency versions change, Vite runs pre-bundling again.🛠 Control Pre-bundling Manually
include
If Vite does not discover a dependency automatically, you can specify it explicitly:
import { defineConfig } from 'vite';
export default defineConfig({
optimizeDeps: {
include: ['lodash-es', 'axios'],
},
});
exclude
Some packages already provide native ES modules and do not need pre-bundling. You can exclude them:
import { defineConfig } from 'vite';
export default defineConfig({
optimizeDeps: {
exclude: ['vue-demi'],
},
});
exclude carefully. If an excluded package is not entirely ES modules, the browser will report an error.force
If the cache causes problems, force Vite to pre-bundle dependencies again:
vite --force
Or:
npx vite --force
This clears the node_modules/.vite cache and rebuilds it.
🛠 A Common Scenario
Suppose you import a large component library but use only a few components:
import { ElButton } from 'element-plus';
By default, Vite pre-bundles all of element-plus. To build only the parts you use, you can use the library's ES module version or an on-demand import plugin. This is beyond the scope of this course, but you can explore it separately later.
❓ Questions and Answers
Q1: Does pre-bundling affect production builds?
No. Pre-bundling is used only during development. Rollup handles production builds directly.
Q2: Can I delete node_modules/.vite?
Yes. It is a cache directory, and Vite regenerates it automatically after deletion.
Q3: When should I specify include manually?
When Vite's automatic scan misses a dependency, or when a dependency is imported dynamically and cannot be discovered by the scan.
🧪 Exercise
Configure pre-bundling for a project that imports lodash-es:
- Install
lodash-es. - Add it to
optimizeDeps.includeinvite.config.js. - Delete
node_modules/.vite, runnpm run devagain, and check whether pre-bundling runs again.
import { defineConfig } from 'vite';
export default defineConfig({
optimizeDeps: {
// Add include here
},
});
🎉 You now understand Vite dependency pre-bundling! Next, we will learn how to build and deploy a Vite project.
