Skip to content

Library tree-shaking doesn't seem to work with 18 RC #27662

@damyanpetev

Description

@damyanpetev

Command

build

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

17

Description

When updating to Angular 18 RC2 , libraries exports no longer seem to tree shake when consumed by apps

Minimal Reproduction

Originally reproduced in IgniteUI/igniteui-angular#14237

Also made an minimal repo: https://github.com/damyanpetev/ng17updateTo18 (see history with each command as commit)
Repo contains just a new project w/ ng new using the current version of @angular/cli@17 & updated 18 RC. Tried both options where the lib is added first and then updated https://github.com/damyanpetev/ng17updateTo18/commits/lib-17-update18-alt, no diff in result.
There are two components, the default from the library schematic and one extra with Lorem ipsum text that's only exported. The app uses just the default.

In the repo run:

  1. ng build my-lib
  2. ng build --configuration production --verbose
  3. Inspect the dist main bundle and look for the Lorem Ipsum text

In this case the second component gets bundled as well, even though it's never used/referenced in any way. For comparison run the same steps in the same state but with v17 in this branch https://github.com/damyanpetev/ng17updateTo18/tree/lib-17 and the second component is no longer included in the bundle.

Exception or Error

No response

Your Environment

Angular CLI: 18.0.0-rc.2
Node: 20.13.1
Package Manager: npm 10.5.2
OS: linux x64

Angular: 18.0.0-rc.2
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.0-rc.2
@angular-devkit/build-angular   18.0.0-rc.2
@angular-devkit/core            18.0.0-rc.2
@angular-devkit/schematics      18.0.0-rc.2
@schematics/angular             18.0.0-rc.2
ng-packagr                      18.0.0-rc.0
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.6

Anything else relevant?

Interestingly, when switching between branches if the app is built with the dist produced by the 17 branch it tree-shakes correctly, which leads me to believe that the issue is with the library build itself, so @angular-devkit/build-angular:ng-packagr as a starting point.
Only noticed one meaningful diff in the FESM build - declared classes now are assigned to vars that are exported, instead of direct exports:
image
Not sure what caused it and for what reason, but it does fix the issue if I manually edit those back to the old class definitions.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions