exec4life
exec4life

Reputation: 91

ReactJS: webpack: Failed to compile with spread syntax

I want to show you my problem.

My env:

Node: 8.9.1
npm: 5.6.0
yarn: 1.3.2

package.json

  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "copy-webpack-plugin": "^4.1.1",
...
    "webpack": "^3.7.1",
    "webpack-dev-server": "^2.9.2"
  },

webpack.config.js

...
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true,
              presets: ['react', 'env']
            }
          }
        },
...

It always compile failed like this forcus on {...}

ERROR in ./src/flux/components/BaseComponent.js Module build failed: SyntaxError: D:/APPs/src/flux/components/BaseComponent.js: Unexpected token (36:29)

 34 |     let {search} = this.state;
  35 |     search.includeActive = !search.includeActive;
> 36 |     this.setState({category : {...this.state.category, name : name}});

     |

However, It work well without webpack. Please, help me to solve this problem! Thank you so much to all of you! :)

Upvotes: 1

Views: 1153

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281656

In order to use spread syntax, you need to configure you babel loader, You have a couple of option

First: Use plugin "transform-object-rest-spread"

Install it using

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

and use it

   options: {
       cacheDirectory: true,
       presets: ['react', 'env']
       plugins: ['transform-object-rest-spread']
   }

Second: use stage-0 preset, Install it using

npm install --save-dev babel-preset-stage-0

and use it like

options: {
   cacheDirectory: true,
   presets: ['react', 'env', 'stage-0']
}

Check this babel-documentation

Upvotes: 2

Related Questions