vite.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { defineConfig, loadEnv } from 'vite';
  2. import vue from '@vitejs/plugin-vue';
  3. import AutoImport from 'unplugin-auto-import/vite'
  4. import Components from 'unplugin-vue-components/vite'
  5. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  6. import { resolve } from 'path'
  7. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  8. import fs from 'fs'
  9. import viteImagemin from 'vite-plugin-imagemin'
  10. // 使用unplugin-vue-components 按需导入的依赖项更新导致页面无限次重载问题
  11. const optimizeDepsElementPlusIncludes = ['element-plus/es'];
  12. fs.readdirSync('node_modules/element-plus/es/components').forEach((dirname) => {
  13. fs.access(
  14. `node_modules/element-plus/es/components/${dirname}/style/css.mjs`,
  15. (err) => {
  16. if (!err) {
  17. optimizeDepsElementPlusIncludes.push(
  18. `element-plus/es/components/${dirname}/style/css`
  19. );
  20. }
  21. }
  22. )
  23. })
  24. export default defineConfig(({ command, mode }) => {
  25. // 获取.env文件里定义的环境变量
  26. const env = loadEnv(mode, process.cwd());
  27. console.log(mode, command, env); // 变量在命令行里打印出来
  28. return {
  29. base: env.VITE_BASE_PREFIX,
  30. // root: resolve(__dirname, 'src'),
  31. // publicDir: resolve(__dirname, './public'),
  32. plugins: [
  33. vue(),
  34. viteImagemin({
  35. gifsicle: {
  36. optimizationLevel: 3, // GIF 压缩级别
  37. interlaced: true, // 是否生成隔行扫描的 GIF
  38. },
  39. optipng: {
  40. optimizationLevel: 5, // PNG 压缩级别
  41. },
  42. mozjpeg: {
  43. quality: 65, // JPEG 压缩质量
  44. },
  45. pngquant: {
  46. quality: [0.65, 0.90], // PNG 压缩质量范围
  47. speed: 4, // 压缩速度
  48. },
  49. svgo: true, // 是否启用 SVG 压缩
  50. webp: {
  51. quality: 75, // WebP 压缩质量
  52. },
  53. include: ['**/*.{jpg,jpeg,png,gif,svg,webp}'], // 包含哪些文件
  54. exclude: ['node_modules/**/*'], // 排除哪些文件
  55. filter(file) {
  56. // 自定义过滤函数,决定哪些文件应该被压缩
  57. return !file.includes('assets/imgs/no-compress');
  58. },
  59. }),
  60. AutoImport({
  61. resolvers: [ElementPlusResolver()],
  62. }),
  63. Components({
  64. resolvers: [ElementPlusResolver({
  65. importStyle: 'sass'
  66. })],
  67. }),
  68. createSvgIconsPlugin({
  69. // 指定需要缓存的图标文件夹
  70. iconDirs: [resolve(process.cwd(), 'src/assets/icon/svg')],
  71. // 指定symbolId格式
  72. symbolId: 'icon-[name]',
  73. })
  74. ],
  75. resolve: {
  76. alias: {
  77. '@': resolve(__dirname, 'src')
  78. },
  79. },
  80. server: {
  81. proxy: {
  82. '/api': {
  83. target: env.VITE_BASE_URL,
  84. changeOrigin: true,
  85. rewrite: path => path.replace(/^\/api/, ''),
  86. }
  87. },
  88. },
  89. define: {
  90. 'process.env': {}
  91. },
  92. css: {
  93. preprocessorOptions: {
  94. }
  95. },
  96. // 构建配置项
  97. build: {
  98. chunkSizeWarningLimit: 1500,
  99. rollupOptions: {
  100. input: {
  101. index: resolve(__dirname, 'index.html'),
  102. },
  103. output: {
  104. manualChunks (id) {
  105. if (id.includes('node_modules')) {
  106. return id
  107. .toString()
  108. .split('node_modules/')[1]
  109. .split('/')[0]
  110. .toString();
  111. }
  112. },
  113. },
  114. },
  115. },
  116. optimizeDeps: {
  117. include: optimizeDepsElementPlusIncludes
  118. }
  119. }
  120. })