What is the difference between browserify external vs. exclude?

此生再无相见时 提交于 2019-12-05 01:25:19

Answer:

You should use exclude.

Explanation:

Both functions prevent the file from being included in the bundle. For your use case, you're probably not going to require jQuery, so it really doesn't matter which you use. However, this is what's going on:

browserify uses module-deps to explore your code and find any require statements, and then tells module-deps where to find the required module.

If the file is in the bundle, it simply needs to provide the key for it in the bundle's module map.

If you said the file is external, the browserify assumes you mean it's included in another bundle, and so provides a path to the file assuming that that as an id will resolve from anther bundle. There is a little additional bookkeeping involved to be able to do this.

If you exclude the file, then browserify will provide undefined to module-deps and there will definitely be a fire when you try to use the bundle that requiring said file. However, this approach lacks the overhead of tracking the file path (which would really be negligible) and doesn't "waste time" looking in other bundles before exploding.

Some examples: I fiddled with node-browserify/example/api to produce some bundles and the examples below are the module maps from various tests, somewhat formatted for readability.

Vanilla - ran as it is in the browserify repo:

{
    1: [function(require, module, exports) {
        module.exports = function(n) {
            return n * 3 };

    }, {}],
    2: [function(require, module, exports) {
        var bar = require('./bar');

        module.exports = function(n) {
            return n * bar(n);
        };

    }, { "./bar": 1 }],
    3: [function(require, module, exports) {
        var foo = require('./foo');
        console.log(foo(5));

    }, { "./foo": 2 }]
}

3 (main.js) depends on ./foo which is at 2

2 (foo.js) depends on ./bar which is at 1

1 (bar.js) has no dependencies

Marked api/bar.js as external:

{
    1: [function(require, module, exports) {
        var bar = require('./bar');

        module.exports = function(n) {
            return n * bar(n);
        };

    }, { "./bar": "/browser/bar.js" }],
    2: [function(require, module, exports) {
        var foo = require('./foo');
        console.log(foo(5));

    }, { "./foo": 1 }]
}

2 (main.js) depends on ./foo which is at 1

1 (foo.js) depends on ./bar which should be labelled /browser/bar.js in some other bundle

Marked api/bar.js to exclude:

{
    1: [function(require, module, exports) {
        var bar = require('./bar');

        module.exports = function(n) {
            return n * bar(n);
        };

    }, { "./bar": undefined }],
    2: [function(require, module, exports) {
        var foo = require('./foo');
        console.log(foo(5));

    }, { "./foo": 1 }]
}

2 (main.js) depends on ./foo which is at 1

1 (foo.js) depends on ./bar which is at ZOMFG! I dunno where it is. yru require??!1!

Removed the exclude/external call, and removed the require of ./bar from foo.js:

{
    1: [function(require, module, exports) {
        module.exports = function(n) {
            return n * bar(n);
        };

    }, {}],
    2: [function(require, module, exports) {
        var foo = require('./foo');
        console.log(foo(5));

    }, { "./foo": 1 }]
}

2 (main.js) depends on ./foo which is at 1

1 (foo.js) has no dependencies, the world is peachy. I wonder if they loaded bar by some other means

The first part of @Will's answer is important too:

For your use case, you're probably not going to require jQuery, so it really doesn't matter which you use

I think the OP's example is confusing to ask the difference between exclude and external. If jQuery is never require()-ed in the code, then there is no questions asked: it will never be parsed by browserify and has to be loaded by other means anyway.

Just wanted to point that out as I found this confusing too.

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