Skip to main content

build()

Build your VitePress site programmatically. The build() function creates a production-ready static site.

Usage

Function Signature

Parameters

string
The root directory of your VitePress project. Defaults to process.cwd().This should be the directory containing the .vitepress folder.
BuildOptions
Configuration options for the build process.
string
Base public path when served in production. Overrides the base path defined in config.Must start and end with a slash (e.g., /my-site/).
string
Enable MPA (Multi-Page Application) mode. When enabled, VitePress will generate a zero-JS static site.This is experimental and disables client-side routing.
string
Output directory for build files. Overrides the outDir specified in config.Defaults to .vitepress/dist.
(siteConfig: SiteConfig) => Awaitable<void>
Hook called after the site config is resolved but before the build starts.Use this to inspect or modify the resolved configuration.

Examples

Basic Build

Build with Custom Root

Build with Custom Options

Build with Config Hook

Build MPA Site

Build Process

The build function performs the following steps:
  1. Config Resolution: Resolves the site configuration from .vitepress/config.js
  2. Bundling: Creates optimized client and server bundles using Vite
  3. Rendering: Pre-renders all pages to static HTML
  4. Asset Generation: Generates CSS, fonts, and other static assets
  5. Sitemap: Generates sitemap.xml if configured
  6. Cleanup: Removes temporary files

Build Output

The build outputs the following files to .vitepress/dist (or your configured outDir):
  • index.html - Homepage
  • *.html - Individual page files
  • assets/ - JavaScript, CSS, fonts, and images
  • hashmap.json - Page hash map for cache invalidation
  • vp-icons.css - Social media icons
  • sitemap.xml - Sitemap (if configured)

Environment Variables

string
Automatically set to 'production' during build.
string
When set, only bundles the app without rendering pages. Useful for debugging build issues.
string
When set, preserves the .vitepress/.temp directory after build for inspection.

Build Hooks

VitePress provides several hooks during the build process:

buildEnd

Called after the build completes:

postRender

Called after SSR rendering:

transformHead

Transform the <head> before writing HTML:

Error Handling