2

I am trying to import the image in my React code like so (I'm using babel):

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

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

What I see when I inspect the broken image in the browser:

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

It used to work until I added in css-modules. This is what my webpack config looks like:

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

};
raptoria7
  • 789
  • 7
  • 12
  • Can give it a try to use `loaders: ['style-loader', {loader: 'css-loader', query: { /* your query object */ }]` instead of the call to `combineLoaders`? Attention it's `loaders` then, not `loader`. – dotcs Dec 01 '16 at 18:56

1 Answers1

2

This fixed it:

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" }
],
},
Jon Willis
  • 6,993
  • 4
  • 43
  • 51
raptoria7
  • 789
  • 7
  • 12
  • 1
    this saved my time, thank you very much! BTW, it looks like a misprint here: "minetype=application/font-woff". It seems should be "mimetype=", isn't it? – ASten Feb 22 '17 at 00:07