When `vite` starts a Vite 8 dev server, why does it pre-bundle dependencies, and what happens to a CommonJS package and a many-module package?
answer
- what the dev server serves
- bare imports found by a scan
- no require inside a browser module
- hundreds of files, one request
- Rolldown output in node_modules/.vite/deps
basics
~20 sVite 8's dev server serves native ES modules, so it pre-bundles bare-imported dependencies with Rolldown: a CommonJS or UMD package is converted to ESM, and a package of hundreds of internal modules becomes one file fetched in one request.
solid answer
~40 sOn a cold start, `vite` scans the app's entries for **bare imports** (specifiers such as `recur-rules` that resolve from `node_modules`), bundles each dependency it finds with Rolldown into `node_modules/.vite/deps`, and rewrites the app's imports to URLs such as `/node_modules/.vite/deps/recur-rules.js?v=…`. There are two reasons. **Compatibility:** the browser loads these files as ES modules and has no `require` or `module.exports`, so CommonJS and UMD packages must become ESM, and Vite analyses the result so named imports from a CommonJS package still work. **Request count:** an ESM package shipped as six hundred internal modules would cost hundreds of requests on page load; bundled, it costs one. Pre-bundling is a development step only: `vite build` bundles dependencies together with the app and has no optimizer pass. Vite 7 and earlier ran the optimizer on esbuild.
go deeper
Recall the two reasons: the dev server speaks native ESM, so CommonJS and UMD packages must be converted, and many-file packages are merged so a page makes one request instead of hundreds.
Explain the cold-start sequence: scan the entries for bare imports, bundle each dependency into node_modules/.vite/deps, rewrite imports to versioned URLs, and reuse the cache on the next start.
Show that you know the optimizer serves development only, so the production build processes a package by a separate path, and that Vite 8 moved the optimizer to Rolldown, which changes how its options are written.
Weigh the trade: converting each dependency set once up front buys fast page loads and CommonJS support, at the cost of a cache whose invalidation rules every team on the config must understand.
## What the dependency optimizer is Vite's dev server does not bundle your application. It serves each source module as a **native ES module** and transforms it only when the browser asks for it. Dependencies are the exception. As it starts serving the first page, Vite runs a **dependency optimizer**, a step its documentation calls **dependency pre-bundling**, which turns the packages your code imports into a small number of browser-ready ES modules. In **Vite 8** the optimizer runs on **Rolldown**, the Rust bundler that `vite build` also uses. Vite 7 and earlier ran the same step on esbuild. The step starts on its own the first time `vite` runs, or whenever its cache is out of date, and most projects never configure it. ## What happens on a cold start 1. **Scan.** With no usable cache, Vite crawls your source from its entries (by default every `.html` file in the project) and records each **bare import**. A bare import is a specifier like `cal-utils-es` or `recur-rules` that is meant to resolve from `node_modules`, as opposed to a relative path such as `./calendar.ts`. 2. **Bundle.** Each recorded dependency becomes an entry point of a Rolldown bundle. The output goes to `node_modules/.vite/deps`, together with a metadata file that records what was optimized and under which hash. 3. **Rewrite.** When the browser requests one of your modules, Vite rewrites its bare imports to the optimized files, for example `/node_modules/.vite/deps/cal-utils-es.js?v=f3sf2ebd`. The `v` query carries a hash, which lets the browser cache the file hard. 4. **Reuse.** On the next start Vite reuses the cache unless one of its inputs has changed: the lockfile, the patches folder, the config that affects dependency bundling, or `NODE_ENV`. ## Reason one: CommonJS and UMD compatibility - In development Vite serves **all** code as native ESM, which the browser loads through `import`. - A **CommonJS** file talks through `require()`, `module` and `exports`, and none of these exists inside a browser module. A **UMD** file expects a global or a module loader. Served as it is, either one fails. - The optimizer converts such a package to ESM once, at start-up, instead of on every request. - Vite then runs **import analysis** on the converted module, so `import { RRule } from 'recur-rules'` works even when the CommonJS package assigns its exports dynamically. How the `default` import of a CommonJS module is chosen is a separate rule, and Vite 8 changed it. ## Reason two: fewer requests Some packages ship their ES module build as hundreds of small files that import one another. Say a date-utility package has six hundred internal modules. Then `import { addDays } from 'cal-utils-es'` makes the browser fetch the whole chain, and Vite's own guide cites a real utility package of more than 600 modules that sets off 600+ requests at once. The dev server copes, but the browser's network queue congests and the page loads noticeably slower. Once pre-bundled, the package is **one module and one request**. | What the app imports | Served without pre-bundling | Served with pre-bundling | |---|---|---| | A CommonJS-only package | Fails in the browser: no `require`, no ES exports | One ESM file whose named imports work | | A UMD package | Expects a global or a loader | One ESM file | | An ESM package of 600 internal modules | Hundreds of requests on first load | One request | | The app's own source modules | Served one by one, transformed on demand | Not pre-bundled; still served one by one | ## What pre-bundling is not - **Not part of the production build.** The guide says pre-bundling applies only in development. `vite build` bundles dependencies and application code together with Rolldown, which handles CommonJS as part of its own bundling. Pre-bundling during the build was an experiment, and Vite 5.1 removed it. - **Not a bundle of your source.** Only dependencies are optimized. A package linked from your own monorepo is treated as source by default. - **Not a minification step.** The goals are module format and request count. ## What changed in Vite 8 - The optimizer moved from esbuild to Rolldown. `optimizeDeps.esbuildOptions` is deprecated, and Vite converts it into `optimizeDeps.rolldownOptions` automatically. - The reasons for pre-bundling, the scan, the cache folder and the rewritten URLs are the same as in Vite 7. What changed is the engine, and with it the option you use to customise the engine. When you hear "Vite pre-bundles with esbuild", you are hearing a description of Vite 7 or earlier.
- Does `vite build` use the files in `node_modules/.vite/deps`?No. Pre-bundling applies only in development. `vite build` bundles dependencies and application code together with Rolldown, which converts CommonJS as part of its own bundling. Pre-bundling during the build was an experiment that Vite 5.1 removed, so the optimizer's cache never feeds the production output.
- Why can you write `import { RRule } from 'recur-rules'` when that package is CommonJS?Because the optimizer converts the package to ESM and Vite runs import analysis on the result, so named imports map onto the CommonJS exports even when the package assigns them dynamically. Without that step the browser would get a file that declares no ES exports at all.
- Why is the first `vite` start slower than the ones after it?The first start, like any start with a stale cache, has to scan the source and bundle every dependency it discovers. Later starts read the result from `node_modules/.vite` and skip that work until the lockfile, the patches folder, the optimizer-related config or `NODE_ENV` changes.
A branch library that, before it opens, binds a box of six hundred loose leaflets into one volume and recatalogues a book from another library's system into its own; readers then borrow one item from the shelf, and the work is redone only when a new delivery arrives.
- Vite 8 still pre-bundles dependencies with esbuild, as Vite 7 did.
- Pre-bundling also runs during vite build to speed up the production bundle.
- The optimizer bundles the application's own source modules together with its dependencies.
- A browser can run a CommonJS file as a module if it is served with a JavaScript MIME type.
- Pre-bundling exists mainly to minify dependencies before the dev server sends them.
Assumes Vite 8: the dependency optimizer runs on Rolldown; Vite 7 and earlier ran it on esbuild, and optimizeDeps.esbuildOptions is now deprecated.