Vben
2021-03-01 b476e1c84c52dab7030fd19b34ecd33e65fadcb2
提交 | 用户 | age
99ac30 1 import type { UserConfig, ConfigEnv } from 'vite';
2f6253 2
bd7b53 3 import { loadEnv } from 'vite';
07c18d 4 import { resolve } from 'path';
bd7b53 5
3d1681 6 import { generateModifyVars } from './build/config/themeConfig';
173d40 7 import { createProxy } from './build/vite/proxy';
bd7b53 8 import { wrapperEnv } from './build/utils';
99ac30 9 import { createVitePlugins } from './build/vite/plugin';
759e53 10 import { OUTPUT_DIR } from './build/constant';
173d40 11
2f6253 12 function pathResolve(dir: string) {
13   return resolve(__dirname, '.', dir);
14 }
15
99ac30 16 export default ({ command, mode }: ConfigEnv): UserConfig => {
07c18d 17   const root = process.cwd();
V 18
bd7b53 19   const env = loadEnv(mode, root);
07c18d 20
V 21   // The boolean type read by loadEnv is a string. This function can be converted to boolean type
bd7b53 22   const viteEnv = wrapperEnv(env);
07c18d 23
b476e1 24   const {
V 25     VITE_PORT,
26     VITE_PUBLIC_PATH,
27     VITE_PROXY,
28     VITE_DROP_CONSOLE,
29     VITE_LEGACY,
30     VITE_DYNAMIC_IMPORT,
31   } = viteEnv;
99ac30 32
V 33   const isBuild = command === 'build';
34
bd7b53 35   return {
f24422 36     base: VITE_PUBLIC_PATH,
bd7b53 37     root,
993538 38     resolve: {
V 39       alias: [
40         {
41           // /@/xxxx  =>  src/xxx
42           find: /^\/@\//,
43           replacement: pathResolve('src') + '/',
44         },
fcee7d 45         {
V 46           // /@/xxxx  =>  src/xxx
47           find: /^\/#\//,
48           replacement: pathResolve('types') + '/',
49         },
993538 50       ],
V 51     },
99ac30 52     server: {
V 53       port: VITE_PORT,
07c18d 54       // Load proxy configuration from .env
99ac30 55       proxy: createProxy(VITE_PROXY),
V 56       hmr: {
57         overlay: true,
bd7b53 58       },
8a1bfd 59     },
1b71db 60
99ac30 61     build: {
759e53 62       outDir: OUTPUT_DIR,
7385ce 63       polyfillDynamicImport: VITE_LEGACY,
99ac30 64       terserOptions: {
V 65         compress: {
66           keep_infinity: true,
07c18d 67           // Used to delete console in production environment
99ac30 68           drop_console: VITE_DROP_CONSOLE,
V 69         },
70       },
07c18d 71       // Turning off brotliSize display can slightly reduce packaging time
1b71db 72       brotliSize: false,
3ba828 73       chunkSizeWarningLimit: 1200,
99ac30 74     },
bd7b53 75     define: {
V 76       // setting vue-i18-next
77       // Suppress warning
b476e1 78       __DYNAMIC_IMPORT__: VITE_DYNAMIC_IMPORT,
bd7b53 79       __VUE_I18N_LEGACY_API__: false,
V 80       __VUE_I18N_FULL_INSTALL__: false,
81       __INTLIFY_PROD_DEVTOOLS__: false,
2f6253 82     },
99ac30 83     css: {
V 84       preprocessorOptions: {
85         less: {
86           modifyVars: {
07c18d 87             // Used for global import to avoid the need to import each style file separately
3509eb 88             // reference:  Avoid repeated references
99ac30 89             hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
3d1681 90             ...generateModifyVars(),
99ac30 91           },
V 92           javascriptEnabled: true,
116a1f 93         },
bd7b53 94       },
V 95     },
96
07c18d 97     // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
V 98     plugins: createVitePlugins(viteEnv, isBuild),
8a1bfd 99
99ac30 100     optimizeDeps: {
07c18d 101       // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
c8e84d 102       include: [
V 103         '@iconify/iconify',
104         'ant-design-vue/es/locale/zh_CN',
105         'moment/dist/locale/zh-cn',
106         'ant-design-vue/es/locale/en_US',
107         'moment/dist/locale/eu',
108       ],
993538 109       exclude: ['vue-demi'],
bd7b53 110     },
V 111   };
6f8d75 112 };