提交 | 用户 | age
99ac30 1 import type { UserConfig, ConfigEnv } from 'vite';
9dd9fc 2 import pkg from './package.json';
3fcfac 3 import dayjs from 'dayjs';
bd7b53 4 import { loadEnv } from 'vite';
07c18d 5 import { resolve } from 'path';
5b8eb4 6 import { generateModifyVars } from './build/generate/generateModifyVars';
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';
3576d0 11
V 12 function pathResolve(dir: string) {
13   return resolve(process.cwd(), '.', dir);
14 }
b54b79 15
6d6e0a 16 const { dependencies, devDependencies, name, version } = pkg;
c03056 17 const __APP_INFO__ = {
6d6e0a 18   pkg: { dependencies, devDependencies, name, version },
3fcfac 19   lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
b54b79 20 };
2f6253 21
99ac30 22 export default ({ command, mode }: ConfigEnv): UserConfig => {
07c18d 23   const root = process.cwd();
V 24
bd7b53 25   const env = loadEnv(mode, root);
07c18d 26
V 27   // The boolean type read by loadEnv is a string. This function can be converted to boolean type
bd7b53 28   const viteEnv = wrapperEnv(env);
07c18d 29
b67cf2 30   const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
99ac30 31
V 32   const isBuild = command === 'build';
33
bd7b53 34   return {
f24422 35     base: VITE_PUBLIC_PATH,
bd7b53 36     root,
993538 37     resolve: {
3576d0 38       alias: [
6ef62b 39         {
V 40           find: 'vue-i18n',
41           replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
42         },
a9462f 43         // /@/xxxx => src/xxxx
3576d0 44         {
V 45           find: /\/@\//,
46           replacement: pathResolve('src') + '/',
47         },
a9462f 48         // /#/xxxx => types/xxxx
3576d0 49         {
V 50           find: /\/#\//,
51           replacement: pathResolve('types') + '/',
52         },
53       ],
993538 54     },
99ac30 55     server: {
4e4ce9 56       https: true,
4805ca 57       // Listening on all local IPs
58       host: true,
99ac30 59       port: VITE_PORT,
07c18d 60       // Load proxy configuration from .env
99ac30 61       proxy: createProxy(VITE_PROXY),
8a1bfd 62     },
ecc213 63     esbuild: {
V 64       pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
65     },
99ac30 66     build: {
ecc213 67       target: 'es2015',
V 68       cssTarget: 'chrome80',
759e53 69       outDir: OUTPUT_DIR,
ecc213 70       // minify: 'terser',
V 71       /**
72        * 当 minify=“minify:'terser'” 解开注释
73        * Uncomment when minify="minify:'terser'"
74        */
75       // terserOptions: {
76       //   compress: {
77       //     keep_infinity: true,
78       //     drop_console: VITE_DROP_CONSOLE,
79       //   },
80       // },
07c18d 81       // Turning off brotliSize display can slightly reduce packaging time
1b71db 82       brotliSize: false,
78191d 83       chunkSizeWarningLimit: 2000,
99ac30 84     },
bd7b53 85     define: {
d01836 86       // setting vue-i18-next
V 87       // Suppress warning
88       __INTLIFY_PROD_DEVTOOLS__: false,
c03056 89       __APP_INFO__: JSON.stringify(__APP_INFO__),
2f6253 90     },
9dd9fc 91
99ac30 92     css: {
V 93       preprocessorOptions: {
94         less: {
5b8eb4 95           modifyVars: generateModifyVars(),
99ac30 96           javascriptEnabled: true,
116a1f 97         },
bd7b53 98       },
V 99     },
100
07c18d 101     // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
V 102     plugins: createVitePlugins(viteEnv, isBuild),
8a1bfd 103
99ac30 104     optimizeDeps: {
07c18d 105       // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
c8e84d 106       include: [
e024f6 107         '@vue/runtime-core',
V 108         '@vue/shared',
c8e84d 109         '@iconify/iconify',
V 110         'ant-design-vue/es/locale/zh_CN',
111         'ant-design-vue/es/locale/en_US',
112       ],
bd7b53 113     },
V 114   };
6f8d75 115 };