import { resolve } from 'path'; import { createReadStream, existsSync } from 'fs'; import { join } from 'path'; // 可选的前端构建配置:不改变现有直出模式 // - 输入:根目录 index.html 与 admin/index.html // - 输出:dist/ (与生产无关,默认不被服务端使用) // - 可选使用:npm run dev:fe / build:fe / preview:fe export default { root: '.', publicDir: 'public', server: { port: 5173, open: false, proxy: { '/api': { target: 'http://localhost:3456', changeOrigin: true }, '/mineru': { target: 'http://localhost:3456', changeOrigin: true }, '/doc2x': { target: 'http://localhost:3456', changeOrigin: true } } }, // 排除 PDF.js 的依赖优化,避免模块转换问题 optimizeDeps: { exclude: ['pdfjs-dist'] }, // 开发服务器中间件:服务 pdfjs 静态文件 plugins: [{ name: 'serve-pdfjs', configureServer(server) { server.middlewares.use((req, res, next) => { if (req.url && req.url.startsWith('/pdfjs/')) { const filePath = join(process.cwd(), req.url); if (existsSync(filePath)) { // 设置正确的 MIME 类型 const ext = filePath.split('.').pop()?.toLowerCase(); const mimeTypes = { 'html': 'text/html', 'js': 'application/javascript', 'css': 'text/css', 'json': 'application/json', 'png': 'image/png', 'jpg': 'image/jpeg', 'gif': 'image/gif', 'svg': 'image/svg+xml', 'woff': 'font/woff', 'woff2': 'font/woff2', 'ttf': 'font/ttf', 'pdf': 'application/pdf' }; const contentType = mimeTypes[ext] || 'application/octet-stream'; res.setHeader('Content-Type', contentType); createReadStream(filePath).pipe(res); return; } } next(); }); } }], build: { outDir: 'dist', emptyOutDir: true, rollupOptions: { input: { main: resolve(__dirname, 'index.html'), // admin 入口已被 .vercelignore 排除,不参与 Vercel 构建 // admin: resolve(__dirname, 'admin/index.html'), }, }, }, };