uno.config.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  79. * h5 里面:mt-4 => margin-top: 1rem
  80. */