我创建了一个带有两个工作区‘Webmail’和‘Component’的Reaction项目.我在‘Component’中放了一个小的TSX文件,我想在‘Webmail’中使用它. 这是Component的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.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'
                        ]
                    }
                }
            }
        ]
    },
}

这是"Mycrient":

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;

发生的事情是,webmail应用程序启动,然后抱怨,

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.

不知何故,我想,巴别塔的react 预设并没有被接受.我已经try 将预设行放在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"
  ]
}

在tsfig.js中,加上两个答案中建议的预设打字脚本行,问题就解决了,至少目前看来是这样.

我不能平分赏金,也不能分两份赏金,所以我会把赏金给第一个响应者,接受最细致的回答.

推荐答案

我看到两个明显的问题,都与您的component工作空间有关.

  1. 在webpack配置中添加@babel/preset-typescript以移除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"
  ]
}

您try 如何构建和运行webmail工作区可能还有其他问题,但如果没有关于它如何配置和如何运行工作区的更多信息,就很难说了.

Typescript相关问答推荐

TS不推断类型在条件判断后具有属性'

使用动态主体初始化变量

如何以Angular 形式显示验证错误消息

TypeScrip 5.3和5.4-对`Readonly<;[...number[],字符串]>;`的测试版处理:有什么变化?

从子类构造函数推断父类泛型类型

Select 下拉菜单的占位符不起作用

在打印脚本中使用泛型扩展抽象类

(TypeScript)TypeError:无法读取未定义的属性(正在读取映射)"

在React中定义props 的不同方式.FunctionalComponent

如何在脚本中输入具有不同数量的泛型类型变量的函数?

ANGLE NumberValueAccessor表单控件值更改订阅两次

TS2739将接口类型变量赋值给具体变量

从类型脚本中元组的尾部获取第n个类型

设置不允许相同类型的多个参数的线性规则

垫子工具栏不起作用的Angular 布线

埃斯林特警告危险使用&as";

如何为对象数组中的每个条目推断不同的泛型

可以';t使用t正确地索引对象;联合;索引类型

将对象数组传递给 Typescript 中的导入函数

从函数参数推断函数返回类型