37 lines
874 B
JavaScript
37 lines
874 B
JavaScript
|
|
const postcss = require('postcss')
|
||
|
|
const uniPostcss = require('@dcloudio/vue-cli-plugin-uni/packages/postcss')
|
||
|
|
|
||
|
|
function toVw(value) {
|
||
|
|
return value.replace(/%\?([+-]?\d+(?:\.\d+)?)\?%/g, (match, number) => {
|
||
|
|
const rpx = Number(number)
|
||
|
|
if (!Number.isFinite(rpx)) {
|
||
|
|
return match
|
||
|
|
}
|
||
|
|
const vw = Number((rpx / 7.5).toFixed(6)).toString()
|
||
|
|
return vw === '0' ? '0' : `${vw}vw`
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
const rpxPlaceholderToVw = postcss.plugin('rpx-placeholder-to-vw', () => {
|
||
|
|
return root => {
|
||
|
|
root.walkDecls(decl => {
|
||
|
|
if (decl.value && decl.value.includes('%?')) {
|
||
|
|
decl.value = toVw(decl.value)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
root.walkAtRules(atRule => {
|
||
|
|
if (atRule.params && atRule.params.includes('%?')) {
|
||
|
|
atRule.params = toVw(atRule.params)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
plugins: [
|
||
|
|
uniPostcss(),
|
||
|
|
rpxPlaceholderToVw()
|
||
|
|
]
|
||
|
|
}
|