2

I'm writing a node tool and I bundle it with webpack before publishing to npm (to have a fast execution with npx)

I don't need any loader / babel setup since it's pure JavaScript which is running fine under my current node 9. It used to work. The webpack config is trivial (13 lines).

However, when bundling, webpack latest (^3.10.0) complains on the spread syntax I started to use: (JS stage 3 at this date, but accepted by node 8.1+ and node 9)

Module parse failed: Unexpected token (47:2)
You may need an appropriate loader to handle this file type.
|
|   return {
|       ...SEC,
|       listenToUncaughtErrors,
|       listenToUnhandledRejections,

So why? The target (node) is accepting this syntax, and isn't webpack supposed to just bundle my code?

Is webpack parsing my code with an embedded JS interpreter? It seems so, but where can I find the specifications of this interpreter and see what features are supported?

I couldn't find anything in the doc. Similar questions here are not in a "no babel" configuration.

So why is webpack complaining here?

[edit] config and full code here and pasted here for your convenience:

module.exports = {
    target: 'node',
    entry: {
        main: './src/index.js'
    },
    output: {
        path: path.join(__dirname, '../dist'),
        filename: 'bundled.js',
    },
    externals: {
        conf: 'commonjs conf',
    },
}
Offirmo
  • 18,962
  • 12
  • 76
  • 97
  • 1
    (`...` is not an operator, it's just syntax.) – T.J. Crowder Jan 14 '18 at 10:27
  • @T.J.Crowder It's linked in the question, but I'll make it more visible. – Offirmo Jan 14 '18 at 10:30
  • 2
    Good deal. Remember, the full content of the question must be **in** the question, not just linked. Links rot, making the question and its answers useless to people in the future, and people shouldn't have to go off-site to help you. – T.J. Crowder Jan 14 '18 at 10:35

1 Answers1

2

(A colleague of mine pointed me to the answer)

YES, webpack is parsing the JavaScript code. It is internally using acorn "A tiny, fast JavaScript parser, written completely in JavaScript.". At the time of this question, webpack is calling acorn with the language version 2017

and YES, acorn is restricting the available JavaScript features. According to the doc: "Only 'stage 4' (finalized) ECMAScript features are being implemented by Acorn."

Spread syntax being a stage 3 feature (at the time of this question), webpack/acorn can't parse it, leading to the error:

Module parse failed: Unexpected token.

You may need an appropriate loader to handle this file type.

So YES, webpack is parsing the code and using stage <= 3 features requires a webpack loader.

Offirmo
  • 18,962
  • 12
  • 76
  • 97