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 🙏

© 2026 – Pkg Stats / Ryan Hefner

svelte-svg-gen

v1.0.4

Published

CLI tool to convert SVG files into typed Svelte components and a dynamic loader.

Readme

svg-to-svelte

A command-line tool to convert SVG files, URLs, or direct SVG content into Svelte components. It optimizes SVGs using SVGO, generates Svelte components, and creates a dynamic loader component and TypeScript type definitions for easy use in your Svelte projects.

Features

  • SVG Optimization: Uses SVGO to optimize SVG files, reducing their size and improving performance. Supports custom SVGO configurations.
  • Component Generation: Automatically generates Svelte components from SVG files.
  • Flexible Input: Accepts input from:
    • Local SVG files
    • Directories containing SVG files (recursively searches for .svg files)
    • HTTP(S) URLs pointing to SVG files
    • Directly pasted SVG content
  • Dynamic Loader: Creates a dynamic Svelte component for lazy-loading SVG icons.
  • TypeScript Support: Generates TypeScript type definitions for the SVG icon names, providing type safety and autocompletion.
  • Customizable Output:
    • Specify the output directory for generated files.
    • Customize the names of the loader component and type definitions.
    • Add a base CSS class to the <svg> element in the generated components.
  • Clean Mode: Optionally clean the output directory before generating new components.
  • Regeneration Mode: Regenerate the type definitions from existing Svelte components. Useful for updating your icon set without re-processing the original SVGs.
  • Interactive Mode: If no input is provided via command-line arguments, the tool enters an interactive mode to guide you through the process.

Usage

npx svelte-svg-gen [options]

Options

  • -i, --input <directories|files|urls...>: Input directories, .svg files, or HTTP(S) URLs (space-separated).
  • --svg <svg>: Direct SVG content string (use with --name).
  • --name <name>: Name for the SVG provided via --svg.
  • -o, --output <directory>: Output directory for generated files (defaults to ./src/lib/svg-icons).
  • --svgoConfig <path>: Path to custom svgo.config.js file.
  • --clean: Clean the 'components' subdirectory within the output directory before generating.
  • --baseClass <class>: Base CSS class added to the <svg> element in components (defaults to svg-icon).
  • -r, --regenerate: Regenerate SvgName.ts from existing components in output/components.
  • -h, --help: Show help message.

Examples

  1. Convert a single SVG file:

    npx svelte-svg-gen -i path/to/my-icon.svg
  2. Convert all SVG files in a directory:

    npx svelte-svg-gen -i path/to/icons-directory
  3. Convert an SVG file from a URL:

    npx svelte-svg-gen -i https://example.com/my-icon.svg
  4. Specify a custom output directory:

    npx svelte-svg-gen -i path/to/my-icon.svg -o path/to/my/custom/output
  5. Specify a custom SVGO configuration file:

    npx svelte-svg-gen -i path/to/my-icon.svg --svgoConfig ./svgo.config.js
  6. Clean the components directory before generating:

    npx svelte-svg-gen -i path/to/my-icon.svg --clean
  7. Regenerate type definitions from existing components:

    npx svelte-svg-gen -r
  8. Interactive mode (no arguments):

    npx svelte-svg-gen

    This mode will prompt you to choose between providing file/URL inputs or pasting SVG content directly.

Output

The tool generates the following files:

  • src/lib/svg-icons/components/<IconName>.svelte: Svelte component for each SVG.
  • src/lib/svg-icons/<SvgName>.ts: TypeScript type definition for the SVG icon names.
  • src/lib/svg-icons/<SvgIcon>.svelte: Dynamic Svelte component for loading SVG icons.

The default output directory is src/lib/svg-icons, but this can be customized using the --output option. The components subdirectory is always created within the specified output directory.

Custom SVGO Configuration

You can provide a custom SVGO configuration file using the --svgoConfig option. The path should point to a valid JavaScript file that exports an SVGO configuration object.

Example svgo.config.js:

export default {
  multipass: true,
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          removeViewBox: false
        }
      }
    },
    { name: 'removeAttrs', params: { attrs: '(stroke|style)' } },
    {
      name: 'addAttributesToSVGElement',
      params: { attributes: [{ focusable: 'false' }, { 'aria-hidden': 'true' }] }
    }
  ]
};

Using the Generated Components

<script>
  import SvgIcon from '$lib/svg-icons/SvgIcon.svelte';
</script>

<SvgIcon name="file-plus" class="h-11 w-11 fill-transparent stroke-black stroke-1" />

Contributing

Contributions are welcome! Please submit a pull request or create an issue to discuss potential changes.

License

MIT