Skip to content

Commit 3e4360c

Browse files
committed
docs(guides): finish up code-splitting and lazy-loading
1 parent 8aa4ed0 commit 3e4360c

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

content/guides/code-splitting.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ __webpack.config.js__
6262

6363
``` js
6464
const path = require('path');
65+
const HTMLWebpackPlugin = require('html-webpack-plugin');
6566

6667
module.exports = {
6768
entry: {
@@ -73,7 +74,7 @@ module.exports = {
7374
path: path.resolve(__dirname, 'dist')
7475
},
7576
plugins: [
76-
new HtmlWebpackPlugin({
77+
new HTMLWebpackPlugin({
7778
title: 'Code Splitting'
7879
})
7980
]
@@ -113,14 +114,15 @@ __webpack.config.js__
113114
``` diff
114115
const path = require('path');
115116
+ const webpack = require('webpack');
117+
const HTMLWebpackPlugin = require('html-webpack-plugin');
116118

117119
module.exports = {
118120
entry: {
119121
index: './src/index.js',
120122
another: './src/another-module.js'
121123
},
122124
plugins: [
123-
new HtmlWebpackPlugin({
125+
new HTMLWebpackPlugin({
124126
title: 'Code Splitting'
125127
- })
126128
+ }),
@@ -170,6 +172,7 @@ __webpack.config.js__
170172
``` diff
171173
const path = require('path');
172174
- const webpack = require('webpack');
175+
const HTMLWebpackPlugin = require('html-webpack-plugin');
173176

174177
module.exports = {
175178
entry: {
@@ -178,7 +181,7 @@ __webpack.config.js__
178181
- another: './src/another-module.js'
179182
},
180183
plugins: [
181-
new HtmlWebpackPlugin({
184+
new HTMLWebpackPlugin({
182185
title: 'Code Splitting'
183186
- }),
184187
+ })
@@ -194,6 +197,21 @@ __webpack.config.js__
194197
};
195198
```
196199

200+
We'll also update our project to remove the now unused files:
201+
202+
__project__
203+
204+
``` diff
205+
webpack-demo
206+
|- package.json
207+
|- webpack.config.js
208+
|- /dist
209+
|- /src
210+
|- index.js
211+
- |- another-module.js
212+
|- /node_modules
213+
```
214+
197215
Now, instead of statically importing lodash, we'll use dynamic importing to separate a chunk:
198216

199217
__src/index.js__

content/guides/lazy-loading.md

+41-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,20 @@ Lazy, or "on demand", loading is a great way to optimize your site or applicatio
1919

2020
Let's take the example from [Code Splitting](/guides/code-splitting#dynamic-imports) and tweak it a bit to demonstrate this concept even more. The code there does cause a separate chunk, `lodash.bundle.js`, to be generated and technically "lazy-loads" it as soon as the script is run. The trouble is that no user interaction is required to load the bundle -- meaning that every time the page is loaded, the request will fire. This doesn't help us too much and will impact performance negatively.
2121

22-
Let's try something different. We'll add an interaction to log some text to the console when the user clicks a button. However, we'll wait to load that code until the actual interaction occurs for the first time. To do this we'll go back and extend the original example from [Getting Started](/guides/getting-started) and leave the `lodash` in the main chunk.
22+
Let's try something different. We'll add an interaction to log some text to the console when the user clicks a button. However, we'll wait to load that code (`print.js`) until the interaction occurs for the first time. To do this we'll go back and rework the [final _Dynamic Imports_ example](/guides/code-splitting#dynamic-imports) from _Code Splitting_ and leave `lodash` in the main chunk.
23+
24+
__project__
25+
26+
``` diff
27+
webpack-demo
28+
|- package.json
29+
|- webpack.config.js
30+
|- /dist
31+
|- /src
32+
|- index.js
33+
+ |- print.js
34+
|- /node_modules
35+
```
2336

2437
__src/print.js__
2538

@@ -34,14 +47,15 @@ export default () => {
3447
__src/index.js__
3548

3649
``` diff
37-
import _ from 'lodash';
38-
39-
function component() {
50+
+ import _ from 'lodash';
51+
+
52+
- async function getComponent() {
53+
+ function component() {
4054
var element = document.createElement('div');
55+
- const _ = await import(/* webpackChunkName: "lodash" */ 'lodash');
4156
+ var button = document.createElement('button');
4257
+ var br = document.createElement('br');
4358

44-
- // Lodash, now imported by this script
4559
+ button.innerHTML = 'Click me and look at the console!';
4660
element.innerHTML = _.join(['Hello', 'webpack'], ' ');
4761
+ element.appendChild(br);
@@ -58,14 +72,34 @@ __src/index.js__
5872
return element;
5973
}
6074

61-
document.body.appendChild(component());
75+
- getComponent().then(component => {
76+
- document.body.appendChild(component);
77+
- });
78+
+ document.body.appendChild(component());
6279
```
6380

6481
W> Note that when using `import()` on ES6 modules you must reference the `.default` property as it's the actual `module` object that will be returned when the promise is resolved.
6582

6683
Now let's run webpack and check out our new lazy-loading functionality:
6784

68-
?> Add bash example of webpack output
85+
``` bash
86+
Hash: e0f95cc0bda81c2a1340
87+
Version: webpack 3.0.0
88+
Time: 1378ms
89+
Asset Size Chunks Chunk Names
90+
print.bundle.js 417 bytes 0 [emitted] print
91+
index.bundle.js 548 kB 1 [emitted] [big] index
92+
index.html 189 bytes [emitted]
93+
[0] ./src/index.js 742 bytes {1} [built]
94+
[2] (webpack)/buildin/global.js 509 bytes {1} [built]
95+
[3] (webpack)/buildin/module.js 517 bytes {1} [built]
96+
[4] ./src/print.js 165 bytes {0} [built]
97+
+ 1 hidden module
98+
Child html-webpack-plugin for "index.html":
99+
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
100+
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
101+
+ 2 hidden modules
102+
```
69103

70104

71105
## Frameworks

0 commit comments

Comments
 (0)