Skip to content

Commit 1866cd3

Browse files
committed
build(fix): 添加构建后处理脚本以支持 clean URLs
1 parent 7facef5 commit 1866cd3

4 files changed

Lines changed: 70 additions & 9 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
},
1212
"scripts": {
1313
"dev": "vitepress dev src",
14-
"build": "vitepress build src",
15-
"preview": "vitepress build src && vitepress preview src"
14+
"build": "vitepress build src && node scripts/post-build.js",
15+
"preview": "vitepress build src && node scripts/post-build.js && vitepress preview src"
1616
},
1717
"dependencies": {
1818
"shiki": "^3.18.0"

scripts/post-build.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { copyFileSync, existsSync, mkdirSync, readdirSync, statSync } from "fs";
2+
import { join, dirname, basename, extname } from "path";
3+
import { fileURLToPath } from "url";
4+
5+
const __dirname = dirname(fileURLToPath(import.meta.url));
6+
const distDir = join(__dirname, "..", "src", ".vitepress", "dist");
7+
8+
// 递归处理所有 HTML 文件,为 cleanUrls 创建目录结构
9+
function createCleanUrlStructure(dir) {
10+
const files = readdirSync(dir);
11+
12+
for (const file of files) {
13+
const fullPath = join(dir, file);
14+
const stat = statSync(fullPath);
15+
16+
if (stat.isDirectory()) {
17+
createCleanUrlStructure(fullPath);
18+
} else if (
19+
file.endsWith(".html") &&
20+
file !== "index.html" &&
21+
file !== "404.html" &&
22+
file !== "200.html"
23+
) {
24+
// 例如: docs/1.1-QuickStarted.html -> docs/1.1-QuickStarted/index.html
25+
const nameWithoutExt = basename(file, ".html");
26+
const targetDir = join(dir, nameWithoutExt);
27+
28+
if (!existsSync(targetDir)) {
29+
mkdirSync(targetDir, { recursive: true });
30+
}
31+
32+
const targetFile = join(targetDir, "index.html");
33+
copyFileSync(fullPath, targetFile);
34+
console.log(`✓ Created ${targetFile}`);
35+
}
36+
}
37+
}
38+
39+
// 创建 SPA fallback
40+
const indexHtml = join(distDir, "index.html");
41+
const fallback200 = join(distDir, "200.html");
42+
const fallback404 = join(distDir, "404.html");
43+
44+
if (existsSync(indexHtml)) {
45+
copyFileSync(indexHtml, fallback200);
46+
console.log("✓ Created 200.html for SPA fallback");
47+
}
48+
49+
if (existsSync(fallback404)) {
50+
// 404.html 指向 index.html 的内容以支持 SPA 路由
51+
copyFileSync(indexHtml, fallback404);
52+
console.log("✓ Updated 404.html for SPA fallback");
53+
}
54+
55+
// 为 cleanUrls 创建目录结构
56+
console.log("\nCreating clean URL directory structure...");
57+
createCleanUrlStructure(distDir);
58+
console.log("\n✓ Build post-processing complete!");

src/.vitepress/config.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ export default withMermaid({
7878
const enPath = isEn
7979
? routePath
8080
: routePath.startsWith("/en/")
81-
? routePath
82-
: "/en" + routePath;
81+
? routePath
82+
: "/en" + routePath;
8383
const zhUrl = hostname + baseNoSlash + zhPath;
8484
const enUrl = hostname + baseNoSlash + enPath;
8585

src/.vitepress/sidebars.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ export function genSidebar(args: VitePressSidebarOptions = {}) {
88
});
99
}
1010

11-
function processRewritesRecursion(routes, targetKV) {
12-
const rewrites = {};
11+
function processRewritesRecursion(
12+
routes: Array<any>,
13+
targetKV: Record<string, string>,
14+
) {
15+
const rewrites: Record<string, string> = {};
1316
routes.forEach((route) => {
1417
let link = route.link as string;
1518
let text = route.text as string;
@@ -31,7 +34,7 @@ function processRewritesRecursion(routes, targetKV) {
3134
return rewrites;
3235
}
3336

34-
function processRoutes(routes) {
37+
function processRoutes(routes: Array<any>) {
3538
routes.forEach((route) => {
3639
if (route["items"]) {
3740
processRoutes(route["items"]);
@@ -47,7 +50,7 @@ function processRoutes(routes) {
4750
}
4851

4952
function getFileNameKV(routes: Array<any>) {
50-
const kv = {};
53+
const kv: Record<string, string> = {};
5154
routes.forEach((route) => {
5255
if (route.items) {
5356
Object.assign(kv, getFileNameKV(route.items));
@@ -66,7 +69,7 @@ function getLocaleSidebars() {
6669

6770
// 生成国际化路径重写
6871
const rewriteList = [zn_routes];
69-
const rewrites = {};
72+
const rewrites: Record<string, string> = {};
7073
const kv = getFileNameKV(en_routes);
7174
rewriteList.forEach((item) => {
7275
const rw = processRewritesRecursion(item, kv);

0 commit comments

Comments
 (0)