Demian
Demian

Reputation: 19

Redux Toolkit is not working with react.tsx

index.js:1 ./node_modules/@reduxjs/toolkit/dist/redux-toolkit.legacy-esm.js 331:12 Module parse failed: Unexpected token (331:12) File was processed with these loaders:

if (cache?.has(value)) return false; | const entries = getEntries != null ? getEntries(value) : Object.entries(value); | const hasIgnoredPaths = ignoredPaths.length > 0;

Someone said to update "@types/react": "18.2.46" but this answer has bad reviews. Please help to solve the problem. Following is my package.json file, in case if there's any compatibility error of anything with redux toolkit

{
  "name": "in",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@reduxjs/toolkit": "^2.0.1",
    "@testing-library/jest-dom": "^5.11.5",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-redux": "^9.1.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.0",
    "redux": "^5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Upvotes: 1

Views: 504

Answers (1)

Paul Crozer
Paul Crozer

Reputation: 68

I've got it working here, by injecting babel-loader into the webpack config:

Install babel loader and plugins/requirements (note: version >=9 didn't work for me with current SPFX 1.18.2)

npm i [email protected] --save-dev
npm i @babel/preset-env --save-dev
npm i @babel/plugin-proposal-optional-chaining --save-dev
npm i core-js@3 --save 

And then in your gulpfile.js:

build.configureWebpack.mergeConfig({
  additionalConfiguration: (generatedConfiguration) => {
    generatedConfiguration.module.rules.push({
      test: /\.js$/,
      include: /@reduxjs/,
      use: [
        {
          loader: 'babel-loader',
          options: {
            presets: [
              ["@babel/preset-env", {"modules": false, "useBuiltIns": "usage", "corejs": 3}],
            ],
            plugins: [
              "@babel/plugin-transform-optional-chaining",
              "@babel/plugin-transform-nullish-coalescing-operator"
            ],
            cacheDirectory: true
          }
        }
      ]
    });

    // console.log({rules: generatedConfiguration.module.rules});

    return generatedConfiguration;
  }
});

Upvotes: 0

Related Questions