What happens when you run vite build?

IntermediateBuild tools: webpack and Vite interview questionBy , updated 21 November 2026

Short answer

vite build resolves your config and environment variables, treats index.html as the entry, and hands the whole module graph to a bundler: Rollup in most current versions, with newer versions moving to Rolldown. Plugins transform each file, the bundler tree-shakes and splits code into chunks, Vite minifies JavaScript and CSS, hashes file names, rewrites the HTML to point at them, and writes static files to dist/.

1. Resolve config and mode

Vite loads vite.config.ts (or .js, .mjs), runs each plugin's config hook so plugins can adjust it, merges defaults, and then calls configResolved with the final result. The mode defaults to production, which decides which env files are read:

.env                # always
.env.local          # always, ignored by git
.env.production     # mode-specific
.env.production.local

Only variables prefixed with VITE_ are exposed to client code. Every import.meta.env.VITE_SOMETHING is replaced with its literal value during the build, so the value ends up as plain text in a JavaScript file anyone can read. Never put secrets there. import.meta.env.MODE, PROD, DEV and BASE_URL are replaced the same way, which lets the minifier delete if (import.meta.env.DEV) branches entirely. Details are in environment variables in Vite and webpack.

You can pick another mode, for example vite build --mode staging, which loads .env.staging instead.

2. Start from index.html

Unlike many bundlers, Vite's default entry is an HTML file, not a JavaScript file. Vite parses index.html, finds <script type="module" src="/src/main.tsx"> and any stylesheets or assets it references, and uses them as entry points. For a multi-page app you list several HTML files in build.rollupOptions.input.

3. Build the module graph

Starting from those entries, the bundler resolves every import and asks the plugins to process each module. For a React app that means:

  • TypeScript and JSX are compiled, with types stripped but not checked.
  • CSS imports are processed with PostCSS, CSS modules and any preprocessor you installed.
  • Asset imports such as import logo from "./logo.svg" become URLs. Small assets under build.assetsInlineLimit can be inlined as data URLs instead.
  • Framework plugins do their own transforms, such as compiling Vue single-file components.

These are the same plugin hooks the dev server uses, which is why most plugins work in both. How Vite plugins and virtual modules work goes through them.

Type checking is not part of this step. The official templates run tsc before vite build in the build script for that reason.

4. Tree-shake and split into chunks

With the full graph in hand, the bundler removes exports nothing uses (tree shaking) and decides how to group modules into chunks:

  • Each entry gets a chunk.
  • Each dynamic import() starts a new async chunk, which is how route-level code splitting works.
  • Modules shared between chunks are pulled into common chunks so they are not duplicated.

CSS is split too: the CSS imported by an async chunk goes into its own file and loads with that chunk. You can steer chunking with manualChunks, for example to put a large charting library in its own file.

// vite.config.ts
export default defineConfig({
  build: {
    sourcemap: "hidden",
    rollupOptions: {
      output: { manualChunks: { charts: ["echarts"] } },
    },
  },
});

If you are on a Rolldown-based Vite version, check its docs for the equivalent bundler options, since names in this area have been changing.

5. Minify, hash and preload

Vite minifies the JavaScript and CSS output; in Rollup-based versions esbuild is the default minifier. Every output file gets a content hash in its name, such as assets/index-4f9a2c1b.js, so the file can be cached forever and a new deploy produces new names. Content hashes for long-term caching explains how stable hashes are kept.

Vite also avoids request waterfalls. It adds <link rel="modulepreload"> tags for the chunks the entry imports, and wraps dynamic imports with a small helper that fetches an async chunk's dependencies in parallel with the chunk itself rather than one after another.

The default output targets modern browsers that support native ES modules. Older browsers need @vitejs/plugin-legacy.

6. Write dist

Finally Vite rewrites index.html to reference the hashed files, copies everything in public/ to the output unchanged, and writes the result:

dist/
  index.html
  favicon.svg               # copied from public/
  assets/
    index-4f9a2c1b.js
    index-a81e03d2.css
    Settings-0c3b9e77.js    # an async route chunk
    logo-5d2e18aa.svg

The terminal report lists every file with its size and gzip size, which is a cheap first check for a bundle that grew. For a proper budget, see investigating a bundle that tripled in size.

dist/ is plain static files. Any static host or CDN can serve it, as long as client-side routes fall back to index.html (see why a deployed SPA returns 404 on refresh). Run vite preview to serve the built output locally before deploying.

Common follow-up questions

Why does the build use a bundler when dev does not?

Because shipping hundreds of unbundled modules to users creates request waterfalls and skips whole-app optimisations. The reasoning is in What is Vite and why is it so fast?.

Can Vite produce a backend manifest of output files?

Yes. build.manifest: true writes a JSON manifest that maps source entries to their hashed output files and imported CSS, so a server-rendered backend can print the right script and link tags.

How do you build a library instead of an app?

Use library mode with build.lib, which builds from a JavaScript entry instead of index.html and lets you mark peer dependencies like React as external.

Are source maps generated by default?

No. Set build.sourcemap to true, or to "hidden" to generate them without the comment that tells browsers where they are. See source maps in production.

Mistakes to avoid

  • Assuming the build checks types

    It does not; run tsc --noEmit or tsc -b in CI.

  • Putting API keys in VITE_ variables, which end up readable in the bundle

  • Judging bundle size or speed from the dev server instead of vite build and vite preview

  • Importing files from public/

    Those are copied as they are and should be referenced by absolute path; files you import from src/ are the ones that get processed and hashed.

  • Caching index.html for a long time, so users keep requesting old hashed files after a deploy

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Build a mini module bundler: resolve imports, build the graph, handle cycles and emit one runnable bundleHard60 min
  2. Name chunks with content hashes for long-term caching: deterministic module ids and a runtime chunkMedium35 min
  3. Simulate tree shaking: follow imports and re-exports, keep side-effectful modules and report what is removedHard50 min
  4. Build a webpack splitChunks-style algorithm: async chunks, parent availability, minChunks and minSizeHard60 min
  5. Investigate a bundle that grew from 300 KB to 900 KB and enforce budgets in CIHard45 min