Blame view

vite.config.ts 2.98 KB
532def1c   王彪总   新版本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  import { resolve } from 'node:path'
  import { defineConfig, loadEnv } from 'vite'
  import vue from '@vitejs/plugin-vue'
  import vueJsx from '@vitejs/plugin-vue-jsx'
  import UnoCSS from 'unocss/vite'
  import AutoImport from 'unplugin-auto-import/vite'
  import Components from 'unplugin-vue-components/vite'
  import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  import viteCompression from 'vite-plugin-compression'
  
  const pathSrc = resolve(__dirname, 'src')
  
  export default defineConfig(({ mode }) => {
    const env = loadEnv(mode, process.cwd())
  
    return {
      base: env.VITE_BASE_PATH || '/',
      resolve: {
        alias: {
          '@': pathSrc
        }
      },
      plugins: [
        vue(),
        vueJsx(),
        UnoCSS(),
        // 自动导入 vue / vue-router / pinia 的 API,无需在每个文件手写 import
        AutoImport({
          imports: ['vue', 'vue-router', 'pinia'],
          resolvers: [ElementPlusResolver()],
          dts: 'types/auto-imports.d.ts'
        }),
        // 按需自动注册 Element Plus 组件(样式仍在 main.ts 全局引入,避免样式缺失)
        Components({
          resolvers: [ElementPlusResolver()],
          dts: 'types/components.d.ts'
        }),
        // 生产构建产出 .gz,交给 Nginx 直接吐
        viteCompression({
          verbose: false,
          disable: mode !== 'prod',
          threshold: 10240,
          algorithm: 'gzip',
          ext: '.gz'
        })
      ],
      css: {
        preprocessorOptions: {
          scss: {
            additionalData: `@use "@/styles/variables.scss" as *;`
          }
        }
      },
      server: {
        host: '0.0.0.0',
        port: 5173,
        open: false,
        // 前后端分离:开发期把 /admin-api、/app-api 代理到本地后端(48080)
        proxy: {
          '/admin-api': {
            target: env.VITE_PROXY_TARGET || 'http://127.0.0.1:48080',
            changeOrigin: true
          },
          '/app-api': {
            target: env.VITE_PROXY_TARGET || 'http://127.0.0.1:48080',
            changeOrigin: true
          }
        }
      },
      build: {
        target: 'es2015',
        outDir: 'dist',
        chunkSizeWarningLimit: 2000,
        reportCompressedSize: false,
        minify: 'terser',
        terserOptions: {
          compress: {
            // 生产包去掉 console / debugger(保留 error、warn 便于排障)
            drop_console: false,
            drop_debugger: true,
            pure_funcs: ['console.log']
          }
        },
        rollupOptions: {
          output: {
            chunkFileNames: 'static/js/[name]-[hash].js',
            entryFileNames: 'static/js/[name]-[hash].js',
            assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
            manualChunks: {
              vue: ['vue', 'vue-router', 'pinia', 'vue-i18n'],
              element: ['element-plus', '@element-plus/icons-vue']
              // editor(wangEditor)与 echarts 在 P4 逐页迁移时开始使用,
              // 现在就写进 manualChunks 会产出空的 chunk 并告警,故等到实际引入时再加。
            }
          }
        }
      }
    }
  })