2

我正在尝试像这样在我的 React 代码中导入图像(我正在使用 babel):

import borgCube from '../assets/cube.png';

<img className="img-rounded" src={borgCube}></img><p />

在浏览器中检查损坏的图像时看到的内容:

<img class="img-rounded" src="data:image/png;base64,bW9kdWxlLmV4cG9ydHMgPSBfX3dlYnBhY2tfcHVibGljX3BhdGhfXyArICI5NTU1ZGJiNWQ3YjUzMjA3N2NjNWQyMzc4ZDgzNzVmZS5wbmciOw==">

在我添加 css-modules 之前,它一直有效。这是我的 webpack 配置的样子:

var webpack = require('webpack');
var path = require('path');
var combineLoaders = require('webpack-combine-loaders');

const isDebug = !process.argv.includes('--release');

module.exports = {
  devtool: 'eval',

  entry: {
    app: [
      'webpack-dev-server/client?http://0.0.0.0:3000',
      'webpack/hot/only-dev-server',
      './src/index'
    ]
  },

  output: {
    filename: '[name].js',
    path: path.join(__dirname, './build'),
    publicPath: 'http://localhost:3000/build/'
  },

  resolve: {
    extensions: ['', '.js', '.jsx', '.css', '.png'],
    modulesDirectories: ['src', 'node_modules']
  },

  module: {
    loaders: [
      {
        test: /\.(js|jsx)$/,
        loaders: ['react-hot', 'babel'],
        exclude: /node_modules/
      },
    { test: /\.(woff|png)$/, loader: 'url-loader?limit=10000' },
    { test: /\.(png|jpg)$/, loader: 'file-loader'},

    {
        test: /\.css$/,
        loader: combineLoaders([
          {
            loader: 'style-loader'
          }, {
            loader: 'css-loader',
            query: {
              modules: true,
              localIdentName: '[name]__[local]___[hash:base64:5]'
            }
           }
          ])
      },
    ]

  },


  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development') }
    })
  ]

};
4

1 回答 1

2

这修复了它:

module: {
loaders: [
  {
    test: /\.(js|jsx)$/,
    loaders: ['react-hot', 'babel'],
    exclude: /node_modules/
  },
  {
      test: /\.css$/,
      loader: combineLoaders([
        {
          loader: 'style-loader'
        }, {
          loader: 'css-loader',
          query: {
            modules: true,
            localIdentName: '[name]__[local]___[hash:base64:5]'
          }
         }
        ])
    },
  { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: "url-loader?limit=100000" },
  { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
  { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
],
},
于 2016-12-12T18:00:07.877 回答