Files
Mycontent/reactH5/postcss-rpx-750.cjs
2026-04-28 18:39:08 +08:00

28 lines
652 B
JavaScript

/**
* 750 设计宽:在 375px 视口下 1rpx = 0.5px
*/
function rpx750() {
return {
postcssPlugin: 'rpx-750',
Once(root) {
root.walkDecls((decl) => {
if (!decl.value || !decl.value.includes('rpx')) return
decl.value = decl.value.replace(
/(\d+(?:\.\d+)?)rpx/g,
(_m, n) => {
const px = parseFloat(n) * 0.5
const s = Number.isInteger(px)
? String(px)
: px
.toFixed(3)
.replace(/\.?0+$/, '')
return `${s}px`
}
)
})
},
}
}
rpx750.postcss = true
module.exports = rpx750