Swift native implementation of SVGO — the popular Node.js SVG optimizer.
SVGift provides the same optimization capabilities as SVGO, with 49 built-in plugins (34 enabled by default). It is available as both a CLI tool and a Swift library.
brew install okooo5km/tap/svgiftOr manually:
brew tap okooo5km/tap
brew install svgiftgit clone https://github.com/okooo5km/SVGift.git
cd svgo-swift
swift build -c release
# Binary at .build/release/svgift.package(url: "https://github.com/okooo5km/SVGift.git", from: "0.1.0")Then add "SVGift" to your target's dependencies.
# Optimize a single file (output to stdout)
svgift input.svg
# Optimize and write to a file
svgift input.svg -o output.svg
# Read from stdin
cat input.svg | svgift -
# Recursively optimize a directory (in-place)
svgift -r icons/
# Recursively optimize to a different directory
svgift -r icons/ -o optimized/
# Enable multipass optimization
svgift input.svg --multipass -o output.svg
# Pretty-print output
svgift input.svg --pretty
# Use a config file
svgift input.svg --config svgo.config.json -o output.svg
# List available plugins
svgift --show-pluginsimport SVGift
// Basic usage with default plugins
let input = "<svg>...</svg>"
let result = try optimize(input)
print(result.data)
// Custom options
var options = OptimizeOptions(
plugins: presetDefaultPlugins,
pluginRegistry: builtinPluginRegistry
)
options.multipass = true
options.js2svg.pretty = true
let result = try optimize(input, options: options)Create a JSON config file to customize plugin behavior:
{
"multipass": true,
"plugins": [
"preset-default",
{
"name": "removeAttrs",
"params": {
"attrs": ["fill", "stroke"]
}
},
{
"name": "addAttributesToSVGElement",
"params": {
"attributes": {
"xmlns": "http://www.w3.org/2000/svg"
}
}
}
]
}{
"plugins": [
"preset-default",
{
"name": "removeComments",
"enabled": false
}
]
}{
"plugins": [
{
"name": "preset-default",
"params": {
"overrides": {
"cleanupIds": {
"minify": false
}
}
}
}
]
}49 built-in plugins are available. See docs/plugins.md for the complete list with descriptions and parameters.
Default plugins (34): 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
SVGift aims for full compatibility with SVGO's optimization output. The test suite validates against 363 SVGO test fixtures with 100% pass rate (L1 byte-exact and L2 normalized matching).
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Run tests (
swift test) - Commit your changes
- Push to the branch and open a Pull Request
MIT License. See LICENSE for details.
- SVGO — the original Node.js SVG optimizer
- Built by okooo5km (十里)