Webpack not loading png's after url-loader is included in web pack.config

511 Views Asked by At

So this is a basic web pack problem that i've seen solved on here before but for some reason I can seem to get web packs to load png's properly.

I've read several of the solutions and have tried many of them unsuccessfully. I've tried requiring the png like <img src=require('someone.png')/> and i've tried importing the image and I still get the same error. The good thing is that it sees the file but it reads the file as a text file.

error

Module build failed: SyntaxError: Unexpected character '�' (1:0)

1 | �PNG | ^ 2 | 3 | 4 | IHD;0��sRGB��� IDATH

Webpacks code

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'},
      {
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
         // inline base64 URLs for <=8k images, direct URLs for the rest
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

Package.json

{
  "name": "moonshot",
  "version": "1.0.0",
  "description": "Simple starter package for Redux with React and Babel support",
  "proxy": "http://loclhost:3090/",
  "main": "index.js",
  "scripts": {
    "start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
    "test:watch": "npm run test -- --watch"
  },
  "author": "Austin Scott",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.2.1",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "chai": "^3.5.0",
    "chai-jquery": "^2.0.0",
    "file-loader": "^0.9.0",
    "jquery": "^2.2.1",
    "jsdom": "^8.1.0",
    "mocha": "^2.4.5",
    "react-addons-test-utils": "^0.14.7",
    "url-loader": "^0.5.7",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0"
  },
  "dependencies": {
    "axios": "^0.15.0",
    "babel-preset-stage-1": "^6.1.18",
    "lodash": "^3.10.1",
    "react": "^0.14.3",
    "react-dom": "^0.14.3",
    "react-redux": "^4.0.0",
    "react-router": "^2.0.1",
    "redux": "^3.0.4",
    "redux-form": "^4.1.3",
    "redux-thunk": "^2.1.0"
  }
}
1

There are 1 best solutions below

1
On

If you are getting above error while running the mocha test cases you need to add this to your test_helper.js

// Disable webpack-specific features for tests since
// Mocha doesn't know what to do with them.
['.css', '.scss', '.png', '.jpg'].forEach(ext => {
  require.extensions[ext] = () => null;
});