webpack - webpack 4 - 文件加载器不工作
问题描述
[webpack.config.js]
test: /\.(png|jpg|jpeg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'img/[name].[hash:10].[ext]',
}
}
]
[在前端代码(MainPage.jsx)]
let imgURL = require('../../../assets/mars.png')
console.log(imgURL)
<CardMedia
className={classes.media}
image={imgURL}
title="Contemplative Reptile"
/>
// <CardMedia> is material-ui component
日志 :
img/mars.ed66029dc3.png
Failed to load resource: net::ERR_FILE_NOT_FOUND mars.ed66029dc3.png
鼠标悬停消息 mars.ed66029dc3.png:
D:/projectname/build/html/img/mars.ed66029dc3.png
我需要的输出是 D:/projectname/build/img/mars.ed66029dc3.png
【项目结构】
build
- html
index.html (electron entrance)
- img
mars.ed66029dc3.png (file-loader copy)
bundle.js (webpack output file, call by index.html)
src
- assets
mars.png
- core
- render
- html
index.html
- js
- components
MainPage.jsx
它怎么能解决它?使用 publicPath 也不起作用,
而且我不知道如何
在 html 中统一所有路径 - 我在 jsx 中使用电子协议
- 导入和要求我在 jsx img 中使用 webpack 别名
- 我使用 file-loader ....
请帮助, 谢谢
解决方案
推荐阅读
- ruby-on-rails - 如何避免关联模型的 where 查询被触发两次(Rails)?
- html - 防止将网站大小调整到限制以下
- c - 如何正确循环嵌套 switch 语句以提供显示的输出?
- ios - UICollectionView 的帧大小大于 UIScreen 的大小
- css - 响应式水平时间线 - Boostrap 4
- python - 如何检查两个三维numpy数组是否相同?
- python - 在 Pycharm 中,即使在我编辑代码并重新运行算法之后,如何保留我以前的运行结果?
- cakephp - 初始化蛋糕项目
- angular - 如何使用 TypeScript 函数在正文中动态添加组件
- python - 如何在 tkinter 中获取标签的当前宽度