@svgr/webpack vs @svgr/cli vs @svgr/rollup
SVG to React Component Converters Comparison
1 Year
@svgr/webpack@svgr/cli@svgr/rollupSimilar Packages:
What's SVG to React Component Converters?

These packages provide tools to convert SVG files into React components, facilitating the integration of SVG graphics into React applications. They streamline the process of using SVGs by transforming them into JSX format, allowing for easier manipulation and styling within React. Each package is tailored for different build tools and environments, making them suitable for various project setups. By leveraging these tools, developers can enhance their applications with scalable and customizable vector graphics, improving both performance and visual appeal.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
@svgr/webpack6,454,54110,69615.2 kB1222 years agoMIT
@svgr/cli400,65610,69620.5 kB1222 years agoMIT
@svgr/rollup305,48510,69613.5 kB1222 years agoMIT
Feature Comparison: @svgr/webpack vs @svgr/cli vs @svgr/rollup

Integration

  • @svgr/webpack:

    @svgr/webpack acts as a Webpack loader, enabling SVG files to be imported as React components seamlessly. This integration allows for a smooth development experience, as SVGs can be managed alongside other assets within the Webpack ecosystem.

  • @svgr/cli:

    @svgr/cli is designed for command-line usage, allowing developers to convert SVG files into React components outside of any build tool. It can be used in scripts or as part of a larger automation process, making it versatile for various workflows.

  • @svgr/rollup:

    @svgr/rollup integrates directly with Rollup, allowing SVG imports to be transformed on-the-fly during the bundling process. This tight integration ensures that SVGs are optimized and ready for use in your application without additional steps.

Customization

  • @svgr/webpack:

    @svgr/webpack provides customization options through Webpack's configuration, allowing developers to specify how SVGs should be transformed, including options for JSX output and component properties, facilitating a more tailored integration.

  • @svgr/cli:

    @svgr/cli offers various options for customization during the conversion process, such as modifying the output component's props, applying custom templates, and controlling the styling of the generated components, allowing for tailored SVG integration.

  • @svgr/rollup:

    @svgr/rollup allows for some level of customization through Rollup's configuration, but it primarily focuses on providing a straightforward integration for SVG imports, making it less customizable compared to the CLI approach.

Performance

  • @svgr/webpack:

    @svgr/webpack leverages Webpack's optimization features, such as code splitting and lazy loading, to enhance performance. It allows for efficient management of SVG assets, ensuring that only the required components are loaded when needed.

  • @svgr/cli:

    @svgr/cli can optimize SVG files during conversion, potentially reducing file sizes and improving load times when SVGs are used in applications. However, the performance impact depends on how the CLI is integrated into the build process.

  • @svgr/rollup:

    @svgr/rollup benefits from Rollup's tree-shaking capabilities, ensuring that only the necessary SVG components are included in the final bundle, which can lead to smaller bundle sizes and improved performance in production builds.

Ease of Use

  • @svgr/webpack:

    @svgr/webpack is designed to be easy to use within Webpack projects, allowing developers to import SVGs directly in their components with little configuration, streamlining the development process.

  • @svgr/cli:

    @svgr/cli is straightforward to use for developers familiar with command-line interfaces, making it easy to convert multiple SVGs quickly. However, it may require additional setup for integration into existing workflows.

  • @svgr/rollup:

    @svgr/rollup is user-friendly for those already using Rollup, as it requires minimal configuration to start importing SVGs as React components, making it an efficient choice for Rollup users.

Community and Support

  • @svgr/webpack:

    @svgr/webpack has a large community due to Webpack's popularity, ensuring ample resources, tutorials, and community support, making it easier for developers to find help and best practices.

  • @svgr/cli:

    @svgr/cli has a growing community and is well-documented, providing resources and examples for users to effectively utilize the tool in various scenarios.

  • @svgr/rollup:

    @svgr/rollup benefits from the Rollup community, which offers support and plugins that can enhance its functionality, although it may have a smaller user base compared to Webpack.

How to Choose: @svgr/webpack vs @svgr/cli vs @svgr/rollup
  • @svgr/webpack:

    Opt for @svgr/webpack if your project utilizes Webpack. This package provides a loader that transforms SVGs into React components during the build process, enabling efficient SVG management and optimization within Webpack's ecosystem.

  • @svgr/cli:

    Choose @svgr/cli if you need a command-line interface to convert SVG files into React components as part of your build process or for batch processing SVGs. It's ideal for projects where SVGs are managed independently from the build toolchain.

  • @svgr/rollup:

    Select @svgr/rollup if you are using Rollup as your module bundler. This package integrates seamlessly with Rollup, allowing you to import SVGs directly into your components without additional configuration, making it perfect for projects focused on modern JavaScript tooling.

README for @svgr/webpack

@svgr/webpack

Build Status Version MIT License

Webpack loader for SVGR.

npm install @svgr/webpack --save-dev

Usage

In your webpack.config.js:

{
  test: /\.svg$/,
  use: ['@svgr/webpack'],
}

In your code:

import Star from './star.svg'

const App = () => (
  <div>
    <Star />
  </div>
)

Passing options

{
  test: /\.svg$/,
  use: [
    {
      loader: '@svgr/webpack',
      options: {
        native: true,
      },
    },
  ],
}

Using with url-loader or file-loader

It is possible to use it with url-loader or file-loader.

In your webpack.config.js:

{
  test: /\.svg$/,
  use: ['@svgr/webpack', 'url-loader'],
}

In your code:

import starUrl, { ReactComponent as Star } from './star.svg'

const App = () => (
  <div>
    <img src={starUrl} alt="star" />
    <Star />
  </div>
)

The named export defaults to ReactComponent, but can be customized with the namedExport option.

Please note that by default, @svgr/webpack will try to export the React Component via default export if there is no other loader handling svg files with default export. When there is already any other loader using default export for svg files, @svgr/webpack will always export the React component via named export.

If you prefer named export in any case, you may set the exportType option to named.

Use your own Babel configuration

By default, @svgr/webpack includes a babel-loader with an optimized configuration. In some case you may want to apply a custom one (if you are using Preact for an example). You can turn off Babel transformation by specifying babel: false in options.

// Example using preact
{
  test: /\.svg$/,
  use: [
    {
      loader: 'babel-loader',
      options: {
        presets: ['preact', 'env'],
      },
    },
    {
      loader: '@svgr/webpack',
      options: { babel: false },
    }
  ],
}

Handle SVG in CSS, Sass or Less

It is possible to detect the module that requires your SVG using Rule.issuer in Webpack 5. Using it you can specify two different configurations for JavaScript and the rest of your files.

;[
  {
    test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
    issuer: /\.[jt]sx?$/,
    use: ['babel-loader', '@svgr/webpack', 'url-loader'],
  },
  {
    test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
    loader: 'url-loader',
  },
]

Rule.issuer in Webpack 4 has additional conditions which are not available in Webpack 5.

License

MIT