| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import { defineConfig, loadEnv } from 'vite'
- import vue from '@vitejs/plugin-vue'
- import { resolve } from 'path'
- import AutoImport from 'unplugin-auto-import/vite'
- import Components from 'unplugin-vue-components/vite'
- import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
- /**
- * 开发代理后台环境(一键切换)
- * - online:线上 Tomcat http://121.43.55.7:11091/sjnmtybt (Swagger)
- * - local :本机 http://localhost:8088/sjnmtybt
- *
- * 切换方式(任选其一):
- * 1) npm run dev → 默认 online
- * 2) npm run dev:local → 本机后台
- * 3) npm run dev:online → 线上后台
- * 4) 改下面 DEFAULT_API_ENV,或设环境变量 VITE_API_ENV=local|online
- *
- * 注意:不能用 vite --mode local(与 .env.local 后缀冲突),故用 backend-local / backend-online。
- */
- const DEFAULT_API_ENV: 'online' | 'local' = 'online'
- const API_TARGETS = {
- online: 'http://121.43.55.7:11091',
- local: 'http://localhost:8088',
- } as const
- function resolveApiEnv(mode: string, fromEnv: string): 'online' | 'local' {
- if (mode === 'backend-local' || mode === 'local') return 'local'
- if (mode === 'backend-online' || mode === 'online') return 'online'
- if (fromEnv === 'local' || fromEnv === 'online') return fromEnv
- return DEFAULT_API_ENV
- }
- // https://vite.dev/config/
- export default defineConfig(({ mode }) => {
- const env = loadEnv(mode, process.cwd(), '')
- const fromEnv = (env.VITE_API_ENV || process.env.VITE_API_ENV || '').toLowerCase()
- const apiEnv = resolveApiEnv(mode, fromEnv)
- const apiTarget = API_TARGETS[apiEnv]
- console.log(`[vite] 开发代理后台 → ${apiEnv} (${apiTarget}/sjnmtybt)`)
- return {
- // 规避 Vite 8 开发客户端未注入 __BUNDLED_DEV__ 导致页面无法启动
- define: {
- __BUNDLED_DEV__: 'false',
- __SERVER_FORWARD_CONSOLE__: 'false',
- },
- plugins: [
- vue(),
- AutoImport({
- imports: ['vue', 'vue-router'],
- resolvers: [ElementPlusResolver()],
- dts: 'src/auto-imports.d.ts',
- }),
- // Element Plus 按需引入,减小首包体积
- Components({
- resolvers: [ElementPlusResolver()],
- dts: 'src/components.d.ts',
- }),
- ],
- resolve: {
- alias: {
- '@': resolve(__dirname, 'src'),
- },
- },
- css: {
- preprocessorOptions: {
- less: {
- javascriptEnabled: true,
- additionalData: (content: string, filename: string) => {
- if (filename.replace(/\\/g, '/').includes('/styles/variables.less')) {
- return content
- }
- return `@import "@/styles/variables.less";\n${content}`
- },
- },
- },
- },
- build: {
- // 单文件超过该体积时告警(单位 KB)
- chunkSizeWarningLimit: 500,
- cssCodeSplit: true,
- // Vite 5 / Rollup:大依赖拆 chunk(兼容本机 Node 20.13)
- rollupOptions: {
- output: {
- manualChunks(id) {
- if (!id.includes('node_modules')) return
- if (/[\\/](vue|vue-router)([\\/]|$)/.test(id)) return 'vue-vendor'
- if (/[\\/]element-plus([\\/]|$)/.test(id)) return 'element-plus'
- if (/[\\/]@element-plus[\\/]icons-vue([\\/]|$)/.test(id)) return 'element-icons'
- if (/[\\/]axios([\\/]|$)/.test(id)) return 'axios'
- return 'vendor'
- },
- },
- },
- },
- // 预构建常用 Element Plus 组件,避免首次进入某菜单时触发「optimized dependencies changed」整页刷新,
- // 刷新会清掉内存中的角色状态,路由守卫再把无权限页打回村合作社工作台。
- optimizeDeps: {
- include: [
- 'vue',
- 'vue-router',
- 'pinia',
- 'axios',
- 'element-plus/es',
- 'element-plus/es/locale/lang/zh-cn',
- '@element-plus/icons-vue',
- 'echarts',
- ],
- },
- // 排除原型目录,避免开发服务与打包过程触及该文件夹
- server: {
- host: true,
- port: 3001,
- fs: {
- deny: ['**/松江区农民土地退养补助金发放管理平台-原型/**'],
- },
- watch: {
- ignored: ['**/松江区农民土地退养补助金发放管理平台-原型/**'],
- },
- proxy: {
- // 业务 API:/api → {target}/sjnmtybt/api
- '/api': {
- target: apiTarget,
- changeOrigin: true,
- rewrite: (p) => `/sjnmtybt${p}`,
- },
- // 上传附件等相对路径(后端返回 /sjnmtybt/api/files/...)
- '/sjnmtybt': {
- target: apiTarget,
- changeOrigin: true,
- },
- // 认证登录(oauth)— 与 DMS/OAuth 网关一致
- '/oauth': {
- target: 'http://121.43.55.7:2101',
- changeOrigin: true,
- },
- '/proxy_oauth': {
- target: 'http://121.43.55.7:2101',
- changeOrigin: true,
- },
- // 其他系统接口
- '/external': {
- target: 'http://121.43.55.7:2101',
- changeOrigin: true,
- },
- },
- },
- }
- })
|