Skip to content

Commit 95831c4

Browse files
committed
docs(guides/api): fill in and some gaps and resolve issues based on review in #1338
1 parent f00d9cf commit 95831c4

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

content/api/module-methods.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ if ( module.hot ) {
6969

7070
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.
7171

72-
One problem with direct spec-based usage is that we have no control over the split chunk's name or other properties. Luckily webpack allows some special parameters via comments so as to not break the spec:
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:
7373

7474
``` js
7575
import(

content/guides/code-splitting.md

+32-7
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,31 @@ module.exports = {
5454

5555
This will yield the following build result:
5656

57-
?> Add bash example of webpack output
57+
``` bash
58+
Hash: d1e4eab63dc6ba09c930
59+
Version: webpack 2.6.1
60+
Time: 531ms
61+
Asset Size Chunks Chunk Names
62+
vendor.bundle.js 544 kB 0 [emitted] [big] vendor
63+
index.bundle.js 544 kB 1 [emitted] [big] index
64+
[0] ./~/lodash/lodash.js 540 kB {0} {1} [built]
65+
[1] (webpack)/buildin/global.js 509 bytes {0} {1} [built]
66+
[2] (webpack)/buildin/module.js 517 bytes {0} {1} [built]
67+
[3] ./src/index.js 216 bytes {1} [built]
68+
[4] multi lodash 28 bytes {0} [built]
69+
```
5870

5971
As mentioned there are some pitfalls to this approach:
6072

6173
- If there are any duplicated modules between entry chunks they will be included in both bundles.
6274
- It isn't as flexible and can't be used to dynamically split code with the core application logic.
6375

64-
The first of these two points is definitely an issue for our example, as `moment` is also imported within `./src/index.js` and will thus be duplicated in the output. See the `CommonChunkPlugin` example below for a solution to this problem.
76+
The first of these two points is definitely an issue for our example, as `lodash` is also imported within `./src/index.js` and will thus be duplicated in the output. See the `CommonChunkPlugin` example below for a solution to this problem.
6577

6678

6779
## Plugins & Loaders
6880

69-
There are multiple plugins and loaders in webpack's ecosystem that can help with splitting and managing split code. The most widely utilized of these is the [`CommonsChunkPlugin`](/plugins/commons-chunk-plugin), a fairly advanced tool that allows us to extract common dependencies into an existing entry chunk or an entirely new chunk. Let's use this to de-duplicate the `moment` dependency from the previous example:
81+
There are multiple plugins and loaders in webpack's ecosystem that can help with splitting and managing split code. The most widely utilized of these is the [`CommonsChunkPlugin`](/plugins/commons-chunk-plugin), a fairly advanced tool that allows us to extract common dependencies into an existing entry chunk or an entirely new chunk. Let's use this to de-duplicate the `lodash` dependency from the previous example:
7082

7183
__webpack.config.js__
7284

@@ -93,9 +105,21 @@ module.exports = {
93105
};
94106
```
95107

96-
This will yield the following build result (notice how our `index.bundle.js` has shrunk in size):
97-
98-
?> Add bash example of webpack output
108+
With the `CommonsChunkPlugin` in place, we should now see the duplicate dependency removed from our `index.bundle.js`. The plugin should notice that we've separated `lodash` out to a separate chunk and remove the dead weight from our main bundle. Let's do an `npm run build` to see if it worked:
109+
110+
``` bash
111+
Hash: 5f3b08906b5e7d8d0799
112+
Version: webpack 2.6.1
113+
Time: 527ms
114+
Asset Size Chunks Chunk Names
115+
index.bundle.js 542 bytes 0 [emitted] index
116+
vendor.bundle.js 547 kB 1 [emitted] [big] vendor
117+
[0] ./~/lodash/lodash.js 540 kB {1} [built]
118+
[1] (webpack)/buildin/global.js 509 bytes {1} [built]
119+
[2] (webpack)/buildin/module.js 517 bytes {1} [built]
120+
[3] ./src/index.js 401 bytes {0} [built]
121+
[4] multi lodash 28 bytes {1} [built]
122+
```
99123

100124
Here are some other useful plugins and loaders provide by the community for splitting code:
101125

@@ -146,7 +170,8 @@ If you've enabled [`async` functions](https://developer.mozilla.org/en-US/docs/W
146170
__src/index.js__
147171

148172
``` diff
149-
async function getComponent() {
173+
- function getComponent() {
174+
+ async function getComponent() {
150175
- return import(/* webpackChunkName: "lodash" */ 'lodash').then(module => {
151176
- var element = document.createElement('div');
152177
-

content/guides/lazy-loading.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ __src/index.js__
3939
function component() {
4040
var element = document.createElement('div');
4141
+ var button = document.createElement('button');
42-
+ var break = document.createElement('br');
42+
+ var br = document.createElement('br');
4343

4444
- // Lodash, now imported by this script
4545
+ button.innerHTML = 'Click me and look at the console!';
4646
element.innerHTML = _.join(['Hello', 'webpack'], ' ');
47-
+ element.appendChild(break);
47+
+ element.appendChild(br);
4848
+ element.appendChild(button);
4949
+
50-
+ button.onclick = e => import(/* webpackChunkName: "console" */ './console').then(module => {
50+
+ button.onclick = e => import(/* webpackChunkName: "print" */ './print').then(module => {
5151
+ var print = module.default;
5252
+
5353
+ print();

0 commit comments

Comments
 (0)