Skip to content

Commit 31ec9fc

Browse files
committed
docs(guides): update code-splitting doc
1 parent ec444c6 commit 31ec9fc

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

content/guides/code-splitting.md

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,26 @@ module.exports = {
7979

8080
This will yield the following build result:
8181

82-
?> Update the bash output
82+
``` bash
83+
Hash: 309402710a14167f42a8
84+
Version: webpack 2.6.1
85+
Time: 570ms
86+
Asset Size Chunks Chunk Names
87+
index.bundle.js 544 kB 0 [emitted] [big] index
88+
another.bundle.js 544 kB 1 [emitted] [big] another
89+
[0] ./~/lodash/lodash.js 540 kB {0} {1} [built]
90+
[1] (webpack)/buildin/global.js 509 bytes {0} {1} [built]
91+
[2] (webpack)/buildin/module.js 517 bytes {0} {1} [built]
92+
[3] ./src/another-module.js 87 bytes {1} [built]
93+
[4] ./src/index.js 216 bytes {0} [built]
94+
```
8395

8496
As mentioned there are some pitfalls to this approach:
8597

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

89-
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 both bundles. See the `CommonsChunkPlugin` example below for a solution to this problem.
101+
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 both bundles. Let's remove this duplication by using the `CommonsChunkPlugin`.
90102

91103

92104
## Prevent Duplication
@@ -118,7 +130,20 @@ __webpack.config.js__
118130

119131
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:
120132

121-
?> Update bash output.
133+
``` bash
134+
Hash: 70a59f8d46ff12575481
135+
Version: webpack 2.6.1
136+
Time: 510ms
137+
Asset Size Chunks Chunk Names
138+
index.bundle.js 665 bytes 0 [emitted] index
139+
another.bundle.js 537 bytes 1 [emitted] another
140+
common.bundle.js 547 kB 2 [emitted] [big] common
141+
[0] ./~/lodash/lodash.js 540 kB {2} [built]
142+
[1] (webpack)/buildin/global.js 509 bytes {2} [built]
143+
[2] (webpack)/buildin/module.js 517 bytes {2} [built]
144+
[3] ./src/another-module.js 87 bytes {1} [built]
145+
[4] ./src/index.js 216 bytes {0} [built]
146+
```
122147

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

@@ -131,7 +156,7 @@ Here are some other useful plugins and loaders provide by the community for spli
131156

132157
Two similar techniques are supported by webpack when it comes to dynamic code splitting. The first and more preferable approach is use to the [`import()` syntax](/api/module-methods#import-) that conforms to the [ECMAScript proposal](https://github.com/tc39/proposal-dynamic-import) for dynamic imports. The legacy, webpack-specific approach is to use [`require.ensure`](/api/module-methods#require-ensure). Let's try using the first of these two approaches...
133158

134-
Before we start, let's remove the `entry` and `CommonsChunkPlugin` from our config as they won't be needed for this next demonstration:
159+
Before we start, let's remove the extra `entry` and `CommonsChunkPlugin` from our config as they won't be needed for this next demonstration:
135160

136161
__webpack.config.js__
137162

@@ -143,17 +168,16 @@ __webpack.config.js__
143168
entry: {
144169
+ index: './src/index.js'
145170
- index: './src/index.js',
146-
- vendor: [
147-
- 'lodash'
148-
- ]
171+
- another: './src/another-module.js'
149172
},
150173
- plugins: [
151174
- new webpack.optimize.CommonsChunkPlugin({
152-
- name: 'vendor' // Specify the common bundle's name.
175+
- name: 'common' // Specify the common bundle's name.
153176
- })
154177
- ],
155178
output: {
156179
filename: '[name].bundle.js',
180+
+ chunkFilename: '[name].bundle.js',
157181
path: path.resolve(__dirname, 'dist')
158182
}
159183
};
@@ -190,7 +214,18 @@ __src/index.js__
190214

191215
Note the use of `webpackChunkName` in the comment. This will cause our separate bundle to be named `lodash.bundle.js` instead of just `[id].bundle.js`. For more information on `webpackChunkName` and the other available options, see the [`import()` documentation](/api/module-methods#import-). Let's run webpack to see lodash separated out to a separate bundle:
192216

193-
?> Add bash example of webpack output
217+
``` bash
218+
Hash: a27e5bf1dd73c675d5c9
219+
Version: webpack 2.6.1
220+
Time: 544ms
221+
Asset Size Chunks Chunk Names
222+
lodash.bundle.js 541 kB 0 [emitted] [big] lodash
223+
index.bundle.js 6.35 kB 1 [emitted] index
224+
[0] ./~/lodash/lodash.js 540 kB {0} [built]
225+
[1] ./src/index.js 377 bytes {1} [built]
226+
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
227+
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
228+
```
194229

195230
If you've enabled [`async` functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) via a pre-processor like babel, note that you can simplify the code as `import()` statements just return promises:
196231

0 commit comments

Comments
 (0)