vben
2021-05-27 566280422de0537c4e31496eaaa95a9d51fe9458
提交 | 用户 | 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: {
V 60       port: VITE_PORT,
07c18d 61       // Load proxy configuration from .env
99ac30 62       proxy: createProxy(VITE_PROXY),
8a1bfd 63     },
99ac30 64     build: {
e09068 65       target: 'es2015',
759e53 66       outDir: OUTPUT_DIR,
99ac30 67       terserOptions: {
V 68         compress: {
69           keep_infinity: true,
07c18d 70           // Used to delete console in production environment
99ac30 71           drop_console: VITE_DROP_CONSOLE,
V 72         },
73       },
07c18d 74       // Turning off brotliSize display can slightly reduce packaging time
1b71db 75       brotliSize: false,
78191d 76       chunkSizeWarningLimit: 2000,
99ac30 77     },
bd7b53 78     define: {
d01836 79       // setting vue-i18-next
V 80       // Suppress warning
81       __INTLIFY_PROD_DEVTOOLS__: false,
c03056 82       __APP_INFO__: JSON.stringify(__APP_INFO__),
2f6253 83     },
99ac30 84     css: {
V 85       preprocessorOptions: {
86         less: {
5b8eb4 87           modifyVars: generateModifyVars(),
99ac30 88           javascriptEnabled: true,
116a1f 89         },
bd7b53 90       },
V 91     },
92
07c18d 93     // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
V 94     plugins: createVitePlugins(viteEnv, isBuild),
8a1bfd 95
99ac30 96     optimizeDeps: {
07c18d 97       // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
c8e84d 98       include: [
V 99         '@iconify/iconify',
100         'ant-design-vue/es/locale/zh_CN',
101         'moment/dist/locale/zh-cn',
102         'ant-design-vue/es/locale/en_US',
103         'moment/dist/locale/eu',
104       ],
8a1406 105       exclude: ['vue-demi'],
bd7b53 106     },
V 107   };
6f8d75 108 };