From c06ce6cc632e23667915ff03af4225e125dfdbcc Mon Sep 17 00:00:00 2001 From: Sergei Eremenko Date: Thu, 18 Feb 2021 21:54:28 +0200 Subject: [PATCH] Add SVGO v2 config. Drop SVGO v1 support! BREAKING CHANGE! This commit provides the new config file for svgo 2.0.0+ and removes the config file for svgo 1.3.2 and lower. To continue use tools in the project, please upgrade svgo to the latest version: npm install -g svgo --- tools/README.md | 2 +- tools/_svgo.yml | 140 ------------------------------------------- tools/ffsvg.sh | 2 +- tools/svgo.config.js | 117 ++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 142 deletions(-) delete mode 100644 tools/_svgo.yml create mode 100644 tools/svgo.config.js diff --git a/tools/README.md b/tools/README.md index a0923142dc..96d51a2bac 100644 --- a/tools/README.md +++ b/tools/README.md @@ -5,7 +5,7 @@ * `_clean_style_attr.sed` — removes unused properties and removes properties with default values from style attributes inside SVG files (part of `ffsvg.sh`) * `_fix_color_scheme.sh FILE...` — looks in the SVG files for certain colors and replaces them with the corresponding stylesheet class. Fixes a color scheme after Inkscape (part of `ffsvg.sh`) * `_scour.sh FILE...` — Scour wrapper (part of `ffsvg.sh`) -* `_svgo.yml` — [SVGO](https://github.com/svg/svgo) configuraion (part of `ffsvg.sh`) +* `svgo.config.js` — [SVGO](https://github.com/svg/svgo) configuraion (part of `ffsvg.sh`) ## Useful snippets diff --git a/tools/_svgo.yml b/tools/_svgo.yml deleted file mode 100644 index 80b2558b4e..0000000000 --- a/tools/_svgo.yml +++ /dev/null @@ -1,140 +0,0 @@ -# Default config for SVGO -# -# Usage: -# -# for d in Papirus/16x16/* Papirus/22x22/* Papirus/24x24/* -# do -# svgo --config=_svgo.yml -f "$d" -# done -# -# or -# -# svgo --config=_svgo.yml -f Papirus/16x16/actions -# -# or -# -# svgo --config=_svgo.yml -i FILE - -full: true -multipass: true - -plugins: - - # removes doctype declaration - - removeDoctype - - # removes XML processing instructions - - removeXMLProcInst - - # removes comments - - removeComments - - # removes - - removeMetadata - - # removes editors namespaces, elements and attributes - - removeEditorsNSData - - # removes unused IDs - # - cleanupIDs: - # minify: false - # preserve: [ 'current-color-scheme' ] - - # removes elements in without id - - removeUselessDefs - - # sorts element attributes (disabled by default) - - sortAttrs: - order: - - id - - fill - - stroke - - opacity - - style - - class - - width - - height - - x - - x1 - - x2 - - y - - y1 - - y2 - - cx - - cy - - rx - - ry - - r - - transform - - marker - - points - - d - - # rounds numeric values to the fixed precision - # removes default ‘px’ units - # y="749.936002px" --> y="749.936" - - cleanupNumericValues: - floatPrecision: 3 - leadingZero: true - defaultPx: true - convertToPx: false - - # rounds list of values to the fixed precision - # viewBox="0 0 16px 16px" --> viewBox="0 0 16 16" - - cleanupListOfValues: - floatPrecision: 2 - leadingZero: true - defaultPx: true - convertToPx: false - - # converts colors: rgb() to #rrggbb - - convertColors: - shorthex: false - shortname: false - - # convert non-eccentric to - - convertEllipseToCircle - - # removes unknown elements content and attributes - # don't touch attrs with default values - # - removeUnknownsAndDefaults: - # defaultAttrs: false - - # removes viewBox attr which coincides with a width/height box - - removeViewBox - - # remove or cleanup enable-background attribute when possible - - cleanupEnableBackground - - # removes hidden elements (zero sized, with absent attributes) - - removeHiddenElems: - opacity0: false - - # removes empty elements - - removeEmptyText - - # removes empty attributes - - removeEmptyAttrs - - # removes empty container elements - - removeEmptyContainers - - # removes unused namespaces declaration - - removeUnusedNS - - # removes - - removeTitle - - # removes <desc> - - removeDesc - - # cleanups attributes from newlines, trailing and repeating spaces - - cleanupAttrs - -# configure the indent (default 4 spaces) used by `--pretty` here: -# -# @see https://github.com/svg/svgo/blob/master/lib/svgo/js2svg.js#L6 for more config options -# -js2svg: - pretty: true - indent: ' ' diff --git a/tools/ffsvg.sh b/tools/ffsvg.sh index a299fd9260..97966344db 100755 --- a/tools/ffsvg.sh +++ b/tools/ffsvg.sh @@ -26,7 +26,7 @@ _run_helpers() { # optimize a SVG if command -v svgo > /dev/null 2>&1; then # use SVGO - svgo --config="$SCRIPT_DIR/_svgo.yml" -i "$1" -o "$1".tmp + svgo --config="$SCRIPT_DIR/svgo.config.js" -i "$1" -o "$1".tmp mv -f "$1".tmp "$1" elif command -v scour > /dev/null 2>&1; then # use scour diff --git a/tools/svgo.config.js b/tools/svgo.config.js new file mode 100644 index 0000000000..9371b829e0 --- /dev/null +++ b/tools/svgo.config.js @@ -0,0 +1,117 @@ +module.exports = { + multipass: true, + plugins: [ + // remove DOCTYPE declaration + 'removeDoctype', + // remove XML processing instructions + 'removeXMLProcInst', + // remove comments + 'removeComments', + // remove <metadata> + 'removeMetadata', + // remove editors namespaces, elements and attributes + 'removeEditorsNSData', + // cleanup attributes values from newlines, trailing and repeating spaces + 'cleanupAttrs', + /** + * { + * // remove unused IDs + * name: 'cleanupIDs', + * params: { + * minify: false, + * preserve: ['current-color-scheme'] + * } + * }, + */ + // remove raster images references in <image> + 'removeRasterImages', + // remove elements in <defs> without id + 'removeUselessDefs', + { + // round numeric values to the fixed precision + // remove default 'px' units + // y="749.936002px" --> y="749.936" + name: 'cleanupNumericValues', + params: { convertToPx: false } + }, + { + // round list of values to the fixed precision + // viewBox="0 0 16px 16px" --> viewBox="0 0 16 16" + name: 'cleanupListOfValues', + params: { + floatPrecision: 2, + leadingZero: true, + defaultPx: true, + convertToPx: false, + } + }, + { + // convert different colors formats to #rrggbb + name: 'convertColors', + params: { + shorthex: false, + shortname: false, + } + }, + /** + * { + * // remove unknown elements content and attributes + * // don't touch attributes with default values + * name: 'removeUnknownsAndDefaults', + * params: { defaultAttrs: false } + * }, + */ + // remove viewBox attr which coincides with a width/height box + 'removeViewBox', + // remove or cleanup enable-background attribute when possible + 'cleanupEnableBackground', + { + // remove hidden elements (zero sized, with absent attributes) + name: 'removeHiddenElems', + params: { opacity0: false } + }, + // remove empty <text/>, <tspan/>, and <tref xlink:href=""/> elements + 'removeEmptyText', + // convert non-eccentric <ellipse> to <circle> + 'convertEllipseToCircle', + // remove attributes with empty values + 'removeEmptyAttrs', + // remove empty containers + 'removeEmptyContainers', + // remove unused namespaces declaration + 'removeUnusedNS', + { + // sort element attributes for epic readability + name: 'sortAttrs', + params: { + order: [ + 'id', + 'fill', 'stroke', 'opacity', + 'style', 'class', + 'width', 'height', + 'x', 'x1', 'x2', + 'y', 'y1', 'y2', + 'cx', 'cy', + 'rx', 'ry', 'r', + 'transform', + 'marker', + 'points', + 'd', + ] + } + }, + // remove <title> + 'removeTitle', + // remove <desc> + 'removeDesc', + // remove elements that are drawn outside of the viewbox + // 'removeOffCanvasPaths', + ], + + // configure the indent (default 4 spaces) used by `--pretty` here: + // @see https://github.com/svg/svgo/blob/master/lib/svgo/js2svg.js#L6 for more config options + js2svg: { + pretty: true, + indent: 1 + } +}