Build Tools
Overview
VitePress leverages modern build tools to create fast, optimized documentation sites. This guide explores the build tools that power VitePress and how you can configure them for your specific needs.
Core Build Tools
Vite
VitePress is built on top of Vite, a next-generation frontend build tool that provides:
- Lightning-fast cold server start
- Instant hot module replacement (HMR)
- True on-demand compilation
- Optimized builds with rollup.js
You can customize Vite's behavior in your VitePress configuration:
// .vitepress/config.js
export default {
vite: {
// Your Vite configuration options
server: {
port: 3000,
strictPort: true
},
build: {
cssCodeSplit: false
},
// Add Vite plugins
plugins: [
// Your plugins here
]
}
}
Vue
VitePress uses Vue 3 as its frontend framework. Vue components can be used directly in your Markdown files:
# My Documentation Page
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
## Interactive Example
Current count: {{ count }}
<button @click="count++">Increment</button>
Markdown Processing
VitePress uses markdown-it for Markdown processing, enhanced with custom plugins:
- Table of contents generation
- Frontmatter parsing
- Code highlighting with Shiki or Prism
- Custom containers
You can customize the Markdown processing:
// .vitepress/config.js
export default {
markdown: {
// Configure markdown-it options
anchor: { permalink: false },
toc: { includeLevel: [1, 2, 3] },
config: (md) => {
// Use markdown-it plugins
md.use(require('markdown-it-custom-plugin'))
}
}
}
Build Process
Development Build
When running vitepress dev
, VitePress:
- Starts a Vite dev server
- Processes Markdown files on demand
- Applies hot module replacement for instant updates
- Serves the site with minimal processing for speed
# Start development server
vitepress dev [root] [--port] [--open] [--host]
Production Build
When running vitepress build
, VitePress:
- Pre-renders all pages to static HTML
- Generates optimized client-side assets
- Creates a production-ready site in the
.vitepress/dist
directory
# Build for production
vitepress build [root]
The production build process includes:
- Code splitting
- CSS extraction
- Asset optimization
- SSR pre-rendering
Preview Build
After building, you can preview the production build locally:
# Preview production build
vitepress preview [root] [--port] [--host]
Extending the Build Process
Custom Build Hooks
You can hook into the build process using Vite plugins:
// .vitepress/config.js
export default {
vite: {
plugins: [
{
name: 'my-custom-plugin',
buildStart() {
console.log('Build starting...')
},
generateBundle() {
console.log('Bundle generated...')
}
}
]
}
}
Build-time Data Fetching
You can fetch data during the build process:
// .vitepress/config.js
import fetch from 'node-fetch'
export default async () => {
const data = await fetch('https://api.example.com/data').then(r => r.json())
return {
title: data.title,
description: data.description,
themeConfig: {
// Use fetched data in your config
sidebar: generateSidebar(data.items)
}
}
}
function generateSidebar(items) {
// Transform API data into sidebar structure
return items.map(item => ({
text: item.title,
link: `/guides/${item.slug}`
}))
}
Integration with Other Build Tools
Integrating with TypeScript
VitePress works seamlessly with TypeScript:
// .vitepress/config.ts
import { defineConfig } from 'vitepress'
export default defineConfig({
// Your TypeScript-powered config
title: 'My Docs',
themeConfig: {
nav: [
{ text: 'Home', link: '/' }
]
}
})
Using with PostCSS
Configure PostCSS for your VitePress site:
// .vitepress/config.js
export default {
vite: {
css: {
postcss: {
plugins: [
require('autoprefixer'),
require('postcss-nested')
]
}
}
}
}
Integrating with Testing Tools
Set up testing for your VitePress components:
// vitest.config.js
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
test: {
environment: 'jsdom',
include: ['.vitepress/theme/**/*.test.js']
}
})
Best Practices
Optimize Assets: Use appropriate image formats and sizes.
Minimize JavaScript: Keep client-side JavaScript to a minimum.
Use Code Splitting: Break large components into smaller chunks.
Cache Dependencies: Use lockfiles and CI caching for faster builds.
Monitor Build Performance: Watch for slow plugins or large dependencies.
Frequently Asked Questions
How can I debug the build process?
Use the --debug
flag to see detailed build information:
vitepress build --debug
Can I use other frontend frameworks with VitePress?
VitePress is built on Vue, but you can use components from other frameworks through web components or iframes. However, for deep integration with another framework, you might want to consider other documentation tools.
How do I deploy my VitePress site?
After building your site with vitepress build
, you can deploy the .vitepress/dist
directory to any static hosting service like Netlify, Vercel, GitHub Pages, or AWS S3.
Related Resources
This document will be continuously updated. If you have any questions, please provide feedback through GitHub Issues.