我有一个使用带有一些文件加载器规则的 webpack 的 React 项目。我试图在我的 React 组件(JSX)中导入/需要一个图像,如下所示:
<img src={require('../../assets/images/msg.png')} />
在 macOS 计算机中,它会引发此错误:
ERROR in ./src/components/Header/index.js
Module not found: Error: Can't resolve '../../assets/images/search.jpg' in '/Users/leonimurilo/Personal projects/4Petson/src/components/Header'
@ ./src/components/Header/index.js 165:56-97
@ ./src/components/App/index.js
@ ./src/routes.js
@ ./src/index.js
@ multi ./src/webpack-public-path webpack-hot-middleware/client?reload=true ./src/index.js
令人惊讶的是,当我尝试在 Windows 7 计算机上运行该项目时,它运行良好!
这是我的 webpack 模块对象:
module: {
rules: [
{test: /\.jsx?$/, exclude: /node_modules/, loaders: ['babel-loader']},
{test: /\.eot(\?v=\d+.\d+.\d+)?$/, loader: 'file-loader'},
{test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader?limit=10000&mimetype=application/font-woff'},
{test: /\.[ot]tf(\?v=\d+.\d+.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=application/octet-stream'},
{test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=image/svg+xml'},
{test: /\.(jpe?g|png|gif)$/i, loader: 'file-loader?name=[name].[ext]'},
{test: /\.ico$/, loader: 'file-loader?name=[name].[ext]'},
{test: /(\.css|\.scss|\.sass)$/, loaders: ['style-loader', 'css-loader?sourceMap', 'postcss-loader', 'sass-loader?sourceMap']}
]
}
有人可以帮我解决这个问题吗?