用TSUP/ESBUILD构建的软件包可以在React-Scripts应用程序中查找“ React”

发布于 2025-01-23 02:25:16 字数 1371 浏览 4 评论 0原文

我有一个使用eSbuild构建的NPM软件包,该软件包在react> react-scripts应用程序中使用。我在软件包上运行一个构建,并在React-Scripts应用程序中的/node_modules手动复制和粘贴构建文件夹,这是由于volta和npm-link的问题。

当我运行应用程序时,我会在NPM软件包代码上下文中获得错误:4 Uncluck typeError:Undefined不是函数它找不到从React导入的createContext

import { createContext } from 'react';

const EventContext = createContext();

这是我的npm软件包的tsup.config.js文件:

import { sassPlugin } from '@es-pack/esbuild-sass-plugin';
import { defineConfig } from 'tsup';

export default defineConfig({
  entry: ['src/index.js'],
  splitting: true,
  sourcemap: true,
  clean: true,
  dts: true,
  format: ['esm'],
  target: 'es2020',
  bundle: true,
  esbuildPlugins: [sassPlugin()],
});

这些是我在NPM软件包中的依赖关系和同行依赖性,

"peerDependencies": {
    "classnames": ">=2.0.0 <3.0.0",
    "prop-types": ">=15.0.0 <16.0.0",
    "react": ">=17.0 <18.0.0",
    "react-dom": ">=17.0 <18.0.0"
  },
  "dependencies": {
    "@xstate/react": "^2.0.1",
    "focus-trap": "^6.6.1",
    "react-share": "^4.4.0",
    "xstate": "^4.30.6"
  },

是否存在反式订阅构建设置与ESBUILD软件包输出之间的问题?我的应用程序具有 。为什么我的NPM软件包代码导入无法反应?

I have an npm package that I built using esBuild that I am using in a react-scripts application. I'm running a build on the package and manually copying and pasting the build folder to /node_modules in the react-scripts app due to an issue with volta and npm-link.

When I run my application I get an error in my npm package code Context.js:4 Uncaught TypeError: undefined is not a function it's not finding createContext that's imported from react.

import { createContext } from 'react';

const EventContext = createContext();

Here is my tsup.config.js file for the npm package:

import { sassPlugin } from '@es-pack/esbuild-sass-plugin';
import { defineConfig } from 'tsup';

export default defineConfig({
  entry: ['src/index.js'],
  splitting: true,
  sourcemap: true,
  clean: true,
  dts: true,
  format: ['esm'],
  target: 'es2020',
  bundle: true,
  esbuildPlugins: [sassPlugin()],
});

And these are my dependencies and peerDependencies in the npm package

"peerDependencies": {
    "classnames": ">=2.0.0 <3.0.0",
    "prop-types": ">=15.0.0 <16.0.0",
    "react": ">=17.0 <18.0.0",
    "react-dom": ">=17.0 <18.0.0"
  },
  "dependencies": {
    "@xstate/react": "^2.0.1",
    "focus-trap": "^6.6.1",
    "react-share": "^4.4.0",
    "xstate": "^4.30.6"
  },

Is there an issue between the react-scripts build setup and the esBuild package output? My application has [email protected] installed and works fine without my npm package installed. Why can't my npm package code import react?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2025-01-30 02:25:16

您需要将从“ react”;导入到您正在使用的文件的顶部。向所有人。

You need to add import React from 'react'; to the top of the files you are using react . To all of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文