Skip to content

Commit 5b364ae

Browse files
pnevaresmontogeek
authored andcommitted
docs(sideEffects): documented rule and updated tree-shaking terms (#2110)
* docs(sideEffects): documented rule and updated tree-shaking terms * Fixed merge typo
1 parent 1bc333a commit 5b364ae

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/content/configuration/module.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ contributors:
88
- jhnns
99
- dylanonelson
1010
- byzyk
11+
- pnevares
1112
---
1213

1314
These options determine how the [different types of modules](/concepts/modules) within a project will be treated.
@@ -246,6 +247,13 @@ module.exports = {
246247
An array of [`Rules`](#rule) that is also used when the Rule matches.
247248

248249

250+
## `Rule.sideEffects`
251+
252+
Possible values: `false | an array of paths`
253+
254+
Indicate what parts of the module contain side effects. See [Tree Shaking](/guides/tree-shaking/#mark-the-file-as-side-effect-free) for details.
255+
256+
249257
## `Rule.test`
250258

251259
`Rule.test` is a shortcut to `Rule.resource.test`. If you supply a `Rule.test` option, you cannot also supply a `Rule.resource`. See [`Rule.resource`](#rule-resource) and [`Condition.test`](#condition) for details.

src/content/guides/tree-shaking.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ contributors:
1212
- gish
1313
- lumo10
1414
- byzyk
15+
- pnevares
1516
related:
1617
- title: "webpack 4 beta — try it today!"
1718
url: https://medium.com/webpack/webpack-4-beta-try-it-today-6b1d27d7d7e2#9a67
@@ -23,7 +24,7 @@ related:
2324

2425
_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).
2526

26-
The webpack 2 release came with built-in support for ES2015 modules (alias _harmony modules_) as well as unused module export detection. The new webpack 4 release expands on this capability with a way to provide hints to the compiler via the `"sideEffects"` `package.json` flag to denote which files in your project are "pure" and therefore safe to prune if unused.
27+
The webpack 2 release came with built-in support for ES2015 modules (alias _harmony modules_) as well as unused module export detection. The new webpack 4 release expands on this capability with a way to provide hints to the compiler via the `"sideEffects"` `package.json` property to denote which files in your project are "pure" and therefore safe to prune if unused.
2728

2829
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.
2930

@@ -167,15 +168,13 @@ T> Note that any imported file is subject to tree shaking. This means if you use
167168
}
168169
```
169170

170-
Finally, `"sideEffects"` can also be set from the [`module.rules` config option](https://github.com/webpack/webpack/issues/6065#issuecomment-351060570).
171+
Finally, `"sideEffects"` can also be set from the [`module.rules` configuration option](/configuration/module/#module-rules).
171172

172173
## Minify the Output
173174

174-
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 use the `-p` (production) webpack compilation flag to enable the uglifyjs minification plugin.
175+
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 use the `-p` (production) webpack compilation flag to enable `UglifyJSPlugin`.
175176

176-
T> Note that the `--optimize-minimize` flag can be used to insert the `UglifyJsPlugin` as well.
177-
178-
As of webpack 4, this is also easily toggled via the `"mode"` config option, set to `"production"`.
177+
As of webpack 4, this is also easily toggled via the `"mode"` configuration option, set to `"production"`.
179178

180179
__webpack.config.js__
181180

@@ -193,6 +192,8 @@ module.exports = {
193192
};
194193
```
195194

195+
T> Note that the `--optimize-minimize` flag can be used to enable `UglifyJSPlugin` as well.
196+
196197
With that squared away, we can run another `npm run build` and see if anything has changed.
197198

198199
Notice anything different about `dist/bundle.js`? Clearly the whole bundle is now minified and mangled, but, if you look carefully, you won't see the `square` function included but will see a mangled version of the `cube` function (`function r(e){return e*e*e}n.a=r`). With minification and tree shaking our 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.
@@ -203,7 +204,7 @@ Notice anything different about `dist/bundle.js`? Clearly the whole bundle is no
203204
So, what we've learned is that in order to take advantage of _tree shaking_, you must...
204205

205206
- Use ES2015 module syntax (i.e. `import` and `export`).
206-
- Add a "sideEffects" entry to your project's `package.json` file.
207+
- Add a "sideEffects" property to your project's `package.json` file.
207208
- Include a minifier that supports dead code removal (e.g. the `UglifyJSPlugin`).
208209

209210
You can imagine your application as a tree. The source code and libraries you actually use represent the green, living leaves of the tree. Dead code represents the brown, dead leaves of the tree that are consumed by autumn. In order to get rid of the dead leaves, you have to shake the tree, causing them to fall.

0 commit comments

Comments
 (0)