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 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-react-rich-svg

v1.2.0

Published

Seamless SVG loader with versatile import options! (Such as React component, dataURI and raw html code)

Downloads

3,499

Readme

Description

A vite plugin that handles SVG loading with zero-config effort.

It handles loading raw (html string), inline data uri (data:svg+xml,...) and SVGR Component (with easily usable SVGO options) imports easily!

This plugin is heavily inspired by vite-svg-loader, which is an SVG loading Vite plugin for Vue. It is one of my favourites. Thanks to everyone contributed to it! 💜

How to use?

  1. Use your favorite package manager to install as dependency
npm install vite-plugin-react-rich-svg --save-dev
  1. Include it in your vite.config.ts
import richSvg from "vite-plugin-react-rich-svg";

export default defineConfig({
  plugins: [react(), richSvg()],
});
  1. If you're using Typescript, you might want to include the typings under vite-env.d.ts:
// Caveat: referencing our plugin first will ensure vite types do not overlap

/// <reference types="vite-plugin-react-rich-svg/client" />
/// <reference types="vite/client" />
  1. Start importing your SVGs! Happy coding!
// Raw string import
import viteLogoRaw from "./assets/vite.svg?raw";

// Data URL import
import viteLogoDataURL from "./assets/vite.svg?url";

// Base64 Encoded import
import viteLogoBase64 from "./assets/vite.svg?base64";

// SVGR Component import
import ViteLogoComponent from "./assets/vite.svg?component";

// Default import, not handled by our plugin
import viteLogo from "./assets/vite.svg";

Plugin Configurations

include option

Acts as a whitelist predicate for the files you want to be processed.

  richSvg({
    include: (path) => /.*\.icon\.svg$/.test(path),
    // ^ This config will only process files that look like:
    // ...chevron-right.icon.svg?raw
    // ...chevron-left.icon.svg?component
    // ...home.icon.svg?url
  }),

exclude option

Acts as a blacklist predicate for the files you want to be ignored.

  richSvg({
    exclude: (path) => /.*\.ignore\.svg$/.test(path),
    // ^ This config will ignore files that look like:
    // ...my-illustration.ignore.svg?raw
    // ...my-illustration.ignore.svg?component
    // ...my-illustration.ignore.svg?url
  }),

componentLoaderOptions option

  • svgrConfig = Options used while running SVGR on the original svg code/asset (See SVGR Options)

  • esbuildConfig = Options used to generate import code with given SVGR output (See ESBuild Transform Options)

  richSvg({
    componentLoaderOptions: {
      svgrConfig: {
        ref: true,
        memo: true,
      },
      // ^ This config will make it load component svg imports loads with forwardedRef & memo wrapped


      esbuildConfig:{
        minify: true
      },
      // ^ This config will make it load component svg imports loads with minification enabled
    },
  }),

rawLoaderOptions option

  • svgoEnabled = Enables SVGO optimizations

  • svgoConfig = Options used while running SVGO optimizations on the original SVG asset (See SVGO Options)

  richSvg({
    rawLoaderOptions: {
      svgoEnabled: true,
      svgoConfig: {},
    },
  }),

base64LoaderOptions option

  • svgoEnabled = Enables SVGO optimizations

  • svgoConfig = Options used while running SVGO optimizations on the original SVG asset (See SVGO Options)

  richSvg({
    base64LoaderOptions: {
      svgoEnabled: true,
      svgoConfig: {},
    },
  }),

urlLoaderOptions option

  • svgoEnabled = Enables SVGO optimizations

  • svgoConfig = Options used while running SVGO optimizations on the original SVG asset (See SVGO Options)

  richSvg({
    urlLoaderOptions: {
      svgoEnabled: true,
      svgoConfig: {},
    },
  }),

SVGO, Prettier & Other SVGR Plugins

SVGO and Prettier are supported out of the box. Just mark them in the svgrConfig and they'll start working.

You can also include your own SVGR plugins as you desire!

import myCustomSvgrPlugin from "my-custom-svgr-plugin";
import myCustomSvgoPlugin from "my-custom-svgo-plugin";
import myCustomPrettierPlugin from "my-custom-prettier-plugin";

richSvg({
  componentLoaderOptions: {
    svgrConfig: {
      svgo: true,
      prettier: true,
      prettierConfig: {
        plugins: ["prettier-plugin-foo"]
      },
      plugins: [myCustomSvgrPlugin],
    },
  },
  rawLoaderOptions: {
    svgoEnabled: true,
    svgoConfig: {
      plugins: [myCustomSvgoPlugin]
    }
  },
  base64LoaderOptions: {
    svgoEnabled: true,
    svgoConfig: {
      plugins: [myCustomSvgoPlugin]
    }
  },
  urlLoaderOptions: {
    svgoEnabled: true,
    svgoConfig: {
      plugins: [myCustomSvgoPlugin]
    }
  }
}),

License

© 2024 Taha Anılcan Metinyurt (iGoodie)

For any part of this work for which the license is applicable, this work is licensed under the Attribution-ShareAlike 4.0 International license. (See LICENSE).