31 lines
		
	
	
		
			765 B
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			765 B
		
	
	
	
		
			JavaScript
		
	
	
	
'use strict';
 | 
						|
 | 
						|
let postcss = require('postcss');
 | 
						|
 | 
						|
let pxRegExp = /\b(\d+(\.\d+)?)px\b/;
 | 
						|
let pxGlobalRegExp = new RegExp(pxRegExp.source, 'g');
 | 
						|
 | 
						|
module.exports = postcss.plugin('postcss-px2rem', (option) => {
 | 
						|
  let opt = Object.assign({
 | 
						|
    scale: 0.5,
 | 
						|
    ignore: []
 | 
						|
  }, option)
 | 
						|
 | 
						|
  return (root, result) => {
 | 
						|
    const file = root.source.input.file
 | 
						|
    const ignore = opt.ignore || []
 | 
						|
    for (const ignoreElement of ignore) {
 | 
						|
      const regExp = new RegExp(ignoreElement)
 | 
						|
      if (regExp.test(file)) return
 | 
						|
    }
 | 
						|
    root.walkDecls(function (decl) {
 | 
						|
      if (/px/.test(decl.value)) {
 | 
						|
        decl.value = decl.value.replace(pxGlobalRegExp, function (match, p1) {
 | 
						|
          return p1 * opt.scale + 'px'
 | 
						|
        })
 | 
						|
      }
 | 
						|
    })
 | 
						|
    result.root = root
 | 
						|
  };
 | 
						|
});
 |