'模块构建失败:具有合法React代码的SyntaxError:Unexpected token'

时间:2018-03-21 17:43:21

标签: javascript node.js reactjs webpack babel

The error I'm getting

当我尝试编译我的React应用程序时出现错误。看起来Babel没有正确配置将React代码转换为vanilla JS。我尝试了很多不同的东西,但都没有用。这是我的最终代码,我认为应该起作用:

这是我的代码:

{
  "presets": ["es2015", "react"],
  "plugins": [
    ["import", { "libraryName": "antd", "style": "css" }],
    "transform-class-properties",
    "transform-object-rest-spread"
  ]
}

这是我的.babelrc文件:

const webpack = require('webpack');

module.exports = {
  entry: [
    'react-hot-loader/patch',
    './src/index.js'
  ],
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      { 
        test: /\.css$/, 
        use: ['style-loader', 'css-loader'] 
      },
  { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader" },
  { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader" }
    ]
  },
  resolve: {
    extensions: ['*', '.js', '.jsx']
  },
  output: {
    path: __dirname + '/public',
    publicPath: '/',
    filename: 'bundle.js'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin()
  ],
  devServer: {
    contentBase: './public',
    hot: true
  }
};

这是我的webpack.config.js:

from itertools import product
from scipy.stats import chisquare
from numpy.random import randint


multiple = 10
number_of_random_numbers = 17576*multiple #There are 17576 possible three letter words. Therefore it's easy use a sequence length of
                                        # 17576 times some natural number.

expectation = multiple     #Given that the null hypothesis is true
                           # (equivalent to saying: given that the random sequence is iid and uniform) we expect
                           # that each word occurs once every 17576 times. Therefore the expected occurrence of each
                           # combinations is equal to the multiple. If we have a random sequence of length 2*17576 we
                           # expect each word to occur twice.

def seedlcg(init_Val):
    global rand
    rand = init_Val

def lcg_randu():
    a = 65539
    c = 0
    m = 2**31
    global rand
    rand = (a*rand + c) % m
    return rand/m

seedlcg(30000)

random_sequence = []
for i in range(1,number_of_random_numbers):
    random_sequence.append(lcg_randu())
random_sequence = [math.ceil(26*number) for number in random_sequence]
#  def collect_words():
#       This function collects the observed words from the sequence of random numbers. It
#       returns a list that contains the observed words.

def collect_words(random_sequence):
    list_of_words = []
    for i in range(0,len(random_sequence)):
        list_of_words.append((random_sequence[i-2],random_sequence[i-1], random_sequence[i]))
    return list_of_words


# def initiate_list_of_possible_words():
#       This function initiates a list of all possible words and returns that list.

def initiate_list_of_possible_words():
    alphabet = [x for x in range(1,27)]
    list_of_possible_words = [letter for letter in product(alphabet, repeat=3)]
    return list_of_possible_words

# def count_words(observed_words):
#       This function checks how many times each possible word combination occurred. It returns
#       a list with the number of occurrences for each word.

def count_words(observed_words):
    list_of_possible_words = initiate_list_of_possible_words()
    word_count = []
    for word in list_of_possible_words:
        word_count.append(observed_words.count(word))
    return word_count

word_combinations = collect_words(random_sequence) #Collect the observed words from the sequence
word_count = count_words(word_combinations) #Collect how many times each possible word has occurred.

print(chisquare(word_count, multiple)) #Compute and print the Chisquare statistic and the corresponding p-value.

不明白缺少什么。感谢

2 个答案:

答案 0 :(得分:0)

您似乎正在添加实例字段/成员。使用作业:

class App extends React.Component {

  onSetSidebarOpen = function(open) {
    this.setState({sidebarOpen: open});
  };

  ...

}

如果以这种方式初始化,您也可以在构造函数中跳过绑定“this”。

答案 1 :(得分:0)

另一种变体:

class App extends React.Component {

    onSetSidebarOpen(open) { this.setState({sidebarOpen: open}) }

    ...

}