【问题标题】:Webpack cant read ReactJS propsWebpack 无法读取 ReactJS 道具
【发布时间】:2023-03-26 12:08:01
【问题描述】:

我想用 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,

产生此错误的文件是:

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

我认为这可能是我的 webpack 的配置。也许 webpack 无法读取 React 中的 prop?我应该在我的 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

我有这个.babelrc

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

谢谢

【问题讨论】:

    标签: javascript reactjs webpack ecmascript-next


    【解决方案1】:

    您需要在 webpack 配置中添加“提案阶段”(允许使用 spread operator):

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

    另外,它应该作为依赖添加到package.json

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

    UPD

    如果在项目中使用,可以将其添加到 .babelrc 文件中,而不是 webpack 配置:

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

    【讨论】:

    • 感谢您的回复。我理解你的回答,但我不知道我应该把这个加载器放在我的 webpack 中的哪个位置(抱歉,我是使用 webpack 和 react 的新手)
    • @Aceconhielo 你使用的是什么版本的 Webpack?
    • 我正在使用 Webpack3
    • 好的,你的项目中有 .babelrc 文件吗?如果是,请显示它的内容。
    • 是的,我有一个 .babelrc。我在这篇文章的edit2中添加了
    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 2021-11-18
    • 2020-09-06
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    相关资源
    最近更新 更多