Can't compile ES7 features with Webpack and React

时间:2016-03-02 10:58:04

标签: reactjs webpack

I'm trying to compile my React app with support for ES7 decorators as I'm using the autobind-decorator but webpack says there is an "Unexpected token" with the router, which is the entry file for the app. I've tried various versions of babel related npms, using the the "state-0" and "transform-runtime" and they all result in the same error. Anyone's help would be very much appreciated :)

main.js
enter image description here
webpack.config
enter image description here
package.json
enter image description here

2 个答案:

答案 0 :(得分:2)

此错误的原因是webpack.config.js文件中的加载程序配置不正确。您需要在加载程序配置中的presets字段下提供query

loaders: [
  {
    test: /\.jsx?$/,
    loader: 'babel',
    query: {
      presets: ['es2015', 'react', 'stage-1']
    }
  }
]

答案 1 :(得分:0)

根据the README of babel-loader,您应该在配置中使用query字段,如下所示:

module: {
  loaders: [
    {
      test: /\.jsx?$/,
      include: // ....
      loader: 'babel',
      query: {
        presets: [/*presets list*/],
        plugins: [/*plugins list*/]
      }
    }
  ]
}