快速上手

注意

VuePress v2 目前仍处于 RC (Release Candidate) 阶段。你已经可以用它来构建你的站点,但是它的配置和 API 还不够稳定,有可能会发生一些微小的 Breaking Changes 。因此,在每次更新 RC 版本之后,请一定要仔细阅读 更新日志在新窗口打开

在线试一试

你可以通过 StackBlitz在新窗口打开 在你的浏览器里直接使用 VuePress 。

安装

依赖环境

提示

创建项目

通过命令行创建

你可以通过 create-vuepress在新窗口打开 直接创建项目模板。

pnpm create vuepress vuepress-starter
yarn create vuepress vuepress-starter
npm init vuepress vuepress-starter

手动创建

这一章节会帮助你从头搭建一个简单的 VuePress 文档网站。

  • 创建并进入一个新目录
mkdir vuepress-starter
cd vuepress-starter
  • 初始化项目
git init
pnpm init
git init
yarn init
git init
npm init
  • 安装 VuePress
# 安装 vuepress 和 vue
pnpm add -D vuepress@next vue
# 安装打包工具和主题
pnpm add -D @vuepress/bundler-vite@next @vuepress/theme-default@next
# 安装 vuepress
yarn add -D vuepress@next
# 安装打包工具和主题
yarn add -D @vuepress/bundler-vite@next @vuepress/theme-default@next
# 安装 vuepress
npm install -D vuepress@next
# 安装打包工具和主题
npm install -D @vuepress/bundler-vite@next @vuepress/theme-default@next
  • 创建 docs 目录和 docs/.vuepress 目录
mkdir docs
mkdir docs/.vuepress
  • 创建 VuePress 配置文件 docs/.vuepress/config.js
import { viteBundler } from '@vuepress/bundler-vite'
import { defaultTheme } from '@vuepress/theme-default'
import { defineUserConfig } from 'vuepress'

export default defineUserConfig({
  bundler: viteBundler(),
  theme: defaultTheme(),
})
  • 创建你的第一篇文档
echo '# Hello VuePress' > docs/README.md

目录结构

创建完成后,你项目的目录结构应该是这样的:

├─ docs
│  ├─ .vuepress
│  │  └─ config.js
│  └─ README.md
└─ package.json

docs 目录是你放置 Markdown 文件的地方,它同时也会作为 VuePress 的源文件目录。

docs/.vuepress 目录,即源文件目录下的 .vuepress 目录,是放置所有和 VuePress 相关的文件的地方。当前这里只有一个配置文件。默认还会在该目录下生成临时文件、缓存文件和构建输出文件。建议你把它们添加到 .gitignore 文件中。

示例 `.gitignore` 文件
# VuePress 默认临时文件目录
.vuepress/.temp
# VuePress 默认缓存目录
.vuepress/.cache
# VuePress 默认构建生成的静态文件目录
.vuepress/dist

开始使用 VuePress

启动开发服务器

你可以在 package.json 中添加一些 scripts在新窗口打开

{
  "scripts": {
    "docs:dev": "vuepress dev docs",
    "docs:build": "vuepress build docs"
  }
}

运行 docs:dev 脚本可以启动开发服务器:

pnpm docs:dev
yarn docs:dev
npm run docs:dev

VuePress 会在 http://localhost:8080在新窗口打开 启动一个热重载的开发服务器。当你修改你的 Markdown 文件时,浏览器中的内容也会自动更新。

构建你的网站

运行 docs:build 脚本可以构建你的网站:

pnpm docs:build
yarn docs:build
npm run docs:build

docs/.vuepress/dist 目录中可以找到构建生成的静态文件。你可以查看 部署 来了解如何部署你的网站。

进一步了解 VuePress

现在,你应该已经有了一个简单可用的 VuePress 网站。但你可能仍需要阅读后续的指南来更加了解 VuePress 。

下一步,前往 配置 了解更多配置文件相关的内容。