vite.config.ts 5.7 KB

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