简体   繁体   English

Webpack React在html标签上构建抛出错误

[英]Webpack React build throwing errors on html tags

I am getting an error every time I run npm start, here is my error.... 每次运行npm start时都会收到错误,这是我的错误....

ERROR in ./app/App.js
Module build failed: SyntaxError: Unexpected token (8:3)

   6 |  render() {
   7 |      return (
>  8 |          <div>
     |          ^
   9 |              Testing this
  10 |          </div>
  11 |      )

This is inside a simple react component. 这是一个简单的反应组件。 For some reason html tags are causing errors in my build. 由于某种原因,html标签在我的构建中导致错误。 Below is my webpack.config.js and package json file. 下面是我的webpack.config.js和包json文件。

var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');


var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  entry: [
    './app/App.js'
  ],
  output: {
    path: __dirname + '/public',
    filename: "bundle.js"
  },
  plugins: [HTMLWebpackPluginConfig],
  devServer: {
       inline:true,
       contentBase: './public',
       port: 3333
     },
  module: {
    loaders: [{ 
         test: /\.js$/, 
         exclude: /node_modules/, 
         loader: "babel-loader"
      },
        {
              test: /\.scss$/,
              loader: 'style!css!sass'
            }]
  }
};

the package json 包json

{
  "name": "lr",
  "version": "1.0.0",
  "description": "",
  "main": "App.js",
  "scripts": {
    "start": "webpack-dev-server"
  },
  "author": "sam",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.21.0",
    "babel-loader": "^6.2.10",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "css-loader": "^0.26.1",
    "html-webpack-plugin": "^2.25.0",
    "sass": "^0.5.0",
    "sass-loader": "^4.1.1",
    "style-loader": "^0.13.1",
    "webpack": "^1.14.0",
    "webpack-dev-server": "^1.16.2"
  },
  "dependencies": {
    "react": "^15.4.1",
    "react-dom": "^15.4.1",
    "react-hot-loader": "^1.3.1",
    "react-router": "^3.0.0"
  }
}

No clue why html tags are throwing errors. 不知道为什么html标签会抛出错误。

You'll need to inform webpack that you also want it to consume .jsx extensions. 您需要通知webpack您还希望它使用.jsx扩展名。 Try updating your test pattern: 尝试更新您的测试模式:

{
    test   : /\.(js|jsx)$/,
    loaders: ['babel'],
    include: path.join(__dirname, 'src')
}

You also need a .babelrc file to then inform babel how you'd like it to perform the compilation: 您还需要一个.babelrc文件,然后通知babel您希望它如何执行编译:

{
  "presets": ["es2015", "react"]
}

Have a peek here for a working implementation: https://github.com/mikechabot/react-boilerplate 请看一下工作实现: https//github.com/mikechabot/react-boilerplate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM