vue-cli3.0 使用 postcss-pxtorem(postcss-px2rem)自動轉換px爲rem

如何在vue-cli3.0中使用 postcss-pxtorem

  • 在vue-cli3.0中。去掉了build和config文件夾。全部的配置都放到了vue.config.js中。

先上代碼,vue.config.js的配置以下css

module.exports = {
    lintOnSave: true,
    css: {
        loaderOptions: {
            postcss: {
                plugins: [
                    require('postcss-pxtorem')({
                        rootValue : 1, // 換算的基數
                        selectorBlackList  : ['weui','mu'], // 忽略轉換正則匹配項
                        propList   : ['*'],
                    }),
                ]
            }
        }
    },
}

若是是使用 postcss-px2rem,都差很少vue

module.exports = {
    lintOnSave: true,
    css: {
        loaderOptions: {
            postcss: {
                plugins: [
                    require('postcss-px2rem')({remUnit: 30}), // 換算的基數
                ]
            }
        }
    },
}

記得npm i 安裝包;vue-cli

可能遇到的坑:npm

  1. 插件會轉化全部的樣式的px。好比引入了三方UI,也會被轉化。目前我使用 selectorBlackList字段,來過濾。可是感受不太靠譜。若是有更好的辦法歡迎大佬補充
  2. 若是個別地方不想轉化px。能夠簡單的使用大寫的 PXPx
相關文章
相關標籤/搜索