vite.config.ts 5.2 KB

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