object spread operator detected as an error on webpack for node 8.6

Viewed 1306

I'm trying to upgrade my application's node version from node6 to node8.6 which supports spread operator natively, all works fine until I try to compile my node script using webpack.

I've created a test script (testing native support for async/await too, for the occasion):

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)

Here is the webpack configuration:

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,
                },
              },
            ],
          ],
        },
      },
    ],
  },
}

And here is my webpack build output: enter image description here

The object spread gives an error and async/await is transpiled by default, even target: 'node' is set on the webpack config...

UPDATE this is the package.json: enter image description here

1 Answers

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

and include the below query in first object if the rules array in webpack.config file

query: {
      plugins:[ 'transform-object-rest-spread' ]
    }
Related