@harrytwigg/postcss-rem-to-px
Version:
PostCSS plugin to replace all rem values with px values.
31 lines (24 loc) • 810 B
JavaScript
const postcss = require('postcss')
const defaultOptions = {
baseSize: 16
}
module.exports = postcss.plugin('postcss-rem-to-px', userOptions => {
let options = Object.assign({}, defaultOptions, userOptions)
return (root, result) => {
root.walkRules(rule => {
rule.walkDecls(decl => {
let matchArray = decl.value.match(/\d*\.*\d+rem/gi)
if (matchArray !== null) {
matchArray.forEach(value => {
let numericValue = value.match(/(\d*\.*\d+)rem/i)[1]
let pxValue = numericValue * options.baseSize
// round to nearest pixel ie convert 1.1px to 1px
pxValue = Math.round(pxValue)
decl.value = decl.value.replace(value, pxValue + 'px')
})
}
})
})
return result
}
})