Sub topic 8
What Vite Is and How It Works
Prerequisites
This chapter assumes you know JavaScript fundamentals, including ES modules (import and export). It also assumes you have a basic idea of what a React component and JSX are. You do not need to have installed Vite yet. The next chapters cover setup.
What Vite is
Vite (French for "quick", pronounced "veet") is a build tool and development server for web projects. It does two separate jobs:
- In development, it serves your source files to the browser on demand and updates the page quickly when you edit a file.
- For production, it bundles your code into a small set of optimized files.
Vite is not part of React. React is a library for describing user interfaces. Vite is the tool that prepares your React code so a browser can run it. You can use Vite with Vue, Svelte, or plain JavaScript as well.
The earlier chapter on build tools explained why a React project needs one. JSX is not valid JavaScript, so something must transform it before a browser can read it. Vite handles that through a plugin, and it also resolves package imports, processes CSS, and handles static assets.
The problem Vite was built to solve
Older tools such as webpack follow a "bundle first" approach. Before the dev server can show anything, the tool reads your whole application, follows every import, and builds one or more bundles. In a large project, starting the server can take many seconds or even minutes. Each edit may also trigger a partial rebuild, and that cost grows as the project grows.
Vite takes a different approach. Modern browsers support ES modules natively. A browser can read import statements itself and request each file it needs. Vite uses that ability during development, so it does not need to bundle your application before it starts.
How the dev server works
When you run npm run dev, Vite starts a server almost immediately. It does little work up front. When the browser asks for a file, Vite transforms that one file and sends it back.
The flow looks like this:
Two details matter here.
- Only requested files are processed. If a route or component is never loaded, Vite never transforms it. Startup time stays about the same as the project grows.
- Your code is transformed one file at a time. The browser's own module loader handles the import graph.
Dependency pre-bundling
Your own source files are served as separate modules, but packages in node_modules are handled differently. Vite pre-bundles them once, using a fast bundler written in Go (esbuild in older versions; newer versions use a Rust-based bundler). It does this for two reasons:
- Some packages are published as CommonJS or UMD, which browsers cannot load as ES modules. Pre-bundling converts them.
- A package such as
lodash-esmay consist of hundreds of small files. Loading each as a separate request would be slow. Pre-bundling merges them into one module.
Vite stores the result in node_modules/.vite. It reuses that cache until your lockfile, your package.json dependencies, or your Vite config change.
The exact bundler Vite uses internally depends on its version. The behavior you see (dependencies bundled once, source served on demand) stays the same.
Hot module replacement
Hot module replacement (HMR) means that after you save a file, the running page swaps in the changed module without a full reload. Vite opens a WebSocket connection between the dev server and the browser. When a file changes, the server tells the browser which module changed. The browser then requests the new version of only that module.
For React, the official plugin adds Fast Refresh. Fast Refresh replaces the changed component and, where it can, keeps the component state. If you edit the text inside a counter component, the page updates and the count often stays the same. Some edits force a state reset or a full reload, for example changing a file that exports more than just components.
How the production build works
The unbundled approach is good for development but not for production. A real app can have hundreds of modules, and requesting each one separately causes many network round trips. So npm run build switches to a bundling step.
The build:
- follows imports from
index.htmland bundles the code, - removes code that is never used (tree shaking),
- minifies the output,
- splits code into chunks, and
- adds a content hash to file names, such as
index-a1b2c3d4.js, so browsers can cache files safely.
The result goes into the dist folder. Development and production use different pipelines, so a rare bug may appear in only one of them. That is why it is worth running npm run preview before you deploy.
| Aspect | Development (`npm run dev`) | Production (`npm run build`) |
|---|---|---|
| Module loading | Native ES modules, one file per request | Bundled chunks |
| Startup cost | Low, work done on demand | Full build step |
| Updates | HMR over WebSocket | None, you rebuild |
| Optimization | None for your code | Minified, tree shaken, hashed names |
| Output | In memory, served by the dev server | Files in `dist` |
Plugins and configuration
Vite has a plugin system. A plugin hooks into the steps of transforming and bundling files. The React plugin @vitejs/plugin-react does the following:
- transforms JSX into JavaScript function calls,
- enables Fast Refresh in development.
Without a plugin, Vite does not know how to process JSX in a .jsx file. Vite reads its settings from vite.config.js in the project root. A later chapter covers that file in detail.
Common errors and gotchas
- Imports without a file extension for your own files. Vite resolves some extensions, but use
.jsxin imports to match the starter template and avoid confusion. - JSX in a
.jsfile. By default, Vite expects JSX syntax in.jsxfiles. Putting JSX in a.jsfile can produce a parse error. Rename the file to.jsx. - Opening
index.htmldirectly from disk. Browsers block module loading fromfile://URLs in many cases, and the source is not transformed anyway. Always use the dev server orpreview. - Stale dependency cache. If a dependency change does not seem to take effect, restart with
npx vite --forceto rebuild the pre-bundled cache. - Node version. Recent Vite versions require a current Node.js release. If
npm run devfails with a syntax or engine error, check your Node version.
A small example
You can see on-demand transformation yourself. With a Vite dev server running at the default address, request a source file directly:
curl http://localhost:5173/src/main.jsxThe response is JavaScript, not JSX. The JSX tags have been replaced with function calls, and the import paths for packages have been rewritten to point at the pre-bundled copies. This shows that Vite does the transformation per request.
Exercise
A teammate says, "Vite is slow to start because it must bundle the whole app first." Explain why that is not how Vite's dev server works, and name the one thing Vite does bundle ahead of time.
Show answer
In development, Vite serves your source files as native ES modules and transforms each file only when the browser requests it, so it does not build the whole app before starting. The one thing it bundles ahead of time is the dependencies in nodemodules (pre-bundling), which converts CommonJS/UMD packages to ES modules and merges packages with many small files into one. The result is cached in nodemodules/.vite.
Exercise
Your app works with npm run dev but you want to check that the production bundle behaves the same. Which two commands do you run, and why can the results differ?
Show answer
Run npm run build and then npm run preview. The two modes use different pipelines: development serves unbundled modules with HMR, while the build bundles, tree shakes, and minifies the code. Because of that, a problem can appear in only one mode, so you test the production output before deploying.