vite.config.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import path from 'node:path'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import Uni from '@dcloudio/vite-plugin-uni'
  4. import UniPages from '@uni-helper/vite-plugin-uni-pages'
  5. import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
  6. import dayjs from 'dayjs'
  7. import svgLoader from 'vite-svg-loader'
  8. import { visualizer } from 'rollup-plugin-visualizer'
  9. import ViteRestart from 'vite-plugin-restart'
  10. import AutoImport from 'unplugin-auto-import/vite'
  11. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  12. import viteCompression from 'vite-plugin-compression'
  13. import viteImagemin from 'vite-plugin-imagemin'
  14. import vueSetupExtend from 'vite-plugin-vue-setup-extend'
  15. import UnoCSS from 'unocss/vite'
  16. import autoprefixer from 'autoprefixer'
  17. const htmlPlugin = (title: string) => {
  18. return {
  19. name: 'html-transform',
  20. transformIndexHtml(html) {
  21. return html
  22. .replace(/<title>(.*?)<\/title>/, `<title>${title}</title>`)
  23. .replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
  24. },
  25. }
  26. }
  27. // https://vitejs.dev/config/
  28. export default ({ mode }) => {
  29. // mode: 区分生产环境还是开发环境
  30. // process.cwd(): 获取当前文件的目录跟地址
  31. // loadEnv(): 返回当前环境env文件中额外定义的变量
  32. const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
  33. console.log(env)
  34. return defineConfig({
  35. plugins: [
  36. // https://github.com/uni-helper/vite-plugin-uni-pages
  37. UniPages(),
  38. // https://github.com/uni-helper/vite-plugin-uni-layouts
  39. UniLayouts(),
  40. Uni(),
  41. UnoCSS(),
  42. htmlPlugin(env.VITE_APP_TITLE),
  43. svgLoader(),
  44. // 打包分析插件
  45. visualizer({
  46. filename: './node_modules/.cache/visualizer/stats.html',
  47. open: true,
  48. gzipSize: true,
  49. brotliSize: true,
  50. }),
  51. ViteRestart({
  52. // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
  53. restart: ['vite.config.js'],
  54. }),
  55. vueSetupExtend(),
  56. AutoImport({
  57. imports: ['vue'],
  58. dts: 'src/auto-import.d.ts',
  59. }),
  60. createSvgIconsPlugin({
  61. // 指定要缓存的文件夹
  62. iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
  63. // 指定symbolId格式
  64. symbolId: 'icon-[dir]-[name]',
  65. }),
  66. viteCompression(), // 会多出一些.gz文件,如xxx.js.gz,这里默认是不会删除xxx.js文件的,如果想删除也可以增加配置
  67. // 这个图片压缩插件比较耗时,希望仅在生产环境使用
  68. viteImagemin({
  69. gifsicle: {
  70. // gif图片压缩
  71. optimizationLevel: 3, // 选择1到3之间的优化级别
  72. interlaced: false, // 隔行扫描gif进行渐进式渲染
  73. // colors: 2 // 将每个输出GIF中不同颜色的数量减少到num或更少。数字必须介于2和256之间。
  74. },
  75. optipng: {
  76. // png
  77. optimizationLevel: 7, // 选择0到7之间的优化级别
  78. },
  79. mozjpeg: {
  80. // jpeg
  81. quality: 20, // 压缩质量,范围从0(最差)到100(最佳)。
  82. },
  83. pngquant: {
  84. // png
  85. quality: [0.8, 0.9], // Min和max是介于0(最差)到1(最佳)之间的数字,类似于JPEG。达到或超过最高质量所需的最少量的颜色。如果转换导致质量低于最低质量,图像将不会被保存。
  86. speed: 4, // 压缩速度,1(强力)到11(最快)
  87. },
  88. svgo: {
  89. // svg压缩
  90. plugins: [
  91. {
  92. name: 'removeViewBox',
  93. },
  94. {
  95. name: 'removeEmptyAttrs',
  96. active: false,
  97. },
  98. ],
  99. },
  100. }),
  101. ],
  102. css: {
  103. postcss: {
  104. plugins: [
  105. autoprefixer({
  106. // 指定目标浏览器
  107. overrideBrowserslist: ['> 1%', 'last 2 versions'],
  108. }),
  109. ],
  110. },
  111. },
  112. resolve: {
  113. alias: {
  114. '@': path.join(process.cwd(), './src'),
  115. },
  116. },
  117. server: {
  118. host: '0.0.0.0',
  119. hmr: true,
  120. port: Number.parseInt(env.VITE_APP_PORT, 10),
  121. // 自定义代理规则
  122. proxy: {
  123. // 选项写法
  124. '/api': {
  125. target: 'http://localhost:6666',
  126. changeOrigin: true,
  127. rewrite: (path) => path.replace(/^\/api/, ''),
  128. },
  129. },
  130. },
  131. build: {
  132. minify: 'terser',
  133. terserOptions: {
  134. compress: {
  135. drop_console: env.VITE_DELETE_CONSOLE === 'true',
  136. drop_debugger: env.VITE_DELETE_CONSOLE === 'true',
  137. },
  138. },
  139. },
  140. })
  141. }