svag
An SVG minifier.
Features
- Remove XML declarations, DOCTYPE, comments
- Remove metadata, title, desc elements
- Remove Inkscape/Sodipodi namespaces and elements
- Remove unused namespace declarations
- Collapse unnecessary groups
- Remove hidden and empty elements
- Minify path data (reduce precision, implicit commands)
- Minify colors (
#ff0000→red,#ffffff→#fff) - Remove default attribute values
- Minify inline styles
- Sort attributes for better gzip
Usage
As a library
use minify;
let svg = r#"<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<!-- A red square -->
<rect x="10" y="10" width="80" height="80" fill="#ff0000" fill-opacity="1"/>
</svg>"#;
let minified = minify.unwrap;
// <svg xmlns="http://www.w3.org/2000/svg" height="100" width="100"><rect fill="red" height="80" width="80" x="10" y="10"/></svg>
As a CLI
# From stdin
|
# From file
# With stats
# 1961 -> 602 bytes (69.3% smaller)
With custom options
use ;
let options = Options ;
let minified = minify_with_options.unwrap;
Installation
Or for the CLI:
Size comparison
Test corpus: tests/corpus/*.svg (3 files, 2.7 KB total)
| File | Original | svag | svgo¹ |
|---|---|---|---|
| Complex Path | 545 B | 335 B (-38.5%) | 336 B (-38.3%) |
| Inkscape Bloated | 1.9 KB | 602 B (-69.3%) | 521 B (-73.4%) |
| Simple | 215 B | 206 B (-4.2%) | 190 B (-11.6%) |
| Total | 2.7 KB | 1.1 KB (-58.0%) | 1.0 KB (-61.5%) |
¹ svgo is a mature, battle-tested Node.js SVG optimizer. The size differences here are minor — svgo has more optimization passes. CLI timing comparisons wouldn't be fair since svgo pays Node.js startup cost; as a library it would perform much better.
To regenerate these benchmarks:
FAQ
Why "svag"?
Because it's swag.
Is it production-ready?
No, but the tests make me reasonably sure it won't mess anything up.
Roadmap
- More optimization passes (match svgo's output sizes)
- SVGO-compatible plugin system
- Streaming support for large files
Inspired by
License
MIT OR Apache-2.0