You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Split out the given `dependencies` to a separate bundle that that will be loaded asynchronously. When using CommonJS module syntax, this is the only way to dynamically load dependencies. Meaning, this code can be run within execution, only loading the `dependencies` if a certain conditions are met. See the [Asynchronous Code Splitting guide](/guides/code-splitting-async) for more details.
170
+
Split out the given `dependencies` to a separate bundle that that will be loaded asynchronously. When using CommonJS module syntax, this is the only way to dynamically load dependencies. Meaning, this code can be run within execution, only loading the `dependencies` if certain conditions are met.
169
171
170
172
``` javascript
171
173
var a = require('normal-dep');
172
174
173
-
if ( /* Some Condition */ ) {
174
-
require.ensure(["b"], function(require) {
175
-
var c =require("c");
175
+
if ( module.hot ) {
176
+
require.ensure(['b'], function(require) {
177
+
var c =require('c');
176
178
177
179
// Do something special...
178
180
});
179
181
}
180
182
```
181
183
184
+
The following parameters are supported in the order specified above:
185
+
186
+
- `dependencies`: An array of strings declaring all modules required for the code in the `callback` to execute.
187
+
- `callback`: A function that webpack will execute once the dependencies are loaded. An implementation of the `require` function is sent as a parameter to this function. The function body can use this to further `require()` modules it needs for execution.
188
+
- `errorCallback`: A function that is executed when webpack fails to load the dependencies.
189
+
- `chunkName`: A name given to the chunk created by this particular `require.ensure()`. By passing the same `chunkName` to various `require.ensure()` calls, we can combine their code into a single chunk, resulting in only one bundle that the browser must load.
190
+
191
+
W> Although the implementation of `require` is passed as an argument to the `callback` function, using an arbitrary name e.g. `require.ensure([], function(request) { request('someModule'); })` isn't handled by webpack's static parser. Use `require` instead, e.g. `require.ensure([], function(require) { require('someModule'); })`.
0 commit comments