Optimize SVG files with SVGO via ESLint.
- SVGO v4.0.0+
- ESLint v9.5.0+
npm install eslint-plugin-svgo -Dyarn add eslint-plugin-svgo -Dpnpm add eslint-plugin-svgo -D// eslint.config.js
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// ...other flat configs
pluginSVGO.configs.recommended,
])// eslint.config.js
import { defineConfig } from 'eslint/config'
import { parserPlain, plugin as pluginSVGO } from 'eslint-plugin-svgo'
export default defineConfig([
// ...other flat configs
{
// Optional plugin name
name: 'svgo',
// Only check SVG files
files: ['**/*.svg'],
// Ignore matched SVG files
ignores: ['icons/foo.svg', 'images/**/*.svg'],
// Use SVGO plugins
plugins: {
svgo: pluginSVGO,
},
// Use parser
languageOptions: {
parser: parserPlain,
},
rules: {
'svgo/svgo': [
'error',
{
// SVGO configuration
floatPrecision: 2,
js2svg: {
pretty: true,
},
plugins: [
// Plugin: preset-default
'preset-default',
// Override preset-default
{
name: 'preset-default',
params: {
overrides: {
// Disable plugin
cleanupAttrs: false,
// Custom plugin parameters
cleanupIds: {
minify: false,
},
},
},
},
// Plugin name
'cleanupIds',
// Plugin with parameters
{
name: 'cleanupIds',
params: {
minify: false,
},
},
],
},
],
},
},
])Enable xml support:
{
"eslint.validate": ["xml"]
}You should ignore **/*.svg in your eslint-plugin-prettier configuration or add it to .prettierignore.
Use SVGO to optimize SVG files.
- type:
boolean | string - default:
undefined
Use an external config file, e.g. svgo.config.mjs.
Set to true to auto-load the config.
Set to path/to/your/svgo.config to specify a custom config file path.
Note
If you use svgoConfig, all other rule options will be ignored unless no config file is found.
- type:
string - default:
context.filename
Can be used by plugins, e.g. prefixIds.
- type:
boolean - default:
false
Run multiple optimization passes to ensure all optimizations are applied.
- type:
number - default:
3
Precision for floating point numbers. This value is passed to each plugin that supports this parameter.
- type:
'base64' | 'enc' | 'unenc' - default:
undefined
Output as a Data URI string.
- type:
object - default:
undefined
Options for rendering optimized SVG from AST. See svgo/lib/types.d.ts for details.
The following options are not supported:
regEntitiesregValEntitiesencodeEntity
- type:
array - default:
['preset-default']
Plugins configuration. See Plugins | SVGO Documentation for details.
ESLint requires rule options to be compatible with JSON schema, so function and regexp types are not supported in the svgo/svgo rule options. See below:
js2svgregEntities-functionregValEntities-functionencodeEntity-function
pluginsprefixIdsprefix-function(onlybooleanandstringare supported)
addClassesToSVGElementclassName-function(onlystringis supported)classNames-function(onlystringis supported)
convertColorscurrentColor-regexp(onlybooleanandstringare supported)
removeCommentspreservePatterns-regexp(onlybooleanandstringare supported)
- Any custom plugins
fn-function
Tip
You can still support all of these by using the svgoConfig option and a SVGO config file.