px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";"> 需求
自從有了postcss來處理css文件,我們可以快速進(jìn)行網(wǎng)站適配的開發(fā),只需要改改參數(shù),樣式按照設(shè)計(jì)稿的px寫,webpack編譯自動轉(zhuǎn)換成rem或者vw等。
但是,標(biāo)簽內(nèi)的px怎么辦呢?postcss并不提供轉(zhuǎn)換這個(gè)的功能。
探索
啟動思路
我正在做一個(gè)vue項(xiàng)目,剛好想要實(shí)現(xiàn)上面提到的需求,例如下面的例子
<h3 style="font-size: 28px;margin-top: 10px" width="500px">Test</h3>
我希望他能根據(jù)我設(shè)置的基準(zhǔn)值自動轉(zhuǎn)換成vw。
<h3 width="00vw" style="font-size: 00vw; margin-top: 00vw;">Test</h3>
要想實(shí)現(xiàn)這樣一個(gè)東西,離不開編譯工具webpack,webpack有loader、plugin,用什么好呢?通過找資料,我從一篇px轉(zhuǎn)rem的文章中得到了提示 react內(nèi)聯(lián)樣式使用webpack將px轉(zhuǎn)rem
沒錯(cuò),就是webpack-loader
寫一個(gè)webpack loader,在webpack編譯階段,讀取vue文件里面的內(nèi)容,通過正則識別出需要轉(zhuǎn)換的像素px,再通過公式轉(zhuǎn)換成vw。
開始行動
1、了解loader的實(shí)現(xiàn)原理
寫一個(gè)loader很簡單,傳入source,干些壞事,干完之后,返回處理過的source。source對應(yīng)的是每一個(gè)通過loader匹配到的文件。
module.exports = function (source) { // 干些壞事 return source}2、如何讓loader干壞事
先看一個(gè)簡單的vue文件,通常分為3部分,<template>、<script>、<style>
<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>部分已經(jīng)有postcss會進(jìn)行轉(zhuǎn)換處理,所以我把重點(diǎn)放到了<template>內(nèi)部的 “00px”。
其實(shí)source對應(yīng)的就是這樣一個(gè)vue文件,該例子中有28px、10px、500px是需要轉(zhuǎn)換的目標(biāo),首先用正則把他們都找出來。
先把template部分提出來,防止把style部分也轉(zhuǎn)換了
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進(jìn)行轉(zhuǎn)換
module.exports = function (source) { let _source = '' // 如果當(dāng)前的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轉(zhuǎn)換vw,核心部分  let $_source = _source.replace(pxGlobalRegExp, createPxReplace(defaults.viewportWidth, defaults.minPixelValue, defaults.unitPrecision, defaults.viewportUnit))  // 轉(zhuǎn)換之后替換回source中,返回函數(shù)值  return source.replace(template, $_source) } else {  //沒有就不轉(zhuǎn),直接返回  return source }}px轉(zhuǎn)vw的公式
我使用的是 postcss-px-to-viewport 內(nèi)部實(shí)現(xiàn)的轉(zhuǎn)換公式
function createPxReplace (viewportSize, minPixelValue, unitPrecision, viewportUnit) { // 不用好奇$0, $1是怎么來的,他們是replace第二個(gè)參數(shù)提供的 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類似的配置
一個(gè)基本的配置大概包含這些信息
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)	好了,現(xiàn)在我們實(shí)現(xiàn)了一個(gè)可以干壞事的loader,
注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。    
新聞熱點(diǎn)
疑難解答