vite.config.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { resolve } from 'path'
  2. import { defineConfig } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import UnoCSS from 'unocss/vite'
  5. import config from './src/config'
  6. import AutoImport from 'unplugin-auto-import/vite'
  7. import Components from 'unplugin-vue-components/vite'
  8. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  9. // https://vitejs.dev/config/
  10. export default defineConfig({
  11. resolve: {
  12. alias: {
  13. '~src': `${resolve(__dirname, './src')}`,
  14. '~com': `${resolve(__dirname, './src/components')}`,
  15. '~api': resolve(__dirname, './src/api/modules'),
  16. },
  17. },
  18. plugins: [
  19. vue(),
  20. UnoCSS(),
  21. AutoImport({
  22. resolvers: [ElementPlusResolver()],
  23. }),
  24. Components({
  25. resolvers: [ElementPlusResolver()],
  26. }),
  27. ],
  28. build: {
  29. rollupOptions: {
  30. output: {
  31. manualChunks(id) {
  32. let libStr = '@vue,element-plus,echarts,vue-router,pinia,js-fast-way'
  33. const libs = libStr.split(',')
  34. if (id.includes('node_modules')) {
  35. const arr = id.toString().split('node_modules/')[1].split('/')
  36. if (libs.indexOf(arr[0]) !== -1) {
  37. return '_' + arr[0]
  38. } else {
  39. return '__vendor'
  40. }
  41. }
  42. },
  43. chunkFileNames: 'static/js1/[name]-[hash].js',
  44. entryFileNames: 'static/js2/[name]-[hash].js',
  45. assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
  46. },
  47. brotliSize: false, // 不统计
  48. target: 'esnext',
  49. minify: 'esbuild', // 混淆器,terser构建后文件体积更小
  50. },
  51. },
  52. server: {
  53. ...config.vite,
  54. proxy: {
  55. '/api': {
  56. ws: true,
  57. changeOrigin: true,
  58. target: config.target,
  59. rewrite: (path) => path.replace(new RegExp('^/api'), '/'),
  60. },
  61. },
  62. },
  63. })