Blame view

vite.config.ts 3.19 KB
vben authored
1
import type { UserConfig, ConfigEnv } from 'vite';
2
import pkg from './package.json';
3
import dayjs from 'dayjs';
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
  lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
20
};
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: {
vben authored
56
      https: true,
57
58
      // Listening on all local IPs
      host: true,
vben authored
59
      port: VITE_PORT,
vben authored
60
      // Load proxy configuration from .env
vben authored
61
62
      proxy: createProxy(VITE_PROXY),
    },
vben authored
63
64
65
    esbuild: {
      pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
    },
vben authored
66
    build: {
vben authored
67
68
      target: 'es2015',
      cssTarget: 'chrome80',
vben authored
69
      outDir: OUTPUT_DIR,
vben authored
70
71
72
73
74
75
76
77
78
79
80
      // minify: 'terser',
      /**
       * 当 minify=“minify:'terser'” 解开注释
       * Uncomment when minify="minify:'terser'"
       */
      // terserOptions: {
      //   compress: {
      //     keep_infinity: true,
      //     drop_console: VITE_DROP_CONSOLE,
      //   },
      // },
vben authored
81
      // Turning off brotliSize display can slightly reduce packaging time
vben authored
82
      brotliSize: false,
Vben authored
83
      chunkSizeWarningLimit: 2000,
84
85
    },
    define: {
vben authored
86
87
88
      // setting vue-i18-next
      // Suppress warning
      __INTLIFY_PROD_DEVTOOLS__: false,
Vben authored
89
      __APP_INFO__: JSON.stringify(__APP_INFO__),
90
    },
91
vben authored
92
93
94
    css: {
      preprocessorOptions: {
        less: {
Vben authored
95
          modifyVars: generateModifyVars(),
vben authored
96
          javascriptEnabled: true,
97
        },
98
      },
vben authored
99
    },
100
vben authored
101
102
    // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
    plugins: createVitePlugins(viteEnv, isBuild),
vben authored
103
vben authored
104
    optimizeDeps: {
vben authored
105
      // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
Vben authored
106
      include: [
vben authored
107
108
        '@vue/runtime-core',
        '@vue/shared',
Vben authored
109
110
111
112
        '@iconify/iconify',
        'ant-design-vue/es/locale/zh_CN',
        'ant-design-vue/es/locale/en_US',
      ],
113
114
115
    },
  };
};