问题
I'm building a bundle with rollUp using styled-components.
My rollup.config.js looks like:
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/**'
})
]
}
And I'm receiving
[!] 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';
Checking on the node_modules itself react-is is a commonjs module as it can be checked out here as well.
Shouldn't the commonjs plugin take care of it since it is inside node_modules/** ?
Thanks.
回答1:
I resolved this by using the rollup-plugin-commonjs
and defining the export manually in the rollup config as below
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']
}
})
]
}
After this everything worked fine.
and for the info, my initial setup was done through https://github.com/transitive-bullshit/react-modern-library-boilerplate
Hope it works for you
回答2:
The fix above didn't work for me. However, adding styled-components
to the rollup.config.js
list of externals and globals worked for me.
export default {
...
external: ['styled-components'],
...
globals: { 'styled-components': 'styled' },
};
I'm using the typescript create-react-library cli, which comes bundled with rollup.
https://github.com/styled-components/styled-components/issues/930
来源:https://stackoverflow.com/questions/50080893/rollup-error-isvalidelementtype-is-not-exported-by-node-modules-react-is-inde