汇总错误:node_modules/react-is/index.js 未导出“isValidElementType”

2024-02-07

我正在使用样式组件构建一个带有 rollUp 的捆绑包。

我的 rollup.config.js 如下所示:

import resolve from 'rollup-plugin-node-resolve'
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
export default {
  input: 'src/index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'cjs'
  },
  external: [
    'react',
    'react-proptypes'
  ],
  plugins: [
    resolve({
      extensions: [ '.js', '.json', '.jsx' ]
    }),
    commonjs({
      include: 'node_modules/**'
    }),
    babel({
      exclude: 'node_modules/**'
    })
  ]
}

我收到了

[!] Error: 'isValidElementType' 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/styled-components/dist/styled-components.es.js (7:9)
5: import stream from 'stream';
6: import PropTypes from 'prop-types';
7: import { isValidElementType } from 'react-is';
            ^
8: import hoistStatics from 'hoist-non-react-statics';

检查node_modules本身react-is是一个commonjs模块,因为它可以被签出here https://github.com/facebook/react/blob/master/packages/react-is/npm/index.js以及。

难道 commonjs 插件不应该处理它吗,因为它位于 node_modules/** 内部?

Thanks.


我通过使用解决了这个问题rollup-plugin-commonjs

并在汇总配置中手动定义导出,如下所示

export default {
  input: 'src/index.js',
  output: [
    {
      file: pkg.main,
      format: 'cjs',
      sourcemap: true
    },
    {
      file: pkg.module,
      format: 'es',
      sourcemap: true
    }
  ],
  plugins: [
    external(),
    postcss({
      modules: true
    }),
    url(),
    babel({
      exclude: 'node_modules/**'
    }),
    resolve(),
    commonjs({
      include: 'node_modules/**',
      namedExports: {
        'node_modules/react-is/index.js': ['isValidElementType']
      }
    })
  ]
}

此后一切正常。

对于信息,我的初始设置是通过完成的https://github.com/transitive-bullshit/react-modern-library-boilerplate https://github.com/transitive-bullshit/react-modern-library-boilerplate

希望对你有帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

汇总错误:node_modules/react-is/index.js 未导出“isValidElementType” 的相关文章

随机推荐