finmavis / razzle-plugin-svg-react-component

Razzle plugin to import SVG as ReactComponent
MIT License
3 stars 2 forks source link

Razzle Plugin SVG as ReactComponent

This package contains a plugin to import SVG as ReactComponent.

Notes: This plugin is only compatible with Razzle v3.3.0 and above. If your project using an older version of Razzle, you could install the 1.0.3 version of this plugin. See below explanation for an older version of Razzle.

Usage in Razzle Projects

Example

By default you can use SVG like below :

import Logo from './logo.svg';

const App = () => (
  <div>
    <img src={Logo} alt="Here are my logo" />
  </div>
);

With this plugin, You can also import SVGs directly as React components.

import { ReactComponent as Logo } from './logo.svg';

const App = () => (
  <div>
    {/* Logo is an actual React component */}
    <Logo />
  </div>
);

Usage with Typescript

If you encounter this error message, Module '"*.svg"' has no exported member 'ReactComponent'.

You need to add declaration type for SVG to your declarations.d.ts like below:

declare module '*.svg' {
  import * as React from 'react';

  export const ReactComponent: React.FunctionComponent<React.SVGProps<
    SVGSVGElement
  > & { title?: string }>;

  const src: string;
  export default src;
}

Notes: If your project doesn't have declarations.d.ts file, you could create a new one inside src folder.

Older version Razzle (below v3.3.0)

Usage in Razzle Projects

Usage with razzle-plugin-typescript

Under the hood, this plugin uses babel-plugin-named-asset-import. By default, razzle-plugin-typescript does not includes babel-loader for .tsx files. To include babel-loader, useBabel: true has to be specified.

babel-plugin-named-asset-import only transforms ES2015 static import and export statements and not require() calls. If target is ES5 or below, static import statements is transformed to require calls. This prevents transformation of babel-plugin-named-asset-import.