diff --git a/.env.production b/.env.production index ecd5c10..94e99a2 100644 --- a/.env.production +++ b/.env.production @@ -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 diff --git a/package.json b/package.json index 1b8c73f..03bae62 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/layout/components/SocialCallback/index.vue b/src/layout/components/SocialCallback/index.vue index a5cd768..15b690b 100644 --- a/src/layout/components/SocialCallback/index.vue +++ b/src/layout/components/SocialCallback/index.vue @@ -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); }; diff --git a/src/router/index.ts b/src/router/index.ts index a6497db..8d9eb88 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -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) { diff --git a/vite.config.ts b/vite.config.ts index 03300de..889ccd1 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -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: [