Rails Webpacker和stage-2类转换属性不起作用

时间:2017-03-29 00:10:57

标签: javascript ruby-on-rails webpack ecmascript-6 babel

我正在尝试使用新的Rails应用程序上的Webpacker来熟悉它,我无法编译我的javascript代码。

以下是代码:

import React, { Component, PropTypes } from 'react'
import DayPicker, { DateUtils } from 'react-day-picker'
import 'react-day-picker/lib/style.css'

export default class Example extends Component {
  constructor(props) {
    super(props)
    this.state = {
      selectedDays: []
    }
  }

  handleDayClick = (day, selected) => {
    console.log(day)
  }

  render() {
    return (
      <p>Test</p>
    )
  }
}

20:05:21 hot.1  | ERROR in ./app/javascript/packs/components/example/example.jsx
20:05:21 hot.1  | Module build failed: SyntaxError: Missing class properties transform.
20:05:21 hot.1  |
20:05:21 hot.1  |   11 |   }
20:05:21 hot.1  |   12 |
20:05:21 hot.1  | > 13 |   handleDayClick = (day, selected) => {
20:05:21 hot.1  |      |   ^

这是一个全新的Rails应用程序,使用来自github的最新Webpacker gem。

我的.babelrc

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

我的devDependencies是:

"devDependencies": {
  "babel-preset-es2015": "^6.24.0",
  "babel-preset-stage-2": "^6.22.0",
  "webpack-dev-server": "^2.4.2"
}

根据Babel REPL链接,这应该有效。 https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-2&targets=&browsers=&builtIns=false&experimental=true&loose=false&spec=false&code=class%20PostInfo%20extends%20React.Component%20%7B%0A%09handleOptionsButtonClick%20%3D%20(e)%20%3D%3E%20%7B%0A%20%20%20%20this.setState(%7BshowOptionsModal%3A%20true%7D)%3B%0A%20%20%7D%0A%7D

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

要修复,我必须将预设添加到各个加载程序文件中,具体而言。

// config/webpack/loaders/babel.js
module.exports = {
  test: /\.js(\.erb)?$/,
  exclude: /node_modules/,
  loader: 'babel-loader',
  options: {
    presets: [
      'es2015',
      'stage-2'
    ]
  }
}

// config/webpack/loaders/react.js
module.exports = {
  test: /\.(js|jsx)?(\.erb)?$/,
  exclude: /node_modules/,
  loader: 'babel-loader',
  options: {
    presets: [
      'es2015',
      'react',
      'stage-2'
    ]
  }
}

现在我可以使用漂亮的速记箭头类函数了。

答案 1 :(得分:-1)

你的评论最有启发性:这是一个类中的方法声明;应该没有等号或胖箭。这应该是它的样子:

handleDayClick (day, selected) {
    console.log(day)
}

了解constructorrender方法的外观;这就是反应组件中所有方法的外观。

d&#39;!哦