How to “remove”/“change” some require(…) calls when using browserify?

我与影子孤独终老i 提交于 2019-12-24 04:24:07

问题


I have app written in node.js that requires some npm modules (react, react-router and others). When I run browserify on it, then all npm modules are "injected" to the bundle.js file. What I want is to provide distribution for bower that won't include react and react-router dependencies in bundle.js file, because they can be referenced as dependencies in bower.json.

app.js:

var React = require('react')
React.render(...)

In bundle.js react is injected into it along with app.js

I need bundle.js that will not contain react and will assume that it is available in global (window) scope.

bundle.js:

React.render(...)

or something like this:

var require = function(name){ return window[name] }
var React = require('react')
React.render(...)

So basically I want to tell browserify that SOME of the modules can be found in window scope and don't have to be injected in to the bundle.js...


回答1:


Use -x [Module Name] to exclude node modules from the bundle.

browserify -d -x react -x react-router app.js > bundle.js


来源:https://stackoverflow.com/questions/30965909/how-to-remove-change-some-require-calls-when-using-browserify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!