You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(guides): rewrite/re-outline the tree-shaking page
This continues our work #1258 and resolves most of the issues mentioned
in #1331. It is a WIP though, we still need to populate the `TODO` code
blocks.
_Tree shaking_ is a term commonly used in the JavaScript context for dead-code elimination, or more precisely, live-code import. It relies on ES2015 module [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)/[export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) for the [static structure](http://exploringjs.com/es6/ch_modules.html#static-module-structure) of its module system. The name and concept have been popularized by the ES2015 module bundler [rollup](https://github.com/rollup/rollup).
17
+
_Tree shaking_ is a term commonly used in the JavaScript context for dead-code elimination. It relies on the [static structure](http://exploringjs.com/es6/ch_modules.html#static-module-structure) of ES2015 module syntax, i.e. [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export). The name and concept have been popularized by the ES2015 module bundler [rollup](https://github.com/rollup/rollup).
18
18
19
-
webpack 2 comes with a built-in support for ES2015 modules (alias *harmony modules*) as well as unused module export detection.
19
+
The webpack 2 release came with built-in support for ES2015 modules (alias _harmony modules_) as well as unused module export detection.
20
20
21
+
T> The remainder of this guide will stem from [Getting Started](/guides/getting-started). If you haven't read through that guide already, please do so now.
21
22
22
-
## Example
23
23
24
-
Consider a **maths.js** library file exporting two functions, `square` and `cube`:
24
+
## Add a Utility
25
25
26
-
```javascript
27
-
// This function isn't used anywhere
26
+
Let's add a new utility file to our project, `src/math.js`, that exports two functions:
27
+
28
+
__project__
29
+
30
+
```diff
31
+
webpack-demo
32
+
|- package.json
33
+
|- webpack.config.js
34
+
|- /dist
35
+
|- bundle.js
36
+
|- index.html
37
+
|- /src
38
+
|- index.js
39
+
|- math.js
40
+
|- /node_modules
41
+
```
42
+
43
+
__src/math.js__
44
+
45
+
```javascript
28
46
exportfunctionsquare(x) {
29
47
return x * x;
30
48
}
31
49
32
-
// This function gets included
33
50
exportfunctioncube(x) {
34
51
return x * x * x;
35
52
}
36
53
```
37
54
38
-
In our **main.js** we are selectively importing `cube`:
55
+
With that in place, let's update our entry script to utilize this one of these new methods:
Running `node_modules/.bin/webpack main.js dist.js` and inspecting `dist.js`reveals that `square` is not being exported (see the "unused harmony export square" comment):
77
+
Note that we __did not `import` the `square` method__ from the `src/math.js`module. That function is what's known as "dead code", meaning an unused `export` that should be dropped. Now let's run our npm script, `npm run build`, and inspect the output:
// TODO: Display contents with `unused harmony export square`...
83
+
```
60
84
61
-
// This function gets included
62
-
functioncube(x) {
63
-
return x * x * x;
64
-
}
85
+
Note the `unused harmony export square` comment above. If you look at the code below it, you'll notice that `square` is not being exported, however, it is still included in the bundle. We'll fix that in the next section.
So we've cued up our "dead code" to be dropped by using the `import` and `export` syntax, but we still need to drop it from the bundle. To do that, we'll add a minifier that supports dead code removal -- the [`UglifyJSPlugin`](/plugins/uglifyjs-webpack-plugin) -- to our configuration...
When running a [production build](/guides/production), `node_modules/.bin/webpack --optimize-minimize main.jsdist.min.js`, only the minimized version of `cube` but not `square` remains in the build:
119
+
T> Note that the `--optimize-minimize` flag can be used to insert the `UglifyJsPlugin` as well.
120
+
121
+
With that squared away, we can run another `npm run build` and see if anything has changed:
T> Note that the `--optimize-minimize` flag enables tree shaking by including the `UglifyJsPlugin` behind the scenes. Alternatively, the `UglifyJsPlugin` can be included manually in the `plugins` section of your configuration file. The plugin, combined with webpack's resolving of `import` and `export` statements, is what makes tree shaking possible. See the [production build](/guides/production) guide for more information.
129
+
Notice anything missing? The `square` function has been dropped and our output bundle is now a few bytes smaller! While that may not seem like much in this contrived example, tree shaking can yield a significant decrease in bundle size when working on larger applications with complex dependency trees.
130
+
131
+
132
+
## Conclusion
133
+
134
+
So, what we've learned is that in order to take advantage of _tree shaking_, you must...
135
+
136
+
- Use ES2015 module syntax (i.e. `import` and `export`).
137
+
- Include a minifier that supports dead code removal (e.g. the `UglifyJSPlugin`).
138
+
139
+
If you are interested in more ways to optimize your output, please jump to the next guide for details on building for [production](/guides/production).
0 commit comments