Skip to content

Commit 15f8155

Browse files
authored
docs(guides): merge and rewrite code-splitting and lazy-load guides (#1338)
Merge Code Splitting guides by removing all caching discussion and other extraneous content. Focuses on code splitting approaches instead. Move certain `import()` documentation to the Module API page. Use links to prevent content duplication. Replace lazy-load-react with a short, framework-agnostic lazy-loading guide.
1 parent 690eb55 commit 15f8155

14 files changed

+433
-882
lines changed

content/api/module-methods.md

+47-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ export default {
5353

5454
### `import()`
5555

56-
Dynamically load modules.
56+
`import('path/to/module') -> Promise`
57+
58+
Dynamically load modules. Calls to `import()` are treated as split points, meaning the requested module and it's children are split out into a separate chunk.
59+
60+
T> The [ES2015 Loader spec](https://whatwg.github.io/loader/) defines `import()` as method to load ES2015 modules dynamically on runtime.
5761

5862
``` javascript
5963
if ( module.hot ) {
@@ -63,8 +67,33 @@ if ( module.hot ) {
6367
}
6468
```
6569

66-
The compiler treats this as a split point and will split everything from `lodash` into a separate bundle. This returns a promise that will resolve to the module once the bundle has been loaded. See the [async code splitting guide](/guides/code-splitting-async) for more information.
70+
W> This feature relies on [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) internally. If you use `import()` with older browsers, remember to shim `Promise` using a polyfill such as [es6-promise](https://github.com/stefanpenner/es6-promise) or [promise-polyfill](https://github.com/taylorhakes/promise-polyfill). See [Shimming](/guides/shimming) for more information.
71+
72+
The spec for `import` doesn't allow control over the chunk's name or other properties as "chunks" are only a concept within webpack. Luckily webpack allows some special parameters via comments so as to not break the spec:
73+
74+
``` js
75+
import(
76+
/* webpackChunkName: "my-chunk-name" */
77+
/* webpackMode: "lazy" */
78+
'module'
79+
);
80+
```
81+
82+
`webpackChunkName`: A name for the new chunk. Since webpack 2.6.0, the placeholders `[index]` and `[request]` are supported within the given string to an incremented number or the actual resolved filename respectively.
83+
84+
`webpackMode`: Since webpack 2.6.0, different modes for resolving dynamic imports can be specified. The following options are supported:
85+
86+
- `"lazy"` (default): Generates a lazy-loadable chunk for each `import()`ed module.
87+
- `"lazy-once"`: Generates a single lazy-loadable chunk that can satisfy all calls to `import()`. The chunk will be fetched on the first call to `import()`, and subsequent calls to `import()` will use the same network response. Note that this only makes sense in the case of a partially dynamic statement, e.g. ``import(`./locales/${language}.json`)``, where there are multiple module paths that could potentially be requested.
88+
- `"eager"`: Generates no extra chunk. All modules are included in the current chunk and no additional network requests are made. A `Promise` is still returned but is already resolved. In contrast to a static import, the module isn't executed until the call to `import()` is made.
6789

90+
T> Note that both options can be combined like so `/* webpackMode: "lazy-once", webpackChunkName: "all-i18n-data" */`. This is parsed as a JSON5 object without curly brackets.
91+
92+
W> Fully dynamic statements, such as `import(foo)`, __will fail__ because webpack requires at least some file location information. This is because `foo` could potentially be any path to any file in your system or project. The `import()` must contain at least some information about where the module is located, so bundling can be limited to a specific directory or set of files.
93+
94+
W> Every module that could potentially be requested on an `import()` call is included. For example, ``import(`./locale/${language}.json`)`` will cause every `.json` file in the `./locale` directory to be bundled into the new chunk. At run time, when the variable `language` has been computed, any file like `english.json` or `german.json` will be available for consumption.
95+
96+
W> The use of `System.import` in webpack [did not fit the proposed spec](https://github.com/webpack/webpack/issues/2163), so it was deprecated in webpack [2.1.0-beta.28](https://github.com/webpack/webpack/releases/tag/v2.1.0-beta.28) in favor of `import()`.
6897

6998

7099
## CommonJS
@@ -132,24 +161,35 @@ require.cache[module.id] !== module
132161

133162
### `require.ensure`
134163

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

139-
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.
140171

141172
``` javascript
142173
var a = require('normal-dep');
143174

144-
if ( /* Some Condition */ ) {
145-
require.ensure(["b"], function(require) {
146-
var c = require("c");
175+
if ( module.hot ) {
176+
require.ensure(['b'], function(require) {
177+
var c = require('c');
147178

148179
// Do something special...
149180
});
150181
}
151182
```
152183
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+
153193
154194
155195
## AMD

content/guides/asset-management.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ bundle.js 560 kB 0 [emitted] [big] main
136136

137137
Open up `index.html` in your browser again and you should see that `Hello webpack` is now styled in red. To see what webpack did, inspect the page (don't view the page source, as it won't show you the result) and look at the page's head tags. It should contain our style block that we imported in `index.js`.
138138

139-
T> Note that you can also [split your CSS](/guides/code-splitting-css) for better load times in production. On top of that, loaders exist for pretty much any flavor of CSS you can think of -- [postcss](/loaders/postcss-loader), [sass](/loaders/sass-loader), and [less](/loaders/less-loader) to name a few.
139+
T> Note that you can also [split your CSS](/plugins/extract-text-webpack-plugin) for better load times in production. On top of that, loaders exist for pretty much any flavor of CSS you can think of -- [postcss](/loaders/postcss-loader), [sass](/loaders/sass-loader), and [less](/loaders/less-loader) to name a few.
140140

141141

142142
## Loading Images
@@ -440,7 +440,7 @@ __project__
440440
|- bundle.js
441441
|- index.html
442442
|- /src
443-
+ |- data.xml
443+
+ |- data.xml
444444
|- my-font.woff
445445
|- my-font.woff2
446446
|- icon.png
@@ -530,7 +530,7 @@ __project__
530530
|- bundle.js
531531
|- index.html
532532
|- /src
533-
- |- data.xml
533+
- |- data.xml
534534
- |- my-font.woff
535535
- |- my-font.woff2
536536
- |- icon.png

content/guides/caching.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Caching
3-
sort: 9
3+
sort: 11
44
contributors:
55
- okonet
66
- jouni-kantola

0 commit comments

Comments
 (0)