20171105 1211/Sunday
Companies in order to better fit the mobile phone, the previous use of PX to do a unit of CSS files, all need to convert to use REM to do units, is currently 1rem=37.5px; When developing new projects, it is customary to write style code of PX, complete the restoration of the UI manuscript, then unify into REM to do the unit, Paste the Nodejs code I wrote:
var fs = require (' FS ');
var path=require (' path ');
Console.log ((__dirname))
var oldcontent= './px/';
var newcontent= './rem/';
Fs.readdir (Oldcontent,function (err,files) {
if (err) {
throw err;
}
Console.log (files);
Files.foreach (function (item, index) {
Fs.readfile (Oldcontent+item, function (err, data) {
if (err) {
return (ERR);
}
var ss = Data.tostring (). Split (';');
for (var i = 0; i < ss.length; i++) {
if (/[\d.) * (px)/.test (ss[i]) = = True) {
var arrpx=ss[i].match (/[\d.) * (px)/g);
var remtmp= ';
This loop resolves multiple px on the same line this case
for (Var j=0;j<arrpx.length;j++) {
var tmp=arrpx[j].replace (' px ', ') * 1;
var rem = (tmp/37.5). toFixed (5) + ' REM ';
Remtmp=ss[i].replace (/[\d.) * (px)/,rem); cannot be replaced globally, replace does not change the value of ss[i]
ss[i]=remtmp; Change Ss[i]
}
}
}
var resultstr=ss.join (';');
Fs.writefile (Newcontent+item, resultstr, function (err) {
if (err) {
return (ERR);
}
})
});
})
})
Use Nodejs to convert all CSS files with PX units into a CSS file using REM.