JetBrains / svg-sprite-loader

Webpack loader for creating SVG sprites.
MIT License
2.02k stars 270 forks source link

extract: true produces sprite with broken structure (all symbols and gradients included in def) #402

Closed adenisovgit closed 4 years ago

adenisovgit commented 4 years ago

First I used loader without extraction and it works perfect, just index.html has too heavy - plus 600kb. Structure of svg sprite in this mode was: 71a1c9c519 All the symbols on upper level. all the gradients inside defs inside symbols - that is OK.

Then I set extract: true and resulted sprite.svg has a broken structure = symbols and gradients on same level inside def. As a result - svg can't get the gradients:

1589561272415

Why the sprite structure is changed? How I could return extracted sprite to normal structure?

Sincerely, Andrey.

adenisovgit commented 4 years ago

I resolved the issue with recommendation from this issue, with direct DOM manipulation:

function arrayFrom(arrayLike) {
  return Array.prototype.slice.call(arrayLike, 0);
}

const moveSymbolsDown = (svg) => {
  arrayFrom(svg.querySelectorAll('symbol')).forEach((symbol) => {
    symbol.parentNode.after(symbol);
  });
};

Just keep in mind, when you inject sprite in document do not use "display: none" for hiding the spritestorage, because this will lead to losing gradients. Hide it with positioning, for instance.