vite.config.ts 4.7 KB

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