uno.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. const isH5 = process.env?.UNI_PLATFORM === 'h5'
  17. const isMp = process.env?.UNI_PLATFORM?.startsWith('mp') ?? false
  18. const presets: Preset[] = []
  19. if (!isMp) {
  20. /**
  21. * you can add `presetAttributify()` here to enable unocss attributify mode prompt
  22. * although preset is not working for applet, but will generate useless css
  23. * 为了不生产无用的css,要过滤掉 applet
  24. */
  25. // 支持css class属性化,eg: `<button bg="blue-400 hover:blue-500 dark:blue-500 dark:hover:blue-600" text="sm white">attributify Button</button>`
  26. presets.push(presetAttributify())
  27. }
  28. if (!isH5) {
  29. presets.push(presetRemRpx())
  30. }
  31. export default defineConfig({
  32. presets: [
  33. presetApplet({ enable: !isH5 }),
  34. ...presets,
  35. // 支持图标,需要搭配图标库,eg: @iconify-json/carbon, 使用 `<button class="i-carbon-sun dark:i-carbon-moon" />`
  36. presetIcons({
  37. scale: 1.2,
  38. warn: true,
  39. extraProperties: {
  40. display: 'inline-block',
  41. 'vertical-align': 'middle',
  42. },
  43. }),
  44. ],
  45. /**
  46. * 自定义快捷语句
  47. * @see https://github.com/unocss/unocss#shortcuts
  48. */
  49. shortcuts: [['center', 'flex justify-center items-center']],
  50. transformers: [
  51. // 启用 @apply 功能
  52. transformerDirectives(),
  53. // 启用 () 分组功能
  54. // 支持css class组合,eg: `<div class="hover:(bg-gray-400 font-medium) font-(light mono)">测试 unocss</div>`
  55. transformerVariantGroup(),
  56. // Don't change the following order
  57. transformerAttributify({
  58. // 解决与第三方框架样式冲突问题
  59. prefixedOnly: true,
  60. prefix: 'fg',
  61. }),
  62. transformerApplet(),
  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. */