vite.config.ts 3.0 KB
Newer Older
V
vben 已提交
1
import type { UserConfig, ConfigEnv } from 'vite';
V
vben 已提交
2
import pkg from './package.json';
V
vben 已提交
3
import dayjs from 'dayjs';
4
import { loadEnv } from 'vite';
V
vben 已提交
5
import { resolve } from 'path';
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

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

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

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

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

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

30
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
31

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

V
vben 已提交
34
  return {
V
vben 已提交
35
    base: VITE_PUBLIC_PATH,
V
vben 已提交
36
    root,
37
    resolve: {
V
Vben 已提交
38
      alias: [
V
vben 已提交
39 40 41 42
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
43
        // /@/xxxx => src/xxxx
V
Vben 已提交
44 45 46 47
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/',
        },
48
        // /#/xxxx => types/xxxx
V
Vben 已提交
49 50 51 52 53
        {
          find: /\/#\//,
          replacement: pathResolve('types') + '/',
        },
      ],
54
    },
V
vben 已提交
55
    server: {
无木 已提交
56 57
      // Listening on all local IPs
      host: true,
V
vben 已提交
58
      port: VITE_PORT,
V
vben 已提交
59
      // Load proxy configuration from .env
V
vben 已提交
60 61 62
      proxy: createProxy(VITE_PROXY),
    },
    build: {
V
vben 已提交
63 64 65
      minify: false,
      // target: 'es2015',
      // cssTarget: 'chrome86',
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 80 81
      // setting vue-i18-next
      // Suppress warning
      __INTLIFY_PROD_DEVTOOLS__: false,
V
Vben 已提交
82
      __APP_INFO__: JSON.stringify(__APP_INFO__),
83
    },
V
vben 已提交
84

V
vben 已提交
85 86 87
    css: {
      preprocessorOptions: {
        less: {
V
Vben 已提交
88
          modifyVars: generateModifyVars(),
V
vben 已提交
89
          javascriptEnabled: true,
V
vben 已提交
90
        },
91
      },
V
vben 已提交
92
    },
93

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

V
vben 已提交
97
    optimizeDeps: {
V
vben 已提交
98
      // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
V
Vben 已提交
99
      include: [
V
vben 已提交
100 101
        '@vue/runtime-core',
        '@vue/shared',
V
Vben 已提交
102 103 104 105
        '@iconify/iconify',
        'ant-design-vue/es/locale/zh_CN',
        'ant-design-vue/es/locale/en_US',
      ],
106 107 108
    },
  };
};