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

next-plugin-svgr

v1.1.12

Published

Next js plugin for SVGR

Downloads

51,194

Readme

Next.js + SVGR

npm package Dependencies License

Flexible Next.js plugin for transforming svg's into react components using the svgr library

Installation

npm

npm install --save next-plugin-svgr

or yarn

yarn add next-plugin-svgr

Usage

Create a next.config.js in your project

// next.config.js
const withSvgr = require('next-plugin-svgr');

module.exports = withSvgr();

Optionally add Next.js configuration as a parameter

// next.config.js
const withSvgr = require('next-plugin-svgr');

module.exports = withSvgr({
  webpack(config, options) {
    return config;
  },
});

Or with next-compose-plugins when composing multiple plugins

// next.config.js
const withPlugins = require("next-compose-plugins");
const withSvgr = require("next-plugin-svgr");

module.exports = withPlugins([
  withSvgr
  // your other plugins here
]);

And now in your components you can use the svg as a component

import Icon from './icon.svg';

export default () => (
  <div>
    <Icon />
  </div>
);

Options

svgrOptions

The plugins supports all available options of svgr webpack loader. Check out the svgr documentation for the full list of options.

Example with options:

module.exports = withSvgr({
  svgrOptions: {
    titleProp: true,
    icon: true,
    svgProps: {
      height: 'auto',
    },
  },
});

You can optionally specify a project configuration file. SVGR uses cosmiconfig for configuration file support, which means that any file type accepted by cosmicconfig is supported.

  • .svgrrc file, written in YAML or JSON, with optional extensions: .yaml/.yml/.json/.js.
  • A svgr.config.js file that exports an object.
  • A "svgr" key in your package.json file.

note: The plugin will automatically detect your config file so you shouldn't have to include the configFile property in svgrOptions. The option to specify exists and can be accomplished following the example below.

// .svgrrc.js
module.exports = {
  icon: true,
  expandProps: false,
};

// next.config.js
module.exports = withSvgr({
  svgrOptions: {
    configFile: path.resolve(__dirname, '.svgrrc.js'),
  },
});

// or with next-compose-plugins
module.exports = withPlugins([
  withGraphql,
  [withSvgr, {
    svgrOptions: {
      configFile: path.resolve(__dirname, '.svgrrc.js'),
    },
  }],
]);

fileLoader

If you would like to use the svgr webpack loader with file-loader. Accepts a boolean or all available options for file-loader.

The fileLoader option is undefined by default. If defined, it will apply the options below.

note: If using file-loader and typescript remember to reference the svgr/file-loader types. See below.

Default options:

{
  limit: 8192,
  publicPath: `${assetPrefix ?? ''}/_next/${path}`,
  outputPath: `${isServer ? '../' : ''}${path}`,
  name: '[path][name].[hash].[ext]',
}
module.exports = withSvgr({
  fileLoader: true,
  svgrOptions: {
    ...options
  },
});
module.exports = withSvgr({
  fileLoader: {
    limit: 16384,
    name(resourcePath, resourceQuery) {
      if (process.env.NODE_ENV === 'development') {
        return '[path][name].[ext]';
      } 
      return '[contenthash].[ext]';
    }
  },
  svgrOptions: {
    ...options
  },
});
import url, { ReactComponent as Icon } from './icon.svg';

export default () => (
  <div>
    <Icon title="my awesome icon" />
    <img src={url} alt="my awesome image" />
  </div>
);

Typescript

Typescript is unable to interpret imported svg files, so next-plugin-svgr includes definitions for svg modules depending on your use case. Per the recommendations of the Next.js maintainers you should no longer reference these types in the next-env.d.ts file. You can instead create a typings directory inside your src directory. Then simple create a definitions file (ie: index.d.ts) and reference any of the definitions there. There shouldn't be any need to adjust your tsconfig.json for your project.

  1. if using the plugin without the fileLoader option

src/typings/index.d.ts

/// <reference types="next-plugin-svgr/types/svg" />
  1. If using the plugin with the fileLoader option

src/typings/index.d.ts

/// <reference types="next-plugin-svgr/types/svgFileLoader" />

Contributors

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT