Skip to content

Instantly share code, notes, and snippets.

@tobspr
Created January 23, 2021 13:48
Show Gist options
  • Save tobspr/fe87d9be048d695e357e85b67a8b7ee8 to your computer and use it in GitHub Desktop.
Save tobspr/fe87d9be048d695e357e85b67a8b7ee8 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
function fixFile(filepath) {
const contents = fs.readFileSync(filepath, "utf-8");
const matcher = /([0-9]+(?:\.[0-9]+)?)rem/igm;
const result = contents.replaceAll(matcher, (substr, size) => {
const rem = Number(size);
const px = Math.round(rem * 16);
console.log(substr, "->", size, "->", px);
return px + "px";
});
fs.writeFileSync(filepath, result);
}
function fixFolder(folder) {
const files = fs.readdirSync(folder);
for (const file of files) {
if (file === "node_modules") {
continue;
}
const abspath = path.join(folder, file);
if (file.endsWith(".scss") || file.endsWith(".vue")) {
fixFile(abspath);
} else if (fs.lstatSync(abspath).isDirectory()) {
fixFolder(abspath);
}
}
}
fixFolder(".");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment