我在做一个自娱自乐的 React library,通过 webpack 打包。在 webpack.config 中配置不打包 react、react-dom。
var webpackConfig = {
entry: './components',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'react-table.js',
library: 'ReactReport',
libraryTarget: 'umd'
},
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
}
]
},
externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
},
在 webpack 中,暴露 react-table 中的 ReactReport 对象到全局。 然后在 html 中这么写,
<body>
<div id="root">
</div>
<script src="../dist/react-table.js"></script>
<script src="https://cdn.bootcss.com/react/15.6.1/react.min.js"></script>
<script src="https://cdn.bootcss.com/react/15.6.1/react-dom.min.js"></script>
<script>
var ReactReport = ReactReport.ReactReport;
var serverData = {someData};
var App = React.createElement(
ReactReport,
{ data: serverData }
);
ReactDOM.render(App, document.getElementById('root'));
</script>
</body>
在 webpack 中配置不打包 react 时运行会报错,找不到 React.Component,
Cannot read property 'Component' of undefined
我在 html 中不是写了 cdn 的 React 了吗?有朋友知道为什么么? google 一下午了。。
完全前端新手,webpack 基本是混沌状态
1
CodingPuppy OP 有人没阿,哪个大神有耐心看完我的问题,指导我一下阿!!
|
2
zenxds 2017-07-13 19:37:10 +08:00 1
依赖关系写错了吧,react-table 应该在 react 之后
|
3
witcher42 2017-07-13 20:46:14 +08:00 via iPhone 1
2 楼正解
|
4
CodingPuppy OP 感谢
|