Which is Better Vite SVG Handling Plugins?
vite-plugin-svgr vs vite-plugin-svg-icons
1 Year
vite-plugin-svgrvite-plugin-svg-icons
What's Vite SVG Handling Plugins?

These plugins enhance the Vite development environment by providing efficient ways to handle SVG files. They cater to different needs when working with SVGs, from optimizing and managing SVG icons to transforming SVG files into React components. By integrating these plugins, developers can streamline their workflow, improve performance, and enhance the maintainability of their projects when dealing with SVG assets.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
vite-plugin-svgr1,258,5445569.56 kB38a year agoMIT
vite-plugin-svg-icons36,74281319.4 kB61-MIT
Feature Comparison: vite-plugin-svgr vs vite-plugin-svg-icons

SVG Handling

  • vite-plugin-svgr: This plugin transforms SVG files into React components, allowing you to use SVGs directly in your JSX. It supports additional features like customizing SVG properties and styles, making it a powerful tool for React developers who want to leverage SVGs effectively.
  • vite-plugin-svg-icons: This plugin allows you to import SVG files as Vue components, enabling easy use of SVG icons throughout your application. It optimizes SVG files for better performance and reduces the overall bundle size, making it suitable for applications that rely heavily on iconography.

Performance Optimization

  • vite-plugin-svgr: This plugin also optimizes SVGs but focuses on converting them into React components, which can lead to better performance in React applications. It allows for tree-shaking of unused SVGs, ensuring that only the necessary components are included in the final bundle.
  • vite-plugin-svg-icons: By optimizing SVG files during the import process, this plugin ensures that your application remains lightweight and performs efficiently. It minimizes the size of SVG assets, which is crucial for applications with many icons, leading to faster load times and improved user experience.

Ease of Use

  • vite-plugin-svgr: With this plugin, importing SVGs as React components is straightforward and intuitive. It streamlines the workflow for React developers, enabling them to easily manipulate SVGs within their components without additional configuration.
  • vite-plugin-svg-icons: The plugin simplifies the process of using SVG icons in Vue applications by allowing direct imports as components. This reduces the complexity of managing SVG assets and enhances developer productivity, especially in projects with numerous icons.

Integration

  • vite-plugin-svgr: Designed specifically for React, this plugin integrates well with Vite and supports modern React features. It allows developers to utilize SVGs in a way that aligns with React's component-based architecture, making it a natural fit for React projects.
  • vite-plugin-svg-icons: This plugin integrates seamlessly with Vite and Vue, making it a perfect choice for Vue-based projects. It leverages Vite's fast build times and hot module replacement, enhancing the development experience when working with SVG icons.

Customization

  • vite-plugin-svgr: This plugin offers extensive customization options for SVG components, allowing developers to modify attributes and styles directly in JSX. This is particularly beneficial for creating dynamic and responsive designs that utilize SVG graphics.
  • vite-plugin-svg-icons: While primarily focused on icon optimization, this plugin allows for some level of customization in how SVGs are rendered as components, providing flexibility for developers to style their icons as needed.
How to Choose: vite-plugin-svgr vs vite-plugin-svg-icons
  • vite-plugin-svgr: Opt for this plugin if your project is built with React and you want to import SVG files as React components directly. It provides a seamless way to use SVGs in JSX, allowing for easy manipulation of SVG properties and styles, which is particularly useful in React-based applications.
  • vite-plugin-svg-icons: Choose this plugin if you need a straightforward solution for managing and optimizing SVG icons in your project. It allows you to import SVG files as Vue components, making it ideal for projects that require a large number of icons and want to keep the bundle size small through optimization.
README for vite-plugin-svgr

vite-plugin-svgr

npm

Vite plugin to transform SVGs into React components. Uses svgr under the hood.

Installation

# npm
npm install --save-dev vite-plugin-svgr

# yarn
yarn add -D vite-plugin-svgr

# pnpm
pnpm add -D vite-plugin-svgr

Usage

// vite.config.js
import svgr from "vite-plugin-svgr";

export default {
  // ...
  plugins: [svgr()],
};

Then SVG files can be imported as React components:

import Logo from "./logo.svg?react";

If you are using TypeScript, there is also a declaration helper for better type inference:

/// <reference types="vite-plugin-svgr/client" />

Options

svgr({
  // svgr options: https://react-svgr.com/docs/options/
  svgrOptions: {
    // ...
  },

  // esbuild options, to transform jsx to js
  esbuildOptions: {
    // ...
  },

  // A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should include.
  include: "**/*.svg?react",

  //  A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.
  exclude: "",
});

If you want to enable SVGO you can install @svgr/plugin-svgo and use following options to enable and configure it:

svgr({
  svgrOptions: {
    plugins: ["@svgr/plugin-svgo", "@svgr/plugin-jsx"],
    svgoConfig: {
      floatPrecision: 2,
    },
  },
  // ...
});

License

MIT