uno.config.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * @Author: wjc
  3. * @Date: 2023-05-07 20:59:28
  4. * @LastEditors: LiZhiWei
  5. * @LastEditTime: 2026-01-20 16:03:07
  6. * @Description:
  7. */
  8. import {
  9. defineConfig,
  10. presetUno,
  11. presetIcons,
  12. transformerDirectives,
  13. transformerVariantGroup,
  14. } from 'unocss'
  15. import { FileSystemIconLoader } from '@iconify/utils/lib/loader/node-loaders'
  16. import iconList from './app/utils/icons'
  17. export default defineConfig({
  18. presets: [
  19. presetUno(),
  20. presetIcons({
  21. scale: 1.2,
  22. extraProperties: {
  23. display: 'inline-block',
  24. },
  25. // cdn: 'https://esm.sh/'
  26. collections: {
  27. custom: FileSystemIconLoader('./app/assets/icons'),
  28. // ep: () => import('@iconify-json/ep/icons.json').then((i) => i.default),
  29. },
  30. }),
  31. ],
  32. transformers: [transformerDirectives(), transformerVariantGroup()],
  33. content: {
  34. pipeline: {
  35. include: [/\.(vue|tsx?|jsx?|mdx?|html)($|\?)/],
  36. },
  37. filesystem: ['app/**/*.{vue,ts,tsx,js,jsx}'],
  38. },
  39. postprocess: (util) => {
  40. const pxRE = /(-?[\d.]+px)/g
  41. // 增强安全性判断,确保在构建扫描阶段不会因 selector 或 parent 为空而报错
  42. const selector = util.selector || ''
  43. const parent = typeof util.parent === 'string' ? util.parent : ''
  44. const isMobile =
  45. selector.includes('lt-sm') ||
  46. selector.includes('lt-xs') ||
  47. parent.includes('max-width: 767.9px') ||
  48. parent.includes('max-width: 479.9px')
  49. if (isMobile) {
  50. util.entries.forEach((i) => {
  51. const value = i[1]
  52. if (typeof value === 'string' && pxRE.test(value)) {
  53. // 移动端:强制按 750px 设计稿转换 (px / 7.5)
  54. i[1] = value.replace(pxRE, (match) => {
  55. const px = parseFloat(match)
  56. if (isNaN(px)) return match
  57. return `${(px / 7.5).toFixed(5)}vw`
  58. })
  59. }
  60. })
  61. }
  62. },
  63. rules: [
  64. [/^m-h-(.+)$/, ([, d]) => ({ 'margin-left': `${d}`, 'margin-right': `${d}` })],
  65. [/^m-v-(.+)$/, ([, d]) => ({ 'margin-top': `${d}`, 'margin-bottom': `${d}` })],
  66. [/^p-h-(.+)$/, ([, d]) => ({ 'padding-left': `${d}`, 'padding-right': `${d}` })],
  67. [/^p-v-(.+)$/, ([, d]) => ({ 'padding-top': `${d}`, 'padding-bottom': `${d}` })],
  68. [/^max-w-(.+)$/, ([, d]) => ({ 'max-width': `${d}` })],
  69. [/^min-w-(.+)$/, ([, d]) => ({ 'min-width': `${d}` })],
  70. [/^max-h-(.+)$/, ([, d]) => ({ 'max-height': `${d}` })],
  71. [/^min-h-(.+)$/, ([, d]) => ({ 'min-height': `${d}` })],
  72. [/^font-s-(\d+(\.\d+)?\w+)$/, ([, d]) => ({ 'font-size': `${d}` })],
  73. [/^wh-(.+)$/, ([, d]) => ({ width: `${d}`, height: `${d}` })],
  74. [/^bg-color-(\d+)$/, ([, d]) => ({ 'background-color': `${d}` })],
  75. ],
  76. safelist: iconList,
  77. shortcuts: {
  78. 'wh-full': 'w-full h-full',
  79. 'mp-0': 'm-0 p-0',
  80. 'ma-x': 'mt-0 mb-0 ml-auto mr-auto',
  81. 'inline-flex-center': 'inline-flex justify-center items-center',
  82. 'flex-center': 'flex justify-center items-center',
  83. 'flex-x-center': 'flex justify-center',
  84. 'flex-y-center': 'flex items-center',
  85. // 文本溢出显示省略号
  86. 'text-overflow': 'overflow-hidden whitespace-nowrap text-ellipsis',
  87. // 文本溢出换行
  88. 'text-break': 'whitespace-normal break-all break-words',
  89. },
  90. theme: {
  91. breakpoints: {
  92. xs: '480px',
  93. sm: '768px',
  94. md: '992px',
  95. lg: '1200px',
  96. xl: '1920px',
  97. xxl: '3840px',
  98. },
  99. colors: {
  100. primary: 'var(--primary-color)',
  101. 'primary-2': 'var(--primary-color-2)',
  102. success: 'var(--success-color)',
  103. warning: 'var(--warning-color)',
  104. danger: 'var(--danger-color)',
  105. error: 'var(--error-color)',
  106. info: 'var(--info-color)',
  107. color: {
  108. '1': 'var(--text-color-1)',
  109. '2': 'var(--text-color-2)',
  110. '3': 'var(--text-color-3)',
  111. 4: 'var(--bg-color-4)',
  112. place: 'var(--text-color-place)',
  113. disabled: 'var(--text-color-disabled)',
  114. whiter: 'var(--text-color-whiter)',
  115. },
  116. bg: {
  117. color: 'var(--bg-color)',
  118. 1: 'var(--bg-color-1)',
  119. 2: 'var(--bg-color-2)',
  120. 3: 'var(--bg-color-3)',
  121. 4: 'var(--bg-color-4)',
  122. page: 'var(--bg-page)',
  123. place: 'var(--text-color-place)',
  124. divider: 'var(--bg-divider)',
  125. overlay: 'var(--bg-overlay)',
  126. primary: 'var(--primary-color)',
  127. },
  128. },
  129. },
  130. })