Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to load SVG images? #22

Open
UlpiusCode opened this issue Jan 15, 2019 · 11 comments
Open

How to load SVG images? #22

UlpiusCode opened this issue Jan 15, 2019 · 11 comments

Comments

@UlpiusCode
Copy link

Nice starter! How I can load SVG images in html and background: url()?

Thanks a lot!

@UlpiusCode
Copy link
Author

can you help me?

@washaweb
Copy link

If you want to use svg files, you have to add a file-loader in your webpack.dev.js and webpack.prod.js config files (in rules[] ) :

...
{
    test: /\.svg$/, 
    loader: 'file-loader' 
}
...

@UlpiusCode
Copy link
Author

UlpiusCode commented Feb 26, 2019

@washaweb this boilerplate use url-loader to loading images, why for svg images i need to use file-loader?
can I add svg here: test: /\.(png|jpg|gif|svg)$/, and load with url-loader? or from file-loader is better?

@chrisschaetzlein
Copy link

How can I reference to the SVG file in the index.html after I added the loader to webpack's config files? <img src="<% require('.src/path/to/image.svg') %>"> doesn't seem to work - it outputs only an empty string.

@washaweb
Copy link

washaweb commented Mar 1, 2019

@chrisschaetzlein either remove the '.' at the beginning of the SVG path or add a slash just after. You URL should look like this:

<%= require('./src/path/to/image.svg') %>
or:
<%= require('src/path/to/image.svg') %>

@chrisschaetzlein
Copy link

@washaweb That was a typo in my comment, sorry. I already had ./src/path/to/image.svg. Webpack seems to find the file, it's included in the console output. Doesn't output anything to index.html though.

@washaweb
Copy link

washaweb commented Mar 1, 2019

@UlpiusCode You could do that (using url-loader for your svg images), but doing so, SVG code would be converted in a base64 encoded image.
That is not a really a good thing if you intend to later modify your SVG code with CSS or JavaScript interactions.

@washaweb
Copy link

washaweb commented Mar 1, 2019

@chrisschaetzlein do you place this code in a <img src=""> tag ?

@chrisschaetzlein
Copy link

@washaweb jeez, I started the include in the .html file with <% require instead of <%= require... sorry! It works with the latter, obviously 🙄

@washaweb
Copy link

washaweb commented Mar 1, 2019

;-)

@lifenautjoe
Copy link
Owner

Thanks for the help @washaweb ! And sorry I couldn't look into this everyone else, stressing out over deadlines currently.

Will incorporate the solution into the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants