vite.config.ts 2.9 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 { createAlias } from './build/vite/alias';
9
import { wrapperEnv } from './build/utils';
V
vben 已提交
10
import { createVitePlugins } from './build/vite/plugin';
V
vben 已提交
11
import { OUTPUT_DIR } from './build/constant';
12 13 14 15 16 17 18
import pkg from './package.json';
import moment from 'moment';

const APP_INFO = {
  pkg,
  lastBuildTime: moment().format('YYYY-MM-DD HH:mm:ss'),
};
19

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

23
  const env = loadEnv(mode, root);
V
vben 已提交
24 25

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

28
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
29

V
vben 已提交
30
  const isBuild = command === 'build';
31

V
vben 已提交
32
  return {
V
vben 已提交
33
    base: VITE_PUBLIC_PATH,
V
vben 已提交
34
    root,
35
    resolve: {
36 37 38 39 40 41
      alias: createAlias([
        // /@/xxxx => src/xxxx
        ['/@/', 'src'],
        // /#/xxxx => types/xxxx
        ['/#/', 'types'],
      ]),
42
    },
V
vben 已提交
43 44
    server: {
      port: VITE_PORT,
V
vben 已提交
45
      // Load proxy configuration from .env
V
vben 已提交
46 47 48
      proxy: createProxy(VITE_PROXY),
    },
    build: {
V
Vben 已提交
49
      target: 'es2015',
V
vben 已提交
50
      outDir: OUTPUT_DIR,
V
vben 已提交
51 52 53
      terserOptions: {
        compress: {
          keep_infinity: true,
V
vben 已提交
54
          // Used to delete console in production environment
V
vben 已提交
55 56
          drop_console: VITE_DROP_CONSOLE,
        },
57
      },
V
vben 已提交
58
      // Turning off brotliSize display can slightly reduce packaging time
V
vben 已提交
59
      brotliSize: false,
V
vben 已提交
60
      chunkSizeWarningLimit: 1200,
61 62 63 64 65 66 67
    },
    define: {
      // setting vue-i18-next
      // Suppress warning
      __VUE_I18N_LEGACY_API__: false,
      __VUE_I18N_FULL_INSTALL__: false,
      __INTLIFY_PROD_DEVTOOLS__: false,
68 69

      __APP_INFO__: JSON.stringify(APP_INFO),
70
    },
V
vben 已提交
71 72 73 74
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
V
vben 已提交
75
            // Used for global import to avoid the need to import each style file separately
V
vben 已提交
76
            // reference:  Avoid repeated references
V
vben 已提交
77
            hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
V
vben 已提交
78
            ...generateModifyVars(),
V
vben 已提交
79 80
          },
          javascriptEnabled: true,
V
vben 已提交
81
        },
82
      },
V
vben 已提交
83
    },
84

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

V
vben 已提交
88
    optimizeDeps: {
V
vben 已提交
89
      // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
V
Vben 已提交
90 91 92 93 94 95 96
      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',
      ],
V
Vben 已提交
97
      exclude: ['vue-demi'],
98 99 100
    },
  };
};