Equal sign in bad place causes unknown token excep

2019-01-15 11:07发布

问题:

I'm upgrading a react babel 5 project to babel 6. The code is from pluralsight on github. The error occurs on app.js. I assume there is a plugin that needs an upgrade that allows this syntax to work:

export default class DriftApp extends React.Component {
  state = {//builder is not happy with this equals sign, unexpected token
    showIndex: 0,
    numSlides: 5
  }

Which plugin does this?

Here's my package.json:

 "homepage": "https://github.com/jaketrent/react-drift#readme",
  "devDependencies": {
    "autobind-decorator": "^1.3.3",
    "babel-core": "^6.5.1",
    "babel-loader": "^6.2.2",
    "babel-plugin-react-transform": "^2.0.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.5.0",
    "babel-preset-react": "^6.5.0",
    "express": "^4.13.3",
    "file-loader": "^0.8.4",
    "radium": "^0.16.6",
    "react": "^0.14.2",
    "react-dom": "^0.14.2",
    "react-hot-loader": "^2.0.0-alpha-4",
    "react-tools": "^0.10.0",
    "react-transform": "0.0.3",
    "react-transform-catch-errors": "^1.0.0",
    "react-transform-hmr": "^1.0.1",
    "redbox-react": "^1.1.1",
    "webpack": "^1.12.2",
    "webpack-dev-middleware": "^1.2.0",
    "webpack-hot-middleware": "^2.4.1"
  }
}

回答1:

You are using property initializers syntax in your code, which is an experimental feature (proposal). You can transpile this to ES5 by installing the babel-plugin-transform-class-properties module

npm install babel-plugin-transform-class-properties  --save-dev

Add the following line to your .babelrc file:

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

ref https://babeljs.io/docs/plugins/transform-class-properties/



回答2:

Add the babel-preset-stage-0 npm module and add stage-0 to your babel presets.

If you want an example babel 6 configuration that works well with React, look at the package.json and .babelrc from the react-redux-starter-kit.