webpack系列——实现一个行内样式px转vw的loader

需求

自从有了postcss来处理css文件,咱们能够快速进行网站适配的开发,只须要改改参数,样式按照设计稿的px写,webpack编译自动转换成rem或者vw等。css

可是,标签内的px怎么办呢?postcss并不提供转换这个的功能。html

探索

启动思路

我正在作一个vue项目,恰好想要实现上面提到的需求,例以下面的例子vue

<h3 style="font-size: 28px;margin-top: 10px" width="500px">Test</h3>

我但愿他能根据我设置的基准值自动转换成vw。react

<h3 width="00vw" style="font-size: 00vw; margin-top: 00vw;">Test</h3>

要想实现这样一个东西,离不开编译工具webpack,webpack有loader、plugin,用什么好呢?经过找资料,我从一篇px转rem的文章中获得了提示 react内联样式使用webpack将px转remwebpack

没错,就是webpack-loader

写一个webpack loader,在webpack编译阶段,读取vue文件里面的内容,经过正则识别出须要转换的像素px,再经过公式转换成vw。git

开始行动

一、了解loader的实现原理
写一个loader很简单,传入source,干些坏事,干完以后,返回处理过的source。source对应的是每个经过loader匹配到的文件。github

module.exports = function (source) {
  // 干些坏事
  return source
}

二、如何让loader干坏事
先看一个简单的vue文件,一般分为3部分,<template>、<script>、<style>web

<template>
  <div>
    <h3 style="font-size: 28px;margin-top: 10px" width="500px">Test</h3>
  </div>
</template>

<script>
  export default {
    name: '',
    components: {},
    created () {},
    mounted () {},
    methods: {}
  }
</script>

<style lang="less">
  h3 {
    font-size: 20px;
  }
</style>

咱们知道<style>部分已经有postcss会进行转换处理,因此我把重点放到了<template>内部的 “00px”。less

其实source对应的就是这样一个vue文件,该例子中有28px、10px、500px是须要转换的目标,首先用正则把他们都找出来。函数

  • 先把template部分提出来,防止把style部分也转换了
const template = /<template>([\s\S]+)<\/template>/gi


// 匹配出来的部分
<template>
  <div>
    <h3 style="font-size: 28px;margin-top: 10px" width="500px">Test</h3>
  </div>
</template>
  • 匹配px的正则
const ZPXRegExp = /(\d+)px/
  • 对template里面的px进行转换
module.exports = function (source) {
  let _source = ''
  // 若是当前的source里面存在template
  if (template.test(source)) {
    // 匹配template部分
    _source = source.match(template)[0]
  }
  // 匹配出template里面的px
  let pxGlobalRegExp = new RegExp(ZPXRegExp.source, 'ig')
  if (pxGlobalRegExp.test(_source)) {
    // px转换vw,核心部分
    let $_source = _source.replace(pxGlobalRegExp, createPxReplace(defaults.viewportWidth, defaults.minPixelValue, defaults.unitPrecision, defaults.viewportUnit))
    // 转换以后替换回source中,返回函数值
    return source.replace(template, $_source)
  } else {
    //没有就不转,直接返回
    return source
  }
}
  • px转vw的公式

我使用的是 postcss-px-to-viewport 内部实现的转换公式

function createPxReplace (viewportSize, minPixelValue, unitPrecision, viewportUnit) {
  // 不用好奇$0, $1是怎么来的,他们是replace第二个参数提供的
  return function ($0, $1) {
    if (!$1) return
    var pixels = parseFloat($1)
    if (pixels <= minPixelValue) return
    return toFixed((pixels / viewportSize * 100), unitPrecision) + viewportUnit
  }
}
function toFixed (number, precision) {
  var multiplier = Math.pow(10, precision + 1),
    wholeNumber = Math.floor(number * multiplier)
  return Math.round(wholeNumber / 10) * 10 / multiplier
}
  • 使用和postcss-px-to-viewport相似的配置

一个基本的配置大概包含这些信息

let defaultsProp = {
  unitToConvert: 'px',
  viewportWidth: 750,
  unitPrecision: 5,
  viewportUnit: 'vw',
  fontViewportUnit: 'vw',
  minPixelValue: 1
}
  • 给webpack-loader加上option
const loaderUtils = require('loader-utils')

const opts = loaderUtils.getOptions(this)
const defaults = Object.assign({}, defaultsProp, opts)

好了,如今咱们实现了一个能够干坏事的loader,😯不,是作好事!

咱们来看看转换成果

<h3 width="66.66667vw" style="font-size: 3.73333vw; margin-top: 1.33333vw;">Test</h3>

反思

虽然实现了我一开始的需求,可是内心老是不淡定,由于还些坑没有想明白,后续若是想明白了,再进行完善。

源码

style-vw-loader

相关文章
相关标签/搜索