(Webpack)使用url-loader或文件加载器,我真的必须在我的.js中为我想要包含的每个静态图像包含一个require()吗? [英] (Webpack) Using the url-loader or file-loader, do I really have to include a require() in my .js for every static image I want to include?

查看:54
本文介绍了(Webpack)使用url-loader或文件加载器,我真的必须在我的.js中为我想要包含的每个静态图像包含一个require()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习webpack,在我的生产版本中出现图像时出现问题,直到我偶然发现一些带有 require的代码('path / to / image.png' )位于.js文件的顶部。所以我试了一下,然后看到它有效。

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.

推荐答案

你可以使用 CopyWebpackPlugin 在构建webpack项目时将 src 文件移动到assets文件夹。

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

此答案中的详细信息: https://stackoverflow.com/a/33374807/492976

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

这篇关于(Webpack)使用url-loader或文件加载器,我真的必须在我的.js中为我想要包含的每个静态图像包含一个require()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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