简体   繁体   English

避免使用Browserify捆绑特定模块

[英]Avoid bundling specific modules with Browserify

I have a dependency tree like this: 我有一个像这样的依赖树:

index.js
      \__ A
      \__ B
          \__ C
          |   \__ D
           \__ E

I want to bundle index.js and that works fine: browserify --node index.js -o bundle.js 我想捆绑index.js ,并且工作正常: browserify --node index.js -o bundle.js

The problem is when one of the dependencies has a dependency. 问题是当其中一个依赖项具有依赖项时。 Let's suppose it's D that has a native dependency (C++ code). 让我们假设具有本地依赖项(C ++代码)的D

I want to install it manually using npm install D and make the bundle.js to really require it from the disk, not from the bundle.js code. 我想使用npm install D手动安装它,并使bundle.js真正从磁盘而不是bundle.js代码中获取它。

How can I exclude the D module from the bundle and make the bundle to require it from the node_modules ? 如何将D模块从包中排除,并使包从node_modules要求它?

I tried using --ignore D , but it returns an empty object when required. 我尝试使用--ignore D ,但是在需要时它返回一个空对象。

How can I require a real module from the node_modules directory (the way like Node's require does? 我如何从node_modules目录中require一个真实的模块(就像Node的require一样?

Use the --exclude option together with --node : --exclude选项与--node一起使用:

browserify --node -s GlobalVariable your-script.js -o bundle.js --exclude some-dependency

This will create the bundle.js file which will define the GlobalVariable variable if there is no CommonJS environment. 这将创建bundle.js文件,如果没有CommonJS环境,它将定义GlobalVariable变量。

--node is a handy option if you want to run the bundle in Node, not in the browser. 如果要在Node(而不是浏览器)中运行捆绑软件,则--node是一个方便的选项。

The --exclude option will exclude the some-dependency module from the output bundle. --exclude选项将从输出包中排除some-dependency模块。

Check out the Browserify Usage section. 查看“ 浏览器使用情况”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM