为什么要打扰CommonJS require.ensure()中的依赖项列表? [英] Why bother with dependencies list in CommonJS require.ensure()?

查看:65
本文介绍了为什么要打扰CommonJS require.ensure()中的依赖项列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

From the Webpack documentation (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.

如果还提取了源部分中的依赖项,并且

If dependencies in the source part are also extracted and loaded on demand, then why bother putting anything in the dependencies list?

我见过像这样的示例,它们非常令人困惑( https://github.com/webpack/webpack/tree/master/examples/extra-async-chunk ):

I've seen examples like this that are very confusing (https://github.com/webpack/webpack/tree/master/examples/extra-async-chunk):

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

b和 d不在依赖项列表中,只是按需加载像一个。那么有什么区别呢?

"b" and "d" are not in the dependencies list, but will be loaded on demand just like "a". So what's the difference?

推荐答案

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

The example in the docs you linked to shows one way that behavior differs. When you specify a dependency, it explicitly creates a new chunk, puts the dependency in it, and adds any other dependencies referenced in the callback. When you don't specify a dependency, any dependencies in the callback are added to the 'current' (last?) chunk, a new chunk is not created.

这篇关于为什么要打扰CommonJS require.ensure()中的依赖项列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆