4

In one of my component, I'm trying to load an svg image but vuejs/webpack fails.

Here's my code :

<template>
    <img src="../../assets/public/images/sad.svg" />
</template>

<script>
export default {}
</script>

And the error :

ERROR in ./src/assets/public/images/sad.svg
Module parse failed: /home/cabox/workspace/src/assets/public/images/sad.svg Line 1: Unexpected token < You may need an appropriate loader to handle this file type.
|
|
| @ ./~/vue-html-loader!./~/vue-loader/lib/selector.js?type=template&index=0!./src/components/tooltip.vue 1:205-258 1:592-645

From what I understand, Vuejs (or Webpack) tries to interpret the content of .svg. And this is not what I want.

Here's my webpack config for svg :

{ test: "\.svg", loader: "file-loader?mimetype=image/svg+xml" },

Thank you for your help.

6
  • Can you open the file in the browser? I'd say it's a Webpack problem rather than VueJS. Commented Jan 26, 2016 at 11:37
  • Yes, the file can be loaded. And it's also it's correct path since the error message contain the beginning of the file. Commented Jan 26, 2016 at 12:14
  • You could try using svg-loader instead of file-loader. Commented Jan 26, 2016 at 12:35
  • I tried svg-loader with hopes. The compilation works, but then when loading the page, I have this new error : Unexpected token < You may need an appropriate loader to handle this file type. Commented Jan 26, 2016 at 13:02
  • In my opinion, svg-loader works for webpack, but when vuejs tries to compile src="...", it doesn't take into consideration the svg-loader rule and then fails. But my knowledge only go up to here. Commented Jan 26, 2016 at 13:03

1 Answer 1

6

This one works for me:

{test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'}

Its a regular expression to process any file with an svg extension as a static resource the way you are looking for.

I use it with other font loaders for bootstrap or other glyphfonts:

{test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff'},
{test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
{test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
{test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'}

Using that regular expression you can probably change the loader to file and inline it like you were using.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome that worked for me. Now I have to understand why it places my files in dist/dist/ instead of dist/ ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.