uno.config.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * @Author: wjc
  3. * @Date: 2023-05-07 20:59:28
  4. * @LastEditors: LiZhiWei
  5. * @LastEditTime: 2026-01-19 15:56:48
  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. postprocess: (util) => {
  34. const pxRE = /(-?[\d.]+px)/g
  35. // 检查是否属于移动端变体(通过选择器或媒体查询父级判断)
  36. const isMobile =
  37. util.selector.includes('lt-sm') ||
  38. util.selector.includes('lt-xs') ||
  39. (util.parent?.includes('max-width: 767') ?? false) ||
  40. (util.parent?.includes('max-width: 479') ?? false)
  41. if (isMobile) {
  42. util.entries.forEach((i) => {
  43. const value = i[1]
  44. if (typeof value === 'string' && pxRE.test(value)) {
  45. // 移动端:强制按 750px 设计稿转换 (px / 7.5)
  46. i[1] = value.replace(pxRE, (match) => {
  47. const px = parseFloat(match)
  48. if (isNaN(px)) return match
  49. return `${(px / 7.5).toFixed(5)}vw`
  50. })
  51. }
  52. })
  53. }
  54. },
  55. rules: [
  56. [/^m-h-(.+)$/, ([, d]) => ({ 'margin-left': `${d}`, 'margin-right': `${d}` })],
  57. [/^m-v-(.+)$/, ([, d]) => ({ 'margin-top': `${d}`, 'margin-bottom': `${d}` })],
  58. [/^p-h-(.+)$/, ([, d]) => ({ 'padding-left': `${d}`, 'padding-right': `${d}` })],
  59. [/^p-v-(.+)$/, ([, d]) => ({ 'padding-top': `${d}`, 'padding-bottom': `${d}` })],
  60. [/^max-w-(\s+)$/, ([, d]) => ({ 'max-width': `${d}` })],
  61. [/^min-w-(\s+)$/, ([, d]) => ({ 'min-width': `${d}` })],
  62. [/^max-h-[(\s+)]$/, ([, d]) => ({ 'max-height': `${d}` })],
  63. [/^min-h-[(\s+)]$/, ([, d]) => ({ 'min-height': `${d}` })],
  64. [/^font-s-(\d+(\.\d+)?\w+)$/, ([, d]) => ({ 'font-size': `${d}` })],
  65. [/^wh-(.+)$/, ([, d]) => ({ width: `${d}`, height: `${d}` })],
  66. [/^bg-color-(\d+)$/, ([, d]) => ({ 'background-color': `${d}` })],
  67. ],
  68. safelist: iconList,
  69. shortcuts: {
  70. 'wh-full': 'w-full h-full',
  71. 'mp-0': 'm-0 p-0',
  72. 'ma-x': 'mt-0 mb-0 ml-auto mr-auto',
  73. 'inline-flex-center': 'inline-flex justify-center items-center',
  74. 'flex-center': 'flex justify-center items-center',
  75. 'flex-x-center': 'flex justify-center',
  76. 'flex-y-center': 'flex items-center',
  77. // 文本溢出显示省略号
  78. 'text-overflow': 'overflow-hidden whitespace-nowrap text-ellipsis',
  79. // 文本溢出换行
  80. 'text-break': 'whitespace-normal break-all break-words',
  81. },
  82. theme: {
  83. breakpoints: {
  84. xs: '480px',
  85. sm: '768px',
  86. md: '992px',
  87. lg: '1200px',
  88. xl: '1920px',
  89. xxl: '3840px',
  90. },
  91. colors: {
  92. primary: 'var(--primary-color)',
  93. 'primary-2': 'var(--primary-color-2)',
  94. success: 'var(--success-color)',
  95. warning: 'var(--warning-color)',
  96. danger: 'var(--danger-color)',
  97. error: 'var(--error-color)',
  98. info: 'var(--info-color)',
  99. color: {
  100. '1': 'var(--text-color-1)',
  101. '2': 'var(--text-color-2)',
  102. '3': 'var(--text-color-3)',
  103. 4: 'var(--bg-color-4)',
  104. place: 'var(--text-color-place)',
  105. disabled: 'var(--text-color-disabled)',
  106. whiter: 'var(--text-color-whiter)',
  107. },
  108. bg: {
  109. color: 'var(--bg-color)',
  110. 1: 'var(--bg-color-1)',
  111. 2: 'var(--bg-color-2)',
  112. 3: 'var(--bg-color-3)',
  113. 4: 'var(--bg-color-4)',
  114. page: 'var(--bg-page)',
  115. place: 'var(--text-color-place)',
  116. divider: 'var(--bg-divider)',
  117. overlay: 'var(--bg-overlay)',
  118. primary: 'var(--primary-color)',
  119. },
  120. },
  121. },
  122. })