ogvs

module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2026 License: MIT

README

ogvs logo

ogvs

A Go implementation of SVGO — the SVG Optimizer

Go Report Card Go Reference Release License


Why?

SVGO is the industry-standard SVG optimizer, written in JavaScript. ogvs is a ground-up Go rewrite with behavioral compatibility — validated against all 363 SVGO test fixtures.

  • Single binary — no Node.js or npm required
  • Zero runtime dependencies — just download and run
  • Fast — 5x to 23x faster than SVGO (benchmarks)
  • Drop-in replacement — same plugins, same defaults, same output

Installation

Homebrew (macOS/Linux)

brew install okooo5km/tap/ogvs

Go install

go install github.com/okooo5km/ogvs/cmd/ogvs@latest

GitHub Releases

Download prebuilt binaries from the Releases page.

CLI Usage

# Optimize a file (in-place)
ogvs input.svg

# Optimize a file to a different output
ogvs input.svg -o output.svg

# Optimize from stdin
cat input.svg | ogvs -

# Optimize from string
ogvs -s '<svg><title>test</title></svg>'

# Optimize all SVGs in a folder (recursive)
ogvs -f ./icons -o ./optimized --recursive

# Multi-pass optimization (run until stable)
ogvs input.svg --multipass

# Pretty-print output
ogvs input.svg --pretty --indent 2

# Custom precision for numeric values
ogvs input.svg --precision 2

# Use a custom config file
ogvs input.svg --config ogvs.config.yaml

# List all available plugins
ogvs --show-plugins

Configuration

ogvs automatically discovers ogvs.config.yaml or ogvs.config.yml in the current directory (searching upward). You can also specify a config file with --config.

# ogvs.config.yaml
multipass: true
floatPrecision: 3

js2svg:
  pretty: true
  indent: 2

plugins:
  # Use preset-default with overrides
  - name: preset-default
    params:
      overrides:
        removeViewBox: false
        cleanupIds:
          minify: false

  # Enable optional plugins
  - name: removeXlink
  - name: prefixIds
    params:
      prefix: "icon-"
  - name: removeDimensions

Built-in Plugins

ogvs includes all 53 plugins from SVGO.

Preset Default (34 plugins)

These plugins run by default (in this order):

Plugin Description
removeDoctype Remove <!DOCTYPE> declaration
removeXMLProcInst Remove XML processing instructions
removeComments Remove comments
removeDeprecatedAttrs Remove deprecated attributes
removeMetadata Remove <metadata>
removeEditorsNSData Remove editors' namespace data
cleanupAttrs Clean up attribute whitespace
mergeStyles Merge multiple <style> elements
inlineStyles Move CSS rules to inline style attributes
minifyStyles Minify <style> content
cleanupIds Minify and remove unused IDs
removeUselessDefs Remove empty or useless <defs>
cleanupNumericValues Round numeric values, remove defaults
convertColors Convert colors to shorter forms
removeUnknownsAndDefaults Remove unknown elements/attributes and defaults
removeNonInheritableGroupAttrs Remove non-inheritable presentational attributes from groups
removeUselessStrokeAndFill Remove useless stroke and fill
cleanupEnableBackground Remove or clean up enable-background
removeHiddenElems Remove hidden or zero-sized elements
removeEmptyText Remove empty <text> elements
convertShapeToPath Convert basic shapes to <path>
convertEllipseToCircle Convert <ellipse> with equal radii to <circle>
moveElemsAttrsToGroup Move common attributes to parent <g>
moveGroupAttrsToElems Move <g> attributes to child elements (if single child)
collapseGroups Collapse useless <g> elements
convertPathData Optimize path data: shorten commands, remove redundancies
convertTransform Collapse and optimize transform attributes
removeEmptyAttrs Remove empty attributes
removeEmptyContainers Remove empty container elements
mergePaths Merge adjacent <path> elements
removeUnusedNS Remove unused namespace declarations
sortAttrs Sort element attributes for consistency
sortDefsChildren Sort <defs> children for consistency
removeDesc Remove <desc>

Optional Plugins (19 more)

These plugins are not included in the default preset and must be enabled explicitly:

Plugin Description
removeXlink Replace xlink:href with href
removeDimensions Remove width/height and add viewBox if missing
removeStyleElement Remove <style> elements
removeScripts Remove <script> elements
removeRasterImages Remove raster image elements
removeViewBox Remove viewBox when possible
removeAttrs Remove attributes by pattern
removeElementsByAttr Remove elements by attribute
removeAttributesBySelector Remove attributes by CSS selector
addAttributesToSVGElement Add attributes to <svg>
addClassesToSVGElement Add classes to <svg>
convertStyleToAttrs Convert style to presentational attributes
cleanupListOfValues Round values in list attributes
convertOneStopGradients Convert single-stop gradients to plain colors
removeOffCanvasPaths Remove <path> elements outside the viewBox
reusePaths Replace duplicated <path> elements with <use>
prefixIds Prefix IDs and classes to avoid conflicts
removeEmptyText Remove empty text elements
removeNonInheritableGroupAttrs Remove non-inheritable group attributes

API Usage (Go)

package main

import (
	"fmt"
	"log"

	"github.com/okooo5km/ogvs/internal/core"
)

func main() {
	input := `<svg xmlns="http://www.w3.org/2000/svg">
		<title>Example</title>
		<circle cx="50" cy="50" r="40"/>
	</svg>`

	result, err := core.Optimize(input, &core.Config{})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(result.Data)
}

Differences from SVGO

ogvs aims for behavioral compatibility with SVGO. Known minor differences:

  • Entity handling: When the SVG contains a DOCTYPE with entity definitions, ogvs outputs the DOCTYPE before the XML processing instruction (SVGO outputs them in the original order).
  • Inline style colors: ogvs does not apply convertColors to inline style attribute values (fill:blue stays as-is, while SVGO may convert to fill:#00f).

Benchmark

Measured with hyperfine on macOS (Apple Silicon). Both tools produce identical output.

Scenario ogvs svgo Speedup
Single file (4 KB) 7.3 ms 169 ms 23x
Single file (22 KB) 11.6 ms 180 ms 15x
Single file (217 KB) 40.6 ms 225 ms 5.5x
Multipass (22 KB) 17.2 ms 183 ms 11x
Batch (20 × 22 KB) 117 ms 296 ms 2.5x

See doc/benchmark.md for full details and reproduction steps.

Development

make build        # Build binary to bin/ogvs
make test         # Run tests with race detection
make test-cover   # Run tests with coverage report
make lint         # Run golangci-lint
make fmt          # Format code
make vet          # Run go vet
make clean        # Remove build artifacts

License

MIT © okooo5km(十里)

Directories

Path Synopsis
cmd
ogvs command
Package main is the entry point for the ogvs CLI tool.
Package main is the entry point for the ogvs CLI tool.
internal
cli
Package cli implements the ogvs command-line interface.
Package cli implements the ogvs command-line interface.
collections
Package collections provides SVG specification constants ported from SVGO's _collections.js.
Package collections provides SVG specification constants ported from SVGO's _collections.js.
core
Package core provides the SVG optimization pipeline.
Package core provides the SVG optimization pipeline.
css
Package css provides CSS parsing, cascading, specificity, and selector matching for SVG optimization, ported from SVGO's lib/style.js.
Package css provides CSS parsing, cascading, specificity, and selector matching for SVG optimization, ported from SVGO's lib/style.js.
geom/path
Package path provides SVG path data parsing and stringification, ported from SVGO's lib/path.js.
Package path provides SVG path data parsing and stringification, ported from SVGO's lib/path.js.
geom/transform
Package transform provides SVG transform parsing, matrix operations, and decomposition, ported from SVGO's plugins/_transforms.js.
Package transform provides SVG transform parsing, matrix operations, and decomposition, ported from SVGO's plugins/_transforms.js.
plugin
Package plugin provides the plugin interface, registry, preset system, and execution engine for the SVG optimization pipeline.
Package plugin provides the plugin interface, registry, preset system, and execution engine for the SVG optimization pipeline.
plugins
Package plugins imports all plugin sub-packages to trigger their init() registrations.
Package plugins imports all plugin sub-packages to trigger their init() registrations.
plugins/addattributestosvgelement
Package addattributestosvgelement implements the addAttributesToSVGElement SVGO plugin.
Package addattributestosvgelement implements the addAttributesToSVGElement SVGO plugin.
plugins/addclassestosvgelement
Package addclassestosvgelement implements the addClassesToSVGElement SVGO plugin.
Package addclassestosvgelement implements the addClassesToSVGElement SVGO plugin.
plugins/cleanupattrs
Package cleanupattrs implements the cleanupAttrs SVGO plugin.
Package cleanupattrs implements the cleanupAttrs SVGO plugin.
plugins/cleanupenablebackground
Package cleanupenablebackground implements the cleanupEnableBackground SVGO plugin.
Package cleanupenablebackground implements the cleanupEnableBackground SVGO plugin.
plugins/cleanupids
Package cleanupids implements the cleanupIds SVGO plugin.
Package cleanupids implements the cleanupIds SVGO plugin.
plugins/cleanuplistofvalues
Package cleanuplistofvalues implements the cleanupListOfValues SVGO plugin.
Package cleanuplistofvalues implements the cleanupListOfValues SVGO plugin.
plugins/cleanupnumericvalues
Package cleanupnumericvalues implements the cleanupNumericValues SVGO plugin.
Package cleanupnumericvalues implements the cleanupNumericValues SVGO plugin.
plugins/collapsegroups
Package collapsegroups implements the collapseGroups SVGO plugin.
Package collapsegroups implements the collapseGroups SVGO plugin.
plugins/convertcolors
Package convertcolors implements the convertColors SVGO plugin.
Package convertcolors implements the convertColors SVGO plugin.
plugins/convertellipsetocircle
Package convertellipsetocircle implements the convertEllipseToCircle SVGO plugin.
Package convertellipsetocircle implements the convertEllipseToCircle SVGO plugin.
plugins/convertonestopgradients
Package convertonestopgradients implements the convertOneStopGradients SVGO plugin.
Package convertonestopgradients implements the convertOneStopGradients SVGO plugin.
plugins/convertpathdata
Package convertpathdata implements the convertPathData SVGO plugin.
Package convertpathdata implements the convertPathData SVGO plugin.
plugins/convertshapetopath
Package convertshapetopath implements the convertShapeToPath SVGO plugin.
Package convertshapetopath implements the convertShapeToPath SVGO plugin.
plugins/convertstylettoattrs
Package convertstylettoattrs converts style attribute declarations to SVG presentation attributes.
Package convertstylettoattrs converts style attribute declarations to SVG presentation attributes.
plugins/converttransform
Package converttransform implements the convertTransform SVGO plugin.
Package converttransform implements the convertTransform SVGO plugin.
plugins/inlinestyles
Package inlinestyles inlines CSS rules from <style> elements into inline style attributes, ported from SVGO's inlineStyles.js.
Package inlinestyles inlines CSS rules from <style> elements into inline style attributes, ported from SVGO's inlineStyles.js.
plugins/mergepaths
Package mergepaths implements the mergePaths SVGO plugin.
Package mergepaths implements the mergePaths SVGO plugin.
plugins/mergestyles
Package mergestyles merges multiple <style> elements into one.
Package mergestyles merges multiple <style> elements into one.
plugins/minifystyles
Package minifystyles minifies <style> elements and style attributes using CSS minification.
Package minifystyles minifies <style> elements and style attributes using CSS minification.
plugins/moveelemsattrstogroup
Package moveelemsattrstogroup implements the moveElemsAttrsToGroup SVGO plugin.
Package moveelemsattrstogroup implements the moveElemsAttrsToGroup SVGO plugin.
plugins/movegroupattrstoelems
Package movegroupattrstoelems implements the moveGroupAttrsToElems SVGO plugin.
Package movegroupattrstoelems implements the moveGroupAttrsToElems SVGO plugin.
plugins/prefixids
Package prefixids implements the prefixIds SVGO plugin.
Package prefixids implements the prefixIds SVGO plugin.
plugins/removeattrs
Package removeattrs implements the removeAttrs SVGO plugin.
Package removeattrs implements the removeAttrs SVGO plugin.
plugins/removeattrsbyselector
Package removeattrsbyselector removes attributes from elements matching CSS selectors.
Package removeattrsbyselector removes attributes from elements matching CSS selectors.
plugins/removecomments
Package removecomments implements the removeComments SVGO plugin.
Package removecomments implements the removeComments SVGO plugin.
plugins/removedeprecatedattrs
Package removedeprecatedattrs implements the removeDeprecatedAttrs SVGO plugin.
Package removedeprecatedattrs implements the removeDeprecatedAttrs SVGO plugin.
plugins/removedesc
Package removedesc implements the removeDesc SVGO plugin.
Package removedesc implements the removeDesc SVGO plugin.
plugins/removedimensions
Package removedimensions implements the removeDimensions SVGO plugin.
Package removedimensions implements the removeDimensions SVGO plugin.
plugins/removedoctype
Package removedoctype implements the removeDoctype SVGO plugin.
Package removedoctype implements the removeDoctype SVGO plugin.
plugins/removeeditorsnsdata
Package removeeditorsnsdata implements the removeEditorsNSData SVGO plugin.
Package removeeditorsnsdata implements the removeEditorsNSData SVGO plugin.
plugins/removeelementsbyattr
Package removeelementsbyattr implements the removeElementsByAttr SVGO plugin.
Package removeelementsbyattr implements the removeElementsByAttr SVGO plugin.
plugins/removeemptyattrs
Package removeemptyattrs implements the removeEmptyAttrs SVGO plugin.
Package removeemptyattrs implements the removeEmptyAttrs SVGO plugin.
plugins/removeemptycontainers
Package removeemptycontainers implements the removeEmptyContainers SVGO plugin.
Package removeemptycontainers implements the removeEmptyContainers SVGO plugin.
plugins/removeemptytext
Package removeemptytext implements the removeEmptyText SVGO plugin.
Package removeemptytext implements the removeEmptyText SVGO plugin.
plugins/removehiddenelems
Package removehiddenelems implements the removeHiddenElems SVGO plugin.
Package removehiddenelems implements the removeHiddenElems SVGO plugin.
plugins/removemetadata
Package removemetadata implements the removeMetadata SVGO plugin.
Package removemetadata implements the removeMetadata SVGO plugin.
plugins/removenoninheritablegroupattrs
Package removenoninheritablegroupattrs implements the removeNonInheritableGroupAttrs SVGO plugin.
Package removenoninheritablegroupattrs implements the removeNonInheritableGroupAttrs SVGO plugin.
plugins/removeoffcanvaspaths
Package removeoffcanvaspaths implements the removeOffCanvasPaths SVGO plugin.
Package removeoffcanvaspaths implements the removeOffCanvasPaths SVGO plugin.
plugins/removerasterimages
Package removerasterimages implements the removeRasterImages SVGO plugin.
Package removerasterimages implements the removeRasterImages SVGO plugin.
plugins/removescripts
Package removescripts implements the removeScripts SVGO plugin.
Package removescripts implements the removeScripts SVGO plugin.
plugins/removestyleelement
Package removestyleelement implements the removeStyleElement SVGO plugin.
Package removestyleelement implements the removeStyleElement SVGO plugin.
plugins/removetitle
Package removetitle implements the removeTitle SVGO plugin.
Package removetitle implements the removeTitle SVGO plugin.
plugins/removeunknownsanddefaults
Package removeunknownsanddefaults implements the removeUnknownsAndDefaults SVGO plugin.
Package removeunknownsanddefaults implements the removeUnknownsAndDefaults SVGO plugin.
plugins/removeunusedns
Package removeunusedns implements the removeUnusedNS SVGO plugin.
Package removeunusedns implements the removeUnusedNS SVGO plugin.
plugins/removeuselessdefs
Package removeuselessdefs implements the removeUselessDefs SVGO plugin.
Package removeuselessdefs implements the removeUselessDefs SVGO plugin.
plugins/removeuselessstrokeandfill
Package removeuselessstrokeandfill implements the removeUselessStrokeAndFill SVGO plugin.
Package removeuselessstrokeandfill implements the removeUselessStrokeAndFill SVGO plugin.
plugins/removeviewbox
Package removeviewbox implements the removeViewBox SVGO plugin.
Package removeviewbox implements the removeViewBox SVGO plugin.
plugins/removexlink
Package removexlink implements the removeXlink SVGO plugin.
Package removexlink implements the removeXlink SVGO plugin.
plugins/removexmlns
Package removexmlns implements the removeXMLNS SVGO plugin.
Package removexmlns implements the removeXMLNS SVGO plugin.
plugins/removexmlprocinst
Package removexmlprocinst implements the removeXMLProcInst SVGO plugin.
Package removexmlprocinst implements the removeXMLProcInst SVGO plugin.
plugins/reusepaths
Package reusepaths implements the reusePaths SVGO plugin.
Package reusepaths implements the reusePaths SVGO plugin.
plugins/sortattrs
Package sortattrs implements the sortAttrs SVGO plugin.
Package sortattrs implements the sortAttrs SVGO plugin.
plugins/sortdefschildren
Package sortdefschildren implements the sortDefsChildren SVGO plugin.
Package sortdefschildren implements the sortDefsChildren SVGO plugin.
svgast
Package svgast provides the XAST (XML Abstract Syntax Tree) data model, parser, visitor, and stringifier for SVG documents.
Package svgast provides the XAST (XML Abstract Syntax Tree) data model, parser, visitor, and stringifier for SVG documents.
testkit/assert
Package assert provides assertion helpers for comparing SVG outputs against SVGO test fixture expectations.
Package assert provides assertion helpers for comparing SVG outputs against SVGO test fixture expectations.
testkit/fixture
Package fixture provides loading and parsing of SVGO test fixture files.
Package fixture provides loading and parsing of SVGO test fixture files.
tools
Package tools provides numeric and string utility functions ported from SVGO's lib/svgo/tools.js.
Package tools provides numeric and string utility functions ported from SVGO's lib/svgo/tools.js.
version
Package version provides the version constant for ogvs.
Package version provides the version constant for ogvs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL