javascript - 类中的箭头函数使用 webpack-encore 抛出错误

标签 javascript reactjs babeljs webpack-encore

配置

我正在使用 和我的 编译我的项目 项目。到目前为止,我已经使用了基本的 webpack.config.js 设置,它应该可以与 react when enabling it 开箱即用。 :

// webpack.config.js
// ...

Encore
    // ...
    .enableReactPreset()
;

我尝试过的:

我继续添加了 babel 配置(我认为不需要),希望它能解决问题,但它没有:

.configureBabel(function(babelConfig) {
        // add additional presets
        babelConfig.presets.push('es2017');
    })

代码示例:

这是一个应该可以工作的例子,但它没有编译并抛出以下错误:

Syntax Error: Unexpected token

import React, {Component} from 'react';

//This works
const someExteriorHandler = () => {};

export default class Example extends Component {
   //error bad syntax, points specifically at the equal sign.
   someHandler = () => {

   }
   render(){return(<h1>This is a test</h1>)}
}

问题

如何在 中获取 babel 编译器?在 中编译箭头函数上课?

最佳答案

这个问题是 solved .

Having assignment of arrow functions in a class is not part of ES6. It is part of a draft proposal for an upcoming ES version. Babel is able to transpile it, but you will need to enable this transformation in the babelrc file. The default babel config shipped in Encore when you don't configure babel does not enable the transpilation of experimental features.

安装实验性功能

yarn add --dev babel-plugin-transform-class-properties babel-plugin-transform-object-rest-spread

配置webpack.config.js

.configureBabel(function(babelConfig) {

    //This is needed.

    babelConfig.plugins = ["transform-object-rest-spread","transform-class-properties"]
})

它现在应该可以编译了,并且具有所有 JSX 特性。

关于javascript - 类中的箭头函数使用 webpack-encore 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445093/

相关文章:

javascript - 我们可以使用 axios 的 onDownloadProgress 来加载 API 吗?

reactjs - this.setState() 不会渲染获取的 Firebase 图片

javascript - 如何将react js组件拆分成多个文件

javascript - 如何导入类的静态成员?

javascript - 我无法对来自 redis 的 JSON 数据使用 parseInt ?

javascript - 简单的 qtip2 示例不起作用

javascript - 无法访问 `process.env` 中的 Node.js 环境变量

javascript - ESLint 如何修复解析错误 : 'import' and 'export' may only appear at the top level

javascript - 如何在单击 SELECT 多行时创建一个选项?

javascript - 调整浏览器大小时调整 jqGrid 的大小?