使用 webpack 动态提供图像 [英] Serve images dynamically with webpack

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

问题描述

我有一个关于 webpack 和提供图片的问题.

I have a question regarding webpack and serving images.

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

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

但是如果我从我的 web 应用下载并添加一个新图像到这个文件夹会发生什么?

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

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

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?

谢谢,

推荐答案

这不是 Webpack 通常会处理的事情.require.context 创建对所有模块(或在这种情况下为图像)在一个目录中,该目录可能需要与正则表达式匹配的请求,因此如果您要使用该目录,则每次从文件夹中添加或删除图像时都需要重新编译您的应用程序.

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.

最好在后端处理,这样您就可以直接使用图片的 URL.

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天全站免登陆