Webpack and React — Unexpected token

巧了我就是萌 提交于 2019-11-29 16:23:23

You also need to add the react preset into your babel-loader config

And it must come after the es2015

  {
    test: /\.js?$/,
    exclude: /(node_modules|bower_components)/,
    loader: 'babel', // 'babel-loader' is also a legal name to reference
    query: {
      presets: ['es2015', 'react']
    }
  }

The problem you're experiencing happens because for babel to know how to transpile JSX - it should know its syntax, which it does not out of the box.

As it was mentioned in the comments - you would also have to install the babel-preset-react npm package (which would obvious anyway since the babel would tell it to you on the first run anyway).

References:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!