webpack/typescript/react - 错误:意外的令牌 React.Component

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

我正在尝试使用 webpack、typescript 和 react 建立一个项目。我正在使用 babel 来转换我的 typscript/react 代码。在启动开发服务器时,出现以下错误:

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 无法处理数组声明?这在我看来有点奇怪。也因为我的反应 class 看起来不一样:

app.tsx

export class App extends React.Component {

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

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

export default App;

我现在尝试阻止 .babelrcbabel.config.js 文件并尝试通过我的 webpack.config.js:

解决它

webpack.config.js

由于说明原因,我删除了大部分不必要的配置:

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

相关软件包的列表如下所示:

 ...
  "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"
  }

也许你们中的一些人有一个解决方案,可以通过适当的配置来防止这个问题。 :)

看起来它与 typescript 或 react 解析无关,我相信你缺少以下 Babel 插件:https://babeljs.io/docs/en/babel-plugin-proposal-class-properties

此插件允许 Babel 正确解析 class 属性,例如您的数组。

安装 npm install --save-dev @babel/plugin-proposal-class-properties

然后将其添加到插件列表中的 babel 加载器配置中。

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