Search code examples
reactjstypescriptwebpackbabeljsbabel-loader

webpack/typescript/react - Err: Unexpected token React.Component


I am trying to set up a project with webpack, typescript and react. I am using babel to transpile my typscript/react code. On starting the development-server i get the following err:

Module parse failed: Unexpected token (4:6)
You may need an appropriate loader to handle this file type.
| 
| export class App extends React.Component {
>   arr = [1, 2, 3];
| 
|   render() {

Babel cant handle an array declaration? That seems to me a little bit strange. Also because my react class looks different:

app.tsx

export class App extends React.Component {

    public arr: number[] = [1, 2, 3];

    public render(): React.ReactNode {
       ...
    }
}

export default App;

I for now try to prevent a .babelrc or babel.config.js file and try to solve it by my webpack.config.js:

webpack.config.js

Due to clarification reasons i cut out most of the unnecessary configurations:

module.exports = (env) => {

    const isProduction = env.production === true;
    const isDevelopment = env.development === true;

    return {
        entry: {
            main: srcDir + '/index.tsx'
        },
        ...
        module: {
            rules: [
                {
                    oneOf: [
                       {
                        test: /\.(js|mjs|jsx|ts|tsx)$/,
                        include: srcDir,
                        loader: 'babel-loader',
                        options: {
                            babelrc: false,
                            configFile: false,
                            presets: [
                                ["@babel/preset-react"],
                                ["@babel/preset-typescript"]
                            ],
                            plugins: [
                                [
                                    'babel-plugin-named-asset-import',
                                    {
                                        loaderMap: {
                                            svg: {
                                                ReactComponent: '@svgr/webpack?-svgo,+ref![path]',
                                            },
                                        },
                                    },
                                ],
                            ],
                            cacheDirectory: true,
                            cacheCompression: isProduction,
                            compact: isProduction,
                        },
                    },
                       ...
                    ]
                }
            ]

        },
        ...
    };
};

package.json

The listings of the involved packages looks like this:

 ...
  "devDependencies": {
    "@babel/core": "^7.4.0",
    "@svgr/webpack": "^4.1.0",
    "babel-loader": "^8.0.5",
    "babel-plugin-named-asset-import": "^0.3.1",
    "@babel/preset-react": "^7.0.0",
    "@babel/preset-typescript": "^7.3.3",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0"
  },
  "dependencies": {
    "@types/react": "^16.8.12",
    "@types/react-dom": "^16.8.3",
    "@types/react-router-dom": "^4.3.1",
    "react": "^16.8.6",
    "react-dom": "^16.8.6"
  }

Maybe some of you have a solution for how to prevent this issue with a propper config. :)


Solution

  • Looks like it's not related to typescript or react parsing, I believe you're missing the following Babel plugin: https://babeljs.io/docs/en/babel-plugin-proposal-class-properties

    This plugin allows Babel to parse class properties like your array properly.

    To install it npm install --save-dev @babel/plugin-proposal-class-properties

    and then add it to your babel loader config in the plugins list.

    plugins: [
          "@babel/plugin-proposal-class-properties",
          [
            'babel-plugin-named-asset-import',
            {
              loaderMap: {
                 svg: {
                    ReactComponent: '@svgr/webpack?-svgo,+ref![path]',
                 },
              },
            },
          ],
     ]