vite.config.ts 4.9 KB

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