Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spread in the path for svgo custom plugins #847

Open
JanStevens opened this issue Oct 3, 2023 · 0 comments
Open

Spread in the path for svgo custom plugins #847

JanStevens opened this issue Oct 3, 2023 · 0 comments

Comments

@JanStevens
Copy link

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant