Files
AIGC/demo/frontend/vite.config.js

102 lines
3.1 KiB
JavaScript
Raw Normal View History

2025-10-21 16:50:33 +08:00
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': resolve(__dirname, 'src')
}
},
2025-11-13 17:01:39 +08:00
// 生产/开发环境配置
base: process.env.NODE_ENV === 'production' ? '/' : '/',
// 开发服务器配置
2025-10-21 16:50:33 +08:00
server: {
2025-11-13 17:01:39 +08:00
port: 8081,
host: '0.0.0.0', // 允许外部访问
allowedHosts: true, // 允许所有主机访问
2025-10-21 16:50:33 +08:00
proxy: {
'/api': {
2025-11-13 17:01:39 +08:00
// 开发时代理到本地后端(统一为 localhost:8080
target: process.env.VITE_APP_API_URL || 'http://localhost:8080',
2025-10-21 16:50:33 +08:00
changeOrigin: true,
secure: false,
2025-11-13 17:01:39 +08:00
// 后端服务器路径已经包含 /api所以不需要 rewrite
// 前端请求 /api/xxx 会转发到 http://localhost:8080/api/xxx
// 调试时将 cookie 域改写为 localhost
cookieDomainRewrite: 'localhost',
2025-10-21 16:50:33 +08:00
cookiePathRewrite: '/',
configure: (proxy, _options) => {
proxy.on('error', (err, _req, _res) => {
console.log('proxy error', err);
});
proxy.on('proxyReq', (proxyReq, req, _res) => {
console.log('Sending Request to the Target:', req.method, req.url);
});
proxy.on('proxyRes', (proxyRes, req, _res) => {
console.log('Received Response from the Target:', proxyRes.statusCode, req.url);
const setCookie = proxyRes.headers['set-cookie'];
if (setCookie) {
console.log('Proxy Set-Cookie:', setCookie);
}
});
}
}
}
},
2025-11-13 17:01:39 +08:00
// public 目录配置(确保字体文件等静态资源被复制)
publicDir: 'public',
2025-11-13 17:01:39 +08:00
// 生产环境构建配置
2025-10-21 16:50:33 +08:00
build: {
outDir: 'dist',
2025-11-13 17:01:39 +08:00
assetsDir: 'static',
copyPublicDir: true,
// 使用 terser 压缩,移除 console
minify: 'terser',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true
}
},
// 启用 CSS 代码分割
cssCodeSplit: true,
// 禁用 source map
sourcemap: false,
2025-11-13 17:01:39 +08:00
// 代码分割优化
rollupOptions: {
output: {
// 为所有资源文件包括SVG生成内容哈希
assetFileNames: (assetInfo) => {
// 获取文件扩展名
const extType = assetInfo.name.split('.').pop();
// 图片类型包括SVG
if (/png|jpe?g|gif|svg|webp|ico/i.test(extType)) {
return `static/images/[name]-[hash][extname]`;
}
// 字体类型
if (/woff2?|eot|ttf|otf/i.test(extType)) {
return `static/fonts/[name]-[hash][extname]`;
}
// 其他资源
return `static/[name]-[hash][extname]`;
},
// JS文件哈希
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
2025-11-13 17:01:39 +08:00
manualChunks: {
'vue-vendor': ['vue', 'vue-router', 'pinia'],
'element-plus': ['element-plus', '@element-plus/icons-vue'],
'utils': ['axios']
}
}
},
// 块大小警告限制
chunkSizeWarningLimit: 1000
2025-10-21 16:50:33 +08:00
}
})