使用Webpack动态提供图片服务 [英] Serve images dynamically with webpack

查看:72
本文介绍了使用Webpack动态提供图片服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Webpack和提供图像的问题.

我有一个webpack配置,它可以构建一个React webapp,还可以提供来自特定文件夹的.jpg文件.

但是,如果我从Web应用程序中下载了新图像并将其添加到此文件夹,会发生什么情况?

我可以刷新webpack以便提供新图像,并可以使用 require.context 导入它吗?还是webpack不应该做的事情,所以我需要在后端处理这个问题?

谢谢

解决方案

Webpack通常不会处理此问题. require.context 创建对所有模块的引用(或在这种情况下,图片是位于与正则表达式匹配的请求所必需的目录中,因此,如果要使用该目录,则每次在文件夹中添加或删除图片时都需要重新编译应用.

最好在后端处理此问题,因此您可以直接使用指向图像的URL.

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,

解决方案

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.

这篇关于使用Webpack动态提供图片服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆