JetBrains / svg-sprite-loader

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

width and height tags are not saved in extract mode #321

Open gzaripov opened 5 years ago

gzaripov commented 5 years ago

Here is one of SVG files I use

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
  <defs>
    <path id="list-a" d="M4,14 L8,14 L8,10 L4,10 L4,14 L4,14 Z M4,19 L8,19 L8,15 L4,15 L4,19 L4,19 Z M4,9 L8,9 L8,5 L4,5 L4,9 L4,9 Z M9,14 L21,14 L21,10 L9,10 L9,14 L9,14 Z M9,19 L21,19 L21,15 L9,15 L9,19 L9,19 Z M9,5 L9,9 L21,9 L21,5 L9,5 L9,5 Z"/>
  </defs>
  <g fill="none" fill-rule="evenodd">
    <polygon points="0 0 24 0 24 24 0 24"/>
    <mask id="list-b" fill="#fff">
      <use xlink:href="#list-a"/>
    </mask>
    <g fill="#fff" mask="url(#list-b)">
      <rect width="24" height="24"/>
    </g>
  </g>
</svg>

And this is svg in extracted file

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" id="icon-list" width="100%" height="100%">

  <g fill="none" fill-rule="evenodd">
    <polygon points="0 0 24 0 24 24 0 24"/>

    <g fill="#fff" mask="url(#icon-list_list-b)">
      <rect width="24" height="24"/>
    </g>
  </g>
</svg>

Expected behaviour: width="24" height="24"

Please tell us about your environment:

here is my config file:

exports.loadSVG = () => ({
  module: {
    rules: [
      {
        test: /\.svg$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "svg-sprite-loader",
            options: {
              extract: true,
              esModule: false,
              spriteFilename: "sprite-[hash:6].svg"
            }
          }
        ]
      }
    ]
  },
  plugins: [new SpriteLoaderPlugin()]
});
jdcmarques commented 5 years ago

Any news on this?

alkorlos commented 3 years ago

This problem has already been raised in https://github.com/JetBrains/svg-sprite-loader/issues/304 @kisenka wrote there "But why you need width and height, if you already has viewBox?", I can explain this moment.

viewBox defines the position and dimension of an SVG viewport, not sizes. SVG without sizes goes to the default size of 300px width and 150px height. For many icons other defaults are more appropriate. Yes, sizes can be set using css, but styles may not load. You can also specify the dimensions in html, but an inexperienced developer can forget about it. In any case, having the default width and height into SVG icon is safer. If someone does not need certain attributes, he can always use svgo-loader, but deleting something without the undo option is very inconvenient.

mr-sanders commented 3 years ago

Seems, that svg attributes are trimmed in extract: false mode as well. I agree with @alkorlos - it's really inconvenient. Why don't allow configure preserve list of svgToSymbol? In my case, I have proper width/height and viewBox attributes inside the exported svg from Figma. I don't need to set them manually. I want to set default width/height attributes to the <use>'s parent <svg> element automatically without css. Workaround in custom-runtime-generator-extract-mode example with dimensions extraction from viewBox seems kludgy.