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/generate/generateModifyVars';
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';
V
Vben 已提交
11

12 13 14
import pkg from './package.json';
import moment from 'moment';

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

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

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

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

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

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

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

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

V
Vben 已提交
81
      __APP_INFO__: JSON.stringify(__APP_INFO__),
82
    },
V
vben 已提交
83 84 85
    css: {
      preprocessorOptions: {
        less: {
V
Vben 已提交
86
          modifyVars: generateModifyVars(),
V
vben 已提交
87
          javascriptEnabled: true,
V
vben 已提交
88
        },
89
      },
V
vben 已提交
90
    },
91

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

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