vite.config.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. filename: './node_modules/.cache/visualizer/stats.html',
  54. open: true,
  55. gzipSize: true,
  56. brotliSize: true,
  57. }),
  58. ViteRestart({
  59. // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
  60. restart: ['vite.config.js'],
  61. }),
  62. vueSetupExtend(),
  63. // 原先引用组件的时候需要在目标文件里面import相关组件,现在就可以直接使用无需在目标文件import了
  64. Components({
  65. dirs: ['src/components'], // 目标文件夹
  66. extensions: ['vue'], // 文件类型
  67. dts: 'src/components.d.ts', // 输出文件,里面都是一些import的组件键值对
  68. // ui库解析器,也可以自定义,需要安装相关UI库
  69. resolvers: [
  70. // VantResolver(),
  71. // ElementPlusResolver(),
  72. // AntDesignVueResolver(),
  73. // HeadlessUiResolver(),
  74. // ElementUiResolver()
  75. ],
  76. }),
  77. AutoImport({
  78. imports: ['vue'],
  79. dts: 'src/auto-import.d.ts',
  80. }),
  81. createSvgIconsPlugin({
  82. // 指定要缓存的文件夹
  83. iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
  84. // 指定symbolId格式
  85. symbolId: 'icon-[dir]-[name]',
  86. }),
  87. viteCompression(), // 会多出一些.gz文件,如xxx.js.gz,这里默认是不会删除xxx.js文件的,如果想删除也可以增加配置
  88. // 这个图片压缩插件比较耗时,希望仅在生产环境使用
  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. }