首页 > 解决方案 > ReactJS 库在更改 rollup.config.js 的 commonjs 函数时在导出它的项目中给出“ReferenceError: __extends is not defined”

问题描述

我正在做一个项目,我们还有一个库项目,其中包含项目中使用的所有通用组件。在这个库中,我必须添加一个使用materialUI. 当我试图导出该组件时,我收到了错误:

[!] Error: 'ForwardRef' is not exported by node_modules\react-is\index.js
https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module
node_modules\@material-ui\utils\esm\getDisplayName.js (2:9)
1: import _typeof from "@babel/runtime/helpers/esm/typeof";
2: import { ForwardRef, Memo } from 'react-is'; // Simplified polyfill for IE 11 support

现在我可以通过改变 rollup.config.js 中的 commonjs 函数来解决这个问题,如下所示:

commonjs({
      include: "node_modules/**",
      namedExports: {
        "node_modules/react-is/index.js": ["ForwardRef", "Memo"],
      },
    }),

这反过来又造成了另一个错误。当我从一开始就直接在主项目上重新安装库时,我得到了错误。

   Uncaught ReferenceError: _extends is not defined
at exactProp (index.es.js:14590)
at Module.<anonymous> (index.es.js:18939)
at Module../node_modules/bt-react-lib/dist/index.es.js (index.es.js:25529)
at __webpack_require__ (bootstrap:782)
at fn (bootstrap:150)
at Module../src/components/main/Main.js (Dashboard.js:52)
at __webpack_require__ (bootstrap:782)
at fn (bootstrap:150)
at Module../src/App.js (log$:24)
at __webpack_require__ (bootstrap:782)
at fn (bootstrap:150)
at Module../src/index.js (index.css?02e3:45)
at __webpack_require__ (bootstrap:782)
at fn (bootstrap:150)
at Object.1 (objects.js:1)
at __webpack_require__ (bootstrap:782)
at checkDeferredModules (bootstrap:45)
at Array.webpackJsonpCallback [as push] (bootstrap:32)
at main.chunk.js:1

我想再次与materialUI有关。有人以前有过这个吗?

标签: javascriptreactjsmaterial-ui

解决方案


您可能应该将材料视为对等依赖项,以及 react 和 react dom。这是在同一问题上帮助我的样板示例。

首先修改package.json并移动材料,"peerDependencies": {... } 然后您必须安装rollup-plugin-peer-deps-external并将其添加到您的汇总文件中。现在您可以撤消命名的导出,您应该已经完成​​了。


推荐阅读