uno.config.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. export default defineConfig({
  29. presets: [
  30. presetApplet({ enable: !isH5 }),
  31. presetRemRpx(),
  32. ...presets,
  33. // 支持图标,需要搭配图标库,eg: @iconify-json/carbon, 使用 `<button class="i-carbon-sun dark:i-carbon-moon" />`
  34. presetIcons({
  35. scale: 1.2,
  36. warn: true,
  37. extraProperties: {
  38. display: 'inline-block',
  39. 'vertical-align': 'middle',
  40. },
  41. }),
  42. ],
  43. /**
  44. * 自定义快捷语句
  45. * @see https://github.com/unocss/unocss#shortcuts
  46. */
  47. shortcuts: [['center', 'flex justify-center items-center']],
  48. transformers: [
  49. // 启用 @apply 功能
  50. transformerDirectives(),
  51. // 启用 () 分组功能
  52. // 支持css class组合,eg: `<div class="hover:(bg-gray-400 font-medium) font-(light mono)">测试 unocss</div>`
  53. transformerVariantGroup(),
  54. // Don't change the following order
  55. transformerAttributify({
  56. // 解决与第三方框架样式冲突问题
  57. prefixedOnly: true,
  58. prefix: 'fg',
  59. }),
  60. transformerApplet(),
  61. ],
  62. rules: [
  63. [
  64. 'p-safe',
  65. {
  66. padding:
  67. 'env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)',
  68. },
  69. ],
  70. ['pt-safe', { 'padding-top': 'env(safe-area-inset-top)' }],
  71. ['pb-safe', { 'padding-bottom': 'env(safe-area-inset-bottom)' }],
  72. ],
  73. })
  74. /**
  75. * 最终这一套组合下来会得到:
  76. * mp 里面:mt-4 => margin-top: 32rpx
  77. * h5 里面:mt-4 => margin-top: 1rem
  78. */