Fab*_*sta 18 rollup reactjs styled-components
我正在使用样式组件构建一个包含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/**'
})
]
}
Run Code Online (Sandbox Code Playgroud)
我收到了
[!] 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';
Run Code Online (Sandbox Code Playgroud)
检查node_modules本身react-is是一个commonjs模块,因为它也可以在这里检查.
commonjs插件不应该处理它,因为它在node_modules/**内吗?
谢谢.
Mar*_*aud 25
我通过使用来解决这个问题 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']
}
})
]
}
Run Code Online (Sandbox Code Playgroud)
在此之后一切正常.
对于信息,我的初始设置是通过https://github.com/transitive-bullshit/react-modern-library-boilerplate完成的
希望这对你有用
Fro*_*Dog 13
上面的修复对我不起作用。但是,添加styled-components到rollup.config.js外部和全局变量列表对我有用。
export default {
...
external: ['styled-components'],
...
globals: { 'styled-components': 'styled' },
};
Run Code Online (Sandbox Code Playgroud)
我使用的是打字稿create-react-library cli,它与汇总捆绑在一起。
https://github.com/styled-components/styled-components/issues/930
这是 2022 年的更新答案。
我使用 vitejs 遇到了这个错误,它使用 rollup 进行生产构建/捆绑。
我已经包含了针对 rollup 和 vite 项目的解决方案。
关于旧答案和文档的说明
在互联网上的文档和论坛中,仍然有很多旧评论提到rollup-plugin-commonjs和namedExports。
rollup-plugin-commonjs是v10.1.0在2019-08-27rollup-plugin-commonjs于2019-12-21更名为@rollup/plugin-commonjswithv11.0.0namedExports@rollup/plugin-commonjs@v13.0.0已于 2020-06-05删除因此,任何引用rollup-plugin-commonjsor 的namedExports内容都已过时rollup且与or的当前版本无关@rollup/plugin-commonjs。
许多 React 库都会发生此错误,因为它们使用动态require(...)语句在开发源文件和生产源文件之间进行选择。
我在这个例子中使用的是,但它在和react中是相同的想法。react-domreact-is
// node_modules/react/index.js
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.min.js');
} else {
module.exports = require('./cjs/react.development.js');
}
Run Code Online (Sandbox Code Playgroud)
当 rollup 处理此文件时,它无法确定导出来自另一个文件,因此 rollup 认为导出为空,这就是为什么我们会收到类似'useState' is not exported by 'node_modules/react/index.js'.
选项 1:将库设为外部库
如果您根本不想将依赖项包含在包中,请使用此选项。
在这种情况下,将这些库标记为external,这意味着它们不会被汇总处理或包含在捆绑包中。这还可以减少捆绑包的大小,并避免意外多次捆绑这些库。
任何标记为外部的库都需要在运行时环境中可用(即它们在运行时在页面上可用,或者您有另一个构建步骤可以解析外部并将它们提供给您的库)。
选项 2:使用别名手动解析文件
如果您的依赖项应该包含在您的包中,请使用此选项 - 即您希望将 React 包含在您的库包中。
$ npm install @rollup/plugin-alias --save-dev
Run Code Online (Sandbox Code Playgroud)
rollup.config.js.// node_modules/react/index.js
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.min.js');
} else {
module.exports = require('./cjs/react.development.js');
}
Run Code Online (Sandbox Code Playgroud)
笔记:
NODE_ENV环境变量来控制生产/开发构建umd反应文件起了别名;如果您需要指向构建,cjs则根据需要调整别名路径这是 vitejs 项目的等效配置:
// vite.config.ts
import * as _path from "path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig((env) => {
// mode: 'production' | 'development'
const mode = env.mode;
/**
* Fix build messages/errors like: `'useState' is not exported by 'node_modules/react/index.js'`
*/
let pathReact = "umd/react.production.min.js";
let pathReactDom = "umd/react-dom.production.min.js";
let pathReactJsx = "cjs/react-jsx-runtime.production.min.js";
if (mode === "development") {
pathReact = "umd/react.development.js";
pathReactDom = "umd/react-dom.development.js";
pathReactJsx = "cjs/react-jsx-dev-runtime.development.js";
}
return {
resolve: {
alias: [
// react alias fix
{
find: /^react$/,
replacement: require.resolve(_path.join("react", pathReact)),
},
{
find: /^react-dom$/,
replacement: require.resolve(_path.join("react-dom", pathReactDom)),
},
{
/*
2022-03-11
https://github.com/vitejs/vite/issues/6215#issuecomment-1064247764
*/
find: /react\/jsx-runtime/,
replacement: require.resolve(_path.join("react", pathReactJsx)),
},
// End react alias fix
],
},
plugins: [react()],
};
});
Run Code Online (Sandbox Code Playgroud)
FrostyDog 发布的解决方案对我有用,所以+1,但是......
当我尝试从 导入钩子时,我立即遇到了同样的错误react,我可以按照上面的解决方案进行操作
export default {
...
external: ['styled-components', 'react'],
...
};
Run Code Online (Sandbox Code Playgroud)
但rollup.config.js我想要一个可以为所有库处理此问题的解决方案,因此这在将来不会重复出现。
所以就这样做了......
import external from "rollup-plugin-peer-deps-external";
...
export default {
...
plugins: [
external({
includeDependencies: true
}),
...
]
};
Run Code Online (Sandbox Code Playgroud)
为我和之后添加到项目中的每个人解决了这个问题。
| 归档时间: |
|
| 查看次数: |
5144 次 |
| 最近记录: |