Closed yoriiis closed 1 year ago
Hello Exactly the same issue. It fail when the returned value is computed π
Work:
svgoConfig: {
plugins: [
{
name: "prefixIds",
params: {
prefixIds: true,
prefixClassNames: false,
prefix(node, info) {
return `toto`;
},
delim: "-",
},
},
{
name: "cleanupIds",
params: {
minify: false,
},
},
],
}
Doesn't work:
svgoConfig: {
plugins: [
{
name: "prefixIds",
params: {
prefixIds: true,
prefixClassNames: false,
prefix(node, info) {
return `toto${Math.random().toString()}`;
},
delim: "-",
},
},
{
name: "cleanupIds",
params: {
minify: false,
},
},
],
}
Try to put prefixIds after cleanupIds
prefixIds
after cleanupIds
there are ids conflics.prefixIds
before cleanupIds
the prefix is not applied.@TrySound can you help us on this please?
The decision to remove the cleanupIds
prefix
plugin was made in the v3 release so I imagine you have an alternative solution? Thanks
Running into this issue as well. Without any changes to the source SVG, previously I would get an ID like youtube_svg__cls-1
. Now, the exact same SVG ends up with an ID of prefix__cls-1
.
This is my config:
module.exports = {
js2svg: {
indent: 2,
pretty: true,
},
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
},
},
},
'convertStyleToAttrs',
'prefixIds',
'removeDimensions',
],
};
Any suggestions on how to fix this please? @TrySound
@botmaster
Try to put prefixIds after cleanupIds
TrySound is correct, prefixIds
must be after cleanupIds
. Doing it the other way around would make cleanupIds
undo prefixIds
.
@yoriiis
so I imagine you have an alternative solution? Thanks
The alternate solution was to use prefixIds
, however it had a bug. Turns out the same bug I noticed and documented in https://github.com/svg/svgo/pull/1763#issuecomment-1742810271 was the bug that was causing you grief here as well.
I've opened the following PR:
With this, I've tested both of your examples using the following configuration, and can confirm they no longer break.
const crypto = require('crypto');
module.exports = {
plugins: [
'preset-default',
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => crypto.randomBytes(20).toString('hex').slice(0, 4)
}
}
],
};
Sorry for the headache, and thanks for being patient with this. The fix will be released in v3.0.3.
@mryechkin
previously I would get an ID like youtube_svgcls-1. Now, the exact same SVG ends up with an ID of prefixcls-1.
This CLI works fine for me, but if you're using the JS API, it may be because you didn't provide the path
option to #optimize
?
More info: https://github.com/svg/svgo#optimize
By default, prefixIds
only uses the file name if it's known. If not, then it uses prefix__
instead.
Thanks so much @SethFalco π Can confirm this works in our project π
Here's a modern ES Modules + JSDoc version of the randomBytes
config:
import { randomBytes } from 'node:crypto';
/** @type {import('svgo').Config} */
const svgoConfig = {
plugins: [
'preset-default',
// Avoid collisions with ids in other SVGs,
// which was causing incorrect gradient directions
// https://github.com/svg/svgo/issues/1746#issuecomment-1803595909
//
// Previously, this was a problem where unique ids
// could not be generated since svgo@3
// https://github.com/svg/svgo/issues/674
// https://github.com/svg/svgo/issues/1746
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => randomBytes(20).toString('hex').slice(0, 4),
},
},
],
};
export default svgoConfig;
A simpler alternative which does not rely on any imports is a simple counter, can also confirm this works in our project:
let svgoPrefixIdsCount = 0;
/** @type {import('svgo').Config} */
const svgoConfig = {
plugins: [
'preset-default',
// Avoid collisions with ids in other SVGs,
// which was causing incorrect gradient directions
// https://github.com/svg/svgo/issues/1746#issuecomment-1803600573
//
// Previously, this was a problem where unique ids
// could not be generated since svgo@3
// https://github.com/svg/svgo/issues/674
// https://github.com/svg/svgo/issues/1746
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => svgoPrefixIdsCount++,
},
},
],
};
export default svgoConfig;
Describe the bug
I need unique ids when multiple SVGs have
<defs>
. Withv2.8
I was using the following code and it works well.But in
v3
theprefix
parameter ofcleanupIDs
is removed replacingprefixIds
. I can't get the same rendering for the IDs. I've tested this code but it breaks the<defs>
.In input there are 2 svgs
gradient.svg
delete-alert.svg
In output, with the
prefixIds
plugin, it producedI've also tested the solution from https://github.com/svg/svgo/issues/674#issuecomment-1353701782 but there is always ids conflics. It produces the following output:
To Reproduce
I don't think it's necessary, tell me if necessary
Expected behavior
Unique ids should be possible as in
v.2.8
.Screenshots
Desktop (please complete the following information):
3.0.2
14.21.2
macOS 12.6
Additional context
The package is used inside the svg-chunk-webpack-plugin. All SVGO options are transmitted from plugin users. The question was posted on the SVGO Discord channel, without response (See question on SVGO Discord).
This issue is linked to https://github.com/svg/svgo/issues/674