修正打包部署配置

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,7 +5,7 @@ VITE_APP_TITLE = 物联网管理系统
VITE_APP_ENV = 'https://fuyuanshen.com/backend'
# 应用访问路径 例如使用前缀 /admin/
VITE_APP_CONTEXT_PATH = '/'
VITE_APP_CONTEXT_PATH = '/sys/'
# 监控地址
VITE_APP_MONITOR_ADMIN = '/admin/applications'
@ -14,7 +14,7 @@ VITE_APP_MONITOR_ADMIN = '/admin/applications'
VITE_APP_SNAILJOB_ADMIN = '/snail-job'
# 生产环境
VITE_APP_BASE_API = '/prod-api'
VITE_APP_BASE_API = '/backend'
# 是否在打包时开启压缩,支持 gzip 和 brotli
VITE_BUILD_COMPRESS = gzip

View File

@ -68,6 +68,7 @@
"globals": "16.0.0",
"prettier": "3.5.2",
"sass": "1.87.0",
"terser": "^5.43.1",
"typescript": "~5.8.3",
"unocss": "66.0.0",
"unplugin-auto-import": "19.1.2",

View File

@ -30,14 +30,14 @@ const processResponse = async (res: any) => {
}
ElMessage.success(res.msg);
setTimeout(() => {
location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
location.href = (import.meta.env.VITE_APP_CONTEXT_PATH || '/') + 'index';
}, 2000);
};
const handleError = (error: any) => {
ElMessage.error(error.message);
setTimeout(() => {
location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
location.href = (import.meta.env.VITE_APP_CONTEXT_PATH || '/') + 'index';
}, 2000);
};

View File

@ -100,7 +100,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
* 创建路由
*/
const router = createRouter({
history: createWebHistory(import.meta.env.VITE_APP_CONTEXT_PATH),
history: createWebHistory(import.meta.env.VITE_APP_CONTEXT_PATH || '/'),
routes: constantRoutes,
// 刷新时,滚动条位置还原
scrollBehavior(to, from, savedPosition) {

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: [