Use Nodejs to convert all CSS files with PX units into a CSS file using REM.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.