搭建VitePress静态资源站点

77 阅读2分钟

简单捣鼓

开发环境

新增仓库名 vitepress-test(化名)(证书添加个,不让空仓库)

拉取项目:git clone 仓库地址

生成package.json:npm init -y

安装依赖

pnpm add -D vitepress

用vitepress脚手架

pnpm vitepress init

回答几个简单的问题(add npm script to package.json要选择yes)

┌  Welcome to VitePress!
│
◇  Where should VitePress initialize the config?
│  ./docs
│
◇  Site title:
│  My Awesome Project
│
◇  Site description:
│  A VitePress Site
│
◆  Theme:
│  ● Default Theme (Out of the box, good-looking docs)
│  ○ Default Theme + Customization
│  ○ Custom Theme
└
  • 开发环境有缓存文件,添加.gitignore,不把这些代码提到仓库去
node_modules
/docs/.vitepress/cache

生产环境

如何部署?

  • 在项目的 .github/workflows 目录中创建一个名为 deploy.yml 的文件,其中包含这样的内容

  • 内容如下 (注意选择 pnpm 的选项 和 node的版本号)

# 构建 VitePress 站点并将其部署到 GitHub Pages 的示例工作流程
#
name: Deploy VitePress site to Pages

on:
  # 在针对 `main` 分支的推送上运行。如果你
  # 使用 `master` 分支作为默认分支,请将其更改为 `master`
  push:
    branches: [main]

  # 允许你从 Actions 选项卡手动运行此工作流程
  workflow_dispatch:

# 设置 GITHUB_TOKEN 的权限,以允许部署到 GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# 只允许同时进行一次部署,跳过正在运行和最新队列之间的运行队列
# 但是,不要取消正在进行的运行,因为我们希望允许这些生产部署完成
concurrency:
  group: pages
  cancel-in-progress: false

jobs:
  # 构建工作
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # 如果未启用 lastUpdated,则不需要
      - uses: pnpm/action-setup@v3 # 如果使用 pnpm,请取消此区域注释
        with:
          version: 9
      # - uses: oven-sh/setup-bun@v1 # 如果使用 Bun,请取消注释
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm # 或 pnpm / yarn
      - name: Setup Pages
        uses: actions/configure-pages@v4
      - name: Install dependencies
        run: pnpm install # 或 pnpm install / yarn install / bun install
      - name: Build with VitePress
        run: pnpm docs:build # 或 pnpm docs:build / yarn docs:build / bun run docs:build
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: docs/.vitepress/dist

  # 部署工作
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    needs: build
    runs-on: ubuntu-latest
    name: Deploy
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
  • 修改config.mjs 的配置, 注意换成自己的仓库名

base: process.env.NODE_ENV === 'production' ? '/注意啦注意啦,填仓库名/' : '/',

  • 在存储库设置中的“Pages”菜单项下,选择“Build and deployment > Source > GitHub Actions”。

  • 将更改推送到 main 分支并等待 GitHub Action 工作流完成。

(你应该看到站点部署到 https://<username>.github.io/[repository]/ 或 https://<custom-domain>/,这取决于你的设置。你的站点将在每次推送到 main 分支时自动部署。)

  • 从Git actions的url点击,得到失去样式的页面?强制再刷新页面