Skip to content

Commit d70d346

Browse files
committed
refactor(DocServer): 移除mime依赖并使用自定义MIME类型映射
- 从package.json和package-lock.json中移除mime依赖包 - 在DocServer.ts中实现自定义MIME类型映射表 - 替换原有的mime.getType调用为自定义getMimeType函数 - 减少项目依赖,提高构建效率
1 parent dbe729d commit d70d346

3 files changed

Lines changed: 28 additions & 20 deletions

File tree

package-lock.json

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"eruda": "3.4.3",
4242
"front-matter": "4.0.2",
4343
"jsdom": "27.4.0",
44-
"mime": "4.1.0",
4544
"nodejieba": "3.5.2",
4645
"rollup-plugin-visualizer": "7.0.0",
4746
"simple-git": "3.32.2",

src/plugins/DocServer.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,33 @@
22
import {ViteDevServer} from 'vite'
33
import fs from 'fs'
44
import path from 'path'
5-
import mime from 'mime'
5+
6+
const MIME_TYPES: Record<string, string> = {
7+
'.html': 'text/html',
8+
'.css': 'text/css',
9+
'.js': 'application/javascript',
10+
'.json': 'application/json',
11+
'.png': 'image/png',
12+
'.jpg': 'image/jpeg',
13+
'.jpeg': 'image/jpeg',
14+
'.gif': 'image/gif',
15+
'.svg': 'image/svg+xml',
16+
'.ico': 'image/x-icon',
17+
'.webp': 'image/webp',
18+
'.woff': 'font/woff',
19+
'.woff2': 'font/woff2',
20+
'.ttf': 'font/ttf',
21+
'.pdf': 'application/pdf',
22+
'.md': 'text/markdown',
23+
'.txt': 'text/plain',
24+
'.xml': 'application/xml',
25+
'.mp4': 'video/mp4',
26+
'.mp3': 'audio/mpeg',
27+
}
28+
29+
function getMimeType(ext: string): string {
30+
return MIME_TYPES[ext.toLowerCase()] ?? 'application/octet-stream'
31+
}
632
import GitService from '../build/GitService'
733
import DocService from '../build/DocService'
834
import ApiMappings from './ApiMappings'
@@ -24,7 +50,7 @@ export default function DocServer(){
2450
next()
2551
return
2652
}
27-
res.writeHead(200, { 'Content-Type': `${mime.getType(path.extname(uri))};charset=utf8` });
53+
res.writeHead(200, { 'Content-Type': `${getMimeType(path.extname(uri))};charset=utf8` });
2854
res.write(fs.readFileSync(fileUri))
2955
res.end()
3056
}else{

0 commit comments

Comments
 (0)