Skip to content

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:

js
// .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:

md
# 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:

js
// .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:

  1. Starts a Vite dev server
  2. Processes Markdown files on demand
  3. Applies hot module replacement for instant updates
  4. Serves the site with minimal processing for speed
bash
# Start development server
vitepress dev [root] [--port] [--open] [--host]

Production Build

When running vitepress build, VitePress:

  1. Pre-renders all pages to static HTML
  2. Generates optimized client-side assets
  3. Creates a production-ready site in the .vitepress/dist directory
bash
# 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:

bash
# 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:

js
// .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:

js
// .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:

js
// .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:

js
// .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:

js
// 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

  1. Optimize Assets: Use appropriate image formats and sizes.

  2. Minimize JavaScript: Keep client-side JavaScript to a minimum.

  3. Use Code Splitting: Break large components into smaller chunks.

  4. Cache Dependencies: Use lockfiles and CI caching for faster builds.

  5. 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:

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


This document will be continuously updated. If you have any questions, please provide feedback through GitHub Issues.

VitePress Development Guide