kazijawad / esbuild-plugin-svgr

An esbuild plugin for importing SVGs as React components.
https://npmjs.com/package/esbuild-plugin-svgr
MIT License
36 stars 12 forks source link

No matching export in in svg file. #8

Closed hannadrehman closed 2 years ago

hannadrehman commented 2 years ago

i am trying to import svg file like this

import { ReactComponent as ActivityIcon } from './pathToActivityIcon'

and it gives following error

 error: No matching export in "/pathToSvgFile.svg" for import "ReactComponent"
 import { ReactComponent } from '.pathToSvgFile.svg'
kazijawad commented 2 years ago

Hi, could you provide a code reference I could take a look at?

hannadrehman commented 2 years ago

@kazijawad i am trying to use it like its implemented in create react app . its mentioned here in their docs

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

function App() {
  return (
    <div>
      {/* Logo is an actual React component */}
      <Logo />
    </div>
  );
}
kazijawad commented 2 years ago

Have you tried import Icon from './icon.svg';? If there is a repository I can look at that would be helpful.

kazijawad commented 2 years ago

Closing this issue because of inactivity.

RIP21 commented 2 years ago

@kazijawad default export works indeed. But most of the SVGR exports I saw generated by Webpack integration and Rollup integration generates a named export of ReactComponent So import aDefault from './smth.svg' will give you a URL to a file to access or a base64 URL. But import { ReactComponent } from './smth.svg' gives what your plugin gives now. So people that are looking for svgr plugin for esbuild look exactly for that UX and not the one you provide I think, maybe not by default, but at least some hint in the README :) I want the same :)

kazijawad commented 2 years ago

@RIP21 Ah I see — yes this is currently possible with the SVGR API. It can be passed into the plugin options like this:

svgrPlugin({ exportType: 'named' })

It looks like the hosted SVGR documentation doesn't include all the possible options 😕 I tested it myself and was able to get import { ReactComponent } working. The named export can also be changed with the namedExport field. It might be useful to reference the SVGR source code.