Vben
2021-02-24 601368921f075aa1870d1c3ce8f4a8330260206a
提交 | 用户 | 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
99ac30 24   const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE, VITE_LEGACY } = viteEnv;
V 25
26   const isBuild = command === 'build';
27
bd7b53 28   return {
f24422 29     base: VITE_PUBLIC_PATH,
bd7b53 30     root,
993538 31     resolve: {
V 32       alias: [
33         {
34           // /@/xxxx  =>  src/xxx
35           find: /^\/@\//,
36           replacement: pathResolve('src') + '/',
37         },
38       ],
39     },
99ac30 40     server: {
V 41       port: VITE_PORT,
07c18d 42       // Load proxy configuration from .env
99ac30 43       proxy: createProxy(VITE_PROXY),
V 44       hmr: {
45         overlay: true,
bd7b53 46       },
8a1bfd 47     },
1b71db 48
99ac30 49     build: {
759e53 50       outDir: OUTPUT_DIR,
7385ce 51       polyfillDynamicImport: VITE_LEGACY,
99ac30 52       terserOptions: {
V 53         compress: {
54           keep_infinity: true,
07c18d 55           // Used to delete console in production environment
99ac30 56           drop_console: VITE_DROP_CONSOLE,
V 57         },
58       },
07c18d 59       // Turning off brotliSize display can slightly reduce packaging time
1b71db 60       brotliSize: false,
3ba828 61       chunkSizeWarningLimit: 1200,
99ac30 62     },
bd7b53 63     define: {
V 64       // setting vue-i18-next
65       // Suppress warning
66       __VUE_I18N_LEGACY_API__: false,
67       __VUE_I18N_FULL_INSTALL__: false,
68       __INTLIFY_PROD_DEVTOOLS__: false,
2f6253 69     },
99ac30 70     css: {
V 71       preprocessorOptions: {
72         less: {
73           modifyVars: {
07c18d 74             // Used for global import to avoid the need to import each style file separately
3509eb 75             // reference:  Avoid repeated references
99ac30 76             hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
3d1681 77             ...generateModifyVars(),
99ac30 78           },
V 79           javascriptEnabled: true,
116a1f 80         },
bd7b53 81       },
V 82     },
83
07c18d 84     // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
V 85     plugins: createVitePlugins(viteEnv, isBuild),
8a1bfd 86
99ac30 87     optimizeDeps: {
07c18d 88       // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
9edc28 89       include: ['@iconify/iconify'],
993538 90       exclude: ['vue-demi'],
bd7b53 91     },
V 92   };
6f8d75 93 };