从 webpack 包中排除 react [英] Exclude react from webpack bundle

查看:39
本文介绍了从 webpack 包中排除 react的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从由 webpack 生成的包中排除 react.原因是,我们在页面上提供了一个全球版本的 react,所以我将使用它.

I'm trying to exclude react from my bundle which is generated by webpack. Reason being, we have a global version of react available on the page so I'll be using that.

我已尝试使用以下内容,如此处所建议的Webpack 和外部库 但这并没有似乎行不通.我可以看到 webpack 已经导出了 React 但它仍然出现在捆绑包中.

I have tried using the below, as suggested here Webpack and external libraries but this doesn't seem to work. I can see webpack has exported React but it still appears in the bundle.

externals: {
  'react': 'React'
}

我在想这可能是另一个依赖,例如反应路由器进口反应?有没有人设法做到这一点?

I was thinking it may be another dependency e.g. react-router importing react? Has anyone managed to do this?

推荐答案

我遇到了和你一样的问题.被困了一晚上,找到了答案,react-dom 需要 react/lib/ReactDOM,它仍然包含 react .解决方案如下:

I had the same problem as you did. Was stuck for one night, found the answer, react-dom requires react/lib/ReactDOM, which still includes react in the bundle. Solution looks like this:

externals: {
 'react': 'react', // Case matters here 
 'react-dom' : 'reactDOM' // Case matters here 
}

然后将 react-dom 添加到页面中的脚本标记中.

Then add react-dom to the script tag in your page.

这篇关于从 webpack 包中排除 react的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆