修正打包部署配置

This commit is contained in:
微微一笑
2025-07-10 09:14:44 +08:00
parent fa6af8602e
commit 9002ce6bc3
5 changed files with 61 additions and 7 deletions

View File

@ -5,11 +5,22 @@ import path from 'path';
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, process.cwd());
// 参考eladmin-web的部署方式使用相对路径
let basePath = '/';
if (mode === 'production') {
// 生产环境:如果设置了上下文路径则使用,否则使用相对路径
basePath = env.VITE_APP_CONTEXT_PATH || './';
} else {
// 开发环境:使用上下文路径或默认根路径
basePath = env.VITE_APP_CONTEXT_PATH || '/';
}
return {
// 部署生产环境和开发环境下的URL。
// 默认情况下vite 会假设你的应用是被部署在一个域名的根路径上
// 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
base: env.VITE_APP_CONTEXT_PATH,
base: basePath,
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
@ -24,7 +35,7 @@ export default defineConfig(({ mode, command }) => {
open: true,
proxy: {
[env.VITE_APP_BASE_API]: {
target: 'http://localhost:8001',
target: env.VITE_APP_PROXY_TARGET || 'http://localhost:8001',
changeOrigin: true,
ws: true,
rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
@ -54,6 +65,48 @@ export default defineConfig(({ mode, command }) => {
]
}
},
// 构建配置
build: {
outDir: 'dist',
assetsDir: 'static',
// 生产环境关闭source map
sourcemap: mode === 'development',
// 构建时清理输出目录
emptyOutDir: true,
// 大文件警告限制
chunkSizeWarningLimit: 2000,
// 代码分割配置
rollupOptions: {
output: {
// 静态资源分类打包
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
// 代码分割
manualChunks: {
// 将 Vue 相关的包打包到一个 chunk 中
vue: ['vue', 'vue-router', 'pinia'],
// 将 Element Plus 相关的包打包到一个 chunk 中
elementPlus: ['element-plus', '@element-plus/icons-vue'],
// 将 ECharts 相关的包打包到一个 chunk 中
echarts: ['echarts'],
// 将工具库打包到一个 chunk 中
utils: ['axios', '@vueuse/core', 'crypto-js', 'js-cookie', 'jsencrypt'],
// 将编辑器相关的包打包到一个 chunk 中
editor: ['@vueup/vue-quill', 'vue-json-pretty']
}
}
},
// 压缩配置
minify: 'terser',
terserOptions: {
compress: {
// 生产环境移除console
drop_console: mode === 'production',
drop_debugger: mode === 'production'
}
}
},
// 预编译
optimizeDeps: {
include: [