Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable tree-shaking for a new Angular 4 project

I just created a new Angular 4 project with the CLI: ng new test

The versions:

@angular/cli: 1.0.0
node: 6.10.0
os: win32 x64
@angular/common: 4.0.1
@angular/compiler: 4.0.1
@angular/core: 4.0.1
@angular/forms: 4.0.1
@angular/http: 4.0.1
@angular/platform-browser: 4.0.1
@angular/platform-browser-dynamic: 4.0.1
@angular/router: 4.0.1
@angular/cli: 1.0.0
@angular/compiler-cli: 4.0.1

However, tree-shaking is not working correctly, as my unused class FooBar is still in the main.*.js file.

My sample component TS file (FooBar should not be in the output):

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
}

export class FooBar {
  foo = "hello";
}

I tried to use rollup (like described in the docs) but this didn't work as well...

Is there a simple way to enable tree-shaking? (I expected it to be enabled by default when creating a project via CLI).

Update: I'm using ng build --prod and it's still not shaked..

like image 401
Rico Suter Avatar asked Apr 04 '17 10:04

Rico Suter


People also ask

Does Angular support Tree Shaking?

By default, Syncfusion Angular components supports Tree Shaking and it dose not require any special changes in application level.

How does a shaking tree work?

Tree shaking is a term commonly used within a JavaScript context to describe the removal of dead code. It relies on the import and export statements to detect if code modules are exported and imported for use between JavaScript files.

Does webpack do Tree Shaking by default?

Basic Webpack ConfigurationWebpack only does tree shaking when doing minification, which will only occur in production model. Second, you must set the optimization option “usedExports” to true. This means that Webpack will identify any code it thinks isn't being used and mark it during the initial bundling step.

How to enable tree-shaking in angular?

You can use ng build --prod to enable tree-shaking. However, the way you inject Angular services may affect tree-shaking. Read more about Angular service and tree shaking.

How to compile angular application with tree shaking in Syncfusion?

Compiling Angular Application with Tree Shaking 1 Create an Angular application with Syncfusion EJ2 Angular components as in the Getting Started documentation using... 2 Run the ng build --prod or ng serve --prod command to build or serve the application with Tree Shaking enabled. More ...

Does angular use rollup for tree shaking?

From the angular.io docs on tree shaking in the cookbook, it uses rollup for the tree shaking. But looking at the source the the CLI, there are no rollup dependencies in the package.json. Unless they use something else for tree shaking.

How do I enable minification and tree shaking?

Use the production mode configuration option to enable various optimizations including minification and tree shaking. 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.


1 Answers

Update: From the angular-cli Wiki:

All builds make use of bundling and limited tree-shaking, while --prod builds also run limited dead code elimination via UglifyJS.

See below, too.

The Ahead-of-Time (AOT) Compiler

An --prod build defaults to --aot=true now; it has for a while.

There aren't any docs that I have found on the angular.io site that offers a great amount of detail on the exact process of tree-shaking. The Angular CLI has been using webpack for some time now, and there is some information on how that tool does tree-shaking here. UglifyJS seems to be a common theme.

As long as you follow the guidelines in the link above about AOT, you should have good results. If you have difficulty doing an AOT compilation with 3rd party libraries, there is always the risk that the library wasn't written to support AOT. Although, AOT compatibility is expected nowadays.

like image 74
R. Richards Avatar answered Sep 21 '22 13:09

R. Richards