代码之家  ›  专栏  ›  技术社区  ›  Rasim Avcı

由于javascript扩展运算符,未生成网页包

  •  0
  • Rasim Avcı  · 技术社区  · 7 年前

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

    这是我的网页包配置文件

     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=需要('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 }
    }
    

    然而,这个网页可以工作,但我需要使用前一个

    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'
      }
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Nick Prozee Achraf C.    7 年前

    应将“spread”操作符更改为“Object”。分配()

    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) } })
    
        2
  •  0
  •   str    7 年前

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

    要使用它,您需要添加相应的 Babel plugin .