Blame view

vite.config.ts 3 KB
vben authored
1
import type { UserConfig, ConfigEnv } from 'vite';
2
3
import pkg from './package.json';
import moment from 'moment';
4
import { loadEnv } from 'vite';
vben authored
5
import { resolve } from 'path';
Vben authored
6
import { generateModifyVars } from './build/generate/generateModifyVars';
7
import { createProxy } from './build/vite/proxy';
8
import { wrapperEnv } from './build/utils';
vben authored
9
import { createVitePlugins } from './build/vite/plugin';
vben authored
10
import { OUTPUT_DIR } from './build/constant';
Vben authored
11
Vben authored
12
13
14
15
function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}
Vben authored
16
const { dependencies, devDependencies, name, version } = pkg;
Vben authored
17
const __APP_INFO__ = {
Vben authored
18
  pkg: { dependencies, devDependencies, name, version },
19
20
  lastBuildTime: moment().format('YYYY-MM-DD HH:mm:ss'),
};
21
vben authored
22
export default ({ command, mode }: ConfigEnv): UserConfig => {
vben authored
23
24
  const root = process.cwd();
25
  const env = loadEnv(mode, root);
vben authored
26
27

  // The boolean type read by loadEnv is a string. This function can be converted to boolean type
28
  const viteEnv = wrapperEnv(env);
vben authored
29
30
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
31
vben authored
32
  const isBuild = command === 'build';
33
vben authored
34
  return {
vben authored
35
    base: VITE_PUBLIC_PATH,
vben authored
36
    root,
37
    resolve: {
Vben authored
38
      alias: [
39
40
41
42
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
43
        // /@/xxxx => src/xxxx
Vben authored
44
45
46
47
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/',
        },
48
        // /#/xxxx => types/xxxx
Vben authored
49
50
51
52
53
        {
          find: /\/#\//,
          replacement: pathResolve('types') + '/',
        },
      ],
54
    },
vben authored
55
    server: {
56
57
      // Listening on all local IPs
      host: true,
vben authored
58
      port: VITE_PORT,
vben authored
59
      // Load proxy configuration from .env
vben authored
60
61
62
      proxy: createProxy(VITE_PROXY),
    },
    build: {
Vben authored
63
      target: 'es2015',
vben authored
64
      outDir: OUTPUT_DIR,
vben authored
65
66
67
      terserOptions: {
        compress: {
          keep_infinity: true,
vben authored
68
          // Used to delete console in production environment
vben authored
69
70
          drop_console: VITE_DROP_CONSOLE,
        },
71
      },
vben authored
72
      // Turning off brotliSize display can slightly reduce packaging time
vben authored
73
      brotliSize: false,
Vben authored
74
      chunkSizeWarningLimit: 2000,
75
76
    },
    define: {
vben authored
77
78
79
      // setting vue-i18-next
      // Suppress warning
      __INTLIFY_PROD_DEVTOOLS__: false,
Vben authored
80
      __APP_INFO__: JSON.stringify(__APP_INFO__),
81
    },
82
vben authored
83
84
85
    css: {
      preprocessorOptions: {
        less: {
Vben authored
86
          modifyVars: generateModifyVars(),
vben authored
87
          javascriptEnabled: true,
88
        },
89
      },
vben authored
90
    },
91
vben authored
92
93
    // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
    plugins: createVitePlugins(viteEnv, isBuild),
vben authored
94
vben authored
95
    optimizeDeps: {
vben authored
96
      // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
Vben authored
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',
      ],
Vben authored
104
      exclude: ['vue-demi'],
105
106
107
    },
  };
};