使对象传播以使用 webpack.config 和 babelrc 文件

R.T*_*son 3 reactjs webpack babeljs ecmascript-next

我试图让对象传播运算符与我的反应项目一起工作。它出现了一个语法错误:

传播算子错误

我已经尝试babel-plugin-transform-object-rest-spread按照 babel docs 的描述使用。

经过一番研究,我还尝试了babel 的GitHub 问题中描述的配置: 扩展运算符的 babelrc 配置

请参阅下面的我的.babelrc文件:

{
    "presets": ["es2015"],
    "plugins": ["transform-object-rest-spread"],
    "sourceMaps": true,
    "retainLines": true
}
Run Code Online (Sandbox Code Playgroud)

我的webpack.config文件如下:

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'public', 'js'),
    publicPath: '/'
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        include: path.resolve(__dirname, 'src'),
        loader: 'babel-loader',
        options: {
          presets: ['react', 'es2015']
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test:/\.scss$/,
        use: [{
                loader: 'style-loader'
            }, {
                loader: 'css-loader'
                }, {
                loader: 'sass-loader', options: {
                    sourceMap: true
                }
            }]
      },
      {
        test: /.(png|jpg|jpeg|gif|svg|woff|woff2|eot|ttf)(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url-loader'
      }
    ]
  },
  devServer: {
   historyApiFallback: true,
   contentBase: path.join(__dirname, 'public'),
   publicPath: '/js/',
   port: 3000
 }
};
Run Code Online (Sandbox Code Playgroud)

我使用扩展运算符的代码是:

import * as types from '../types/types'; 

const initialState ={
    mesage:''
}

export default function doodlesReducer (state = initialState, action) {
    switch(action.type) {
        case 'SET_MESSAGE' :
        return {…state, message: action.payload.message}
        default :
        return state
        }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我尝试找出使用对象扩展运算符的正确配置吗?

lin*_*new 5

您在.babelrc和中都列出了您的预设webpack.config,请尝试将其全部移至其中之一,即仅.babelrc

{
    "presets": ["es2015", "react"],
    "plugins": ["transform-object-rest-spread"]
}
Run Code Online (Sandbox Code Playgroud)

编辑:也不要使用es2015现在已弃用的env预设npm install --save-dev babel-preset-env,而是安装预设并.babelrc替换es2015env.

编辑2:您使用用途的一些不支持的字符编码,你必须有一份从某处这搞砸编码,代之以粘贴它...