angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.77k stars 11.97k forks source link

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

Closed damyanpetev closed 5 months ago

damyanpetev commented 5 months ago

Command

build

Is this a regression?

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 https://github.com/IgniteUI/igniteui-angular/pull/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.

angular-automatic-lock-bot[bot] commented 4 months ago

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.