svg-sprite / svg-sprite

SVG sprites & stacks galore β€” A low-level Node.js module that takes a bunch of SVG files, optimizes them and bakes them into SVG sprites of several types along with suitable stylesheet resources (e.g. CSS, Sass, LESS, Stylus, etc.)

Home Page:https://github.com/svg-sprite/svg-sprite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spread in the path for svgo custom plugins

JanStevens opened this issue Β· comments

Hi! πŸ‘‹

We use a custom svgo plugin to determine if we should remove the fill property or keep it. This is based on a naming convention:

import removeAttrsPlugin from 'svgo/plugins/removeAttrs';

const svgoConfig = [ plugins: {
  name: 'customRemovePlugin',
  params: { attrs: '(fill|stroke)' },
  fn: (ast, params, info) => {
    const basename = path.basename(info.path, path.extname(info.path));
    if (basename.startsWith('brand-logo-')) return ast;
    return removeAttrsPlugin.fn(ast, params, info);
  },
}],

For this to work the path needs to be available, I noticed that this is not explicitly set when calling svgo.optimize. I now used patch-package to solve it for my case, if acceptable I can create a PR with the changes below.

diff --git a/node_modules/svg-sprite/lib/svg-sprite/transform/svgo.js b/node_modules/svg-sprite/lib/svg-sprite/transform/svgo.js
index 82ebf3c..7f6f004 100644
--- a/node_modules/svg-sprite/lib/svg-sprite/transform/svgo.js
+++ b/node_modules/svg-sprite/lib/svg-sprite/transform/svgo.js
@@ -41,7 +41,7 @@ module.exports = function(shape, config, spriter, cb) {
     const svgLength = svg.length;
 
     try {
-        const result = svgo.optimize(svg, config);
+        const result = svgo.optimize(svg, {...config, path: shape.name});
         shape.setSVG(result.data);
         let optSVGLength = null;
 

Regards,