Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  130] [ 5]  / answers: 1 / hits: 16313  / 7 Years ago, mon, october 9, 2017, 12:00:00

I'd like to import an SVG image into a little React app written with TypeScript and bundled with Webpack. The problem is that that no image is shown (only the browser's fallback image that no image was found). It seems like the data URL is wrong, because when I copy the data URL in a base64 decoder the image is broken.



I'm tried different Webpack loaders (url-loader, file-loader, svg-loader, ...)



webpack.config.json



module: {
rules: [
{
test: /.svg$/,
loader: 'url-loader'
}
]
}


tsd.d.ts



declare module *.svg {
const content:string;
export default content;
}


App.tsx



import content from './logo.svg';

class App extends React.Component {
render() {
<div>
<img src={content} />
</div>
}
}


Any ideas what to change?



Thanks in advance!


More From » reactjs

 Answers
12

My problem was that Webpack was misconfigured.



I had, beside others, the following two loader configurations:



module: {
rules: [
{
test: /.svg$/,
loader: 'url-loader'
},
{
test : /.(ttf|eot|svg|woff(2)?)(?[a-z0-9=&.]+)?$/,
loader : 'file-loader'
}
]
}


The problem was that I had svg in both rules, therefore webpack used the wrong loader. Removing the svg part from the second rule solved the issue


[#56271] Friday, October 6, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
;