Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

svgo

БесплатноБез исполняемых скриптовНе проверен

SVGO is a Node.js tool that optimizes SVG files by removing editor metadata, comments and redundant attributes and by shortening paths. Use when asked to…

TerminalSkillsот TerminalSkills★ 221 звезда на GitHub

Об этом скилле

SVGO

Overview

SVGO (SVG Optimizer) parses an SVG, runs a list of plugins and prints a smaller file. It removes editor metadata and comments, collapses groups, shortens path data and minifies attributes. The current release is 4.1.0 (Node.js 16+). Version 4 changed two defaults that older guides still assume: removeViewBox and removeTitle are no longer part of preset-default, so the viewBox and the <title> now survive unless you add those plugins. Plugin names such as removeScriptElement were renamed (removeScripts). The commands below were run against svgo 4.1.0.

Instructions

Install and run the CLI

$ npm install --save-dev svgo
$ npx svgo icons/cart.svg -o icons/cart.min.svg
icons/cart.svg:
Done in 9 ms!
0.473 KiB - 59.9% = 0.189 KiB

The CLI prints the saved percentage for every file. Without -o, the input file is overwritten in place, so commit or copy first.

Task Command
One file to another npx svgo in.svg -o out.svg
Folder, recursive, to a new folder npx svgo -r -f ./icons --output ./icons-optimized
Folder, rewrite in place npx svgo -r -f ./icons
Print to stdout npx svgo in.svg -o -
Pretty-printed output npx svgo in.svg --pretty --indent 2 -o -
From stdin cat in.svg | npx svgo -i - -o -
Several passes npx svgo --multipass in.svg -o out.svg
Fewer decimals npx svgo -p 1 in.svg -o out.svg
Data URI for CSS npx svgo --datauri unenc in.svg -o -
List plugins npx svgo --show-plugins
Use a specific config npx svgo --config svgo.config.mjs in.svg -o out.svg

SVGO loads svgo.config.js, .mjs or .cjs from the current directory automatically, so a config next to your icons changes every run, including the ones in the table above.

Configuration

Use ESM (export default) in a project with "type": "module" and module.exports in a CommonJS project; .mjs and .cjs work in either.

// svgo.config.mjs
/** @type {import('svgo').Config} */
export default {
  multipass: true,
  plugins: [
    {
      name: "preset-default",
      params: {
        overrides: {
          mergePaths: false,         // keeps separate paths for icon animation
          convertShapeToPath: false, // keeps <rect> and <circle> readable
        },
      },
    },
    "removeDimensions", // drop width/height, size the icon with CSS from the viewBox
    "removeXMLNS",      // only for SVG pasted inline into HTML, never for standalone .svg files
  ],
};

Plugins outside preset-default go after it in the list. removeViewBox, removeTitle, removeDimensions, removeXMLNS, removeStyleElement, prefixIds and reusePaths are opt-in. Putting an opt-in plugin in overrides (for example removeViewBox: false) throws "You are trying to configure removeViewBox which is not part of preset-default", so delete such lines left from v3 configs.

Programmatic API

optimize is synchronous, takes a string, and throws SvgoParserError on invalid SVG. Pass path so the error names the file. SVGO v4 has named exports only.

// src/svg/optimize.ts
import { optimize, type Config } from "svgo";

const config: Config = {
  multipass: true,
  plugins: ["preset-default", "removeDimensions"],
};

export function optimizeSvg(svg: string, file: string): string {
  return optimize(svg, { ...config, path: file }).data;
}

Batch processing

// scripts/optimize-icons.mjs
import { optimize } from "svgo";
import fs from "node:fs/promises";
import path from "node:path";

export async function optimizeDirectory(inputDir, outputDir) {
  const files = (await fs.readdir(inputDir)).filter((f) => f.endsWith(".svg"));
  await fs.mkdir(outputDir, { recursive: true });
  let before = 0;
  let after = 0;
  for (const file of files) {
    const input = await fs.readFile(path.join(inputDir, file), "utf-8");
    const { data } = optimize(input, { path: file, multipass: true });
    before += Buffer.byteLength(input);
    after += Buffer.byteLength(data);
    await fs.writeFile(path.join(outputDir, file), data);
  }
  console.log(`Optimized ${files.length} files, saved ${((1 - after / before) * 100).toFixed(1)}%`);
}

await optimizeDirectory("icons", "icons-optimized");

Custom plugin

A plugin's fn returns visitors. This one adds a class to every <path>:

const addPathClass = {
  name: "addPathClass",
  fn: () => ({
    element: {
      enter: (node) => {
        if (node.name === "path") node.attributes.class = "icon-path";
      },
    },
  }),
};

optimize(svgString, { plugins: ["preset-default", addPathClass] });

In TypeScript, type it as CustomPlugin imported from svgo.

Build tools

vite-plugin-svgo (3.x) loads .svg imports as optimized raw strings, not as components; it takes the same multipass and plugins options.

// vite.config.ts
import { defineConfig } from "vite";
import svgo
Полный SKILL.md на GitHub

Установить svgo в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

_scores.json

FAQ

Что делает скилл svgo?

SVGO is a Node.js tool that optimizes SVG files by removing editor metadata, comments and redundant attributes and by shortening paths. Use when asked to reduce SVG file size, clean up SVGs exported from Figma, Illustrator or Sketch, configure SVGO plugins, optimize icons in a build, or fix an SVG that broke after optimization (lost viewBox, title or IDs).

Как установить скилл svgo?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл svgo запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить svgo с