eslint-plugin-svgo v0.7.0
eslint-plugin-svgo
Optimize SVG files with SVGO using ESLint.
Supported
- SVGO v4.0.0-rc.1+
- ESLint v9.5.0+
Install
npm install eslint-plugin-svgo -Dyarn add eslint-plugin-svgo -Dpnpm add eslint-plugin-svgo -DBasic Usage
// eslint.config.js
import { defineConfig } from 'eslint/config'
import pluginSVGO from 'eslint-plugin-svgo'
export default defineConfig([
// ...other flat configs
pluginSVGO.configs.recommended,
])Advanced Usage
// eslint.config.js
import { defineConfig } from 'eslint/config'
import { parserPlain, plugin as pluginSVGO } from 'eslint-plugin-svgo'
export default defineConfig([
// ...other flat configs
{
// plugin name, optional
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 config
floatPrecision: 2,
js2svg: {
pretty: true,
},
plugins: [
// plugin preset-default
'preset-default',
// overrides preset-default
{
name: 'preset-default',
params: {
overrides: {
// disable plugin
cleanupAttrs: false,
// custom plugin params
cleanupIds: {
minify: false,
},
},
},
},
// plugin name
'cleanupIds',
// plugin with params
{
name: 'cleanupIds',
params: {
minify: false,
},
},
],
},
],
},
},
])Intergrated
VSCode
Enable xml support.
{
"eslint.validate": ["xml"]
}Prettier
You should ignores **/*.svg for eslint-plugin-prettier config or add it to .prettierignore
Rules
svgo/svgo
Use svgo to optimize SVG files.
Options
Default options:
{
"js2svg": {
"indent": 2,
"pretty": true
},
"plugins": ["preset-default"]
}svgoConfig
- type:
boolean | string - default:
undefined
Use an external config file, e.g: svgo.config.mjs.
Set to true, svgo will auto load config.
Set to path/to/your/svgo.config to custom config file path.
!NOTE If you use
svgoConfig, any other rule options will be ignored unless no config file is found.
path
- type:
string - default:
context.filename
Can be used by plugins, for example prefixids.
multipass
- type:
boolean - default:
false
Pass over SVGs multiple times to ensure all optimizations are applied.
floatPrecision
- type:
number - default:
3
Precision of floating point numbers. Will be passed to each plugin that supports this param.
datauri
- type:
'base64' | 'enc' | 'unenc' - default:
undefined
Output as Data URI string.
js2svg
- type:
object - default:
{ indent: 2, pretty: true }
Options for rendering optimized SVG from AST. Check svgo/lib/types.d.ts for details.
Options bellow are not supported:
regEntitiesregValEntitiesencodeEntity
plugins
- type:
array - default:
['preset-default']
Plugins configuration. Check Plugins | SVGO Documentation for details.
Limitation
For ESLint use json schema compatible syntax as its rule options, so function, regexp types are not supported in rule svgo/svgo options. See bellow:
js2svgregEntities-functionregValEntities-functionencodeEntity-function
pluginsprefixIdsprefix-function, but typebooleanandstringis supported
addClassesToSVGElementclassName-function, but typestringis supportedclassNames-function, but typestringis supported
convertColorscurrentColor-regexp, but typebooleanandstringis supported
removeCommentspreservePatterns-regexp, but typebooleanandstringis supported
- any custom plugins
fn-function
!TIP You can still support all of them by using options svgoConfig and a svgo config file.
Credits
License
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
11 months ago
12 months ago
12 months ago
1 year ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
6 years ago
6 years ago