Webpack.config中包含webpack未在url-loader之后加载png

时间:2016-12-20 07:57:57

标签: node.js reactjs webpack babeljs webpack-dev-server

所以这是我之前看到的基本网络包问题,但出于某种原因我似乎可以通过网络包来正确加载png。

我已经阅读了几个解决方案并尝试了其中许多解决方案。我试过要求像<img src=require('someone.png')/>那样的png,我尝试导入图像,但我仍然得到同样的错误。好处是它看到了文件,但它将文件作为文本文件读取。

错误

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

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

Webpacks代码

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 个答案:

答案 0 :(得分:0)

如果您在运行mocha测试用例时遇到上述错误,则需要将其添加到// 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; });

<section id="attorneys" data-navigate="attorneys" data-position="-126">
    <div class="attorneys-content">
        <div class="container">
            <div class="clear clearfix"></div>
            <div class="col-sm-12 ">
                <div class="typography style2 webkit" data-animate="fadeInUp">
                    <i class="fa fa-university" aria-hidden="true"></i>
                    <h3 class="text1">Meet here</h3>
                    <h1 class="text2">Our Attorneys</h1>
                </div>
            </div>
        </div>
    </div>
</section>