From de9b21ed33a3a5b15284472c946311493bc0a65f Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Sat, 15 Jul 2023 11:36:09 +0200 Subject: [PATCH] Automatically compress SVGs with svgo --- .pre-commit-config.yaml | 10 +++++ svgo.config.js | 92 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 svgo.config.js diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 76e1ff3..74c80f7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,3 +26,13 @@ repos: - eslint-config-prettier - eslint-plugin-unicorn - prettier@3 + - repo: local + hooks: + - id: svgo + name: optimize SVG files + entry: svgo + types: [svg] + files: 'src/icons/.*\.svg' + language: node + additional_dependencies: + - "svgo" diff --git a/svgo.config.js b/svgo.config.js new file mode 100644 index 0000000..1dcd46a --- /dev/null +++ b/svgo.config.js @@ -0,0 +1,92 @@ +/*! + * SVGO configuration from simple-icons, modified. + * + * See: https://github.com/simple-icons/simple-icons/blob/4e36921e759278e83f2e6775e0fb78ba76131eec/svgo.config.mjs + */ + +export default { + multipass: true, + eol: "lf", + plugins: [ + "cleanupAttrs", + "mergeStyles", + "minifyStyles", + "inlineStyles", + "removeDoctype", + "removeXMLProcInst", + "removeComments", + "removeMetadata", + "removeDesc", + "removeUselessDefs", + "removeEditorsNSData", + "removeEmptyAttrs", + "removeHiddenElems", + "removeEmptyText", + "removeEmptyContainers", + "convertStyleToAttrs", + "convertColors", + "cleanupEnableBackground", + { + name: "convertPathData", + params: { + // 3 decimals of precision in floating point numbers + floatPrecision: 3, + }, + }, + "convertTransform", + { + name: "removeUnknownsAndDefaults", + }, + "removeUselessStrokeAndFill", + "removeNonInheritableGroupAttrs", + "removeUnusedNS", + "cleanupIds", + "cleanupNumericValues", + "cleanupListOfValues", + "collapseGroups", + "removeRasterImages", + { + // Compound all s into one + name: "mergePaths", + params: { + force: true, + }, + }, + { + // Convert basic shapes (such as ) to + name: "convertShapeToPath", + params: { + // including + convertArcs: true, + }, + }, + "convertEllipseToCircle", + { + // Sort the attributes on the tag + name: "sortAttrs", + params: { + order: ["fill", "stroke", "viewBox"], + xmlnsOrder: "end", + }, + }, + "sortDefsChildren", + "removeDimensions", + + { + name: "removeAttrs", + params: { + attrs: ["svg:(?!(role|xmlns))", "path:(?!d)"], + }, + }, + { + name: "addAttributesToSVGElement", + params: { + attributes: [{ xmlns: "http://www.w3.org/2000/svg" }], + }, + }, + "removeOffCanvasPaths", + "removeStyleElement", + "removeScriptElement", + "removeTitle", + ], +};