对象扩展运算符在节点8.6的webpack上检测为错误

cl0*_*k3r 5 javascript node.js webpack babeljs

我正在尝试将我的应用程序的节点版本从node6升级到node8.6,它本身支持spread运算符,一切正常,直到我尝试使用webpack编译我的节点脚本.

我已经创建了一个测试脚本(测试async/await的本机支持,适用于此场合):

const fs = require('fs')
const {promisify} = require('util')

const app = async () => {
  try {
    const todosString = await promisify(fs.readFile)('todos.txt', {
      encoding: 'utf8',
    })
    return todosString
      .split('\n')
      .filter(Boolean)
      .reduce((acc, val) => ({...acc, [val]: true}), {})
  } catch (e) {
    console.error('wooot', e)
  }
}

app().then(console.log)
Run Code Online (Sandbox Code Playgroud)

这是webpack配置:

const path = require('path')
const nodeExternals = require('webpack-node-externals')

module.exports = {
  entry: './index.js',
  output: {filename: '[name].js'},
  target: 'node',
  externals: [nodeExternals()],
  node: {
    __dirname: true,
    __filename: true,
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        options: {
          presets: [
            [
              'env',
              {
                targets: {
                  node: 'current',
                  modules: false,
                },
              },
            ],
          ],
        },
      },
    ],
  },
}
Run Code Online (Sandbox Code Playgroud)

这是我的webpack构建输出: 在此输入图像描述

对象传播给出了一个错误,默认情况下会转换async/await,甚至target: 'node'在webpack配置中设置...

更新这是package.json: 在此输入图像描述

小智 0

npm install babel-plugin-transform-object-rest-spread --save

如果 webpack.config 文件中有规则数组,则在第一个对象中包含以下查询

query: {
      plugins:[ 'transform-object-rest-spread' ]
    }
Run Code Online (Sandbox Code Playgroud)