vite.config.ts 5.2 KB

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