webpack-tsx文件宽度babel-loader错误

问题描述 投票:0回答:1

当我使用babel-loader编写我的react项目时,告诉我解析失败。这是我的webpack.config.js文件。

module: {
            rules: [
                {
                    test: /^\.([jt])sx$/,
                    include: path.resolve(__dirname, 'src'),
                    use: [
                        {
                            loader: "babel-loder",
                            options: {
                               "presets": [
                                  ["@babel/preset-env"],
                                  "@babel/preset-react",
                                  ["@babel/preset-typescript"]
                              ]
                            }
                        }
                    ]
                }
            ]
        }

当我运行webpack cli时,出现如下错误:

Module parse failed: Unexpected token (3:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to 
process this file. See https://webpack.js.org/concepts#loaders
| import React, {PureComponent} from 'react';
> export interface ILazyImageProps extends React.DetailsHTMLAttributes<HTMLImageElement>

看起来像“导出接口”会引发错误。

但是使用babel cli,没关系。谁能告诉我为什么?

typescript webpack babel-loader
1个回答
0
投票

此配置应为您提供帮助:

webpack.config.js

module.exports = {
    // Your configuration of entry and output
    module: {
        rules: [
            {
                test: /\.(j|t)sx?$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: "babel-loader",
                        options: {
                            presets: [
                                "@babel/env",
                                "@babel/react",
                                "@babel/typescript"
                            ]
                        }
                    }
                ]
            }
        ]
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.