@RGraham 的回答很有帮助——还想分享更多细节...
我正在使用 gulp,所以首先我必须从 gulp-babel 5.x 升级到 6.x:
npm install --save-dev babel/gulp-babel
然后我需要安装 ES2015 预设和我想使用的两个插件(+ 反应预设,因为我使用的是 ReactJS):
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-react
npm install --save-dev babel-plugin-transform-class-properties
npm install --save-dev babel-plugin-transform-decorators
最后,我将以下配置传递给 Babel(您也可以在 .babelrc 文件中指定):
{
presets: ['es2015', 'react'],
plugins: [
["transform-class-properties"],
["transform-decorators"]
]
}
您的 JavaScript linter 会抱怨 ES7 语法。 This blog post 描述了解决方案:修改 lintjs 以使用 Babel 作为 JavaScript 解析器。然后在你的.eslintrc 文件中设置"parser": "babel-eslint"
npm install --save-dev babel-eslint
最后,对于任何其他 ReactJS 人员,请确保修改组件的构造函数以包含道具和上下文参数,否则您可能会收到类似 TypeError: Cannot read property 'xxx' of undefined 的运行时错误。这是一个例子:
/**
* ES6 constructor method (takes the place of the old React componentWillMount() method).
*
* @param {Object} props See https://facebook.github.io/react/docs/reusable-components.html
* @param {Object} context See https://facebook.github.io/react/docs/context.html
*/
constructor(props, context) {
super(props, context);
log.debug(`Creating ${this.constructor.name} instance`, props, context);
}