Svgr: import xx from 'path/xx.svg' Error

Created on 9 Apr 2019  Β·  16Comments  Β·  Source: gregberge/svgr

test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, use: [ { loader: '@svgr/webpack', options: { icon: true } } ]
image

import logoSvg from 'paths/logo.svg';

help wanted πŸ†˜ needs more info πŸ– question ❓

Most helpful comment

you need to add a svg.d.ts to your project like this:

declare module '*.svg' {
  import { FunctionComponent, SVGProps } from 'react';
  const _: FunctionComponent<SVGProps<HTMLOrSVGElement>>;
  export = _;
}

and it should work as expected (although I'm not sure if the props will actually be passed down to the SVG that is about to be inlined, if it doesn't, change SVGProps to any)

All 16 comments

Hello @XianZhengquan, can you please submit a repro example?

Thank. @neoziro Thanks.I will create a new repository on github and put the repo on it.

It looks like a Typescript issue, help is welcome here. @theKashey maybe πŸ™

What should I do?πŸ€”πŸ€”πŸ€”

Probably asking a Typescript user πŸ˜“

God bless!πŸ™πŸ™πŸ™
This has been bothering me.

So - there are only two questions:

  • try it has typescript-ish output
  • why there is no second loader to handle TS or, to be more concrete, JSX

3:22 is props>:<, not a valid JS. So you need to specify a loader to handle a result "SVG".

@neoziro I don't understandπŸ€”πŸ€”πŸ€”

should be like

{ 
  test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, 
  use: [ 
     "ts-loader", // !
     { loader: '@svgr/webpack', options: { icon: true } }  // probably with `babel:false`
   ]

But still the question - why svgr output is in typescript.

Because there is a configuration file .svgrrc.js, the template output is typescript

should be like

{ 
  test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, 
  use: [ 
     "ts-loader", // !
     { loader: '@svgr/webpack', options: { icon: true } }  // probably with `babel:false`
   ]

But still the question - why svgr output is in typescript.

Can it run normally like this?

So - you don't need to compile it to typescript if you are using webpack-loader - for TS it still SVG and nothing more.

It worth to use only if you are pre-compiling these images.

you need to add a svg.d.ts to your project like this:

declare module '*.svg' {
  import { FunctionComponent, SVGProps } from 'react';
  const _: FunctionComponent<SVGProps<HTMLOrSVGElement>>;
  export = _;
}

and it should work as expected (although I'm not sure if the props will actually be passed down to the SVG that is about to be inlined, if it doesn't, change SVGProps to any)

Looks like it is solved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AskAlice picture AskAlice  Β·  3Comments

gregberge picture gregberge  Β·  6Comments

chloesun picture chloesun  Β·  6Comments

troch picture troch  Β·  5Comments

smashercosmo picture smashercosmo  Β·  3Comments