Skip to content

Commit 95f1595

Browse files
committed
docs(api): finish moving documentation from code-splitting-async
1 parent 4ebdd30 commit 95f1595

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

content/api/module-methods.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,24 +161,35 @@ require.cache[module.id] !== module
161161

162162
### `require.ensure`
163163

164+
W> `require.ensure()` is specific to webpack and superseded by `import()`.
165+
164166
``` javascript
165-
require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])
167+
require.ensure(dependencies: String[], callback: function(require), errorCallback: function(error), chunkName: String)
166168
```
167169

168-
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.
169171

170172
``` javascript
171173
var a = require('normal-dep');
172174

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');
176178

177179
// Do something special...
178180
});
179181
}
180182
```
181183
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'); })`.
192+
182193
183194
184195
## AMD

0 commit comments

Comments
 (0)