React 使用img标签加载图片,加载成功,但是一刷新页面就出现BUG: Unexpected character '�' (1:0)

#1

第一次加载成功,但是一刷新页面图片就报错。

img标签

<img src={require('./style/ydg_logo.jpg')} alt="云东购logo" />

webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'cheap-module-eval-source-map',
  devtool:false,
  entry: [
    'webpack-hot-middleware/client',
    './client/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        //supresses warnings, usually from module minification
        warnings: false
      }
    }),
    new webpack.DefinePlugin({
      'process.env':{
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      __SERVER__: false
    })
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        exclude: /node_modules/,
        include: __dirname,
        query: {
          presets: [ 'react-hmre' ]
        }
      },
      {
        test: /\.css?$/,
        loaders: [ 'style', 'raw' ],
        include: __dirname
      },
      { test: /\.(jpeg|jpg|png|gif)$/,
        loader: 'url-loader?limit=10240' },
    ]
  }
};
#2

看了你的配置,提醒一点:开发模式下不要开启UglifyJsPlugin,热更新会很慢的。。

#3

楼主解决了吗,我今天也遇到了和你同样的问题

#4

你们都自己写配置文件的吗?

#5

好像是webpack的一个bug

#6

我也遇到这个问题了,不知道解决了没有哦。

#7

刚刚搜到 starkover 上面,好像也没有太多解决办法。 只能重新生成 整个包。

react-native run-ios

重新后图片就正常了。如果之前有 单独 启动 react-native start , 记得 这个也要重启。