由于 javascript 传播运算符,Webpack 未构建

Webpack is not building because of javascript spread operator

我收到传播运算符的意外令牌错误,我如何在不删除代码的情况下构建捆绑包?

这是我的 webpack 配置文件

 Unexpected token (85:32)

  83 |   console.log(
  84 |     'return value 1 ' +
> 85 |       JSON.stringify({ value: { ...this.value(), ...newState } })
     |                                 ^
  86 |   )
  87 |   return {
  88 |     value: {

var path = require('path')

module.exports = {
  entry: path.resolve(__dirname, 'partner/index.js'),
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/, // Check for all js files
        loader: 'babel-loader',
        query: {
          presets: ['babel-preset-es2015'].map(require.resolve)
        },
        exclude: /node_modules\/(?!other-module)/
      }
    ]
  },
  stats: {
    colors: true
  },
  devtool: 'source-map',
  resolve: { symlinks: false }
}

但是这个 webpack 可以工作,但我需要使用以前的

module.exports = {
  entry: {
    partner: '../workout-example/partner/index.js',
    test: '../workout-example/test/example.spec.js'
  },
  target: 'web',
  mode: 'development',
  node: {
    fs: 'empty'
  },
  output: {
    filename: '[name]_bundle.js'
  }
}

您应该将传播运算符更改为 Object.assign()

来自 mozilla 的例子

const object1 = {
  a: 1,
  b: 2,
  c: 3
};

const object2 = Object.assign({}, object1);

console.log(object2.c);
// expected output: 3

你的JS版本可能不支持最新的es6语法

示例代码

JSON.stringify({ value: { Object.assign({}, this.value(), newState) } })

Object Rest/Spread Properties 还不是 ECMAScript 功能(但很快就会)。

使用需要添加对应的Babel plugin.