Blame view

vite.config.ts 3.08 KB
vben authored
1
import type { UserConfig, ConfigEnv } from 'vite';
陈文彬 authored
2
3
import { loadEnv } from 'vite';
vben authored
4
import { resolve } from 'path';
5
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
12
13
14
import pkg from './package.json';
import moment from 'moment';
Vben authored
15
16
17
18
function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}
Vben authored
19
const { dependencies, devDependencies, name, version } = pkg;
Vben authored
20
const __APP_INFO__ = {
Vben authored
21
  pkg: { dependencies, devDependencies, name, version },
22
23
  lastBuildTime: moment().format('YYYY-MM-DD HH:mm:ss'),
};
24
vben authored
25
export default ({ command, mode }: ConfigEnv): UserConfig => {
vben authored
26
27
  const root = process.cwd();
28
  const env = loadEnv(mode, root);
vben authored
29
30

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