Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load SVG images in Webpack?

Tags:

webpack

I'd like to load SVG files using svgo-loader in Webpack, but I get the following error:

ERROR in ./~/css-loader?modules&localIdentName=[name][local][hash:base64:5]!./~/autoprefixer-loader!./~/less-loader!./demo/src/components/App/components/Header/Header.less Module not found: Error: Cannot resolve module 'logo.svg' in /Users/mishamoroshko/react-autosuggest/demo/src/components/App/components/Header @ ./~/css-loader?modules&localIdentName=[name][local][hash:base64:5]!./~/autoprefixer-loader!./~/less-loader!./demo/src/components/App/components/Header/Header.less 6:199-218

ERROR in ./demo/src/components/App/components/Header/Header.less Module build failed: ModuleNotFoundError: Module not found: Error: Cannot resolve module 'logo.svg' in /Users/mishamoroshko/react-autosuggest/demo/src/components/App/components/Header

Here is the relevant code bits:

Webpack config

module: {
  loaders: [
    {
      test: /\.less$/,
      loader: ExtractTextPlugin.extract('style', 'css?modules&localIdentName=[name]__[local]___[hash:base64:5]!autoprefixer!less'),
      exclude: /node_modules/
    },
    {
      test: /\.svg$/,
      loader: 'url?limit=8192!svgo'
    },
    ...
  ]
}

package.json

"svgo": "^0.6.1",
"svgo-loader": "^1.1.0",
"url-loader": "^0.5.7",
...

Header.less

.logo {
  background-image: url(./logo.svg);
}

logo.svg and Header.less are located in the same directory.

What am I missing here?


To reproduce:

1. git clone [email protected]:moroshko/react-autosuggest.git
2. cd react-autosuggest
3. git checkout a5f1a99
4. npm install
5. npm start
like image 245
Misha Moroshko Avatar asked Dec 27 '15 12:12

Misha Moroshko


1 Answers

This css-loader issue seems related.

As a workaround, changing:

background-image: url(./logo.svg);

to:

background-image: url(../parent-folder/logo.svg);

solves the error.

like image 103
Misha Moroshko Avatar answered Oct 01 '22 22:10

Misha Moroshko