当前位置 : 主页 > 手机开发 > 其它 >

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

来源:互联网 收集:自由互联 发布时间:2021-06-22
从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 cal
从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”一样.那有什么区别?

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