uno.config.ts 2.6 KB

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