Hello! Welcome to the final module of our course. We are now on the home stretch, focusing on getting your application ready for the world.
In our last lesson, we explored a crucial performance optimization: code splitting with React.lazy and <Suspense>. We saw how to break our application into smaller chunks and load them on demand, dramatically improving the initial load time. We also noted that Vite handles the creation of these chunks for us automatically.
Today, we'll zoom out and look at the entire production process. Our goal is to configure a build process with Vite for a production-optimized application. We will demystify what happens when you run npm run build, explore the key optimizations Vite performs, and learn how to customize this process through the vite.config.ts file to squeeze every last drop of performance out of your app.
1. The Two Modes: Development vs. Production
In modern web development, we work in two distinct environments, each with different priorities:
- Development (
npm run dev): The primary goal is developer experience (DX). We want fast startup times, instantaneous updates via Hot Module Replacement (HMR), and detailed source maps for easy debugging. File size and network performance are secondary. - Production (
npm run build): The primary goal is user experience (UX) and performance. We want the smallest possible bundle sizes, fast execution in the browser, and efficient caching. Build time is less critical than the final output's quality.
Visual Guide to the Modern Frontend Toolchain (Vite)
The video 'Visual Guide to the Modern Frontend Toolchain' provides a great high-level overview of these two distinct processes. Let's start there to set the stage.
Watch from 01:04 to 01:49. This clip introduces the dev and build commands and explains the different goals of a good developer experience versus a good production experience.
2. The Production Build Pipeline
When you run npm run build, Vite orchestrates a series of steps to transform your development source code into a highly optimized bundle ready for deployment. Given your background in software engineering, you can think of this as a compilation and optimization pipeline. Let's walk through the main stages.

Stage 1: Type Checking and Transpilation
Your browser understands JavaScript, but it doesn't understand TypeScript or JSX. The first step is to convert your .tsx files into plain JavaScript.
In a default Vite React+TS project, the build script in package.json is typically "build": "tsc -b && vite build".
- Type Checking (
tsc -b): The TypeScript compiler (tsc) runs first to check your entire project for type errors. This ensures type safety before the build proceeds. - Transpilation (
vite build): Vite then takes over. It uses an incredibly fast tool called esbuild to strip out the TypeScript syntax and transform JSX intoReact.createElementcalls (or the modern equivalent).
Stage 2: Bundling with Rollup
Next, Vite resolves all the import statements and bundles your modules into a coherent set of files. For this critical production step, Vite uses a bundler called Rollup.
While esbuild is also a bundler, Vite uses Rollup for production builds because it's more mature and has a vast plugin ecosystem, allowing for more advanced optimizations like sophisticated code splitting and tree shaking.
Stage 3: Optimization
This is where the magic happens to make your app small and fast. Rollup, configured by Vite, performs several key optimizations:
- Code Splitting: As we saw in the last lesson, Rollup automatically creates separate files (chunks) for your lazy-loaded components.
- Tree Shaking: Unused code is eliminated. If you import a library but only use one function from it, tree shaking ensures the unused parts of the library are not included in your final bundle.
- Minification: All unnecessary characters—whitespace, comments, newlines—are removed from the code. Variable names are shortened (e.g.,
productDetailComponentbecomesa). This makes the files much smaller. Vite uses esbuild for this by default because it is significantly faster than other minifiers like Terser. - Asset Hashing: The names of your output JS and CSS files will include a hash (e.g.,
index-BjGCt3-L.js). This hash changes whenever the file's content changes. This strategy, known as cache busting, allows you to tell browsers to cache your assets forever, as any update will result in a new file name, forcing a fresh download.
Visual Guide to the Modern Frontend Toolchain (Vite)
The 'Visual Guide' video provides a clear, step-by-step walkthrough of this entire pipeline. Watching this will solidify your understanding of how these tools work together.
Watch from 01:49 to 07:13, and then the summary from 08:12 to 08:43. Pay close attention to the distinction between the tools used: tsc for type checking, esbuild for transpilation, and Rollup for the final bundling and optimization.
3. Running and Previewing the Build
Let's put this theory into practice.
- Open your terminal in the project root.
- Run the command:
npm run build
You'll see output in your terminal that details the created files and their sizes.

After the build completes, you'll have a new dist directory. This folder contains your entire production-ready application.
To test this production version locally, Vite provides a handy command:npm run preview
This starts a simple static web server that serves your dist folder, allowing you to interact with your app exactly as a user would.
Vite Crash Course | Faster Alternative To CRA
The 'Vite Crash Course' video has a great practical demonstration of running the build and preview commands.
Watch from 14:44 to 15:28. This short clip will show you the npm run build and npm run preview commands in action and the resulting dist folder.
4. Customizing the Build in vite.config.ts
Vite's defaults are excellent, but for a production application, you'll often want to fine-tune the build process. This is done inside the build object in your vite.config.ts file.
Vite Docs: Building for Production & Guide to vite.config.js
To understand the available options, let's turn to the official Vite documentation and a helpful guide. We'll use these to explore some of the most common and useful build configurations.
First, quickly read the introduction in the Vite documentation, 'Building for Production'. Then, review the 'Browser Compatibility' and 'Customizing the Build' sections to get an idea of the core options. We'll refer to specific options from here in our examples.
Here are some of the most important properties you can configure within the build object:
outDir: Specifies the output directory. Defaults to'dist'. You might change this to'build'or another name depending on your deployment workflow.sourcemap: Determines if source maps are generated for the production build. A great option is'hidden', which generates source maps but doesn't link them in the JS files. This is perfect for feeding to error reporting services (like Sentry or Bugsnag) without exposing your source code to end-users.minify: Controls minification. Defaults to'esbuild'. You can set it to'terser'for potentially smaller (but slower to build) bundles, orfalseto disable it.rollupOptions: This is your gateway to advanced configuration. It gives you direct access to the underlying Rollup options.
Example: A Custom Chunking Strategy
In the last lesson, we let Vite handle code splitting automatically. With rollupOptions, we can define our own chunking strategy. A very common pattern is to group all code from node_modules into a single vendor chunk.
The Complete Guide to Mastering vite.config.js
The article 'The Complete Guide to Mastering vite.config.js' has an excellent, practical section on production build optimizations that shows how to implement a custom chunking strategy.
Read the section titled 'Production Build Optimization'. Focus on the 'Build Target and Compatibility' and 'Chunk Splitting Strategy' subsections. The code example for manualChunks is particularly relevant.
Here is how you would implement that vendor chunking strategy in your vite.config.ts:
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
output: {
manualChunks(id) {
// All modules from node_modules are placed in a 'vendor' chunk
if (id.includes('node_modules')) {
return 'vendor';
}
},
},
},
},
});
With this configuration, libraries like react, react-dom, and react-router-dom will be bundled together. Since vendor code changes less frequently than your application code, the browser can cache this large vendor.js file for longer, improving load times on subsequent visits.
5. Extending the Build with Plugins
One of Vite's greatest strengths is its plugin ecosystem, which can further enhance your build process. Here are two incredibly useful plugins for production:
rollup-plugin-visualizer: This plugin generates an interactive treemap that shows you exactly what's inside your bundles. It's an indispensable tool for identifying large dependencies and optimizing your bundle size.vite-plugin-compression: This plugin automatically creates compressed versions of your assets (e.g.,.gzusing Gzip or.brusing Brotli) during the build. Modern web servers can be configured to serve these pre-compressed files directly, saving CPU time and improving response speed.
Let's add the bundle visualizer.
First, install it as a dev dependency:npm install --save-dev rollup-plugin-visualizer
Then, update your vite.config.ts:
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer'; // Import the plugin
export default defineConfig({
plugins: [
react(),
visualizer({ open: true }), // Add the plugin, `open: true` will automatically open the report
],
build: {
// ... your other build options
},
});
Now, when you run npm run build, a file named stats.html will be generated in your project root, and it will open automatically in your browser, showing you a detailed map of your bundles.
Test your understanding!
Modify your vite.config.ts file to achieve the following:
- Change the output directory from
disttobuild_output. - Configure a chunking strategy that splits
reactandreact-dominto a separate chunk namedreact-vendor.
After making the changes, run npm run build. What new directory do you see? If you also have the visualizer plugin, how does the output report reflect your new chunking strategy?
Show answer
Your vite.config.ts should look something like this:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';
export default defineConfig({
plugins: [react(), visualizer({ open: true })],
build: {
outDir: 'build_output', // Changed output directory
rollupOptions: {
output: {
manualChunks(id) {
// Create a 'react-vendor' chunk for react and react-dom
if (id.includes('react-dom') || id.includes('react')) {
return 'react-vendor';
}
},
},
},
},
});
After running npm run build, you will see a new build_output folder in your project root instead of dist.
The stats.html report from the visualizer plugin will now show a distinct block labeled react-vendor.js, confirming that react and react-dom have been successfully isolated into their own chunk.
Conclusion
You now have a solid understanding of Vite's production build process. You've gone from simply running a command to understanding the entire pipeline and knowing how to customize it for optimal performance.
Key Takeaways:
- Vite uses different processes for development (
npm run dev) and production (npm run build) to optimize for different goals. - The production build pipeline involves type checking (
tsc), transpilation (esbuild), bundling (Rollup), and various optimizations (minification, tree shaking, asset hashing). - The
npm run buildcommand creates a production-ready application in thedistdirectory, which you can test locally withnpm run preview. - You can customize nearly every aspect of the build process within the
buildobject of yourvite.config.tsfile. - Plugins like
rollup-plugin-visualizerare powerful tools for analyzing and further optimizing your production bundle.
Preview of the Next Lesson:
We've created a highly optimized, production-ready application and placed it in our dist folder. The final step is to share it with the world. In our next lesson, we will learn how to deploy a production-ready React application to a static hosting platform like Vercel or Netlify, completing the development lifecycle.