如何排除“require('react')”从我的Browserified包? [英] How do I exclude the "require('react')" from my Browserified bundle?
问题描述
我正在使用 Browserify
捆绑 ReactJS
应用程序。
I'm using Browserify
to bundle a ReactJS
application.
我的所有组件都在顶部包含 require(react)
。这导致 Browserify
在我的包中包含 ReactJS
源。但我想排除它。
All my components include a require("react")
at the top. This causes Browserify
to include the ReactJS
source in my bundle. But I'd like to exclude it.
我该怎么做?这是正确的做法吗?
How do I do that? Is this the right thing to do?
推荐答案
@NickTomlin给出了这个答案,但随后将其删除了。
@NickTomlin gave this answer, but then deleted it.
您可以使用 外部
:
You can use external
:
browserify --external react src.js> dest.js
使用api的示例:
var bundler = browserify('src.js');
bundler.external('react');
bundler.bundle();
这是一个可行的选择。 external
需要另一个脚本以兼容的方式提供模块。您可以生成如下脚本:
This is a viable option. external
requires another script to provide the module in a compatible way. You can produce such a script like this:
browserify -r react > react.js
env NODE_ENV=production browserify -r react | uglifyjs -m > react.min.js
在HTML中:
<script src="react.js"></script>
<script src="dest.js"></script>
dest.js是除反应之外的代码。 react.js只是反应及其依赖。
dest.js is your code except react. react.js is just react and its dependencies.
需要更多外部事物吗?除了反应之外,只需添加它们。
Need more things external? Just add them in addition to react.
browserify -x react -x react-bootstrap src.js > dest.js
browserify -r react -r react-bootstrap > vendor.js
您也可以在package.json中执行类似的操作
You could also do something like this in package.json
"browser": {"react": "./react-fake.js"}
// ./react-fake.js
try {
module.exports = require('react');
} catch(e){
module.exports = window.React;
}
用编译-x react
。这允许您接受 -r react
构建,并回退到全局React。
And compile with -x react
. This allows you to accept a -r react
build, and fallback to a global React.
这篇关于如何排除“require('react')”从我的Browserified包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!