自定义postcss插件,根据倍率增大font-size大小

使用侧

    loaderOptions: {
   
      /**自适应配置 */
      postcss: {
   
        plugins: [
          // postcss
          require("./index.js")({
   
            unit: 1.2,
          }),
        ],
      },
    }

定义侧


const postcss = require('postcss')

module.exports = postcss.plugin('postcss-change-css-fontSize', function (opts = {
    }) {
   
  const {
    unit = 1 } = opts || {
   }
  console.log(99999)
  console.log(unit)
  // 接收两个参数,第一个是每个css文件的ast,第二个参数中可获取转换结果相关信息(包括当前css文件相关信息)
  function plugin(css, result) {
   
    css.walkRules((rule) => {
   
      // 遍历当前ast所有rule节点
      const {
    nodes } = rule
      for (let index = 0; index < nodes.length; index++) {
   
        const element = nodes[index];
        let elementprop = element.prop + ''
        if (elementprop && typeof elementprop === "string" && elementprop.includes('font-size')) {
   
          element.value = element.value.replace('px','') * unit + 'px'
         // const clone = rule.clone()
          // clone.selector = replaceStr(selector, prefix, replace)
          //rule.replaceWith(clone)
        }
      }
    })
  }

  return plugin
})

// module.exports = (opts = {}) => {
   
//   return {
   
//     postcssPlugin: 'postcss-dark-theme-class', // 插件名
//     // Once (root, { result }) { // root为根节点树,Once方法会在该节点下的所有子元素被处理之前调用
//     //    root.walkDecls(decl=>{...}) // 遍历CSS声明
//     // }
//   }
// }
// module.exports.postcss = true // 声明导出为postcss插件

// const Options = {
   
//   viewportWidth: 375,//默认视口宽度
// }


// module.exports = (options) =>{
   
//   const opt = Object.assign({},Options,options);
//   return {
   
//     postcssPlugin: 'postcss-change-css-fontSize',
//     //钩子函数
//     Declaration(node){
   
//       debugger
//       console.log(node)
//       //判断需要转换的单位
//       if(node.value.indexOf('xm') != -1){
   
//         console.log(node.prop,node.value);
//         const num = parseFloat(node.value);
//         node.value = `${((num / opt.viewportWidth)*100).toFixed(2)}vw`
//       }
      
//     }
//   }
// }

// module.exports.postcss = true // 声明导出为postcss插件

相关推荐

  1. 定义postcss根据增大font-size大小

    2024-02-22 09:22:02       23 阅读
  2. Mybatis运行原理及定义

    2024-02-22 09:22:02       69 阅读
  3. vue3+ts定义

    2024-02-22 09:22:02       35 阅读
  4. PostCSS及其常用介绍

    2024-02-22 09:22:02       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-22 09:22:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-22 09:22:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-22 09:22:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-22 09:22:02       20 阅读

热门阅读

  1. 子网络划分与互通,上网行为审计

    2024-02-22 09:22:02       34 阅读
  2. MyBatis框架-EhCache(三级缓存)

    2024-02-22 09:22:02       36 阅读
  3. springboot实现Aop(通知)切面编程的案例

    2024-02-22 09:22:02       30 阅读
  4. 微信小程序canvas开发的一些坑和总结-持续更新

    2024-02-22 09:22:02       32 阅读
  5. Ubuntu22.04配置静态ip

    2024-02-22 09:22:02       36 阅读
  6. 【工具】Ubuntu开机黑屏、NVIDIA显卡驱动问题

    2024-02-22 09:22:02       26 阅读
  7. vue、thinkphp实现腾讯云对象存储COS图片上传

    2024-02-22 09:22:02       26 阅读
  8. 数据结构:结构体定义方式

    2024-02-22 09:22:02       26 阅读
  9. 数据结构day2

    2024-02-22 09:22:02       25 阅读
  10. 机器学习科普及学习路线

    2024-02-22 09:22:02       29 阅读