@nexapp/vite-plugin-react-svg

3.0.0 • Public • Published

vite-plugin-react-svg

Extend Vite with ability to use SVG files as React components.

Original Repo: https://github.com/visualfanatic/vite-svg/tree/master/packages/vite-plugin-react-svg

Merge Request Pending

Features:

  • SVGO optimization
  • SVGR customization
  • Hot Module Replacement support
  • Support for ?url and ?component query string

Currently supported Vite version:

>=3.0.0

Install

yarn add --dev @nexapp/vite-plugin-react-svg

npm i -D @nexapp/vite-plugin-react-svg

Setup

// vite.config.js
const reactRefresh = require('@vitejs/plugin-react-refresh');
const reactSvgPlugin = require('@nexapp/vite-plugin-react-svg');

module.exports = {
  plugins: [
    reactRefresh(),
    reactSvgPlugin(),
  ],
};

Options

reactSvgPlugin({
  // Default behavior when importing `.svg` files, possible options are: 'url' and `component`
  defaultExport: 'url',

  // Boolean flag to enable/disable SVGO
  svgo: true,

  // SVGO configuration object
  svgoConfig: {},

  // Props to be forwarded on SVG tag, ossible options: "start", "end" or false
  expandProps: 'end',

  // Setting this to true will forward ref to the root SVG tag
  ref: false,

  // Setting this to true will wrap the exported component in React.memo
  memo: false,

  // Replace an attribute value by an other.
  // The main usage of this option is to change an icon color to "currentColor" in order to inherit from text color.
  // replaceAttrValues: { old: 'new' },
  replaceAttrValues: null,

  // Add props to the root SVG tag
  // svgProps: { name: 'value' },
  svgProps: null,

  // Add title tag via title property
  // <SvgIcon title="Accessible icon name" /> => <svg><title>Accessible icon name</title><...></svg>
  // <SvgIcon title="Accessible icon name" titleId="iconName" /> => <svg aria-labelledby="iconName><title id="iconName">Accessible icon name</title><...></svg>
  titleProp: false,
})

Usage

import MyIcon from './svgs/my-icon.svg?component';

function App() {
  return (
    <div>
      <MyIcon />
    </div>
  );
}

Package Sidebar

Install

npm i @nexapp/vite-plugin-react-svg

Weekly Downloads

0

Version

3.0.0

License

MIT

Unpacked Size

6 kB

Total Files

4

Last publish

Collaborators

  • jgdoyon-nexapp
  • pluc_nexapp
  • mbergeron
  • tristanod
  • hsebouai-nexapp
  • sebdufbeau
  • gabsima-nexapp
  • drichard-nexapp