21 lines
586 B
JavaScript
21 lines
586 B
JavaScript
// 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;
|