94 lines
3.2 KiB
JavaScript
94 lines
3.2 KiB
JavaScript
|
import fs from "fs";
|
||
|
import path from "path";
|
||
|
import { fileURLToPath } from "url";
|
||
|
import { optimize } from "svgo";
|
||
|
|
||
|
const __filename = fileURLToPath(import.meta.url);
|
||
|
const __dirname = path.dirname(__filename);
|
||
|
|
||
|
const svgDir = path.join(__dirname, "..", "src", "svgs", "fa");
|
||
|
|
||
|
const svgoConfig = {
|
||
|
plugins: [
|
||
|
{ name: "removeDoctype", active: true },
|
||
|
{ name: "removeXMLProcInst", active: true },
|
||
|
{ name: "removeComments", active: true },
|
||
|
{ name: "removeMetadata", active: true },
|
||
|
{ name: "removeEditorsNSData", active: true },
|
||
|
{ name: "cleanupAttrs", active: true },
|
||
|
{ name: "minifyStyles", active: true },
|
||
|
{ name: "convertStyleToAttrs", active: true },
|
||
|
{ name: "cleanupIDs", active: true },
|
||
|
{ name: "removeRasterImages", active: true },
|
||
|
{ name: "mergePaths", active: true },
|
||
|
{ name: "convertTransform", active: true },
|
||
|
{ name: "removeUnusedNS", active: true },
|
||
|
{ name: "removeUselessDefs", active: true },
|
||
|
{ name: "cleanupNumericValues", active: true },
|
||
|
{ name: "cleanupListOfValues", active: true },
|
||
|
{ name: "convertColors", active: true },
|
||
|
{ name: "removeUnknownsAndDefaults", active: true },
|
||
|
{ name: "removeNonInheritableGroupAttrs", active: true },
|
||
|
{ name: "removeUselessStrokeAndFill", active: true },
|
||
|
{ name: "removeViewBox", active: false },
|
||
|
{ name: "cleanupEnableBackground", active: true },
|
||
|
{ name: "removeHiddenElems", active: true },
|
||
|
{ name: "removeEmptyText", active: true },
|
||
|
{ name: "convertShapeToPath", active: true },
|
||
|
{ name: "moveElemsAttrsToGroup", active: true },
|
||
|
{ name: "moveGroupAttrsToElems", active: true },
|
||
|
{ name: "collapseGroups", active: true },
|
||
|
{ name: "removeEmptyAttrs", active: true },
|
||
|
{ name: "removeEmptyContainers", active: true },
|
||
|
{ name: "mergePaths", active: true },
|
||
|
{ name: "removeUnusedNS", active: true },
|
||
|
{ name: "reusePaths", active: true },
|
||
|
{ name: "sortAttrs", active: true },
|
||
|
{ name: "removeDimensions", active: false },
|
||
|
],
|
||
|
};
|
||
|
|
||
|
fs.readdir(svgDir, (err, files) => {
|
||
|
if (err) {
|
||
|
console.error(`Error reading directory: ${err.message}`);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
files.forEach((file) => {
|
||
|
if (path.extname(file) === ".svg") {
|
||
|
const filePath = path.join(svgDir, file);
|
||
|
fs.readFile(filePath, "utf8", (err, data) => {
|
||
|
if (err) {
|
||
|
console.error(`Error reading file ${file}: ${err.message}`);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const result = optimize(data, { ...svgoConfig, path: filePath });
|
||
|
if (result.error) {
|
||
|
console.error(`Error optimizing ${file}: ${result.error}`);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
let optimizedSVG = result.data;
|
||
|
if (!/xmlns="http:\/\/www\.w3\.org\/2000\/svg"/.test(optimizedSVG)) {
|
||
|
optimizedSVG = optimizedSVG.replace(
|
||
|
/^<svg/,
|
||
|
'<svg xmlns="http://www.w3.org/2000/svg"'
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Manually remove comments if they still exist
|
||
|
optimizedSVG = optimizedSVG.replace(/<!--.*?-->/g, "");
|
||
|
|
||
|
fs.writeFile(filePath, optimizedSVG, "utf8", (err) => {
|
||
|
if (err) {
|
||
|
console.error(`Error writing file ${file}: ${err.message}`);
|
||
|
} else {
|
||
|
console.log(`Processed ${file}`);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
});
|