vite.config.ts 3.0 KB
Newer Older
V
vben 已提交
1
import type { UserConfig, ConfigEnv } from 'vite';
陈文彬 已提交
2

3
import { loadEnv } from 'vite';
V
vben 已提交
4
import { resolve } from 'path';
5

V
vben 已提交
6
import { generateModifyVars } from './build/config/themeConfig';
7
import { createProxy } from './build/vite/proxy';
8
import { wrapperEnv } from './build/utils';
V
vben 已提交
9
import { createVitePlugins } from './build/vite/plugin';
V
vben 已提交
10
import { OUTPUT_DIR } from './build/constant';
11

陈文彬 已提交
12 13 14
function pathResolve(dir: string) {
  return resolve(__dirname, '.', dir);
}
B
bin 已提交
15

V
vben 已提交
16
export default ({ command, mode }: ConfigEnv): UserConfig => {
V
vben 已提交
17 18
  const root = process.cwd();

19
  const env = loadEnv(mode, root);
V
vben 已提交
20 21

  // The boolean type read by loadEnv is a string. This function can be converted to boolean type
22
  const viteEnv = wrapperEnv(env);
V
vben 已提交
23

24 25 26 27 28 29 30 31
  const {
    VITE_PORT,
    VITE_PUBLIC_PATH,
    VITE_PROXY,
    VITE_DROP_CONSOLE,
    VITE_LEGACY,
    VITE_DYNAMIC_IMPORT,
  } = viteEnv;
32

V
vben 已提交
33
  const isBuild = command === 'build';
34

V
vben 已提交
35
  return {
V
vben 已提交
36
    base: VITE_PUBLIC_PATH,
V
vben 已提交
37
    root,
38 39 40 41 42 43 44
    resolve: {
      alias: [
        {
          // /@/xxxx  =>  src/xxx
          find: /^\/@\//,
          replacement: pathResolve('src') + '/',
        },
V
Vben 已提交
45 46 47 48 49
        {
          // /@/xxxx  =>  src/xxx
          find: /^\/#\//,
          replacement: pathResolve('types') + '/',
        },
50 51
      ],
    },
V
vben 已提交
52 53
    server: {
      port: VITE_PORT,
V
vben 已提交
54
      // Load proxy configuration from .env
V
vben 已提交
55 56 57 58 59
      proxy: createProxy(VITE_PROXY),
      hmr: {
        overlay: true,
      },
    },
V
vben 已提交
60

V
vben 已提交
61
    build: {
V
vben 已提交
62
      outDir: OUTPUT_DIR,
V
vben 已提交
63
      polyfillDynamicImport: VITE_LEGACY,
V
vben 已提交
64 65 66
      terserOptions: {
        compress: {
          keep_infinity: true,
V
vben 已提交
67
          // Used to delete console in production environment
V
vben 已提交
68 69
          drop_console: VITE_DROP_CONSOLE,
        },
70
      },
V
vben 已提交
71
      // Turning off brotliSize display can slightly reduce packaging time
V
vben 已提交
72
      brotliSize: false,
V
vben 已提交
73
      chunkSizeWarningLimit: 1200,
74 75 76 77
    },
    define: {
      // setting vue-i18-next
      // Suppress warning
78
      __DYNAMIC_IMPORT__: VITE_DYNAMIC_IMPORT,
79 80 81 82
      __VUE_I18N_LEGACY_API__: false,
      __VUE_I18N_FULL_INSTALL__: false,
      __INTLIFY_PROD_DEVTOOLS__: false,
    },
V
vben 已提交
83 84 85 86
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
V
vben 已提交
87
            // Used for global import to avoid the need to import each style file separately
V
vben 已提交
88
            // reference:  Avoid repeated references
V
vben 已提交
89
            hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
V
vben 已提交
90
            ...generateModifyVars(),
V
vben 已提交
91 92
          },
          javascriptEnabled: true,
V
vben 已提交
93
        },
94
      },
V
vben 已提交
95
    },
96

V
vben 已提交
97 98
    // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
    plugins: createVitePlugins(viteEnv, isBuild),
V
vben 已提交
99

V
vben 已提交
100
    optimizeDeps: {
V
vben 已提交
101
      // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
V
Vben 已提交
102 103 104 105 106 107 108
      include: [
        '@iconify/iconify',
        'ant-design-vue/es/locale/zh_CN',
        'moment/dist/locale/zh-cn',
        'ant-design-vue/es/locale/en_US',
        'moment/dist/locale/eu',
      ],
109
      exclude: ['vue-demi'],
110 111 112
    },
  };
};