Building and Deploying Vite Projects
🎯 Introduction
By the end of this article, you will be able to build a Vite project into deployable static assets, understand the files in dist, and deploy the site to common static hosting platforms. This is a key step in taking a project from development to production.
🧱 Production Builds
Run:
npm run build
Vite uses Rollup to bundle, minify, and split your code, generating a dist directory.
dist/
├── assets/
│ ├── index-xxx.js
│ ├── index-xxx.css
│ └── vendor-xxx.js
├── index.html
└── favicon.ico
xxx is a content hash used for long-term caching. A filename changes only when its contents change.✨ Common Build Options
Set the Output Directory
import { defineConfig } from 'vite';
export default defineConfig({
build: {
outDir: 'build',
},
});
Generate Source Maps
A source map is a mapping between original source code and minified code.
Production builds minify code into a single line and shorten variable names. When a production error occurs, the browser console points to the minified code, making the original location hard to find. With sourcemap: true, the browser can map the error back to an original location, such as line 20 of src/App.vue, making debugging easier.
import { defineConfig } from 'vite';
export default defineConfig({
build: {
sourcemap: true,
},
});
Disable Code Splitting
By default, Vite splits code into multiple files. For example, third-party libraries such as vue and element-plus can go into vendor-xxx.js, while application code goes into index-xxx.js. Visitors need to load only the files required for the home page; files for other pages load when needed, improving initial page load times.
Disabling code splitting combines all code into one file. It is like reading a book as separate chapters versus one bound volume. Small projects or component libraries may consider disabling it, but this is not recommended for typical applications.
import { defineConfig } from 'vite';
export default defineConfig({
build: {
rollupOptions: {
output: {
manualChunks: undefined,
},
},
},
});
🛠 Preview the Production Build Locally
After building, use preview to test locally:
npm run preview
By default, the preview is available at http://localhost:4173/, providing an experience close to the deployed site.
dev development server and the preview production preview serve different purposes. Always check the build with preview before deployment.🚀 Deploy to Static Hosting
Vite outputs static files that you can deploy to any static server:
- Nginx: use the
distdirectory as the site root. - Vercel / Netlify: upload the
distdirectory or connect a Git repository for automatic deployments. - GitHub Pages: push
distto thegh-pagesbranch.
base to the corresponding path; otherwise, asset requests will return 404 errors.❓ Questions and Answers
Q1: What if the development server works but preview shows a blank page?
Common causes include an incorrect base path or history-mode routing without a server fallback. Check the browser console for 404 errors.
Q2: Can I upload the dist directory directly?
Yes. Its contents are the final deployment files. Upload them directly to a static server.
Q3: How do I clear the build cache?
Delete dist and node_modules/.vite, then run npm run build again.
🧪 Exercise
Complete a build and preview it locally:
- Set
outDirtooutputinvite.config.js. - Run
npm run buildand inspect the generatedoutputdirectory. - Run
npm run previewand confirm that the page displays correctly.
import { defineConfig } from 'vite';
export default defineConfig({
build: {
// Change outDir here
},
});
🎉 You have completed the Vite course! You can now create projects, configure aliases, use plugins, manage environment variables, optimize dependency pre-bundling, and build and deploy your application.
Dependency Pre-bundling
Understand Vite's dependency pre-bundling and learn to control it with include, exclude, and force for a better development experience.
Introduction to Nuxt.js and Installation
Learn what Nuxt.js is, why it suits full-stack Vue applications, and how to create and run your first project.
