angular: lvy:Library build by angular-cli can not tree shaking in apps

🐞 bug report

Affected Package

@angular/cli

Is this a regression?

it can not work in ng8 also, but i try to ask if it can work in ng9 or not

Description

A library build by angular-cli, improt a module or declare a component of the library into a app, then build the app with aot, the whole library code gets into bundle, the tree shaking did not work

🔬 Minimal Reproduction

https://stackblitz.com/edit/angular-srduuh

🔥 Exception or Error

In the demo i use the library jigsaw(https://github.com/rdkmaster/jigsaw) which build by angular-cli

image

and i import the module JigsawButtonModule which just has the component JigsawButton into AppModule

image

if you build the app with this environment,you will get the whole jigsaw code in vender.js

🌍 Your Environment

Angular CLI: 9.0.0-rc.7 Node: 10.18.0 OS: win32 x64

Angular: 9.0.0-rc.7 … animations, cli, common, compiler, compiler-cli, core, forms … language-service, platform-browser, platform-browser-dynamic … router Ivy Workspace: Yes

Package Version

@angular-devkit/architect 0.900.0-rc.7 @angular-devkit/build-angular 0.900.0-rc.7 @angular-devkit/build-optimizer 0.900.0-rc.7 @angular-devkit/build-webpack 0.900.0-rc.7 @angular-devkit/core 9.0.0-rc.7 @angular-devkit/schematics 9.0.0-rc.7 @ngtools/webpack 9.0.0-rc.7 @schematics/angular 9.0.0-rc.7 @schematics/update 0.900.0-rc.7 rxjs 6.5.3 typescript 3.6.4 webpack 4.41.2

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

To elucidate, in ES2015 formats, where the class is defined via a let statement, ngcc is incorrectly taking the class expression, rather than the variable declaration statement as the node to use for inserting definitions. This should be a simple fix in EsmRenderingFormatter.addDefinitions().

--aot alone will not do any treeshaking or dead code elimination, hence the entire library will be retained.

The flags which are associated with tree-shaking and dead-code elimination are --optimization and --build-optimizer which are enabled by default under the production configuration.

To disable mangling you can use the NG_BUILD_MANGLE environment variable:

NG_BUILD_MANGLE=false ng build --prod