Coder Social home page Coder Social logo

gatsby-plugin-svgr-svgo's Introduction

gatsby-plugin-svgr-svgo

Plugin allows you to use SVGs as react components, configure SVGO(SVG optimization) settings and declare rules for SVG url loader with or without optimization.

npm version Drone

Install

npm install gatsby-plugin-svgr-svgo @svgr/webpack --save

Setup

Default configuration

Add to your gatsby-config.js

plugins: [`gatsby-plugin-svgr-svgo`];

By default there are two rules will be added:

  1. SVG as a react component(inline svg), SVGO enabled for all SVGs that have .inline postfix. Example: cat.inline.svg
import React from "react";
import CatInlineSvg from "../images/cat.inline.svg";

const IndexPage = () => (
  <div>
    <CatInlineSvg />
  </div>
);

export default IndexPage;
  1. SVG as a file that available by url, SVGO enabled for all svgs that have .svg. Example: cat.svg.
import React from "react";
import CatSvg from "../images/cat.svg";

const IndexPage = () => (
  <div>
    <img src={CatSvg} />
  </div>
);

export default IndexPage;

Advanced configuration

plugins: [
  {
    resolve: "gatsby-plugin-svgr-svgo",
    options: {
      inlineSvgOptions: [
        {
          test: /\.inline.svg$/,
          svgoConfig: {
            plugins: [
              {
                removeViewBox: false
              }
            ]
          }
        }
      ],
      urlSvgOptions: [
        {
          test: /\.svg$/,
          svgoConfig: {
            plugins: [
              {
                removeViewBox: false
              }
            ]
          }
        }
      ]
    }
  }
];

You can declare various rules based on loader that should be used under inlineSvgOptions and urlSvgOptions.

test - pattern that will be used to match file name

svgoConfig - accepts plugins list with settings, list of available plugins(options) you can find here

svgo - disables SVGO if set in false

SVGO disabled example:

plugins: [
  {
    resolve: "gatsby-plugin-svgr-svgo",
    options: {
      urlSvgOptions: [
        {
          test: /\.svg$/,
          svgo: false
        }
      ]
    }
  }
];

Set URL loader fallback limit

By default webpack url-loader has a fallback to file-loader that converts the file from the original extension to base64. So if you want to set the limit from which it should be loaded directly from url instead of base64 loading just use limit option within urlLoaderOptions.

Limits you set in bytes. Read about more options there

urlSvgOptions: [
  {
    test: /\.svg$/,
    svgoConfig: {
      plugins: [
        {
          removeViewBox: false,
        },
      ],
    },
    urlLoaderOptions: {
      limit: 512,
    },
  },
],

gatsby-plugin-svgr-svgo's People

Contributors

abemedia avatar lnikell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.