Webpack 在 React.js 项目中显示“您可能需要一个额外的加载器”

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

我创建了一个包含两个工作区“webmail”和“component”的 React 项目。我在“组件”中放置了一个小 tsx 文件,我想在“网络邮件”中使用它。这是组件的package.json:

{
  "name": "@monorepo/component",
  "version": "0.1.0",
  "description": "Creating a component module project",
  "main": "src/index.tsx",
  "scripts": {
    "dev": "webpack serve",
    "build": "webpack"
  },
  "keywords": [
    "react",
    "component",
    "npm"
  ],
  "author": "Christine",
  "license": "MIT",
  "peerDependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.24.0",
    "@babel/preset-env": "^7.12.11",
    "@babel/preset-react": "^7.18.6",
    "@babel/register": "^7.12.10",
    "babel-loader": "^9.1.3",
    "html-webpack-plugin": "^5.6.0",
    "webpack": "^5.90.3",
    "webpack-cli": "^5.1.4"
  },
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }
}

这是组件中的webpack.config.js

const path = require('path');

module.exports = {
    entry: {
        index: { import: "./src/index.tsx" }
    },
    output: {
        path: path.resolve(__dirname, "dist"),
    },
    mode: "production",
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: [
                            '@babel/preset-env',
                            '@babel/preset-react'
                        ]
                    }
                }
            }
        ]
    },
}

这是“我的组件”:

import React from "react-dom";

interface ComponentProps {
   label: string
}

export const MyComponent=({label, ...props}: ComponentProps)=> {

    return (
        <div>
            <button>{label}</button>
        </div>
    );
}

这是使用 MyComponent 的 tsx 文件:

import './css/App.css';
import React from 'react'
import { MyComponent } from "@monorepo/component"
function App() {

   return (
        <div className="flex flex-col h-screen w-screen">
            <nav className="nav">
                <div className='flex-wrap flex justify-between w-4/6'>
                    <div className='flex-wrap flex '>
              <MyComponent label='my button'/>

                    </div>
                   
                </div>
            </nav>
         </div>
    );
}

export default App;

发生的情况是网络邮件应用程序启动,然后抱怨

ERROR in ../component/src/index.tsx 4:0
Module parse failed: The keyword 'interface' is reserved (4:0)
File was processed with these loaders:
 * ../node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
You may need an additional loader to handle the result of these loaders.
| 
| import React from "react-dom";
> interface ComponentProps {
|    label: string
| }

编辑: 如果我删除组件代码中的“接口”,我得到的错误是

Module parse failed: Unexpected token (9:8)
File was processed with these loaders:
 * ../node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
You may need an additional loader to handle the result of these loaders.
| 
|     return (
>         <div>
|             <button>{label}</button>
|         </div>
ERROR in ../component/src/index.tsx 9:8

如果我将组件中的“index.tsx”更改为“index.js”,我会得到

Add @babel/preset-react (https://github.com/babel/babel/tree/main/packages/babel-preset-react) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-jsx) to the 'plugins' section to enable parsing.

不知何故,我认为 babel 的反应预设没有被选中。我尝试将预设行放入 package.json 或 webpack.config.json 或 babelrc.json 中。我的配置有什么问题吗?

解决方案

我使用 this 作为我的项目的示例。我现在在本地重新创建了该示例,它给了我与 tsx 组件相同的错误。放上这个之后

{
  "compilerOptions": {
    "target": "ES6",
    "module": "ES2015",
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "jsx": "react"
  },
  "include": [
    "src/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

在 tsconfig.js 中,并添加两个答案中建议的预设打字稿行,问题已解决,或者目前看来是这样。

reactjs typescript webpack babeljs
2个回答
1
投票

我看到两个明显的问题,都与您的组件工作区相关。

  1. @babel/preset-typescript
    添加到您的 webpack 配置中以删除 TypeScript 语法。

    npm i @babel/preset-typescript --save-dev
    
    use: {
      loader: "babel-loader",
      options: {
        presets: [
          '@babel/preset-env',
          '@babel/preset-react',
          '@babel/preset-typescript' // <--- add this
        ]
      }
    }
    
  2. 更新 package.json 中的

    main
    字段以指向 webpack 构建的
    output
    位置。

    "main": "dist/index.js"
    

现在请确保在运行 webmail 工作区之前构建 component 工作区。

您可能希望/需要将预设配置放入 babel.config.json 文件中:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-typescript",
    "@babel/preset-react"
  ]
}

您尝试构建和运行 webmail 工作区的方式可能存在其他问题,但如果没有有关如何配置以及如何运行该工作区的更多信息,很难说。


1
投票

我有一个同样的问题,我是这样解决的。

npm install ts-loader typescript --save-dev

webpack 配置:

const path = require('path');

module.exports = {
    entry: {
        index: "./src/index.tsx"
    },
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "[name].js"
    },
    mode: "production",
    resolve: {
        extensions: ['.tsx', '.ts', '.js']
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                exclude: /node_modules/,
                use: 'ts-loader'
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            }
        ]
    },
};

对于 .babelrc 文件:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.