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-sprite-forge

v1.0.0

Published

A lightweight CLI tool to generate SVG sprite files and TypeScript definitions from SVG icon collections.

Readme

svg-sprite-forge

A lightweight, efficient CLI tool to generate SVG sprite files and TypeScript definitions from your SVG icon collections.

Features

  • 🚀 Fast Processing: Utilizes fast-glob for efficient file handling
  • 🧹 Optimization: Automatically optimizes SVGs with SVGO
  • 🎨 Customizable: Configure prefixes, colors, and more
  • 📝 TypeScript Support: Generates TypeScript definition files for your icons
  • 🔄 Modern: Built with modern JavaScript and TypeScript

Installation

# Using npm
npm install svg-sprite-forge --save-dev

# Using yarn
yarn add svg-sprite-forge --dev

# Using pnpm
pnpm add svg-sprite-forge --save-dev

Usage

Basic Usage

npx svg-sprite-forge

This will use the default options to:

  1. Look for SVG files in an ./icons directory
  2. Generate a sprite file at ./sprite.svg
  3. Generate TypeScript definitions at ./icons.d.ts

Custom Options

npx svg-sprite-forge --input=src/assets/icons --output=public/sprite.svg --types=src/types/icons.d.ts --prefix=my-icon-

NPM Scripts

Add to your package.json:

{
  "scripts": {
    "build:icons": "svg-sprite-forge --input=src/icons --output=public/assets/sprite.svg"
  }
}

Then run:

npm run build:icons

Options

| Option | Default | Description | | ------------------- | -------------- | ------------------------------------------------------------ | | -v, --version | - | Display version information and exit | | -h, --help | - | Display help message and exit | | --input | ./icons | Directory containing SVG files | | --output | ./sprite.svg | Output path for the generated sprite | | --types | ./icons.d.ts | Output path for TypeScript definitions or 'false' to disable | | --prefix | icon- | Prefix for icon IDs in the sprite | | --no-strip-colors | false | Don't convert SVG colors to currentColor | | --no-optimize | false | Don't optimize SVGs with SVGO |

How It Works

svg-sprite-forge takes your individual SVG files and combines them into a single SVG sprite file. This approach enables:

  1. Reduced HTTP Requests: Load a single file instead of multiple SVGs
  2. Efficient Caching: Browser caches one file for all icons
  3. Easy Styling: By default, icons use currentColor for easy CSS styling

The generated sprite looks like:

<svg
  xmlns="http://www.w3.org/2000/svg"
  aria-hidden="true"
  style="width: 0; height: 0; position: absolute;"
  viewBox="0 0 16 16"
>
  <defs>
    <symbol id="icon-arrow"><!-- SVG content --></symbol>
    <symbol id="icon-close"><!-- SVG content --></symbol>
    <!-- More symbols -->
  </defs>
</svg>

Same-Origin Requirement

Important: SVG sprites must be served from the same domain as your website. Due to security restrictions in most browsers, the <use> element with xlink:href cannot reference external SVG sprites across different domains or CDNs. This cross-origin restriction (CORS) prevents potential security vulnerabilities. If you need to serve your sprite from a CDN, you'll need to ensure proper CORS headers are set, though even this might not work in all browsers. For best compatibility, host the sprite file on the same domain as your website.

Using the Generated Sprite

HTML

When you run svg-sprite-forge, it generates a sprite file that you need to include in your project. For optimal performance and to take advantage of browser caching, reference the external sprite file:

<svg width="24" height="24" aria-hidden="true">
  <use xlink:href="sprite.svg#icon-arrow"></use>
</svg>

Make sure to place the generated sprite.svg file in a location that's accessible to your web pages, and maintain the correct path in your references.

React

import type { SVGProps } from "react";
import ReactDOM from "react-dom";

import sprite from "./path/to/sprite.svg";

type Props = {
  name: IconName; // type safe icon name through generated types
  size?: number;
} & SVGProps<SVGSVGElement>;

const spritePath = (sprite as { src: string }).src;

export function Icon({ name, size = 20, ...props }: Props) {
  ReactDOM.preload(spritePath, {
    as: "image",
  });

  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      width={size}
      height={size}
      aria-hidden="true"
      {...props}
    >
      <use xlinkHref={`${spritePath}#icon-${name}`} />
    </svg>
  );
}

Best Practices

  1. Consistent Viewbox: Ensure your SVGs have a consistent viewBox for predictable sizing
  2. Clean SVGs: Remove unnecessary elements and attributes from source files
  3. Build Integration: Add icon generation to your build process
  4. Naming Convention: Use a consistent naming pattern for your SVG files

License

This project is licensed under the ISC License - see the LICENSE file for details.