uno.config.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // uno.config.ts
  2. import {
  3. Preset,
  4. defineConfig,
  5. presetAttributify,
  6. presetIcons,
  7. transformerDirectives,
  8. transformerVariantGroup,
  9. } from 'unocss'
  10. import {
  11. presetApplet,
  12. presetRemRpx,
  13. transformerApplet,
  14. transformerAttributify,
  15. } from 'unocss-applet'
  16. // @see https://unocss.dev/presets/legacy-compat
  17. import presetLegacyCompat from '@unocss/preset-legacy-compat'
  18. const isH5 = process.env?.UNI_PLATFORM === 'h5'
  19. const isMp = process.env?.UNI_PLATFORM?.startsWith('mp') ?? false
  20. const presets: Preset[] = []
  21. if (!isMp) {
  22. /**
  23. * you can add `presetAttributify()` here to enable unocss attributify mode prompt
  24. * although preset is not working for applet, but will generate useless css
  25. * 为了不生产无用的css,要过滤掉 applet
  26. */
  27. // 支持css class属性化,eg: `<button bg="blue-400 hover:blue-500 dark:blue-500 dark:hover:blue-600" text="sm white">attributify Button</button>`
  28. presets.push(presetAttributify())
  29. }
  30. if (!isH5) {
  31. presets.push(presetRemRpx())
  32. }
  33. export default defineConfig({
  34. presets: [
  35. presetApplet({ enable: !isH5 }),
  36. ...presets,
  37. // 支持图标,需要搭配图标库,eg: @iconify-json/carbon, 使用 `<button class="i-carbon-sun dark:i-carbon-moon" />`
  38. presetIcons({
  39. scale: 1.2,
  40. warn: true,
  41. extraProperties: {
  42. display: 'inline-block',
  43. 'vertical-align': 'middle',
  44. },
  45. }),
  46. // 将颜色函数 (rgb()和hsl()) 从空格分隔转换为逗号分隔,更好的兼容性app端,example:
  47. // `rgb(255 0 0)` -> `rgb(255, 0, 0)`
  48. // `rgba(255 0 0 / 0.5)` -> `rgba(255, 0, 0, 0.5)`
  49. presetLegacyCompat({
  50. commaStyleColorFunction: true,
  51. }) as Preset,
  52. ],
  53. /**
  54. * 自定义快捷语句
  55. * @see https://github.com/unocss/unocss#shortcuts
  56. */
  57. shortcuts: [['center', 'flex justify-center items-center']],
  58. transformers: [
  59. // 启用 @apply 功能
  60. transformerDirectives(),
  61. // 启用 () 分组功能
  62. // 支持css class组合,eg: `<div class="hover:(bg-gray-400 font-medium) font-(light mono)">测试 unocss</div>`
  63. transformerVariantGroup(),
  64. // Don't change the following order
  65. transformerAttributify({
  66. // 解决与第三方框架样式冲突问题
  67. prefixedOnly: true,
  68. prefix: 'fg',
  69. }),
  70. transformerApplet(),
  71. ],
  72. rules: [
  73. [
  74. 'p-safe',
  75. {
  76. padding:
  77. 'env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)',
  78. },
  79. ],
  80. ['pt-safe', { 'padding-top': 'env(safe-area-inset-top)' }],
  81. ['pb-safe', { 'padding-bottom': 'env(safe-area-inset-bottom)' }],
  82. ],
  83. })
  84. /**
  85. * 最终这一套组合下来会得到:
  86. * mp 里面:mt-4 => margin-top: 32rpx
  87. * h5 里面:mt-4 => margin-top: 1rem
  88. */