How Vite pre-bundles dependencies and caches them
Trace esbuild dependency optimization in Vite 7 and earlier, inspect the .vite/deps cache, and see what changes with Vite 8's Rolldown engine.
Vite serves application files as native ES modules in development, but npm dependencies can still require format conversion or hundreds of requests. Pre-bundling makes those packages practical to load and gives the browser a stable cache key.
The esbuild examples cover the Vite 5–7 dependency optimizer. Vite 8 uses Rolldown for that work.
Why dependencies need special handling
In the native ESM development server walkthrough, the browser requested only the application files it needed. Packages in node_modules create two additional problems.
CommonJS and UMD packages such as the React builds used in this example are not necessarily directly importable as native ESM. Browsers understand import and export in module scripts. They do not supply Node’s require or module.exports APIs. A dependency optimizer can convert CommonJS and make named imports work for compatible packages.
File count is a separate problem. An ESM package such as lodash-es can contain more than 600 files. Several layers of imports can force the browser to make many dependent HTTP requests on the first load even if the package already has the right module format.
flowchart TD
subgraph ProblemWaterfall["Request waterfall without pre-bundling"]
Browser["Browser"] -->|"1. import lodash-es"| Server["Dev Server"]
Server -->|"2. Return lodash.js (600 internal modules)"| Browser
Browser -->|"3. GET /debounce.js"| Server
Browser -->|"4. GET /isObject.js"| Server
Browser -->|"5. Request internal modules such as now.js"| Server
end
See what esbuild prepares
In Vite versions that use esbuild for dependency optimization, Vite discovers bare dependency imports across the initial entries, then hands those dependencies to esbuild. It can convert CommonJS to ESM and flatten many internal files into a few generated modules. Your application’s source is still transformed as the browser requests it.
flowchart LR
subgraph PreBundlingProcess["Dependency optimization with esbuild"]
Scan["1. Scan entry files"] --> Discover["2. Find bare dependencies"]
Discover --> Esbuild["3. Convert and bundle into ESM"]
Esbuild --> Cache["4. Cache output in .vite/deps"]
end
With import { debounce } from 'lodash-es', the browser can load the generated lodash-es.js and any shared chunks rather than traversing the original package’s entire graph over HTTP. The number of chunks and the time required depend on the dependencies and configuration. The example below shows a rebuild that took about 18 milliseconds.
Read the disk cache metadata
The generated dependencies live in node_modules/.vite/deps. Inspecting that directory answers two questions when a dependency behaves unexpectedly. Is Vite serving the original package or its optimized copy, and has the cached output been replaced since a dependency change?
Figure 1. The optimizer’s output directory and the cache hashes in its metadata file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// node_modules/.vite/deps/_metadata.json
{
"hash": "7a9f24b8",
"configHash": "1f8e3c4d",
"lockfileHash": "9b12d34e",
"browserHash": "8c5a21ef",
"optimized": {
"react": {
"src": "../../react/index.js",
"file": "react.js",
"fileHash": "4a71c89e"
},
"react-dom/client": {
"src": "../../react-dom/client.js",
"file": "react-dom_client.js",
"fileHash": "9b52f10c"
},
"lodash-es": {
"src": "../../lodash-es/lodash.js",
"file": "lodash-es.js",
"fileHash": "3c84e12d"
}
}
}
lockfileHashreflects the package manager’s lockfile. Installing or updating dependencies typically changes it.configHashreflects relevant Vite configuration, including optimizer settings.browserHashappears in dependency URLs, such as?v=8c5a21ef. A different URL lets the browser fetch a new copy without mutating an already cached one.
Vite can reuse the disk output across restarts. Lockfile or optimizer configuration changes can invalidate it. The Vite guide also lists patches-directory changes and NODE_ENV as inputs to this decision. A linked package’s source can change without any of those inputs moving.
Force a rebuild and configure exceptional dependencies
When an optimized dependency no longer reflects the linked version, I start Vite with --force. Deleting the local optimizer cache is an alternative if I want to inspect a clean rebuild.
1
2
3
# bash
# Clear the optimizer directory and force dependency optimization
pnpm vite --force --debug deps
Figure 2. The measured force run rebuilt four dependency entries with esbuild in 17.82 milliseconds.
Most packages can use automatic discovery. The following esbuild-era configuration handles cases where a dependency is missed, an internal package should stay as ESM source, or the optimizer needs an esbuild option.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
optimizeDeps: {
// 1. Include dependencies that discovery may miss
include: [
'lodash-es',
'dayjs',
'dayjs/plugin/utc',
'dayjs/plugin/timezone'
],
// 2. Keep a linked ESM workspace package out of pre-bundling
exclude: [
'@my-org/shared-utils'
],
// 3. Set esbuild options for the optimizer
esbuildOptions: {
target: 'es2022',
define: {
global: 'globalThis'
}
}
}
});
I reach for optimizeDeps.include when a dependency is introduced through a plugin or a dynamic import pattern that scanning can’t discover reliably. Dynamic syntax alone does not mean that Vite will miss an import with a static string. I use exclude when a linked workspace package is already valid ESM and I want it to behave like editable source. A package with a CommonJS dependency may still need an explicit inclusion for that nested package.
Keep the browser cache distinct and account for Vite 8
The files generated by the optimizer use long-lived HTTP caching. This response shows the cache policy and the versioned URL.
1
2
3
4
HTTP/1.1 200 OK
Content-Type: application/javascript
Cache-Control: max-age=31536000, immutable
ETag: W/"8c5a21ef"
The immutable directive lets the browser reuse a fresh cached result without a round trip. When the optimizer produces a new browser hash, the URL changes and the next request uses a different cache entry. If debugging seems to show a stale package, inspect that URL in the Network panel and distinguish the browser cache from Vite’s cache on disk.
Vite 5–7 could optimize dependencies with esbuild in development and use Rollup for a production build. Differences in CommonJS interoperability could surface only in production. Vite 8 uses Rolldown for both dependency optimization and production bundling. A shared bundler reduces that source of divergence. The build and serve paths still differ in configuration and lifecycle, so I smoke-test the production output when changing a difficult dependency.