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

新手上路,请多包涵

我不知道在 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
};

非常感激!!

原文由 Abdulrahman Mushref 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 492
2 个回答

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

首先,您需要安装两个加载器(file-loader、url-loader)。例如 $ npm install –save file-loader url-loader

如果你想支持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' }]
  },

原文由 Arman Ortega 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以使用 文件加载器。您需要先使用 npm 安装它,然后像这样编辑您的 webpack 配置

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"]
      }
    },
    {
      test: /\.(gif|svg|jpg|png)$/,
      loader: "file-loader",
    }],
  },

原文由 Prakash Sharma 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题