最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - webpack 4 react unexpected token ...(spread operator) - Stack Overflow

programmeradmin0浏览0评论

Recently i have implemented Webpack 4 setup for my react app.

My webpack.config.js looks like this

const HtmlWebPackPlugin = require('html-webpack-plugin');

const htmlWebpackPlugin = new HtmlWebPackPlugin({
  template: './src/index.js',
  filename: './index.html',
});

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
            options: {
              modules: true,
              importLoaders: 1,
              localIdentName: '[name]_[local]_[hash:base64]',
              sourceMap: true,
              minimize: true,
            },
          },
        ],
      },
    ],
  },
  plugins: [htmlWebpackPlugin],
};

Here is my package.json scripts

"scripts": {
    "dev": "webpack-dev-server --mode development --open",
    "prod": "webpack --mode production"
}

Here the problem is when i use ...(spread operator) it's throwing an error i believe this is something related to babel which is not properly transpiling. Any suggestions would be appreciated. Thanks.

It throws an error something like the one below.

 ERROR in ./src/index.js
    Module build failed: SyntaxError: D:/cp/src/index.js: Unexpected token (31:6)

      29 |   return {
      30 |     headers: {
    > 31 |       ...headers,
         |       ^
      32 |       authorization: token ? `Bearer ${token}` : null,
      33 |     },
      34 |   };

Recently i have implemented Webpack 4 setup for my react app.

My webpack.config.js looks like this

const HtmlWebPackPlugin = require('html-webpack-plugin');

const htmlWebpackPlugin = new HtmlWebPackPlugin({
  template: './src/index.js',
  filename: './index.html',
});

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
            options: {
              modules: true,
              importLoaders: 1,
              localIdentName: '[name]_[local]_[hash:base64]',
              sourceMap: true,
              minimize: true,
            },
          },
        ],
      },
    ],
  },
  plugins: [htmlWebpackPlugin],
};

Here is my package.json scripts

"scripts": {
    "dev": "webpack-dev-server --mode development --open",
    "prod": "webpack --mode production"
}

Here the problem is when i use ...(spread operator) it's throwing an error i believe this is something related to babel which is not properly transpiling. Any suggestions would be appreciated. Thanks.

It throws an error something like the one below.

 ERROR in ./src/index.js
    Module build failed: SyntaxError: D:/cp/src/index.js: Unexpected token (31:6)

      29 |   return {
      30 |     headers: {
    > 31 |       ...headers,
         |       ^
      32 |       authorization: token ? `Bearer ${token}` : null,
      33 |     },
      34 |   };
Share Improve this question asked May 14, 2018 at 9:56 Murali NMurali N 3,4986 gold badges30 silver badges38 bronze badges 4
  • 1 ... isn't (and can't be) an operator. – T.J. Crowder Commented May 14, 2018 at 9:59
  • @kiarashws: The error is reported in a .js file, how will that help? – T.J. Crowder Commented May 14, 2018 at 9:59
  • whats in your .babelrc? – WayneC Commented May 14, 2018 at 10:00
  • @wgcrouch { "presets": ["env", "react"] } – Murali N Commented May 14, 2018 at 10:03
Add a comment  | 

2 Answers 2

Reset to default 16

Just install babel-plugin-transform-object-rest-spread module. https://www.npmjs.com/package/babel-plugin-transform-object-rest-spread

Then add it to .babelrc:

"plugins": [
    "babel-plugin-transform-object-rest-spread",
  ],
   "babel-plugin-transform-object-rest-spread"

does not even have a github page. if you use this, you will see too much output on the console and there is no option to silent it or use "verbose". I suggest you to use
@babel/plugin-transform-spread. then add this to the .babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": [
    "@babel/plugin-transform-spread"
  ]
}
发布评论

评论列表(0)

  1. 暂无评论