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

gatsby-plugin-react-svgs

v3.2.0

Published

Adds svg-react-loader to gatsby webpack config.

Downloads

1

Readme

gatsby-plugin-react-svg npm version

Adds svg-react-loader to gatsby webpack config.

Note: the plugin can remove SVGs from the built-in url-loader config in case invalid configuration.

Install

npm install --save gatsby-plugin-react-svg

How to use

// In your gatsby-config.js

plugins: [
  {
    resolve: "gatsby-plugin-react-svg",
    options: {
      rule: {
        include: /assets/ // See below to configure properly
      }
    }
  }
];

Configuration

The rule plugin option can be used to pass rule options. If either include or exclude options are present, svg-react-loader will use them and url-loader will be re-enabled with the inverse.

The following configuration uses svg-react-loader to process SVGs from a path matching /assets/, and url-loader to process SVGs from everywhere else.

{
  resolve: 'gatsby-plugin-react-svg',
  options: {
    rule: {
      include: /assets/
    }
  }
}

From now on you can import SVGs and use them as Components:

import Icon from "./path/assets/icon.svg";

// ...

<Icon />;

Another common configuration:

  • name SVGs used in React components like something.inline.svg and process them with svg-react-loader
  • name other SVGs (e.g. used in css/scss) something.svg and process them with the default url-loader
{
  resolve: 'gatsby-plugin-react-svg',
  options: {
    rule: {
      include: /\.inline\.svg$/
    }
  }
}

In React components:

import Something from "./path/something.inline.svg";

// ...

<Something />;

In styles file:

.header-background {
  background-image: url(./path/something.svg);
}

Using with typescript

To use SVGs with Typescript, create a custom type definition like this:

declare module "*.svg" {
  const content: any;
  export default content;
}

Make sure the file is contained in your tsconfig.json include.

SVG-React-Loader options

Any of the svg-react-loader query parameters can be passed down via the webpack config by including an options prop within the rule prop.

// In your gatsby-config.js

plugins: [
  {
    resolve: "gatsby-plugin-react-svg",
      options: {
        rule: {
          include: /\.inline\.svg$/,
          options: {
            tag: "symbol",
            name: "MyIcon",
            props: {
              className: "my-class",
              title: "example"
            },
            filters: [value => console.log(value)]
          }
        }
      }
  }
];

They can also be defined at the import level:

  import Fork from "-!svg-react-loader?props[]=className:w-4 h-4!../components/Icons/Fork.inline.svg";

Removing svg props (filters)

Unwanted SVG props can be removed with filters. Since filters are quite complex this plugin adds a simple key omitKeys to allow end users to quickly remove props that are problematic from their svg files.

{
  resolve: `gatsby-plugin-react-svg`,
  options: {
    rule: {
      include: /images\/.*\.svg/,
      omitKeys: ['xmlnsDc', 'xmlnsCc', 'xmlnsRdf', 'xmlnsSvg', 'xmlnsSodipodi', 'xmlnsInkscape']
      ///OR
      filters: [(value) => { console.log(value); }]
    }
  }
},

Troubleshooting

I get "InvalidCharacterError" overlay in my browser during development

Example of this error:

InvalidCharacterError: Failed to execute 'createElement' on 'Document':
The tag name provided ('data:image/svg+xml; ...

It's likely that you use SVG in your React component, that is processed by url-loader instead of svg-react-loader due to incorrect configuration.

I get endless spinner (with an infinite loop in the background) in my browser during development

It's likely that some of your SVGs used in css/sass files are processed by svg-react-loader instead of url-loader due to incorrect configuration.

I get error "Module parse failed" in console

Example of this error:

ERROR in ./src/images/some-image.png 1:0
Module parse failed: Unexpected character '�' (1:0)

In case you see such error, it's likely that you configured exclude/include rule options incorrectly. Please check configuration section above.