Documentation
¶
Overview ¶
Package plugin provides the plugin interface, registry, preset system, and execution engine for the SVG optimization pipeline.
This mirrors SVGO's plugin architecture:
- Plugins are functions that receive the AST + params and return a Visitor
- Plugins are registered in a global registry by name
- Presets group multiple plugins into an ordered execution list
- invokePlugins iterates plugins sequentially, calling each fn then visiting the AST
Index ¶
- Variables
- func Has(name string) bool
- func InvokePlugins(root *svgast.Root, info *PluginInfo, pluginConfigs []PluginConfig, ...) error
- func InvokeResolved(root *svgast.Root, info *PluginInfo, plugins []*ResolvedPlugin)
- func Names() []string
- func Register(p *Plugin)
- type Plugin
- type PluginConfig
- type PluginFunc
- type PluginInfo
- type ResolvedPlugin
Constants ¶
This section is empty.
Variables ¶
var PresetDefaultPluginNames = []string{
"removeDoctype",
"removeXMLProcInst",
"removeComments",
"removeDeprecatedAttrs",
"removeMetadata",
"removeEditorsNSData",
"cleanupAttrs",
"mergeStyles",
"inlineStyles",
"minifyStyles",
"cleanupIds",
"removeUselessDefs",
"cleanupNumericValues",
"convertColors",
"removeUnknownsAndDefaults",
"removeNonInheritableGroupAttrs",
"removeUselessStrokeAndFill",
"cleanupEnableBackground",
"removeHiddenElems",
"removeEmptyText",
"convertShapeToPath",
"convertEllipseToCircle",
"moveElemsAttrsToGroup",
"moveGroupAttrsToElems",
"collapseGroups",
"convertPathData",
"convertTransform",
"removeEmptyAttrs",
"removeEmptyContainers",
"mergePaths",
"removeUnusedNS",
"sortAttrs",
"sortDefsChildren",
"removeDesc",
}
PresetDefaultPluginNames lists the 34 plugins in preset-default order. Matches SVGO's plugins/preset-default.js exactly.
Functions ¶
func InvokePlugins ¶
func InvokePlugins( root *svgast.Root, info *PluginInfo, pluginConfigs []PluginConfig, globalOverrides map[string]any, ) error
InvokePlugins resolves plugin configs and invokes them on the AST. This is the main entry point called from the optimize pipeline.
It matches SVGO's invokePlugins flow:
- For each plugin config, resolve it (find the builtin fn, merge params)
- Call plugin.fn(ast, params, info) to get a Visitor
- If visitor is non-nil, call Visit(ast, visitor)
func InvokeResolved ¶
func InvokeResolved(root *svgast.Root, info *PluginInfo, plugins []*ResolvedPlugin)
InvokeResolved invokes already-resolved plugins on the AST. Used by presets that have already resolved their sub-plugins.
Types ¶
type Plugin ¶
type Plugin struct {
// Name is the unique plugin identifier (e.g. "removeComments").
Name string
// Description is a human-readable description of what the plugin does.
Description string
// Fn is the plugin implementation function.
Fn PluginFunc
// IsPreset indicates this is a preset (contains sub-plugins).
IsPreset bool
// Plugins holds the sub-plugins for presets.
// Only used when IsPreset is true.
Plugins []*Plugin
}
Plugin represents a registered plugin with metadata.
func CreatePreset ¶
CreatePreset creates a preset plugin that groups multiple plugins. Matches SVGO's createPreset() function in plugins.js.
A preset is itself a plugin whose fn calls invokePlugins on its sub-plugins. Preset params support:
- "overrides": map[string]any — per-plugin param overrides (or false to disable)
- "floatPrecision": float64 — global float precision for all sub-plugins
func RegisterPresetDefault ¶
func RegisterPresetDefault() *Plugin
RegisterPresetDefault creates and registers the preset-default plugin. This should be called after all individual plugins are registered. Returns the created preset plugin.
type PluginConfig ¶
type PluginConfig struct {
// Name is the plugin name (required).
Name string
// Params is plugin-specific parameters (optional).
Params map[string]any
// Fn is a custom plugin function (optional).
// If nil, the builtin plugin is looked up by Name.
Fn PluginFunc
}
PluginConfig represents a user-specified plugin configuration. This is the input format — it gets resolved against the registry.
type PluginFunc ¶
PluginFunc is the function signature for plugin implementations.
It receives:
- root: the full AST tree
- params: merged parameters (plugin defaults + global overrides + user overrides)
- info: contextual information (file path, multipass count)
It returns a Visitor to traverse the AST, or nil to skip this plugin. Matches SVGO's: (ast, params, info) => visitor | null
type PluginInfo ¶
type PluginInfo struct {
// Path is the file path of the SVG being optimized.
Path string
// MultipassCount is the current pass number (0-based) during multipass optimization.
MultipassCount int
}
PluginInfo provides contextual information to plugin functions. Matches SVGO's info object passed to plugin fn.
type ResolvedPlugin ¶
type ResolvedPlugin struct {
Name string
Params map[string]any
Fn PluginFunc
}
ResolvedPlugin is a plugin ready for execution with its merged params. This is the result of resolving a PluginConfig against the registry.