Vben
2021-04-10 2037293aa3b38ad8dc9bee4324647cb1484c1a9a
提交 | 用户 | 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: [
a9462f 42         // /@/xxxx => src/xxxx
3576d0 43         {
V 44           find: /\/@\//,
45           replacement: pathResolve('src') + '/',
46         },
a9462f 47         // /#/xxxx => types/xxxx
3576d0 48         {
V 49           find: /\/#\//,
50           replacement: pathResolve('types') + '/',
51         },
52         // ['@vue/compiler-sfc', '@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js'],
53       ],
993538 54     },
99ac30 55     server: {
V 56       port: VITE_PORT,
07c18d 57       // Load proxy configuration from .env
99ac30 58       proxy: createProxy(VITE_PROXY),
8a1bfd 59     },
99ac30 60     build: {
e09068 61       target: 'es2015',
759e53 62       outDir: OUTPUT_DIR,
99ac30 63       terserOptions: {
V 64         compress: {
65           keep_infinity: true,
07c18d 66           // Used to delete console in production environment
99ac30 67           drop_console: VITE_DROP_CONSOLE,
V 68         },
69       },
07c18d 70       // Turning off brotliSize display can slightly reduce packaging time
1b71db 71       brotliSize: false,
edc752 72       chunkSizeWarningLimit: 1500,
99ac30 73     },
bd7b53 74     define: {
V 75       // setting vue-i18-next
76       // Suppress warning
77       __VUE_I18N_LEGACY_API__: false,
78       __VUE_I18N_FULL_INSTALL__: false,
79       __INTLIFY_PROD_DEVTOOLS__: false,
b54b79 80
c03056 81       __APP_INFO__: JSON.stringify(__APP_INFO__),
2f6253 82     },
99ac30 83     css: {
V 84       preprocessorOptions: {
85         less: {
5b8eb4 86           modifyVars: generateModifyVars(),
99ac30 87           javascriptEnabled: true,
116a1f 88         },
bd7b53 89       },
V 90     },
91
07c18d 92     // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
V 93     plugins: createVitePlugins(viteEnv, isBuild),
8a1bfd 94
99ac30 95     optimizeDeps: {
07c18d 96       // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
c8e84d 97       include: [
V 98         '@iconify/iconify',
99         'ant-design-vue/es/locale/zh_CN',
100         'moment/dist/locale/zh-cn',
101         'ant-design-vue/es/locale/en_US',
102         'moment/dist/locale/eu',
103       ],
8a1406 104       exclude: ['vue-demi'],
bd7b53 105     },
V 106   };
6f8d75 107 };