6

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

cl0udw4lk3r
  • 2,663
  • 5
  • 26
  • 46
  • Nit: It's not an operator, it's just syntax. Operators have a single value result. Spread doesn't, and can't. – T.J. Crowder Oct 03 '17 at 15:32
  • 1
    https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Operators/Spread_operator – cl0udw4lk3r Oct 03 '17 at 15:34
  • If anyone else is wondering: Yes, Node v8.6 really does support object spread, not just array spread. I double-checked. – T.J. Crowder Oct 03 '17 at 15:34
  • Remember MDN is community-edited and while normally good-to-excellent it *is* sometimes wrong. That's the case with the Spanish article you linked above. The English version was also wrong for along time before being corrected (and still has "operator" in the URL, sadly). (A couple of the polyfills the site had for a long time were quite off, as well.) If you want the straight dope, go to [the specification](https://tc39.github.io/ecma262/). – T.J. Crowder Oct 03 '17 at 15:36
  • I think at the moment object spread is not available to babel. You might try using Stage 2 presets. There's a SO Q/A here: https://stackoverflow.com/questions/40897154/spread-operator-not-recognized-by-webpack-under-vue – Mark Oct 03 '17 at 15:42
  • Oh dang! (Its Italian BTW) :) – cl0udw4lk3r Oct 03 '17 at 15:42
  • 2
    @Mark_M the goal is to not transpile the object spread operator, as its native to node 8.6 now! – cl0udw4lk3r Oct 03 '17 at 15:48
  • 1
    @cl0udw4lk3r: OMG I can't believe I said Spanish. Parlo *meglio* italiano che parlo spanolo! Mi scusa, mi guardavo troppo in fretta! – T.J. Crowder Oct 03 '17 at 16:00
  • 1
    @T.J.Crowder I had to translate that just to see how badly you had to govel for getting the language wrong :D – Jamiec Apr 20 '18 at 08:36

1 Answers1

0

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' ]
    }
  • This will not solve my issue: node 8.6 supports object spread natively and I wont use babel to transpile it anymore! – cl0udw4lk3r Apr 23 '18 at 08:59