SyntaxError in Webpack / React.js Unexpected token = { } in React

大兔子大兔子 提交于 2019-12-11 16:54:11

问题


In one of my .js file, I'm getting the following error from Webpack:

  4 | export default function(ComposedComponent) {
  5 |   class Authentication extends Component {
> 6 |     static contextTypes = {
    |                         ^
  7 |       router: React.PropTypes.object
  8 |     }
  9 | 

These are my devdependencies:

  "devDependencies": {
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-3": "^6.24.1",
    "css-loader": "^0.26.1",
    "file-loader": "^1.1.4",
    "html-webpack-plugin": "^2.30.1",
    "image-webpack-loader": "^3.4.2",
    "rimraf": "^2.6.2",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.9",
    "webpack": "2.2.0-rc.0",
    "webpack-dev-server": "^2.2.0-rc.0"
  }

And this is my Webpack configuration:

{
  "presets": ["env", "react", "stage-3"],
  "plugins": ["transform-object-rest-spread"]
}

Could anybody tell which plugin or package is missing? Thank you!


回答1:


Nothing is missing, it's just incorrect syntax.

Static methods should be (as the name implies) functions.

Try:

class Authentication extends Component {
   ...
}

Authentication.contextTypes = {
    router: React.PropTypes.object
}



回答2:


Class properties is a stage 2 proposal at the moment, so it's not included in babel-preset-env. You can either:

  • include only this specific transform with .babelrc:

    { "plugins": ["transform-class-properties"] }

  • include all stage 2 proposals with :

    { "presets": ["env", "react", "stage-2"] }

Please consider the risks before adding all stage-2 proposals : https://babeljs.io/docs/plugins/preset-stage-2/



来源:https://stackoverflow.com/questions/46532922/syntaxerror-in-webpack-react-js-unexpected-token-in-react

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