-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ideas for better tree-shaking of the Forms package #41011
Comments
Instead of referencing it directly here:
|
Improving tree shaking and reducing the bundle size of default blank app generated by CLI will allow more users to take advantage of Angular. At present we have some embedded apps built with Vue because we can't fit in Angular. And I would like in the near future to use Angular for all our apps and be able to use the same libraries, styles, project structure and coding standards we already have and use in our current Angular apps. |
Just want to mention a couple more things that might contribute to the payload size reduction (not necessarily related to tree-shaking), mostly micro-optimizations though (i.e. likely without massive size reductions), but still worth looking at.
Currently all CVAs implement the angular/packages/forms/src/directives/radio_control_value_accessor.ts Lines 205 to 207 in e12d9de
which is in fact duplicated across all of them. We should consider creating a base class (for ex. angular/packages/forms/src/directives/radio_control_value_accessor.ts Lines 197 to 199 in e12d9de
One more function that can be implemented there is
to just:
Eventually, we can consider making Important note: the
The angular/packages/forms/src/directives/abstract_control_directive.ts Lines 48 to 50 in e12d9de
We can refactor it by creating a tiny function to call There might be more places where we can do refactoring to reduce payload size (without sacrificing code maintainability), I will keep this thread updated and keep adding more info. Also adding a reference to issue #39640 that I created earlier, which talks about AbstractControl-based classes in a context of tree-shaking of util methods. |
Update: items 1 (Built-in CVAs), 2 (Validators class), 4 (RadioControlRegistry provider) and 5 (FormBuilder provider) from the list above were implemented and are likely to be included into the next patch release (v11.2.6). I've also investigated the We could still look into making the code a bit more compact as described in items 6 (Base class for CVAs, draft PR #41225) and 7 (AbstractControlDirective refactoring), but it'd not provide any major improvements from code size perspective. I also did a quick test just to confirm that the above-mentioned optimizations work correctly and for a simple app (that just has one input element Due to the fact that Forms logic heavily uses classes, achieving better tree-shaking without major architectural changes is not feasible at this moment (see #39640 for additional info). I'm closing this ticket since most of the work outlined here is completed. Thank you. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
While doing investigation in a context of #40997, I came across a few areas in Forms module that can be refactored to provide better tree-shaking.
IMPORTANT: I'm creating this ticket primarily to capture some ideas/proposals. The numbers that are used in the ticket are for a particular use-case only, the real result might be different based on a particular scenario.
The use-case that I've looked at was an
<input>
with anngModel
on it:<input type="text" [(ngModel)="title">
.Currently (with Angular 11.2.2) the Forms package size for such use-case is 41.81kb. I've built an app using command from #40997 (comment), so it's not a fully optimized version (with all optimizations it's 30.11kb). While doing the investigation I prototyped some of the changes to get a sense of potential savings we can get from improved tree-shaking.
angular/packages/forms/src/directives/shared.ts
Lines 312 to 323 in 6c05c80
This can be refactored to be tree-shakable by adding a special field onto these classes (for ex
ɵbuiltinCVA
) and checking if this field exists in theisBuiltInAccessor
function instead of referring to these classes directly.Performing this refactoring allows to drop 10.12kb (~25% reduction), so the Forms package reduced to 31.69kb for that use-case.
Validators
class #41189] Extract some logic from the Validators class and use them directly in the Forms code.Currently the Validators class contains some helper functions (such as
Validators.compose
,Validators.composeAsync
andValidators.nullValidator
) that are used across the code of the Forms package. We should consider extracting these functions, using them directly in the code (to avoid references to Validators class) and use these helpers in the Validators class too (to avoid any breaking changes).This optimization allows to save 3.36kb (~8% reduction), so the Forms package reduced to 28.33kb for that use-case.
FormArray
andFormGroup
in the _find function.The mentioned
_find
function is used in theAbstractControl
, so once theFormControl
is present in the code (which is used under the hood in theNgModel
directive), theFormArray
andFormGroup
become non-tree-shakable as well (even if they are not used anywhere else). Possible refactoring it to do something similar to what I mentioned in the 1st section (regarding Built-in CVAs).That can give another 7kb of savings (or 16.7% reduction), so the Forms package size is 21.3kb.
FormBuilder
andRadioControlRegistry
tree-shakable #41126] I also noticed that theRadioControlRegistry
class is non-tree-shakable (as it's directly referenced in theFormsModule
andReactiveFormsModule
). We can consider making it a tree-shakable provider (by usingprovidedIn: FormsModule
), but that might potentially be a breaking change.Making
RadioControlRegistry
tree-shakable saves a bit less than 1kb, so the final Forms package size in my experiment was 20.5kb, which is half the size of the original package (41.81kb).Note: in real scenarios savings would be much less than in my example, since most of the Forms code would actually be used (such as validators, FormGroup and FormArray, different types of inputs -> more CVAs), but improved tree-shaking should still provide benefits.
The text was updated successfully, but these errors were encountered: