Catalog My learning Blog
Frontend Developer Course: 100 Concepts in 100 Days · Phase 5 · Lesson 47 · 5 min read

How Vite Works: Bundlers and the Module Graph

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: A Vite Configuration for a React App with an API Proxy
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Explain what a bundler does with a module graph, why development and production builds are handled differently, how Vite serves native modules with on-demand transforms and pre-bundled dependencies in development, how hot module replacement works, and what a production build outputs.

Concept Explanation

Browsers can load ES modules natively, so why bundle at all? Because a real application's module graph can contain thousands of files. Loading each one as a separate request, discovering imports only after each file arrives, creates long waterfalls. Source code also needs transforming: TypeScript and JSX must be compiled, CSS modules and assets must be processed, and bare specifiers such as react must be resolved to files in node_modules. A bundler starts from entry points, follows every import to build the module graph, transforms each module with loaders or plugins, and writes a small number of optimized output files.

Older bundlers such as webpack bundled the whole application before the development server could serve anything, and rebundled on every change. As projects grew, startup took minutes and updates took seconds. Vite took a different approach in development. It serves your source files to the browser as native ES modules and transforms each file on request, so startup cost no longer grows with the size of the app; only the modules the current page imports are processed. Dependencies in node_modules are different: they change rarely and are often published as many small CommonJS files, so Vite pre-bundles them once with esbuild into a few ES module files and caches them.

Hot module replacement, HMR, is what makes editing feel instant. When you save a file, the dev server sends a message over a WebSocket, the browser fetches only the changed module and its HMR boundary, the nearest module that knows how to accept updates, and swaps it in without a full reload. Framework plugins such as the React plugin define those boundaries at the component level, so component state is preserved while you change markup or styles. When a change cannot be applied, for example in a module with side effects that is not accepted anywhere, Vite falls back to a full reload.

Production is a different problem: there you want the fewest, smallest requests and long-term caching. Vite's production build has used Rollup, with the newer Rust-based Rolldown designed to replace it, to bundle the graph into optimized chunks, minify code, split shared dependencies, inline small assets, and give every file a content hash in its name, such as index-4f9a2c.js, so it can be cached forever (Day 88). The build also writes a manifest describing which chunks each entry needs, which server frameworks use to emit preload links.

Configuration in Vite is mostly plugins and conventions. index.html is the entry, environment variables prefixed with VITE_ are exposed to client code and inlined at build time, so they must never contain secrets, and the dev server can proxy API requests to your backend to keep everything same-origin, as Day 10 recommended. Other modern tools, such as Rspack, Turbopack and esbuild directly, apply similar ideas, so understanding the module graph transfers between them.

StageDevelopment (Vite)Production build
Your source filesServed as native ES modules, transformed on requestBundled into chunks
DependenciesPre-bundled once with esbuild, cachedBundled and split into vendor chunks
TypeScript and JSXTranspiled per file, no type checkingTranspiled; type check separately
UpdatesHMR over a WebSocketNot applicable
File namesOriginal pathsContent-hashed for long caching
OptimizationsMinimal, for speedMinify, tree shake, split, compress
Environment variablesVITE_ prefix exposedInlined into the bundle, public

Worked Example: A Vite Configuration for a React App with an API Proxy

The configuration adds the React plugin, proxies API calls to the backend during development, splits a large vendor library into its own chunk and produces a manifest and hashed assets for production.

JavaScript
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig(({ mode }) => ({
  plugins: [react()],                               // JSX transform and component-level HMR
  server: {
    port: 5173,
    proxy: {
      '/api': { target: 'http://localhost:8080', changeOrigin: true },   // same-origin in dev
    },
  },
  build: {
    target: 'es2022',
    sourcemap: mode === 'production' ? 'hidden' : true,   // upload privately, do not publish
    manifest: true,
    rollupOptions: {
      output: {
        manualChunks: { charts: ['chart.js'] },     // a heavy library gets its own cacheable chunk
      },
    },
  },
}));
JavaScript
// src/env.ts: only VITE_ variables reach the client, and they are public
const apiBase = import.meta.env.VITE_API_BASE ?? '/api';
export const config = { apiBase, isDev: import.meta.env.DEV };

// HMR: a plain module can accept its own updates
if (import.meta.hot) {
  import.meta.hot.accept((mod) => console.log('format.ts updated', mod));
}

Run vite and open the Network panel: you see your source files requested individually as modules and dependencies served from a pre-bundled cache directory. Run vite build and the dist folder contains a few hashed JavaScript and CSS files, the assets and a manifest.

In development Vite serves source files as native modules, transforms each on request, pre-bundles dependencies once and swaps changed modules with HMR. The production build bundles, splits, minifies and hashes everything for caching.
In development Vite serves source files as native modules, transforms each on request, pre-bundles dependencies once and swaps changed modules with HMR. The production build bundles, splits, minifies and hashes everything for caching.

Real-World Examples

  • Vite was created by Evan You, the author of Vue, and released in 2020; within a few years it became the default build tool for Vue, SvelteKit, Astro, Remix and the React Router framework, and the React team recommended it as an option after deprecating Create React App in 2025.
  • The State of JavaScript survey has shown Vite with the highest satisfaction ratings among build tools for several consecutive years, and its native module development server is the main reason cited for fast startup on large projects.
Key TakeawaysA bundler builds a module graph starting from your entry points, transforms each module, and writes optimized output files for the browser. Vite serves source as native modules with on-demand transforms and pre-bundled dependencies in development, updates the page with component-level HMR, and bundles hashed, minified, split chunks for production. VITE_ variables are public, type checking runs separately, and a dev proxy keeps the API same-origin.

Review Questions & Answers

It does not bundle the application before serving it. It serves source files as native ES modules and transforms each only when the browser requests it, so startup work does not grow with the number of files, and dependencies are pre-bundled once and cached.

Packages in node_modules change rarely and are often made of many small files or CommonJS modules. Pre-bundling them with esbuild into a few ES modules reduces the number of browser requests and converts CommonJS into a format the browser can import.

The nearest module, walking up the importers of a changed file, that has declared it can accept updates. The changed code is swapped in at that boundary without a full page reload. Framework plugins make components boundaries so their state is preserved.

Its value is inlined into the JavaScript bundle at build time and shipped to every user, who can read it in the downloaded files. Secrets must stay on the server.