An ESLint plugin that brings the power of SVGO to your SVG optimization workflow.
Optimize SVG files with SVGO via ESLint rules, enabling seamless integration into your development and CI/CD pipelines.
- 🚀 Full integration with SVGO v4+
- 📋 Lint SVG files with ESLint
- ⚙️ Highly configurable SVGO plugins
- 🔧 Support for external SVGO config files
- 📦 Works with ESLint's flat config format
- 🎯 TypeScript support
| Package | Version |
|---|---|
| SVGO | v4.0.0+ |
| ESLint | v9.5.0+ |
| Node.js | v20.0.0+ |
Choose your package manager:
npm i eslint-plugin-svgo -Dyarn add eslint-plugin-svgo -Dpnpm add eslint-plugin-svgo -DThe quickest way to get started - use the recommended configuration:
// eslint.config.js
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
pluginSVGO.configs.recommended,
])This will automatically:
- Target all
**/*.svgfiles - Enable the
svgo/svgorule with sensible defaults - Use the
preset-defaultSVGO plugin configuration
For more control over the SVGO optimization process, configure the plugin manually:
// eslint.config.js
import { defineConfig } from 'eslint/config'
import { parserPlain, plugin as pluginSVGO } from 'eslint-plugin-svgo'
export default defineConfig([
{
name: 'svgo',
files: ['**/*.svg'],
ignores: ['icons/foo.svg', 'images/**/*.svg'],
plugins: {
svgo: pluginSVGO,
},
languageOptions: {
parser: parserPlain,
},
rules: {
'svgo/svgo': [
'error',
{
floatPrecision: 2,
js2svg: {
pretty: true,
},
plugins: [
'preset-default',
{
name: 'cleanupIds',
params: {
minify: false,
},
},
],
},
],
},
},
])You can configure SVGO plugins in two ways:
1. By plugin name (using defaults):
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': [
'error',
{
plugins: ['cleanupIds', 'convertColors'],
},
],
},
},
])2. With custom parameters:
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': [
'error',
{
plugins: [
{
name: 'cleanupIds',
params: {
minify: false,
},
},
],
},
],
},
},
])3. Override preset defaults:
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': [
'error',
{
plugins: [
{
name: 'preset-default',
params: {
overrides: {
cleanupAttrs: false,
cleanupIds: {
minify: false,
},
},
},
},
],
},
],
},
},
])Enable ESLint validation for SVG files by adding to your .vscode/settings.json:
{
"eslint.validate": ["xml", "svg"]
}Add SVG files to your .prettierignore to prevent conflicts with ESLint:
**/*.svg
Or configure in package.json:
{
"prettier": {
"ignorePatterns": ["**/*.svg"]
}
}Optimize SVG files using SVGO.
- Type:
boolean | string - Default:
undefined - Example:
trueor'./svgo.config.js'
Use an external SVGO config file. This is useful when you need to use features that aren't JSON-schema compatible (like function-based configuration).
true— Auto-loadsvgo.config.mjsorsvgo.config.jsfrom your project root'path/to/config'— Use a specific config file path
Behavior details:
- When
svgoConfigistrue, the rule auto-searches for a SVGO config file from the SVG file directory upward. - If no external config is found with
svgoConfig: true, the rule falls back to in-rule options (plugins,floatPrecision, etc.). - When
svgoConfigis a string path, that path must exist and be loadable, otherwise the rule reports an optimization error. - When an external config is loaded successfully, other rule options (except
path) are ignored.
- Type:
'diff' | 'summary' - Default:
'diff'
Choose how diagnostics are reported when SVG can be optimized:
'diff'— Report each text diff as a separate ESLint message (detailed)'summary'— Report a single ESLint message with a full-file autofix (quiet CI output)
Set ESLINT_PLUGIN_SVGO_DEBUG=1 (or DEBUG=eslint-plugin-svgo) to include error stacks in diagnostic messages.
- Type:
string - Default:
context.filename
Override the file path. Some plugins (like prefixIds) use this for context.
- Type:
boolean - Default:
false
Enable multiple optimization passes to ensure all optimizations are fully applied.
- Type:
number - Default:
3
Set precision for floating-point numbers in the output (e.g., 2 converts 1.234 to 1.23). This is passed to plugins that support it.
- Type:
'base64' | 'enc' | 'unenc' - Default:
undefined
Output format for Data URIs:
'base64'— Base64 encoded'enc'— URL-encoded'unenc'— Unencoded
- Type:
object - Default:
undefined
Options for rendering the optimized SVG from the AST. See SVGO's js2svg documentation for all available options.
Common options:
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': [
'error',
{
js2svg: {
pretty: true, // Add newlines and indentation
indent: 2, // Indentation size
eol: 'lf', // Line ending style
final: true, // Add final newline
},
},
],
},
},
])Unsupported options:
regEntities,regValEntities,encodeEntity(require functions)
- Type:
array - Default:
['preset-default']
List of SVGO plugins to apply. See SVGO's plugin documentation for available plugins and their parameters.
Example configuration:
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': [
'error',
{
plugins: [
'preset-default',
'removeDoctype',
{
name: 'cleanupIds',
params: {
minify: false,
prefix: 'icon-',
},
},
],
},
],
},
},
])ESLint rule options must be JSON schema compatible, which means functions and regular expressions cannot be used directly in rule options. However, you can work around this using external config files.
js2svg:
regEntities— functionregValEntities— functionencodeEntity— function
plugins:
prefixIds.prefix— function (usebooleanorstringinstead)addClassesToSVGElement.className— function (usestringinstead)convertColors.currentColor— regexp (usebooleanorstringinstead)removeComments.preservePatterns— regexp (usebooleanorstringinstead)
For advanced configurations requiring functions or regexes, create an external config file:
// svgo.config.mjs
export default {
plugins: [
'preset-default',
{
name: 'prefixIds',
params: {
prefix: node => `svg-${node.attributes.id}`,
},
},
],
}Then reference it in your ESLint config:
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': ['error', { svgoConfig: './svgo.config.mjs' }],
},
},
])Tip: External config files are the most flexible way to configure SVGO with all its advanced features.
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': 'error',
},
},
])import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': [
'error',
{
floatPrecision: 2,
},
],
},
},
])import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': [
'error',
{
js2svg: {
pretty: true,
indent: 2,
},
},
],
},
},
])import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': [
'error',
{
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeDoctype: false,
removeComments: false,
},
},
},
],
},
],
},
},
])import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// Your other configs...
{
...pluginSVGO.configs.recommended,
rules: {
'svgo/svgo': ['error', { svgoConfig: true }],
},
},
])With svgo.config.mjs:
export default {
multipass: true,
floatPrecision: 2,
plugins: [
'preset-default',
'removeDoctype',
{
name: 'cleanupIds',
params: { minify: false },
},
],
}- antfu/eslint-plugin-format — Inspiration for plugin structure
- SVGO — The powerful SVG optimization library
- ESLint — Pluggable linting utility