vite.config.ts 5.1 KB

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