uno.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. presetLegacyCompat({
  43. commaStyleColorFunction: true,
  44. }) as Preset,
  45. ],
  46. /**
  47. * 自定义快捷语句
  48. * @see https://github.com/unocss/unocss#shortcuts
  49. */
  50. shortcuts: [['center', 'flex justify-center items-center']],
  51. transformers: [
  52. // 启用 @apply 功能
  53. transformerDirectives(),
  54. // 启用 () 分组功能
  55. // 支持css class组合,eg: `<div class="hover:(bg-gray-400 font-medium) font-(light mono)">测试 unocss</div>`
  56. transformerVariantGroup(),
  57. // Don't change the following order
  58. transformerAttributify({
  59. // 解决与第三方框架样式冲突问题
  60. prefixedOnly: true,
  61. prefix: 'fg',
  62. }),
  63. ],
  64. rules: [
  65. [
  66. 'p-safe',
  67. {
  68. padding:
  69. 'env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)',
  70. },
  71. ],
  72. ['pt-safe', { 'padding-top': 'env(safe-area-inset-top)' }],
  73. ['pb-safe', { 'padding-bottom': 'env(safe-area-inset-bottom)' }],
  74. ],
  75. })
  76. /**
  77. * 最终这一套组合下来会得到:
  78. * mp 里面:mt-4 => margin-top: 32rpx == 16px
  79. * h5 里面:mt-4 => margin-top: 1rem == 16px
  80. *
  81. * 另外,我们还可以推算出 UnoCSS 单位与设计稿差别4倍。
  82. * 375 * 4 = 1500,把设计稿设置为1500,那么设计稿里多少px,unocss就写多少述职。
  83. * 举个例子,设计稿显示某元素宽度100px,就写w-100即可。
  84. *
  85. * 如果是传统方式写样式,则推荐设计稿设置为 750,这样设计稿1px,代码写1rpx。
  86. * rpx是响应式的,可以让不同设备的屏幕显示效果保持一致。
  87. */