vite.config.ts 4.4 KB

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