17

I'm still learning webpack, and I was having trouble getting images to show up in my production build until I stumbled upon some code which had a require('path/to/image.png') at the top of a .js file. So I tried it, and lo and behold it works.

This seems wonky to me. Do I really have to include one of these for every static image I need to serve? Is there a better way to do this? This is going to be messy if not.

3 Answers 3

6

You can use the CopyWebpackPlugin to move src files to an assets folder when building the webpack project.

Details in this answer: https://stackoverflow.com/a/33374807/492976

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

1 Comment

If you use this plugin then how do you append a hash-code to your assets? If you don't you have problems with browser not updating the cache on deploys.
6

There are loaders like css-loader and url-loader which resolve urls to base64 inlined data strings instead of serving up the static asset.

You can see this great guide for how to implement with url-loader. If you are having issues you need to make sure you are using the correct relative path.

'./path/to/image.png'

6 Comments

I am aware of these loaders you mention and am already using both. Sometimes it doesn't make sense to base64 encode the images though, and in any case I believe you still have to require('path/to/image.png') which is what I'm really asking about. Is there a way to include my images by simply referring to them in the html, rather than having to require them in my js?
If you set the limit query for url-loader it will only online images up the the specified asset size. If it is larger then file loader will automatically emit that file with your bundles for you.
Yes, I know that as well, but this doesn't really answer my question. I want to know if there is some way I can circumvent the need to do a require('path/to/image.png') in my javascript file. I don't know how I could be any more clear that this is what I'm asking.
To answer the question you're actually asking, no, you can't include the image without importing it in some way into your javascript, whether it's with require() or es6 imports.
@Anj, Not sure about webpack 1, but the url-loader in webpack 2 doesn't require the use of require(). It works by simply using the correct path syntax as Sean mentioned above. It works for me, and I'm definitely not using require() for images.
|
0

Using React, the npm package babel-plugin-transform-react-jsx-img-import resolves any img elements and there is no need to explicit import or require those image resources.

import React from 'react'; const Image = () => { {* webpack configured to resolve the alias 'images' *} return (img src='images/foo.png'/>); } export default Image; 

1 Comment

A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. See meta.stackexchange.com/a/8259

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.