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.
โก Features
- Blazing Fast: Built with Rust,
xmlparserandrayon. - 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).
๐ Installation & Usage
๐ฆ Node.js / Web (WASM)
Use svgtidy directly in your JavaScript/TypeScript projects.
Usage:
import from 'svgtidy';
const svg = '<svg>...</svg>';
const optimized = ;
console.log;
๐ฆ CLI (Command Line)
Install the binary tool using Rust's cargo:
# Install from crates.io (Recommended)
# Or build from source
Usage:
# Optimize a single file
# Optimize a directory (recursive)
# Set precision and disable specific plugins
โก Vite
Install the dedicated Vite plugin:
Usage (vite.config.js):
import svgtidy from 'vite-plugin-svgtidy';
export default
๐ฆ Webpack
Install the Webpack loader:
Usage (webpack.config.js):
module.exports =
โ๏ธ Configuration (CLI)
Usage: svgtidy [OPTIONS] <INPUT>
Arguments:
<INPUT> Input file or directory
Options:
-o, --output <OUTPUT> Output file or directory
-p, --precision <PRECISION> Set numeric precision [default: 3]
--enable <ENABLE> Enable specific plugins (comma-separated)
--disable <DISABLE> Disable specific plugins (comma-separated)
--pretty Pretty print output
-h, --help Print help
๐ Plugins
svgtidy enables these plugins by default to ensure maximum reduction:
| Plugin Name | Description |
|---|---|
removeDoctype |
Removes <!DOCTYPE> declaration. |
removeXMLProcInst |
Removes <?xml ... ?> instructions. |
removeComments |
Removes comments. |
removeMetadata |
Removes <metadata> elements. |
removeTitle |
Removes <title> elements. |
removeDesc |
Removes <desc> 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 <g> 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
Build WASM locally
To build the WASM package for web usage (NPM):
Running Tests
- Rust:
cargo test - JS/WASM:
cd npm && npm run test:suite
๐ค Contributing
Contributions are welcome!
- Fork the repository.
- Create a feature branch.
- Submit a Pull Request.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.