reactjs - Webpack 在 React 图像导入时抛出错误
问题描述
我正在尝试使用 React (create-react-app) 和 Firebase 实现 SSR。为此,我目前正在webpack
按照本教程和github dir进行配置:
module.exports = [{
entry: './src/index.js',
module: {
rules: [
{test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/},
{test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/},
{test: /\.css$/i, use: ['style-loader', 'css-loader']},
]
},
output: {
filename: 'public/bundle.js',
path: __dirname
}
}];
我有一个包含一些图像的资产文件夹。不知何故,对于所有导入,webpack
返回以下错误:
ERROR in ./src/assets/images/capture_5_move_on/1.JPG 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
@ ./src/shared/data.js 28:0-59 78:9-12
@ ./src/modules/CartHolder/CartHolderMobile.js
@ ./src/App.js
@ ./src/index.js
解决方案
您应该为图像使用文件加载器:https ://github.com/webpack-contrib/file-loader 。
不知何故像这样:
module.exports = [{
entry: './src/index.js',
module: {
rules: [
{test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/},
{test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/},
{test: /\.(png|jpe?g|gif)$/i, loader: 'file-loader', exclude: /node_modules/},
{test: /\.css$/i, use: ['style-loader', 'css-loader']},
]
},
output: {
filename: 'public/bundle.js',
path: __dirname
}
}];
推荐阅读
- javascript - Angular 我如何用异步和承诺初始化和填充数组
- amazon-web-services - 调用 AssumeRole 操作时发生错误 (AccessDenied)
- python - x轴的seaborn matplotlib包装不起作用
- google-maps-api-3 - 如何返回有服务区但没有位置的地方的评论?
- android - Android Q 连接 WiFi 问题
- javascript - 每次单击都会增加 Firestore 更新值
- python - 发送多个选择数据时的Django MultiValueDictKeyError
- remote-debugging - 使用 Visual Studio Mac 进行远程调试
- c# - Asp.net zero 会自动记录错误吗?
- python - 为什么 Pytest 对夹具参数执行嵌套循环