我的目标是为使用 webpack 在 ES6 中制作的反应组件库创建一个模块。
我在用:
- 网络包:“2.1.0-beta.25”
- 反应:“15.4.1”
我需要分配react
和react-dom
作为 peerDependencies,这样它就不会被开发人员下载。
这是我的相关部分package.json
:
"peerDependencies": {
"react": "15.4.1"
},
"dependencies": {
"chalk": "1.1.3",
"compression": "1.6.2",
"cross-env": "3.1.3",
"immutable": "3.8.1",
"invariant": "2.2.1",
"lodash": "4.16.4",
"minimist": "1.2.0",
"sanitize.css": "4.1.0",
"warning": "3.0.0",
"react-onclickoutside": "5.8.3",
"styled-components": "1.1.2",
"classnames": "2.2.5"
},
"devDependencies": {
"react": "15.4.1",
"react-dom": "15.4.1",
根据https://webpack.github.io/docs/configuration.html#externals和https://webpack.github.io/docs/library-and-externals.html,
我尝试了以下配置:
1.
externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
},
2.外部:{反应:'react','react-dom':'react-dom',}
3.
externals: {
react: 'React',
'react-dom' : 'ReactDOM',
}
4.
externals: {
react: 'umd react',
'react-dom' : 'umd react-dom'
}
5.
externals: {
// Use external version of React
"react": "React",
"react-dom": "ReactDOM"
},
我总是有这个错误:
Dynamic page loading failed Error: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded (details: https://bf.me/react-refs-must-have-owner).(…)
所以我想知道,文档对此不是很清楚。
2017 年 12 月,最多 2018 年 1 月,我怎样才能排除React
和ReactDOM
被捆绑?