uno.config.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // uno.config.ts
  2. import {
  3. type Preset,
  4. defineConfig,
  5. presetUno,
  6. presetAttributify,
  7. presetIcons,
  8. transformerDirectives,
  9. transformerVariantGroup,
  10. SourceCodeTransformer,
  11. cssIdRE,
  12. } from 'unocss'
  13. import { presetApplet, presetRemRpx, transformerAttributify } from 'unocss-applet'
  14. // const common = require('./scripts/common.js')
  15. // @see https://unocss.dev/presets/legacy-compat
  16. // import { presetLegacyCompat } from '@unocss/preset-legacy-compat'
  17. const isMp = process.env?.UNI_PLATFORM?.startsWith('mp') ?? false
  18. const presets: Preset[] = []
  19. if (isMp) {
  20. // 使用小程序预设
  21. presets.push(presetApplet(), presetRemRpx())
  22. } else {
  23. presets.push(
  24. // 非小程序用官方预设
  25. presetUno({
  26. transition: '',
  27. }),
  28. // 支持css class属性化
  29. presetAttributify(),
  30. )
  31. }
  32. // const index = 0
  33. /** 性能优化,
  34. * 去掉unocss全局变量,解决浏览器调试卡顿问题
  35. */
  36. function optimization(): SourceCodeTransformer {
  37. return {
  38. name: 'my-transformer',
  39. enforce: 'pre', // enforce before other transformers
  40. idFilter(id) {
  41. // 样式文件
  42. return cssIdRE.test(id)
  43. },
  44. async transform(code, id, { uno }) {
  45. // common.files.write(common.files.getPath(`temp/${index++}.html`), code.toString())
  46. code.replace(/\/\* layer: preflights \*\/[\s\S]+un-backdrop-sepia: ;\}\n/, '')
  47. },
  48. }
  49. }
  50. export default defineConfig({
  51. presets: [
  52. ...presets,
  53. // 支持图标,需要搭配图标库,eg: @iconify-json/carbon, 使用 `<button class="i-carbon-sun dark:i-carbon-moon" />`
  54. presetIcons({
  55. scale: 1.2,
  56. warn: true,
  57. extraProperties: {
  58. display: 'inline-block',
  59. 'vertical-align': 'middle',
  60. },
  61. }),
  62. // 将颜色函数 (rgb()和hsl()) 从空格分隔转换为逗号分隔,更好的兼容性app端,example:
  63. // `rgb(255 0 0)` -> `rgb(255, 0, 0)`
  64. // `rgba(255 0 0 / 0.5)` -> `rgba(255, 0, 0, 0.5)`
  65. // 与群友的正常写法冲突,先去掉!(2024-05-25)
  66. // presetLegacyCompat({
  67. // commaStyleColorFunction: true,
  68. // }) as Preset,
  69. ],
  70. /**
  71. * 自定义快捷语句
  72. * @see https://github.com/unocss/unocss#shortcuts
  73. */
  74. shortcuts: [['center', 'flex justify-center items-center']],
  75. transformers: [
  76. optimization(),
  77. // 启用 @apply 功能
  78. transformerDirectives(),
  79. // 启用 () 分组功能
  80. // 支持css class组合,eg: `<div class="hover:(bg-gray-400 font-medium) font-(light mono)">测试 unocss</div>`
  81. transformerVariantGroup(),
  82. // Don't change the following order
  83. transformerAttributify({
  84. // 解决与第三方框架样式冲突问题
  85. prefixedOnly: true,
  86. prefix: 'fg',
  87. }),
  88. ],
  89. rules: [
  90. [
  91. 'p-safe',
  92. {
  93. padding:
  94. 'env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)',
  95. },
  96. ],
  97. ['pt-safe', { 'padding-top': 'env(safe-area-inset-top)' }],
  98. ['pb-safe', { 'padding-bottom': 'env(safe-area-inset-bottom)' }],
  99. ],
  100. })
  101. /**
  102. * 最终这一套组合下来会得到:
  103. * mp 里面:mt-4 => margin-top: 32rpx == 16px
  104. * h5 里面:mt-4 => margin-top: 1rem == 16px
  105. *
  106. * 如果是传统方式写样式,则推荐设计稿设置为 750,这样设计稿1px,代码写1rpx。
  107. * rpx是响应式的,可以让不同设备的屏幕显示效果保持一致。
  108. */