vite.config.ts 3.3 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
import pkg from './package.json';
import moment from 'moment';

V
Vben 已提交
14 15 16 17
function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

V
Vben 已提交
18
const { dependencies, devDependencies, name, version } = pkg;
V
Vben 已提交
19
const __APP_INFO__ = {
V
Vben 已提交
20
  pkg: { dependencies, devDependencies, name, version },
21 22
  lastBuildTime: moment().format('YYYY-MM-DD HH:mm:ss'),
};
23

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

27
  const env = loadEnv(mode, root);
V
vben 已提交
28 29

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

32
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
33

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

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

V
Vben 已提交
80
      __APP_INFO__: JSON.stringify(__APP_INFO__),
81
    },
V
vben 已提交
82 83 84 85
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
V
vben 已提交
86
            // Used for global import to avoid the need to import each style file separately
V
vben 已提交
87
            // reference:  Avoid repeated references
V
vben 已提交
88
            hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
V
vben 已提交
89
            ...generateModifyVars(),
V
vben 已提交
90 91
          },
          javascriptEnabled: true,
V
vben 已提交
92
        },
93
      },
V
vben 已提交
94
    },
95

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

V
vben 已提交
99
    optimizeDeps: {
V
vben 已提交
100
      // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
V
Vben 已提交
101 102 103 104 105 106 107
      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 已提交
108
      exclude: ['vue-demi'],
109 110 111
    },
  };
};