npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svgo-autocrop

v1.1.2

Published

SVGO plugin that reduces viewBox to minimum possible size so no wasted transparent space around svg

Downloads

1,870

Readme

svgo-autocrop

Plugin which can be added to svgo.

Reduces viewBox to minimum possible size so no wasted transparent space around svg.

This plugin focuses on making svgs as consistent as possible. It's great for taking multiple libraries of 1000s of svgs, removing all padding, removing deprecated/redundant tags and replacing the arbitrary colors svg libraries tend to use with a set color (we recommend setting 'currentColor' so the color is inherited/easily modifiable from html/css). Tested with heaps of free svg libraries like Bootstrap Icons, Entypo+, Fontawesome and Google's Material Design Icons and more using this config file.

Example Input

<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
   <rect x="5" y="5" width="10" height="10" fill="#000"/>
</svg>

Example Output

<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
   <rect x="0" y="0" width="10" height="10" fill="#000"/>
</svg>

Example Output if using {setColor: 'currentColor'} as parameters

<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
   <rect x="0" y="0" width="10" height="10" fill="currentColor"/>
</svg>

Parameters

| Parameter | Default | Recommended | Description | | ------------------------------- | ----------- | --------------------- | ------------| | autocrop | true | true | Disable auto cropping. Useful if you only want to use the removeClass or setColor functionality described below. | | includeWidthAndHeightAttributes | undefined | false | If undefined, only updated width/height if SVG already has width/height (this is the default). If true, writes width/height. If false, deletes width/height (which we recommend so your SVG always scales). If true, you should also disable svgo's default removeViewBox plugin and optionally enable svgo's removeDimensions plugin - see example-config.js below for how to do this. | | padding | undefined | undefined | Either number, object defining {top, bottom, left, right} or function accepting the parameters (viewboxNew, viewbox?, ast?, params?, info?) where viewboxNew should be updated with the required padding. We don't recommend specifying padding on your SVG - padding should be defined in your css. | | removeClass | false | true (typically can be safely set to true) | If true, removes class attribute if encountered. Good for Bootstrap Icons which unnecessarily include the svg name in the class. | | removeStyle | false | true (typically can be safely set to true) | If true, removes style/font-family attribute if encountered. Good for lower quality that redundantly include styling that's never actually used. | | removeDeprecated | false | true | If true, then deletes <svg version/baseProfile> attributes. Also deletes other non-standard/not useful attributes like sketch:type/data-name/etc. | | setColor | undefined | 'currentColor' | If defined, replace all colors with color specified. Usually set to 'currentColor' so color is inherited/easily modifiable from html/css. If multiple colors are encountered, how this is handled is defined by the setColorIssue property. | | setColorIssue | "warn" | 'warn' or 'fail' | Defines the action to take when setColor is set and multiple colors are encountered. If 'warn', then a warning is outputted to the console that your will be converted from a multicolor/multitone to a single color/monotune using the 'setColor' specified. If 'fail', then an error is thrown instead of being logged as a warning. If 'rollback', then is still autocropped but translate back to (0,0)/'removeClass'/'setColor' will be undone/rolled back. If 'ignore', then all colors are changed to the 'setColor' specified without any warnings/errors. | | disableTranslate | false | false | Global override you generally shouldn't need - disables attempt to translate back to (0, 0) if not already (0,0), also has the effect of disabling removeClass, removeDeprecated and setColor. If this is set to true, then this plugin will only autocrop, nothing else. This can be safely left enabled, but added just in case someone needs it or whats to disable all the extra functionality this plugin supports. | | disableTranslateWarning | false | Set to true if the warning becomes annoying | Disable warning when translation back to (0, 0) fails, or removeClass, removeDeprecated and setColor fails. If this warning is annoying you, just set this to true. FYI In many cases these warnings are only outputted on the first pass, but by the second pass the warning no longer needs to be outputted because the svgo plugins have all worked together to remove the problem. | | debug | false | false | Debug option: Log old/new viewbox to console | | debugWriteFiles | false | false | Debug option: If true, then writes "${srcSvg}.png" and "${srcSvg}.html" file to disk for easier debugging. If string, then then writes "${debugWriteFiles}.png" and "${debugWriteFiles}.html" file to disk for easier debugging. FYI Setting this to true is an easy way to convert all your SVGs to PNGs. | | debugWorkerThread | false | false | Debug option: Log all worker thread communication. Warning: have to manually terminate terminate application when this is true. |

Based off the above, your parameters should typically be {} or {includeWidthAndHeightAttributes: false} or something like {includeWidthAndHeightAttributes: false, removeClass: true, removeStyle: true, removeDeprecated: true, setColor: 'currentColor', setColorIssue: 'fail'}.

Example config

See example-config.js config.

Then run with svgo;

svgo --input 'input.svg' --output 'output.svg' --config 'example-config.js'

Plugin walkthrough

See index.js and AutocropUtils.js for starting points.