Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Tree Shaking and Why Would I Need It?

I've started learning about Angular 2 and have come across this term "tree shaking" and I haven't been able to find any good explanation of it from a beginners' perspective.

I have two questions here:

  1. What is tree shaking and why would I need it?
  2. How do I use it?
like image 924
TrevorBrooks Avatar asked Aug 25 '17 15:08

TrevorBrooks


People also ask

Does tree shaking remove unused imports?

Tree shaking dynamic importsDynamic imports resolve the entire module — with its default and named exports — without tree shaking unused imports.

How do you implement tree shaking?

In order to implement tree shaking in react application you will need to have a module bundler that will bundle the entire app's code. You can either use WebPack or RollUp for bundling your application.

What is tree shaking in WebPack?

Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module syntax, i.e. import and export . The name and concept have been popularized by the ES2015 module bundler rollup.

What is tree shaking react?

What is tree shaking and why? Tree shaking is a process that bundlers like webpack and rollup go through to remove unused code. Tree shaking means that only components from our library used in the app are included in the app bundle. If not tree shaken, all the components will be included even if they are not used.


1 Answers

I see you have three questions here; 1. What is tree shaking? 2. What's the need of it? 3. And, how do you use it?

1. What's tree shaking?

Tree shaking refers to dead code elimination. It means that unused modules will not be included in the bundle during the build process.

When we import and export modules in JavaScript, most of the time there is unused code floating around. Excluding that unused code (also referred as dead code) is called tree shaking.

Utilizing the tree shaking and dead code elimination can significantly reduce the code size we have in our application. The less code we send over the wire the more performant the application will be.

enter image description here

enter image description here

2. What's the need of tree shaking?

Tree Shaking helps us to reduce the weight of the application. For example, if we just want to create a “Hello World” Application in AngularJs 2 then it will take around 2.5MB, but by tree shaking we can bring down the size to just few hundred KBs, or maybe a few MBs.

3. How to use / implement tree shaking?

Tools like webpack will detect dead code and mark it as “unused module” but it won’t remove the code. Webpack relies on minifiers to cleanup dead code, one of them is UglifyJS plugin, which will eliminate the dead code from the bundle.

// modules.js export function drive(props) {  return props.gas }  export function fly(props) {  return props.miles  }  // main.js import { drive } from modules;  /// some code eventHandler = (event) => {   event.preventDefault()   drive({ gas: event.target.value }) } /// some code  // fly() was never importent and won't be included in our bundle 

It only works with import and export. It won’t work with CommonJS require syntax.

Same applies to npm dependencies. great example is lodash, just import pick from 'lodash/pick' and your bundle will only include one small module instead of entire lodash library.

like image 133
Aaditya Sharma Avatar answered Sep 21 '22 22:09

Aaditya Sharma