T
T
toly192017-06-14 20:55:36
JavaScript
toly19, 2017-06-14 20:55:36

Why don't webpack2 and babel build code with the unwrap operator "..."?

Main file code ./src/index.js:

let a = { s: 4 }
let b = { ...a };
console.log(b);

Here is the webpack.config.js file:
const path = require("path");
module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.join(__dirname, "dist"),
    filename: "bundle.js"
  },
  module: {
    rules: [{
      test: /\.(js|jsx)$/,
      exclude: /node_modules/, 
      loader: "babel-loader"
    }]
  }
}

Here is package.json:
{
    "name": "webpackreacttest",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "start": "webpack"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "babel-core": "^6.25.0",
        "babel-loader": "^7.0.0",
        "babel-preset-env": "^1.5.2",
        "babel-preset-es2015": "^6.24.1",
        "webpack": "^2.6.1"
    }
}

and .babelrc:
{
  "presets": ["es2015"]
}

if you try to build this webpack project via >> npm start, then an error occurs:
D:\projects\nodejs\webpackreacttest>npm start
> [email protected] start D:\projects\nodejs\webpackreacttest
> webpack

Hash: ea52a0a36c328ecb7082
Version: webpack 2.6.1
Time: 612ms
    Asset     Size  Chunks             Chunk Names
bundle.js  3.19 kB       0  [emitted]  main
   [0] ./src/index.js 558 bytes {0} [built] [failed] [1 error]

ERROR in ./src/index.js
Module build failed: SyntaxError: D:/projects/nodejs/webpackreacttest/src/index.js: Unexpected token (2:10)

  1 | let a = { s: 4 }
> 2 | let b = { ...a };
    |           ^
  3 | console.log(b);

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] start: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\toly1\AppData\Roaming\npm-cache\_logs\2017-06-14T17_53_46_285Z-debug.log

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lemme, 2017-06-14
@toly19

Object Spread is not included in ES2015
babeljs.io/docs/plugins/preset-stage-0
babeljs.io/docs/plugins/preset-stage-1
babeljs.io/docs/plugins/preset-stage-2
babeljs.io/docs /plugins/preset-stage-3 -- already included here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question