rollup-plugin-svgo
raw JSON → 2.0.0 verified Mon Apr 27 auth: no javascript
A Rollup plugin that imports SVG files and optimizes them using SVGO (SVG Optimizer). Current stable version is 2.0.0. The plugin processes SVG files through SVGO to remove redundant and useless information (editor metadata, comments, hidden elements, non-optimal values) and exports the cleaned SVG content as a string. Key differentiators: simple integration with Rollup, passes through raw SVG content with `raw: true`, and allows direct configuration of SVGO plugins via plugin options. Unlike generic asset imports, it specifically pairs with SVGO for optimized inline SVG usage in JavaScript bundles.
Common errors
error 'svgo' is not exported from 'rollup-plugin-svgo' ↓
cause Importing as named export instead of default import.
fix
Use
import svgo from 'rollup-plugin-svgo' instead of import { svgo } from 'rollup-plugin-svgo'. error TypeError: svgo is not a function ↓
cause Plugin is used incorrectly, possibly destructured from require.
fix
Use
const svgo = require('rollup-plugin-svgo') (no destructuring). error Error: Unexpected plugin options shape. Expected an array. ↓
cause Passed SVGO plugins as an object instead of array.
fix
Wrap plugins in array:
plugins: [{ removeViewBox: false }] Warnings
breaking Version 2.0.0 changed plugin signature from `svgo({ raw: true })` to `svgo({ raw: true })`? No changes reported, but ensure compatibility with Rollup 3+. ↓
fix Update to latest rollup-plugin-svgo and Rollup 3 or 4.
gotcha If you pass no options, defaults apply: removeViewBox: false and removeDimensions: true. This may strip width/height attributes unexpectedly. ↓
fix Set removeDimensions: false if you want to preserve SVG dimensions.
gotcha SVGO options are passed directly and must follow SVGO's plugin array format. A common mistake is passing an object instead of an array. ↓
fix Wrap plugin configs in an array as required by SVGO.
deprecated The `raw: true` option disables all SVGO processing; not deprecated but may produce unexpected results if SVGO config is expected. ↓
fix Use `raw: true` intentionally; ensure resulting SVG is not optimized.
Install
npm install rollup-plugin-svgo yarn add rollup-plugin-svgo pnpm add rollup-plugin-svgo Imports
- default wrong
import { svgo } from 'rollup-plugin-svgo'correctimport svgo from 'rollup-plugin-svgo' - svgo (require) wrong
const { svgo } = require('rollup-plugin-svgo')correctconst svgo = require('rollup-plugin-svgo') - type import (TypeScript)
import svgo from 'rollup-plugin-svgo'
Quickstart
import svgo from 'rollup-plugin-svgo';
export default {
input: 'src/main.js',
output: {
dir: 'dist',
format: 'esm'
},
plugins: [
svgo({
plugins: [
{ removeViewBox: false },
{ removeDimensions: true }
]
})
]
};