首页 > 解决方案 > webpack 4对意外令牌做出反应......(传播运算符)

问题描述

最近我已经Webpack 4为我的应用程序实现了设置react

我的webpack.config.js长相是这样的

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],
};

这是我的package.json脚本

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

这里的问题是,当我使用...(扩展运算符)时,它会抛出一个error我相信这是与babel未正确转译有关的东西。任何建议,将不胜感激。谢谢。

它抛出一个error类似下面的东西。

 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 |   };

标签: javascriptreactjswebpackbabeljs

解决方案


只需安装babel-plugin-transform-object-rest-spread模块。 https://www.npmjs.com/package/babel-plugin-transform-object-rest-spread

然后将其添加到 .babelrc:

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

推荐阅读