A comprehensive SVG optimization library for the Dart/Flutter ecosystem, providing powerful tools to reduce file sizes while preserving visual quality. Inspired by proven optimization techniques used in popular SVG tools.
| Package | Description | pub.dev |
|---|---|---|
| svgo | Core SVGO library | |
| svgo_cli | Command-line interface | |
| svgo_hooks_example | Build hooks example |
- ✅ Parse SVG files into an Abstract Syntax Tree (XAST)
- ✅ Apply various optimization plugins
- ✅ Serialize optimized AST back to SVG string
- ✅ Fully configurable with plugin system
- ✅ Multipass optimization support
- ✅ 54 builtin optimization plugins (full parity with Node.js SVGO)
- ✅ CSS style parsing and computation
- ✅ Path data manipulation utilities
- ✅ Command-line interface
Add svgo to your pubspec.yaml:
dependencies: svgo: anyThen use it in your Dart code:
import'package:svgo/svgo.dart'; voidmain(){final input =''' <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"> <!-- A red rectangle --> <rect x="0" y="0" width="100" height="100" fill="red"/> </svg> '''; final result =optimize(input); print(result.data); // Output: <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100" fill="red"/></svg> }Install globally:
dart pub global activate svgo_cliThen use it:
# Optimize a single file (overwrites input) svgo input.svg # Optimize to output directory svgo -o dist input.svg # Optimize all SVGs in a directory svgo "src/**/*.svg"# Multipass optimization with custom precision svgo -m -p 2 icon.svgfinal result =optimize(input, SvgoConfig( plugins: [presetDefault], ));final result =optimize(input, SvgoConfig( plugins: [ removeComments, removeMetadata, cleanupNumericValues.withParams( CleanupNumericValuesParams(floatPrecision:2), ), ], ));final result =optimize(input, SvgoConfig( multipass:true, ));cleanupAttrs- Cleanup attributes from newlines, trailing spacescleanupNumericValues- Round numeric values, remove default units
removeComments- Remove commentsremoveDesc- Remove<desc>elementsremoveDoctype- Remove DOCTYPE declarationremoveEditorsNSData- Remove editor-specific namespacesremoveEmptyAttrs- Remove empty attributesremoveEmptyContainers- Remove empty container elementsremoveEmptyText- Remove empty text elementsremoveMetadata- Remove<metadata>elementsremoveTitle- Remove<title>elementsremoveUnusedNS- Remove unused namespace declarationsremoveUselessDefs- Remove elements in<defs>without idremoveXMLProcInst- Remove XML processing instructions
convertColors- Convert color values to shorter formatsconvertEllipseToCircle- Convert ellipses to circles when rx=ryconvertShapeToPath- Convert shapes to path elements
collapseGroups- Collapse useless groupsmoveElemsAttrsToGroup- Move common attributes to parent groupmoveGroupAttrsToElems- Move group transforms to childrensortAttrs- Sort element attributessortDefsChildren- Sort<defs>children for better compression
mergeStyles- Merge multiple style elements
Version note: Support for build hooks was introduced in Dart 3.10.
The svgo_hooks_example package demonstrates how to use Dart Hooks to automatically optimize SVG files during build time. See the svgo_hooks_example package for a complete implementation.
This project uses Melos for monorepo management.
# Install melos dart pub global activate melos # Bootstrap (install dependencies for all packages) melos bootstrap # Run analysis melos analyze # Run tests melos test# Format code melos formatMIT License - Copyright (c) 2025 iota9star
See LICENSE for details.
Thanks to SVGO for inspiration and reference.
