Files
zMainPage/vite.config.js
zikai 39f3056099 feat: 环境感知配置(dev 经 vite proxy 指向本地 zTools2,prod 指向 f.zikai.wang)
修复 PDF 页签在开发环境返回 {"detail":"Not Found"}:
此前 baseUrl 始终指向线上 f.zikai.wang,开发环境跨域访问线上
既无法命中本地 /pdf 路由又会暴露生产。

- app.config.js: BASE_URL 按 import.meta.env.PROD 切换
  dev -> ''(同源,经 vite proxy 转发到本地 zTools2)
  prod -> https://f.zikai.wang(同源)
  VITE_API_BASE 可显式覆盖
- vite.config.js: dev server 新增 /api /pdf /health /static /upload
  /files /wb /ws 代理到 127.0.0.1:6867,使 iframe 与 fetch 同源命中本地服务

测试环境指向测试环境,生产环境指向生产环境。
2026-07-27 13:59:50 +08:00

103 lines
3.0 KiB
JavaScript
Raw 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 { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { minify } from 'html-minifier-terser'
// 构建到项目本地 dist/(每次清空,避免旧产物残留),
// 再由 `npm run deploy` 用 rsync 同步到 Apache 站点根 /root/html(软链到 /var/www/html)。
// 文件名去掉内容 hash保持稳定JS 由 esbuild 压缩HTML 由下方插件压缩。
// 用 mainPage/ 命名空间子目录避免与 /root/html/js、/root/html/css 既有旧文件冲突。
function minifyHtml() {
return {
name: 'minify-html',
apply: 'build',
transformIndexHtml: {
order: 'post',
handler(html) {
return minify(html, {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
collapseBooleanAttributes: true,
minifyCSS: true,
minifyJS: true,
})
},
},
}
}
export default defineConfig({
plugins: [vue(), minifyHtml()],
base: '/',
build: {
outDir: 'dist',
// ★ dist 在项目内,安全清空,杜绝旧 hash 文件堆积
emptyOutDir: true,
rollupOptions: {
output: {
// 稳定文件名:去掉 [hash],便于部署与长效缓存策略
entryFileNames: 'js/mainPage/[name].js',
chunkFileNames: 'js/mainPage/[name].js',
assetFileNames: (assetInfo) => {
const name = assetInfo.name || ''
if (name.endsWith('.css')) {
return 'css/mainPage/[name][extname]'
}
return 'assets/mainPage/[name][extname]'
},
},
},
},
server: {
// 开发服务器把站点根的静态资源代理到 /root/html方便引用 /448 /449 等
port: 5173,
proxy: {
'/448': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/449': {
target: 'http://localhost:8080',
changeOrigin: true,
},
// zTools2 后端同源代理:开发环境把 /api /pdf /health /static 等转发到本地 zTools2
// 使 iframe/pdf与 fetch/api/pdf/*)同源命中本地服务(测试环境指向测试环境)。
// 生产构建时 app.config.js 的 baseUrl 指向线上 f.zikai.wang不走此代理。
'/api': {
target: 'http://127.0.0.1:6867',
changeOrigin: true,
},
'/pdf': {
target: 'http://127.0.0.1:6867',
changeOrigin: true,
},
'/health': {
target: 'http://127.0.0.1:6867',
changeOrigin: true,
},
'/static': {
target: 'http://127.0.0.1:6867',
changeOrigin: true,
},
'/upload': {
target: 'http://127.0.0.1:6867',
changeOrigin: true,
},
'/files': {
target: 'http://127.0.0.1:6867',
changeOrigin: true,
},
'/wb': {
target: 'http://127.0.0.1:6867',
changeOrigin: true,
},
'/ws': {
target: 'ws://127.0.0.1:6867',
ws: true,
},
},
},
})