微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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”一样.那有什么区别?

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐