High-performance SVG optimizer written in Rust
npm install svgtidy!CI
!License
!Crates.io Version
!NPM Version
> svgtidy is a high-performance SVG optimizer written in Rust.
It removes redundant information from SVG files (like comments, metadata, and hidden elements) and creates a minimized, cleaner version without affecting rendering.
Compared to SVGO, svgtidy is fastโup to 100x faster for single icons and 50x faster for complex files.
---
- Blazing Fast: Built with Rust, xmlparser and rayon.
- Batch Processing: Parallel directory scanning and optimization.
- AST-based: Robust DOM-like mutations.
- Configurable: Toggle plugins, set precision, and precision formatting.
- Cross-Platform: Runs on CLI, Node.js, and in the browser (WASM).
Use svgtidy directly in your JavaScript/TypeScript projects.
``bash`
npm install svgtidy
Usage:
`javascript
import { optimize } from 'svgtidy';
const svg = '';
const optimized = optimize(svg);
console.log(optimized);
`
Install the binary tool using Rust's cargo:
`bashInstall from crates.io (Recommended)
cargo install svgtidy
Usage:
`bash
Optimize a single file
svgtidy input.svg -o output.svgOptimize a directory (recursive)
svgtidy icons/ -o dist/Set precision and disable specific plugins
svgtidy input.svg -o output.svg -p 5 --disable removeTitle
`$3
Install the dedicated Vite plugin:
`bash
npm install vite-plugin-svgtidy
`Usage (
vite.config.js):`javascript
import svgtidy from 'vite-plugin-svgtidy';export default {
plugins: [svgtidy()]
}
`$3
Install the Webpack loader:
`bash
npm install svgtidy-loader
`Usage (
webpack.config.js):`javascript
module.exports = {
module: {
rules: [
{
test: /\.svg$/,
use: [
{ loader: 'svgtidy-loader' }
]
}
]
}
}
`โ๏ธ Configuration (CLI)
`text
Usage: svgtidy [OPTIONS] Arguments:
Input file or directory
Options:
-o, --output
`๐ Plugins
svgtidy enables these plugins by default to ensure maximum reduction:| Plugin Name | Description |
| :--- | :--- |
|
removeDoctype | Removes declaration. |
| removeXMLProcInst | Removes instructions. |
| removeComments | Removes comments. |
| removeMetadata | Removes elements. |
| removeTitle | Removes elements. |
| removeDesc | Removes elements. |
| removeEditorsNSData| Removes editor namespaced attributes (Inkscape, etc.). |
| cleanupAttrs | Trims attribute whitespace. |
| mergePaths | Merges adjacent paths with same attributes. |
| convertShapeToPath | Converts basic shapes (rect, circle) to path. |
| convertPathData | Optimizes path commands (relative, precision). |
| convertTransform | Collapses multiple transforms into one. |
| removeHiddenElems | Removes hidden elements (display="none"). |
| removeEmptyText | Removes empty text nodes. |
| convertColors | Converts colors (rgb to hex, etc.). |
| collapseGroups | Removes redundant tags. |
| moveGroupAttrsToElems| Moves attributes from groups to elements. |
| moveElemsAttrsToGroup| Moves common attributes from elements to groups. |(And more...)
๐ Benchmarks
| Scenario | Input Size | svgtidy Time | vs SVGO (Node) |
| :--- | :--- | :--- | :--- |
| Simple Icon | ~0.5 KB | ~16 ยตs | ~100x Faster |
| Complex SVG | ~30 KB | ~1 ms | ~50x Faster |
๏ฟฝ Development
$3
To build the WASM package for web usage (NPM):
`bash
wasm-pack build --target bundler --out-dir npm/svgtidy-wasm
`$3
- Rust:
cargo test
- JS/WASM: cd npm && npm run test:suite`Contributions are welcome!
1. Fork the repository.
2. Create a feature branch.
3. Submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.