简体   繁体   English

Webpack无法读取ReactJS道具

[英]Webpack cant read ReactJS props

I want to run my ReactJS app with webpack command npm run dev but when I run the command, I've got this error: 我想使用webpack命令npm run dev运行我的ReactJS应用,但是当我运行该命令时,出现了以下错误:

ERROR in ./src/js/services/reducers/login/auth.js
Module build failed: SyntaxError: Unexpected token (16:10)

      14 |         access: {
      15 |           token: action.payload.access,
    > 16 |           ...jwtDecode(action.payload.access),
         |           ^
      17 |         },
      18 |         refresh: {
      19 |           token: action.payload.refresh,

The file that produces this error is: 产生此错误的文件是:

import jwtDecode from 'jwt-decode';
import * as auth from '../../actions/login/auth';

const initialState = {
  access: undefined,
  refresh: undefined,
  errors: {},
};

export default (state = initialState, action) => {
  switch (action.type) {
    case auth.LOGIN_SUCCESS:
      return {
        access: {
          token: action.payload.access,
          ...jwtDecode(action.payload.access),
        },
        refresh: {
          token: action.payload.refresh,
          ...jwtDecode(action.payload.refresh),
        },
        errors: {},
      };
    case auth.TOKEN_RECEIVED:
      return {
        ...state,
        access: {
          token: action.payload.access,
          ...jwtDecode(action.payload.access),
        },
      };
    case auth.LOGIN_FAILURE:
    case auth.TOKEN_FAILURE:
      return {
        access: undefined,
        refresh: undefined,
        errors:
            action.payload.response || { non_field_errors: action.payload.statusText },
      };
    default:
      return state;
  }
};

I think that may be it could be the configuration of my webpack. 我认为可能是我的webpack的配置。 Maybe the webpack can not read the prop in React? 也许webpack无法在React中阅读道具? Should I add some rule for this in my webpack? 我应该在我的webpack中添加一些规则吗?

EDIT 编辑

This is my actual webpack 这是我实际的webpack

// We are using node's native package 'path'
// https://nodejs.org/api/path.html
const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

// Constant with our paths
const paths = {
  DIST: path.resolve(__dirname, 'dist'),
  SRC: path.resolve(__dirname, 'src'),
  JS: path.resolve(__dirname, 'src/js'),
  PUBLIC: path.resolve(__dirname, 'public'),
};

// Webpack configuration
module.exports = {
  entry: path.join(paths.JS, 'app.jsx'),
  output: {
    path: paths.DIST,
    filename: 'app.bundle.js',
  },
  // Tell webpack to use html plugin
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(paths.PUBLIC, 'index.html'),
    }),
    new ExtractTextPlugin('style.bundle.css'), // CSS will be extracted to this bundle file
  ],
  // Loaders configuration
  // We are telling webpack to use 'babel-loader' for .js and .jsx files
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader']        
      },
      // CSS loader for CSS files
      // Files will get handled by css loader and then passed to the extract text plugin
      // which will write it to the file we defined above
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          use: 'css-loader',
        }),
      },
      {
        test: /\.scss$/,

        loader: ExtractTextPlugin.extract({
          // Loader chaining works from right to left

          use: ['css-loader', 'sass-loader'],
        }),
      },
      // File loader for image assets -> ADDED IN THIS STEP
      // We'll add only image extensions, but you can things like svgs, fonts and videos
      {
        test: /\.(png|jpg|gif)$/,
        use: ['file-loader'],
      },
    ],
  },
  // Enable importing JS files without specifying their's extenstion
  //
  // So we can write:
  // import MyComponent from './my-component';
  //
  // Instead of:
  // import MyComponent from './my-component.jsx';
  resolve: {
    extensions: ['.js', '.jsx'],
  },
};

EDIT2 编辑2

I have this .babelrc 我有这个.babelrc

{
  "presets": ["react", "es2015"]
}

Thanks 谢谢

You need to add "proposal stage" (which would allow using of spread operator ) to your webpack configuration: 您需要在Webpack配置中添加“建议阶段”(允许使用spread操作符 ):

module: {
  loaders: [
    {
      // ...
      query: {
        presets: ["es2015", "react", "stage-2"]
      }
    }
  ]
}

Also, it should be added as a dependency in package.json : 另外,应将其作为依赖项添加到package.json

npm install --save babel-preset-stage-2

UPD UPD

Instead of webpack config, this may be added to .babelrc file if it is used in the project: 如果项目中使用了它,则可以将其添加到.babelrc文件中,而不是webpack配置:

{
  "presets": ["react", "es2015", "stage-2"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM