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/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: [
V
vben 已提交
42 43 44 45
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
46
        // /@/xxxx => src/xxxx
V
Vben 已提交
47 48 49 50
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/',
        },
51
        // /#/xxxx => types/xxxx
V
Vben 已提交
52 53 54 55 56 57
        {
          find: /\/#\//,
          replacement: pathResolve('types') + '/',
        },
        // ['@vue/compiler-sfc', '@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js'],
      ],
58
    },
V
vben 已提交
59 60
    server: {
      port: VITE_PORT,
V
vben 已提交
61
      // Load proxy configuration from .env
V
vben 已提交
62 63 64
      proxy: createProxy(VITE_PROXY),
    },
    build: {
V
Vben 已提交
65
      target: 'es2015',
V
vben 已提交
66
      outDir: OUTPUT_DIR,
V
vben 已提交
67 68 69
      terserOptions: {
        compress: {
          keep_infinity: true,
V
vben 已提交
70
          // Used to delete console in production environment
V
vben 已提交
71 72
          drop_console: VITE_DROP_CONSOLE,
        },
73
      },
V
vben 已提交
74
      // Turning off brotliSize display can slightly reduce packaging time
V
vben 已提交
75
      brotliSize: false,
V
Vben 已提交
76
      chunkSizeWarningLimit: 2000,
77 78
    },
    define: {
V
Vben 已提交
79
      __APP_INFO__: JSON.stringify(__APP_INFO__),
80
    },
V
vben 已提交
81 82 83
    css: {
      preprocessorOptions: {
        less: {
V
Vben 已提交
84
          modifyVars: generateModifyVars(),
V
vben 已提交
85
          javascriptEnabled: true,
V
vben 已提交
86
        },
87
      },
V
vben 已提交
88
    },
89

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

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