Serve images dynamically with webpack

爱⌒轻易说出口 提交于 2019-12-11 02:08:23

问题


I have a question regarding webpack and serving images.

I have a webpack config that build a React webapp and also serves .jpg files from a specific folder.

But what happens if from my webapp I download and add a new image to this folder?

Can I refresh webpack so that it will serve the new image and I will be able to import it with require.context? Or, is it something that webpack is not supposed to do, and so I need to have this handled in the backend?

Thanks,


回答1:


This isn't something that would typically be handled by Webpack. require.context creates references to all modules (or in this case images) in a directory that can be required with a request matching a regular expression, so if you were to use that, you'd need to recompile your app every time you add or remove an image from the folder.

It would be best to handle this in the backend, so you can just use the URLs to the images directly.



来源:https://stackoverflow.com/questions/46889634/serve-images-dynamically-with-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!