vite.config.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // TODO: 很多用户无法安装这个插件所以先注释掉了,如果你可以安装成功,那就可以放开这个注释,以及下面的viteImagemin配置
  28. // 另外,小程序有主包2M的限制,所以一般图片会放到图片服务器(不放本地),那这个插件就没用,所以在开发h5的时候,使用本地图片才用得到,既然如此那就不装吧
  29. // import viteImagemin from 'vite-plugin-imagemin'
  30. // 微信生产环境、本地真机调试不要demo路由,本地开发可以有;
  31. // TODO: 下面2个根据使用条件选一个即可
  32. export const hideDemoPages = process.env.UNI_PLATFORM === 'mp-weixin'
  33. // export const hideDemoPages =
  34. // process.env.UNI_PLATFORM === 'mp-weixin' && process.env.NODE_ENV === 'production'
  35. // https://vitejs.dev/config/
  36. export default ({ command, mode }) => {
  37. console.log(mode === process.env.NODE_ENV)
  38. // mode: 区分生产环境还是开发环境
  39. console.log(command, mode)
  40. // pnpm dev:h5 时得到 => serve development
  41. // pnpm build:h5 时得到 => build development
  42. // pnpm dev:mp-weixin 时得到 => build development (注意区别,command为build)
  43. // pnpm build:mp-weixin 时得到 => build production
  44. // process.cwd(): 获取当前文件的目录跟地址
  45. // loadEnv(): 返回当前环境env文件中额外定义的变量
  46. const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
  47. // console.log(env)
  48. console.log(process.env.UNI_PLATFORM) // 得到 mp-weixin, h5 等
  49. return defineConfig({
  50. envDir: './env', // 自定义env目录
  51. plugins: [
  52. UniPages({
  53. // TODO: 生产环境小程序要过滤掉demo(demo里面很多图片,超过2M的包大小)
  54. exclude: hideDemoPages
  55. ? ['**/components/**/**.*', '**/demo/**/**.*']
  56. : ['**/components/**/**.*'],
  57. }),
  58. UniLayouts(),
  59. UniPlatform(),
  60. UniManifest(),
  61. // UniXXX 需要在 Uni 之前引入
  62. Uni(),
  63. UnoCSS(),
  64. // svg 可以当做组件来使用(Vite plugin to load SVG files as Vue components, using SVGO for optimization.)
  65. svgLoader(),
  66. createSvgIconsPlugin({
  67. // 指定要缓存的文件夹
  68. iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
  69. // 指定symbolId格式
  70. symbolId: 'icon-[dir]-[name]',
  71. }),
  72. vueSetupExtend(),
  73. AutoImport({
  74. imports: ['vue'],
  75. dts: 'src/auto-import.d.ts',
  76. }),
  77. viteCompression(),
  78. ViteRestart({
  79. // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
  80. restart: ['vite.config.js'],
  81. }),
  82. // h5环境增加编译时间
  83. process.env.UNI_PLATFORM === 'h5' && {
  84. name: 'html-transform',
  85. transformIndexHtml(html) {
  86. return html.replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
  87. },
  88. },
  89. // 打包分析插件
  90. mode === 'production' &&
  91. visualizer({
  92. filename: './node_modules/.cache/visualizer/stats.html',
  93. open: true,
  94. gzipSize: true,
  95. brotliSize: true,
  96. }),
  97. // 这个图片压缩插件比较耗时,希望仅在生产环境使用
  98. // mode === 'production' &&
  99. // viteImagemin({
  100. // gifsicle: {
  101. // // gif图片压缩
  102. // optimizationLevel: 3, // 选择1到3之间的优化级别
  103. // interlaced: false, // 隔行扫描gif进行渐进式渲染
  104. // // colors: 2 // 将每个输出GIF中不同颜色的数量减少到num或更少。数字必须介于2和256之间。
  105. // },
  106. // optipng: {
  107. // // png
  108. // optimizationLevel: 7, // 选择0到7之间的优化级别
  109. // },
  110. // mozjpeg: {
  111. // // jpeg
  112. // quality: 20, // 压缩质量,范围从0(最差)到100(最佳)。
  113. // },
  114. // pngquant: {
  115. // // png
  116. // quality: [0.8, 0.9], // Min和max是介于0(最差)到1(最佳)之间的数字,类似于JPEG。达到或超过最高质量所需的最少量的颜色。如果转换导致质量低于最低质量,图像将不会被保存。
  117. // speed: 4, // 压缩速度,1(强力)到11(最快)
  118. // },
  119. // svgo: {
  120. // // svg压缩
  121. // plugins: [
  122. // {
  123. // name: 'removeViewBox',
  124. // },
  125. // {
  126. // name: 'removeEmptyAttrs',
  127. // active: false,
  128. // },
  129. // ],
  130. // },
  131. // }),
  132. ],
  133. css: {
  134. postcss: {
  135. plugins: [
  136. autoprefixer({
  137. // 指定目标浏览器
  138. overrideBrowserslist: ['> 1%', 'last 2 versions'],
  139. }),
  140. ],
  141. },
  142. },
  143. resolve: {
  144. alias: {
  145. '@': path.join(process.cwd(), './src'),
  146. },
  147. },
  148. server: {
  149. host: '0.0.0.0',
  150. hmr: true,
  151. port: Number.parseInt(env.VITE_APP_PORT, 10),
  152. // 自定义代理规则
  153. proxy: {
  154. // 选项写法
  155. '/api': {
  156. target: 'http://localhost:6666',
  157. changeOrigin: true,
  158. rewrite: (path) => path.replace(/^\/api/, ''),
  159. },
  160. },
  161. },
  162. build: {
  163. minify: 'terser',
  164. terserOptions: {
  165. compress: {
  166. drop_console: env.VITE_DELETE_CONSOLE === 'true',
  167. drop_debugger: env.VITE_DELETE_CONSOLE === 'true',
  168. },
  169. },
  170. },
  171. })
  172. }