The first parameter is rarely useful. To learn why it is there and causes confusion, please see my another answer.
Comply with the spec
One use case for the first parameter could be to specify all dependencies for clarity and to comply with the spec. But that's completely optional.
Add modules to chunks to make the chunks similar
Consider you have two split points in different parts of an app. The first split point depends on module a
, the second depends on modules a
and b
.
To eliminate the risk of downloading a
twice, you could decide to place both modules into a single chunk:
// First split point
require.ensure(['b'], (require) => {
require('a');
});
Pull modules into parent chunks
Consider the following code splitting scenario:
require.ensure([], (require) => {
...
require.ensure([], (require) => {
require('a');
require('b');
});
require.ensure([], (require) => {
require('a');
require('c');
});
...
});
In this case, module a
will end up in both nested chunks. If at least one of the nested chunks is frequently loaded, you could decide to move a
into the parent chunk:
require.ensure(['a'], (require) => {
...
Add modules to chunks with require.include
Consider the previous example. There is another way to pull a
into the parent chunk:
require.ensure([], (require) => {
require.include('a');
...
In this particular example, both solutions are equivalent and there is no advantage in using require.include
. However, if you don't have access to the split point's code, parent chunk is an entry chunk or you use the modern import()
syntax, require.include
is your only choice.
It is possible to pull modules into chunks using synchronous require
or import
. The advantage of require.include
is that it only loads modules and doesn't evaluate them. This could be useful to defer modules' evaluation if it is expensive or depends on the application state, e.g., requires polyfills to be loaded, DOM nodes to be present, etc.