自动化转化
var fs = require('fs');
var paths = require('path');
var config = {
px: 12,
fixNumber: 9,
type: 'rem',
cssClass: 'em'
};
var reg = {
// 数字+px 10px
px: /\d+px/g,
// font-size 10px
fontSize: /font-size.*\d+px/g,
// number
num: /[^0-9.]+/g
};
var cssFormate = function (path, px, callback) {
var file = fs.createReadStream(path);
var fileContent = [];
var css = [];
file.setEncoding('utf8');
file.on('data', function (chunk) {
// 直接}来分割块会有问题 无法处理{}嵌套的情况
//fileContent = chunk.split('}');
fileContent = cssSplit(chunk);
cssBlock(px, fileContent, function (emFile) {
emToFile(path, emFile, callback);
});
});
file.on('end', function () {
});
};
var cssSplit = function (chunk) {
var stack = [];
var stackTmp = [];
var tmp = '';
var index = 0;
var fileContent = chunk.split('{');
for (var i = 0; i < fileContent.length; i++) {
if (i < fileContent.length - 1) fileContent[i] += '{';
if (fileContent[i].indexOf('}') !== -1) {
var right = fileContent[i].split('}');
for (var j = 0; j < right.length; j++) {
if (j < right.length - 1) right[j] += '}';
stack.push(right[j]);
}
} else {
stack.push(fileContent[i]);
}
}
stack.forEach(function (item) {
if (item.indexOf('{') !== -1) {
index++;
} else if (item.indexOf('}') !== -1) {
index--;
}
tmp += item;
if (index === 0) {
stackTmp.push(tmp);
tmp = '';
}
});
return stackTmp;
};
var pxToem = function (chunk, px) {
var emChunk = chunk.replace(/[\d\.]+px/g, function () {
var args = Array.prototype.slice.call(arguments)[0];
var num = args.split('px')[0];
// 特殊处理1px的情况
if(num == 0) return 0;
return (num / px * 12 < 1) ? ('1px') : (num / px).toFixed(config.fixNumber) + config.type;
});
return emChunk;
};
var cssBlock = function (px, cssfile, callback) {
var emFile = '';
for (var i = 0; i < cssfile.length; i++) {
var fontSize = 0;
//if (cssfile[i].indexOf('{') !== -1) {
// cssfile[i] += '}';
//}
if (cssfile[i].indexOf('font-size') !== -1) {
// 先把font-size/14处理
cssfile[i] = cssfile[i].replace(reg.fontSize, function () {
var args = Array.prototype.slice.call(arguments)[0];
fontSize = args.replace(reg.num, '');
return 'font-size: ' + (fontSize / (px || config.px)) + config.type;
});
}
fontSize = (fontSize && config.type !== 'rem') ? fontSize : (px || config.px);
emFile += pxToem(cssfile[i], fontSize);
}
callback(emFile);
};
var emToFile = function (path, emChunk, callback) {
fs.writeFile(path, emChunk, 'utf-8', function () {
console.log(path + '-->px2em success!');
if (callback) {
callback();
}
});
};
module.exports = cssFormate;
开头添加
<script>
(function () {
var createStyle = function (cssText) {
var document = window.document;
var styleTag = document.createElement("style");
styleTag.setAttribute("type", "text/css");
styleTag.innerHTML = cssText;
document.getElementsByTagName("head").item(0).appendChild(styleTag);
};
var ua = window.navigator.userAgent;
var isMobile = ua.match(/(Android|iPhone|iPad)/);
var width = 320;
if (isMobile) {
width = window.innerWidth;
var height = window.innerHeight;
createStyle('html { font-size: ' + width * 12 / 320 + 'px; font-family: "Helvetica"; }');
createStyle('.doc { width: ' + width + 'px; height: ' + height+ 'px; }');
} else {
createStyle('html { font-size: ' + width * 12 / 320 + 'px; font-family: "黑体-简", "Helvetica"; } ');
createStyle('textarea, input { font-family: "黑体-简"; } ');
createStyle('.doc { width: ' + width + 'px; height: ' + width * 16 / 9 + 'px; }');
}
}());
</script>
csswatcher = gulp.watch(['./css/editor.css','./css/editor2.css','./css/header.css'], function(event){
var srcPath = event.path;
var buildPath = srcPath.substr(0,srcPath.length-4) + "-min.css";
var cssContent = fs.readFileSync(srcPath, 'utf8');
fs.writeFileSync(buildPath, cssContent, 'utf8');
px2em(buildPath);
});