问题描述
我不知道在 ReactJS webpack 中加载图像的正确加载器是什么,
I can't figure what is the proper loader to load images in ReactJS webpack,
你能帮我一把吗?我收到此错误:
May you give me a hand?I get this error:
Module parse failed: /Users/imac/Desktop/fakeeh/imgs/logo.png Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
这里是 webpack 配置:
Here is webpack config:
const path = require('path');
module.exports = {
// the entry file for the bundle
entry: path.join(__dirname, '/client/src/app.jsx'),
// the bundle file we will get in the result
output: {
path: path.join(__dirname, '/client/dist/js'),
filename: 'app.js',
},
module: {
// apply loaders to files that meet given conditions
loaders: [{
test: /\.jsx?$/,
include: path.join(__dirname, '/client/src'),
loader: 'babel-loader',
query: {
presets: ["react", "es2015", "stage-1"]
}
}],
},
// start Webpack in a watch mode, so Webpack will rebuild the bundle on changes
watch: true
};
非常感谢!!
推荐答案
我也遇到了这个问题,我已经找到了解决方法.
I also encountered this problem too and I've found a workaround.
首先,您需要安装两个加载器(file-loader、url-loader).例如$ npm install --save file-loader url-loader
First, you need to install the two loaders(file-loader, url-loader).e.g.$ npm install --save file-loader url-loader
如果你想支持css.确保安装样式加载器.例如.,$ npm install --save style-loader css-loader
If you want to support the css. Make sure you install the style loaders.e.g.,$ npm install --save style-loader css-loader
接下来,您更新 webpack 配置,请检查下面我的示例配置.希望有帮助.
Next, you update the webpack config, kindly check below my sample configurations. Hope it helps.
module: {
loaders: [{
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/
}, {
test: /\.css$/,
loader: "style-loader!css-loader"
}, {
test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: 'url-loader?limit=100000' }]
},
这篇关于IMAGE:您可能需要一个合适的加载器来处理这种文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!