A tool to fine tune svgo to meet my SVG optimization requirements.
- 🎨 Set
fillto"currentColor"to fit in any color context. - ⚛️ React: Correct common invalid attribute
classtoclassName. - ⚛️ React: Correct hyphen case to camelCase. For example
fill-ruletofillRule. - 🗳️ Add
widthandheightaccording to the viewBox. - 🚰 Output to
stdoutby default to make it easier to pipe to other tools (Save to file, copy to clipboard, etc.). - ➡️ Pretty print with indent
2by default (WHY? Because it will be compressed anyway by your bundler, and indent is necessary for human readability andgit diff). - 🌈 Colorful output by shikijs.
optimize a SVG file and copy to clipboard:
pnpx svgo-fine -i input.svg | clip # Windowspnpx svgo-fine -i input.svg | pbcopy # MacosBefore:
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none"><path fill="#626267" d="M5.379 15.833 1.667 18.75V3.333c0-.46.373-.833.833-.833h15c.46 0 .833.373.833.833V15c0 .46-.373.833-.833.833zm-.577-1.666h11.865v-10H3.333V15.32z"/><path fill="#626267" d="M5.379 15.833 1.667 18.75V3.333c0-.46.373-.833.833-.833h15c.46 0 .833.373.833.833V15c0 .46-.373.833-.833
.833zm-.577-1.666h11.865v-10H3.333V15.32z"/>
</svg>After:
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor">
<path d="M5.379 15.833 1.667 18.75V3.333c0-.46.373-.833.833-.833h15c.46 0 .833.373.833.833V15c0 .46-.373.833-.833
.833zm-.577-1.666h11.865v-10H3.333V15.32z"/>
<path d="M5.379 15.833 1.667 18.75V3.333c0-.46.373-.833.833-.833h15c.46 0 .833.373.833.833V15c0 .46-.373.833-.833
.833zm-.577-1.666h11.865v-10H3.333V15.32z"/>
</svg>How it works
It is just some adjustment of plugin settings of svgo#removeAttributesBySelector&addAttributesToSVGElement.
So You can use settings to meet your requirements instead of using this package.
{
js2svg: { indent: 2, pretty: true },
plugins: [
{
name: "removeAttributesBySelector",
params: {
selectors: [
{selector: 'path', attributes: ['fill']},
{selector: 'svg', attributes: ['fill']}
]
}
},
{
name: "addAttributesToSVGElement",
params: {
attributes: [{fill: 'currentColor'}]
}
}
]
}