gulp-px2rem-converter
Version:
Gulp plugin. Converts all properties in pixels to rem, also converts all media in pixels in em
38 lines (33 loc) • 1.02 kB
text/typescript
const replace = require("./replace");
const findRoot = require("./findRoot");
const getType = require("./getType");
let param:number;
let type:string;
let rootParam:string;
const template:string = '<template root>'
const pxToRem = (file:string, rootSize:string ) :string => {
if(rootSize){
type = getType(rootSize).toLocaleLowerCase() || 'px';
param = parseInt(rootSize.replace(type,'').trim());
} else {
const arg:string = findRoot.getParams(file);
type = getType(arg).toLocaleLowerCase();
param = parseInt(arg.replace(type,'').trim());
rootParam = findRoot.getRoot();
if(rootParam) file = file.replace(rootParam,template);
}
switch(type){
case 'px':
file = replace(file, param);
if(rootParam) file = file.replace(template, rootParam);
break;
case '%':
param = 16 * param / 100;
file = replace(file, param);
if(rootParam) file = file.replace(template, rootParam);
break;
}
file = file.replace(/rem+[ \)|\)|\)\n]+{/ig,'em){')
return file;
}
module.exports = pxToRem;