Files
2026-09-21 15:32:12 +08:00

145 lines
3.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { resolve } from "node:path";
import { defineConfig } from "vitepress";
import {
componentPreview,
containerPreview
} from "@vitepress-demo-preview/plugin";
import AutoImport from "unplugin-auto-import/vite";
import { groupByCategory, toDocLink } from "./components";
import { groupUtilsByCategory, toUtilLink } from "./utils";
import { docgenPlugin } from "./plugins/docgen";
import { tsdocgenPlugin } from "./plugins/tsdocgen";
/** `__dirname` 由 VitePress 打包配置时注入,指向 `docs/.vitepress`。 */
const docsDir = resolve(__dirname, "..");
/** 仓库根目录,组件源码位于它的 `src` 下。 */
const repoRoot = resolve(docsDir, "..");
/**
* 源码引用别名。
*
* demo 组件直接引用 `src` 源码而非构建产物,改组件即可热更新文档。
* 源码自身的传递依赖(prismjs、@tiptap/* 等)会从 timi-web 自己的
* node_modules 解析,docs 无需重复声明。
*
* `timi-ts` 例外:它编译为 CommonJS,桶文件用 `__exportStar(require(...))` 转发,
* Rollup 在生产构建里无法静态识别其具名导出(开发期走 esbuild 预打包所以正常)。
* 该包是本地 link 且带源码,直接指向源码即可绕开,同时获得热更新。
*/
const timiTsRoot = resolve(repoRoot, "../timi-ts");
const alias = {
"~": resolve(repoRoot, "src"),
"timi-web": resolve(repoRoot, "src/index.ts"),
"timi-web/editor": resolve(repoRoot, "src/editor/index.ts"),
"timi-ts": resolve(timiTsRoot, "src/index.ts")
};
/** 组件文档侧边栏,按 components.ts 的分类与顺序生成。 */
const componentSidebar = groupByCategory().map(group => ({
text: group.category,
collapsed: false,
items: group.items.map(item => ({
text: item.title,
link: toDocLink(item)
}))
}));
/** 工具文档侧边栏,按 utils.ts 的分类与顺序生成。 */
const utilSidebar = groupUtilsByCategory().map(group => ({
text: group.category,
collapsed: false,
items: group.items.map(item => ({
text: item.title,
link: toUtilLink(item)
}))
}));
export default defineConfig({
title: "timi-web",
description: "Vue 3 通用组件与编辑器能力的演示与 API 文档",
lang: "zh-CN",
cleanUrls: true,
themeConfig: {
nav: [
{ text: "指南", link: "/guide/" },
{ text: "组件", link: "/components/" },
{ text: "工具", link: "/utils/" }
],
sidebar: [
{
text: "指南",
items: [
{ text: "快速开始", link: "/guide/" },
{ text: "演示与文档约定", link: "/guide/demo" }
]
},
...componentSidebar,
{
text: "工具",
collapsed: false,
items: [
{ text: "工具总览", link: "/utils/" }
]
},
...utilSidebar
],
outline: {
level: [2, 3],
label: "本页目录"
},
search: {
provider: "local"
},
docFooter: {
prev: "上一篇",
next: "下一篇"
},
darkModeSwitchLabel: "主题",
sidebarMenuLabel: "目录",
returnToTopLabel: "回到顶部"
},
markdown: {
theme: {
light: "vitesse-light",
dark: "vitesse-dark"
},
lineNumbers: true,
config(md) {
// 组件大多依赖浏览器 API(Prism、ResizeObserver、requestAnimationFrame 等),
// 统一开启 clientOnly 交给浏览器渲染,避免 SSR 阶段报错。
md.use(componentPreview, { clientOnly: true, alias });
md.use(containerPreview, { clientOnly: true, alias });
}
},
vite: {
resolve: {
alias
},
plugins: [
docgenPlugin(repoRoot),
// 工具模块是 TS 文件,vue-docgen-api 无法解析,走 TypeScript AST 提取。
tsdocgenPlugin(repoRoot),
// src 源码依赖自动导入(内部没有任何一处显式 import vue),
// 这里仅为其提供支持;docs 自身的代码一律显式导入。
AutoImport({
imports: ["vue"],
dts: false
})
],
server: {
port: 3003,
host: true,
fs: {
// demo 需要读取 docs 之外的 src 与 timi-ts 源码。
allow: [repoRoot, timiTsRoot]
}
}
}
});