Files
----/前端源码/uni-app/postcss-rpx-to-vw.js

21 lines
586 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// PostCSS 插件:将 rpx 单位转换为 vw
// 基于 750px 设计稿1rpx = 100vw / 750 = 0.1333vw
const postcssRpxToVw = () => {
return {
postcssPlugin: 'postcss-rpx-to-vw',
Declaration(decl) {
if (decl.value.includes('rpx')) {
decl.value = decl.value.replace(/(\d+(?:\.\d+)?)rpx/g, (match, num) => {
const vw = (parseFloat(num) / 750 * 100).toFixed(4);
return `${vw}vw`;
});
}
}
};
};
postcssRpxToVw.postcss = true;
export default postcssRpxToVw;