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

width and height attributes are being set when config is set to false #790

Open
twitcher07 opened this issue Mar 31, 2023 · 0 comments
Open

Comments

@twitcher07
Copy link

twitcher07 commented Mar 31, 2023

I am trying to run a folder of SVGs through svg-sprite in order to create a <symbol> sprite and also to make individual SVGs have uniform code by configuring the shape settings in the config. One of the things I want to be uniform is to remove any width and height attributes on the SVG tag so that they resize responsively. But no matter what I seem to do the width and height are being adding to the individual SVGs that the svg-sprite shape config outputs.

Here is my code:

const spriter = new SVGSpriter({
        log: true,
        dest: 'web/assets/icons',
        shape: {
            dimension: {
                attributes: false
            },
            spacing: { // Add padding
              padding: 0
            },
            id: {
                separator: '-'
            },
            dest: 'intermediate-svg', // Keep the intermediate files
            transform: [
                {
                    svgo: {
                        plugins: [
                            {
                                name: 'preset-default',
                                params: {
                                    overrides: {
                                        removeComments: false,
                                        removeViewBox: false
                                    },
                                },
                            },
                            {
                                name: 'removeAttrs',
                                params: {
                                    attrs: '(fill|stroke|class|id|data-name|width|height)',
                                },
                            },
                            'removeStyleElement',
                            'removeScriptElement',
                            'removeDimensions'
                        ]
                    }
                }
            ]
        },
        svg: {
            xmlDeclaration: false,
            dimensionAttributes: false,
        },
        mode: {
            symbol: {
                inline: true,
                example: true
            },
            view: true,
            stack: true
        }
    });

    const cwd = path.resolve('src');
    var icon_files = glob.sync('icons/**/*.svg', { cwd });

    for (const file of icon_files) {
        // Create and add a vinyl file instance for each SVG
        spriter.add(new _File({
            path: path.join(cwd, file), // Absolute path to the SVG file
            base: cwd, // Base path (see `name` argument)
            contents: fs.readFileSync(path.join(cwd, file)) // SVG file contents
        }));
    }

    spriter.compile((error, result, data) => {

        for (var mode in result) {
            for (var resource in result[mode]) {
                fs.mkdirSync(path.dirname(result[mode][resource].path), { recursive: true });
                fs.writeFileSync(result[mode][resource].path, result[mode][resource].contents);
            }
        }

    });

Here is an example of a SVG before it is ran through svg-sprite:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33.13 43.83">
  <g id="Layer_2" data-name="Layer 2" fill="currentColor">
    <g id="Layer_1-2" data-name="Layer 1">
      <path d="M32.5,24.1a2,2,0,0,0-2.83.07L18.56,35.92V2a2,2,0,0,0-4,0V36L3.43,24.65A2,2,0,1,0,.57,27.44l16,16.39,16-16.91A2,2,0,0,0,32.5,24.1Z"/>
    </g>
  </g>
</svg>

Here it is afterwards:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33.13 43.83" width="33.13" height="43.83"><g><path d="M32.5 24.1a2 2 0 0 0-2.83.07L18.56 35.92V2a2 2 0 0 0-4 0v34L3.43 24.65a2 2 0 1 0-2.86 2.79l16 16.39 16-16.91a2 2 0 0 0-.07-2.82Z"/></g></svg>

Notice how the output has a width and height even though my config for svg-sprite contains svg.dimensionAttributes = false. Unless I'm misunderstanding something about how this works, but i would think that that would mean that the width and height shouldn't be added to the output.

(Also, you'll notice that I set shape.dimension.attribute to false even though i think that was deprecated, because I was just trying anything to keep the output from containing the width and height attributes.)

Versions

svg-sprite version: 2.0.2
node js version: 16.13.1

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