Building and Deploying Vite Projects

Learn Vite's production build command, the output directory structure, and common static deployment options.

🎯 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

vite.config.js
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.

vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
    build: {
        sourcemap: true,
    },
});
Source map files are used for debugging and investigating production issues and are generally not presented directly to ordinary users.

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.

vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
    build: {
        rollupOptions: {
            output: {
                manualChunks: undefined,
            },
        },
    },
});
For most applications, keep the default splitting behavior to improve initial page loading. Consider disabling it only when you are sure the project is small and contains few files.

🛠 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.

The 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 dist directory as the site root.
  • Vercel / Netlify: upload the dist directory or connect a Git repository for automatic deployments.
  • GitHub Pages: push dist to the gh-pages branch.
When deploying to a subpath, set 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:

  1. Set outDir to output in vite.config.js.
  2. Run npm run build and inspect the generated output directory.
  3. Run npm run preview and confirm that the page displays correctly.
vite.config.js
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.