uno.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // uno.config.ts
  2. import {
  3. type Preset,
  4. defineConfig,
  5. presetUno,
  6. presetAttributify,
  7. presetIcons,
  8. transformerDirectives,
  9. transformerVariantGroup,
  10. } from 'unocss'
  11. import { presetApplet, presetRemRpx, transformerAttributify } from 'unocss-applet'
  12. // @see https://unocss.dev/presets/legacy-compat
  13. // import { presetLegacyCompat } from '@unocss/preset-legacy-compat'
  14. const isMp = process.env?.UNI_PLATFORM?.startsWith('mp') ?? false
  15. const presets: Preset[] = []
  16. if (isMp) {
  17. // 使用小程序预设
  18. presets.push(presetApplet(), presetRemRpx())
  19. } else {
  20. presets.push(
  21. // 非小程序用官方预设
  22. presetUno(),
  23. // 支持css class属性化
  24. presetAttributify(),
  25. )
  26. }
  27. export default defineConfig({
  28. presets: [
  29. ...presets,
  30. // 支持图标,需要搭配图标库,eg: @iconify-json/carbon, 使用 `<button class="i-carbon-sun dark:i-carbon-moon" />`
  31. presetIcons({
  32. scale: 1.2,
  33. warn: true,
  34. extraProperties: {
  35. display: 'inline-block',
  36. 'vertical-align': 'middle',
  37. },
  38. }),
  39. // 将颜色函数 (rgb()和hsl()) 从空格分隔转换为逗号分隔,更好的兼容性app端,example:
  40. // `rgb(255 0 0)` -> `rgb(255, 0, 0)`
  41. // `rgba(255 0 0 / 0.5)` -> `rgba(255, 0, 0, 0.5)`
  42. // 与群友的正常写法冲突,先去掉!(2024-05-25)
  43. // presetLegacyCompat({
  44. // commaStyleColorFunction: true,
  45. // }) as Preset,
  46. ],
  47. /**
  48. * 自定义快捷语句
  49. * @see https://github.com/unocss/unocss#shortcuts
  50. */
  51. shortcuts: [['center', 'flex justify-center items-center']],
  52. transformers: [
  53. // 启用 @apply 功能
  54. transformerDirectives(),
  55. // 启用 () 分组功能
  56. // 支持css class组合,eg: `<div class="hover:(bg-gray-400 font-medium) font-(light mono)">测试 unocss</div>`
  57. transformerVariantGroup(),
  58. // Don't change the following order
  59. transformerAttributify({
  60. // 解决与第三方框架样式冲突问题
  61. prefixedOnly: true,
  62. prefix: 'fg',
  63. }),
  64. ],
  65. theme: {
  66. colors: {
  67. primary: '#FFDC00',
  68. default: '#777777',
  69. bgc: '#010A29',
  70. cardlight: '#0B1B37',
  71. cards: '#17294A',
  72. textc: '#0E0E0E',
  73. },
  74. },
  75. rules: [
  76. [
  77. 'p-safe',
  78. {
  79. padding:
  80. 'env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)',
  81. },
  82. ],
  83. ['pt-safe', { 'padding-top': 'env(safe-area-inset-top)' }],
  84. ['pb-safe', { 'padding-bottom': 'env(safe-area-inset-bottom)' }],
  85. [
  86. /^bg-custom-(.+)-(.+)-(.+)$/,
  87. ([, img, size, path]) => ({
  88. 'background-image': `url('@/static/images/${path}/${img}')`,
  89. 'background-repeat': 'no-repeat',
  90. 'background-size': size.replace('_', ' '),
  91. }),
  92. ],
  93. ],
  94. })
  95. /**
  96. * 最终这一套组合下来会得到:
  97. * mp 里面:mt-4 => margin-top: 32rpx == 16px
  98. * h5 里面:mt-4 => margin-top: 1rem == 16px
  99. *
  100. * 如果是传统方式写样式,则推荐设计稿设置为 750,这样设计稿1px,代码写1rpx。
  101. * rpx是响应式的,可以让不同设备的屏幕显示效果保持一致。
  102. */