webpack – 为什么要在CommonJS中使用依赖项列表require.ensure()?

从Webpack文档( https://webpack.github.io/docs/api-in-modules.html#require-ensure):

Download additional dependencies on demand. The dependencies array lists modules that should be available. When they are,callback is called. If the callback is a function expression,dependencies in that source part are extracted and also loaded on demand. A single request is fired to the server,except if all modules are already available.

如果源部分中的依赖项也是按需提取和加载的,那么为什么还要在依赖项列表中添加任何内容呢?

我见过这样的例子很混乱(https://github.com/webpack/webpack/tree/master/examples/extra-async-chunk):

require.ensure(["./a"],function(require) {
    require("./b");
    require("./d");
});

“b”和“d”不在依赖项列表中,但将按需加载,就像“a”一样.那有什么区别?

解决方法

链接到的文档中的示例显示了行为不同的一种方式.指定依赖项时,它会显式创建一个新块,将依赖项放入其中,并添加回调中引用的任何其他依赖项.如果未指定依赖项,则回调中的任何依赖项将添加到“当前”(最后?)块中,不会创建新块.

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...