npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

svg-power-opt

v1.2.3

Published

Powerful SVG optimizer with zero quality loss and PNG export.

Downloads

25

Readme

svg-power-opt

svg-power-opt is a powerful and extensible SVG optimizer that compresses SVG files without sacrificing visual quality. It wraps SVGO with a curated set of safe plugins, provides advanced CLI reporting, supports aggressive optimization modes, allows custom plugin configuration, and can export high-quality PNG thumbnails using sharp. The tool supports both programmatic usage and Webpack integration — making it suitable for build pipelines, design systems, and automation tasks.


✨ Features

  • 🔧 Safe default optimizations (zero visual loss)
  • Aggressive mode for maximum compression
  • 📁 Supports recursive folders and glob patterns
  • 📊 Before/after size reporting per file
  • ⚠️ Warns on excessive reductions (>10%)
  • ✅ Usable from CLI, Node.js API, or Webpack
  • 📦 Handles buffers, streams, files, and URLs
  • 🖼️ Optional PNG thumbnail export
  • 🔌 Supports custom SVGO plugins
  • 🔄 Configurable concurrency for bulk ops

📦 Installation

npm install -g svg-power-opt

Or use via NPX:

npx svg-power-opt icons/*.svg

🚀 CLI Usage

svg-power-opt <input> [options]

Arguments:

  • <input>: File path or glob pattern (e.g., icons/**/*.svg)

Options:

  • -o, --out <dir>: Output directory (default: optimized)
  • --aggressive: Enable aggressive mode (default: false)
  • --dry-run: Preview changes without writing files
  • --export-png: Also export PNG thumbnail (requires sharp)
  • --concurrency <number>: Number of files to process in parallel (default: CPU count)
  • --plugin <plugin>: Add a custom SVGO plugin (JSON string)

Example:

npx svg-power-opt icons/**/*.svg --out optimized/
npx svg-power-opt icons/**/*.svg --aggressive --export-png --out dist/icons

📊 CLI Reporting

For each file, svg-power-opt shows:

  • Original size
  • Optimized size
  • Compression %
  • Warning if drop >10% in aggressive mode
✔ Optimized: icons/logo.svg → optimized/logo.svg (84.1KB → 66.7KB, ↓20.69%) ⚠️ (Aggressive)

⚙️ API Functions & Usage

| Function Name | Description | Parameters | Returns / Output | | ------------------------------------------------ | ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | | optimizeSVG(svgString, options) | Optimizes an SVG string with given options | svgString (string): raw SVG contentoptions (object): optimization settings (see below) | Optimized SVG result object with .data property (string) | | optimizeSVGFromFile(filePath, options) | Reads an SVG file, optimizes it, returns result | filePath (string): path to SVG fileoptions (object): optimization settings | Promise resolving to optimized SVG result object | | optimizeSVGFromBuffer(buffer, options) | Optimizes SVG from a Buffer | buffer (Buffer): SVG data bufferoptions (object): optimization settings | Optimized SVG result object | | optimizeSVGFromURL(url, options) | Downloads an SVG from a URL and optimizes it | url (string): remote SVG URLoptions (object): optimization settings | Promise resolving to optimized SVG result object | | optimizeSVGStream(readableStream, options) | Optimizes SVG data from a readable stream | readableStream (Readable): input streamoptions (object): optimization settings | Promise resolving to optimized SVG result object | | exportPNGThumbnail(input, outputPath, options) | Converts an SVG (file path or buffer) to PNG thumbnail | input (string | Buffer): SVG file path or data bufferoutputPath (string): PNG output pathoptions (object): PNG export settings (width, height, density, quality) | Promise resolving when PNG file is saved | | validateSVG(svgString) | Validates if the string is a well-formed SVG | svgString (string): raw SVG content | Boolean true if valid, false if invalid |


⚙️ Configuration Options

SVG Optimization

| Option | Type | Description | Default | | ------------------ | ------- | -------------------------------- | ------- | | aggressive | Boolean | Enables deeper optimization | false | | multipass | Boolean | Run multiple optimization passes | true | | plugins | Array | Custom SVGO plugins | [] | | preserveViewBox | Boolean | Prevents viewBox removal | true | | removeDimensions | Boolean | Removes width/height attributes | true |

PNG Export Options

| Option | Type | Description | Default | | --------- | ------ | ------------------------------- | ------- | | width | Number | Output width in pixels | 512 | | height | Number | Output height in pixels | 512 | | density | Number | SVG render density (DPI) | 120 | | quality | Number | PNG compression quality (0–100) | 90 |


🧑‍💻 Programmatic Usage

// minimal working example
// create example.js
import { optimizeSVGFromFile } from "svg-power-opt";

const run = async () => {
  const result = await optimizeSVGFromFile("icons/logo.svg", {
    aggressive: false,
  });
  // Write the optimized SVG to a new file
  await fs.promises.writeFile("icons/logo.optimized.svg", result, "utf-8");
  console.log("Optimized SVG saved to output/logo.optimized.svg");
};

run();

//execute
//node example.js

// Full API usage example:

import {
  optimizeSVG,
  optimizeSVGFromFile,
  optimizeSVGFromBuffer,
  optimizeSVGFromURL,
  optimizeSVGStream,
  exportPNGThumbnail,
  validateSVG,
} from "svg-power-opt";
import fs from "fs";

// Optimize from file
const optimized = await optimizeSVGFromFile("logo.svg", { aggressive: true });
console.log(optimized);

// Optimize from string
const rawSvg = await fs.promises.readFile("./logo.svg", "utf-8");
const result = optimizeSVG(rawSvg, {
  aggressive: true,
  preserveViewBox: false,
  removeDimensions: true,
});
console.log(result.data);

// Optimize from buffer
const buffer = fs.readFileSync("logo.svg");
const resultFromBuffer = optimizeSVGFromBuffer(buffer);
console.log(resultFromBuffer);

// Optimize from URL
const optimizedFromURL = await optimizeSVGFromURL(
  "https://example.com/image.svg"
);

// Optimize from readable stream (e.g., upload)
const optimizedFromStream = await optimizeSVGStream(req);

// Export PNG thumbnail
// PNG export preserves original SVG transparency and background by default.
await exportPNGThumbnail("icons/logo.svg", "output/logo.png", {
  width: 1024,
  height: 1024,
  density: 150,
  quality: 95,
});

⚙️ Optimization Strategy

  • Safe Mode: Removes metadata, comments, hidden elements, etc.
  • Aggressive Mode: Also removes attributes, sorts keys, strips non-inheritable styles.

🔧 Requirements

  • Node.js Version: Node.js v18.17.0 or higher

📄 License

MIT © 2025