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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow svgo's prefixIds plugin to use filenames #152

Closed
tremby opened this issue Aug 7, 2018 · 6 comments
Closed

Allow svgo's prefixIds plugin to use filenames #152

tremby opened this issue Aug 7, 2018 · 6 comments

Comments

@tremby
Copy link

tremby commented Aug 7, 2018

See https://github.com/svg/svgo/blob/master/plugins/prefixIds.js#L137

This looks to me like there's some way to pass a data structure "extra" to SVGO, which can contain the SVG's filename. This would then be used to prefix IDs in the SVG, sidestepping problems with duplicate IDs in inline SVGs.

Would it be possible to have SVGR populate this, so that this plugin can be used in this way?

@gregberge
Copy link
Owner

Yeah I think we have to do it!

@tremby
Copy link
Author

tremby commented Aug 13, 2018

I gave the new version a try but unfortunately this isn't working for me as expected.

I've added some trace logs to node_modules/svgo/plugins/prefixIds.js so I can see what's happening, and in extra I'm just getting {input: 'string'} for each SVG.

From looking at the svgr source, that therefore looks like the state has no filePath. Why might htis be?

I'm using the svgo/webpack, if that makes a difference. I'm loading SVGs like so:

import { ReactComponent as LinkIcon } from '@svgr/webpack!images/link.svg'

@gregberge
Copy link
Owner

gregberge commented Aug 13, 2018

Actually the filename is not passed when used with Webpack. I don't know if we have access to it, probably not always. Anyway there is still a fix to do in @svgr/webpack.

@gregberge gregberge reopened this Aug 13, 2018
@tremby
Copy link
Author

tremby commented Aug 13, 2018

Hope there's a way to do it!

@gersomvg
Copy link

gersomvg commented Aug 24, 2018

I'm doing this as a workaround now (to prevent duplicate ids):

const _ = require('lodash');
const svgPrefix = {};
svgPrefix.toString = () => `${_.uniqueId()}_`;

---

svgoConfig: {
    plugins: [
        { cleanupIDs: { prefix: svgPrefix } },
    ],
}

@gregberge
Copy link
Owner

Follow up #188

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

No branches or pull requests

3 participants