当前位置: 首页 > 面试题库 >

图片:您可能需要适当的加载程序来处理此文件类型

蒲德曜
2023-03-14
问题内容

我不知道什么是在ReactJS webpack中加载图像的合适的加载器,

你能帮我一下吗?我收到此错误:

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配置:

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
};

非常感激!!


问题答案:

我也遇到了这个问题,并且找到了解决方法

首先,您需要安装两个加载器(文件加载器,URL加载器)。例如 $ npm install-保存文件加载器url加载器

如果要支持css。确保安装样式加载器。例如, $ npm install –save style-loader css-loader

接下来,您更新webpack配置,请在我的示例配置下面进行检查。希望能帮助到你。

  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' }]
  },


 类似资料: