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