feat: provide and use lightweight tokens for optimized treeshaking#19736
Merged
feat: provide and use lightweight tokens for optimized treeshaking#19736
Conversation
The Angular Material `MatRadioButton` component currently optionally injects the `MatRadioGroup`. This causes the `MatRadioGroup` implementation to be always retained in application bundles. This is problematic as the use of radio groups is not always required. i.e. it's valid to use a standalone radio button. In those cases, we do not want to retain the radio group implementation unnecessarily. This has always been an issue, but the issue became more significant in Ivy where component factory and definitions are attached directly to the `MatRadioGroup` class (resulting in a more significant size increase). We fix this by using a lightweight token for injecting the parent radio group optionally. This solves the retention issue if a standalone radio button is used. Related to: #19576.
Angular Material supports lazy content for menus. It does this by querying for the `MatMenuContent` directive in the `MatMenu` component. This is problematic though as it always causes `MatMenuContent` to be retained as its used as actual query predicate token. To solve this, we use a lightweight injection token for querying the lazy content (if present). This optimizes applications using the Angular Material menu without lazy content. Related to: #19576.
The date range picker could be used outside the `MatFormField` component in practice. Right now though, the date-picker input always has a hard-reference on the `MatFormField` component, causing it to be retained. This would be the same in View Engine, but with Ivy this signifies a larger size issue as factories/definitions are directly attached to the component. To allow for better tree-shaking / optimizations, we use the form-field light-weight injection token, so that the date range picker can be used outside of a form-field with minimal cost. Related to: #19576.
The `MatButtonToggleGroup` component is not always used in applications. e.g. it's an optional part, and standalone button toggles are totally valid. Currently though, we always reference the `MatButtonToggleGroup` component directly as a value, and it will be retained in applications regardless of the usage. We can improve this by providing a light-weight injection token for the button-toggle group that we can then use DI injection to avoid full retention of the class. This obviously was already an issue in View Engine too, but it became significantly worse in Ivy as factories are now directly attached to the component class. Related to: #19576.
The `MatOptgroup` component is not always used in applications. e.g. it's an optional part of the autocomplete or select. Currently though, we always reference the `MatOpgroup` component directly as a value, and it will be retained in applications regardless of the usage. We can improve this by providing a light-weight injection token for the optgroup that we can then use in queries or optional DI injection. This obviously was already an issue in View Engine too, but it became significantly worse in Ivy as factories are now directly attached to the component class. Related to: #19576.
Currently the `CdkDropList` directive always retains the `CdkDropListGroup` directive. This is not desired as the drop list group is not necessarily always used in combination with a drop list. Additionally, the `CdkDrag` directive always retains the `CdkDragHandle`, `CdkDragPlaceholder` and `CdkDragPreview` directives. This is neither desired because a handle, placeholder or preview can be consumed optionally. We should only bring those directives into applications when actually needed. Size measurements show that this saves around ~1.3kb if only a drop list and `cdkDrag` is used. The downside is that we add up ~200b if _all_ directives of the drag-drop module are used. Given the drag-drop has a size of around 45kb, the 1.3kb size improvement in the common case should be good (~3% reduction). Related to #19576.
The `CdkAccordionItem` currently always brings in the `CdkAccordion` class. This is not desired as an accordion item could be used outside of a `<cdk-accordion>`. In those cases, we do not want to retain the accordion class w/ metadata unnecessarily. This also affects the `MatExpansionPanel` as it extends from the `CdkAccordionItem` and therefore always brings in the `CdkAccordion` list unnecessarily right now. Related to #19576.
The size tests automatically will include a bootstrap module call. The boostrap calls in those test files are noop and will be removed by rollup as build optimizer considers it as a side-effect free call that is unused.
Sorts the integration size-test golden lexicographically. This should make the golden more readable.
…icon (#19693) * build: add size test for basic chip-list with chip * refactor: use lightweight token for chip avatar, remove and trailing icon Currently `MatChip` always causes `MatChipAvatar`, `MatChipRemove` and `MatChipTrailingIcon` to be retained. These are optional parts of a chip and shouldn't be retained always. The retention of these classes wasn't noticeable in View Engine as no factories were generated for directives. With Ivy though, factories and definitions are generated for these directives and a size increase is noticeable. We should use lightweight tokens for querying of those directives. That will improve tree-shaking for the case where not all of these parts are used. If all parts are used, the app will slightly increase due to the provided injection tokens, but that is of average 150b, while the savings are of ~800b in the common case. Related to: #19576
Currently the `MatFormField` always retains `MatHint`, `MatError`, `MatPrefix` and `MatSuffix`. These are optional parts of a form-field so we ideally would not retain those always. We can achieve this by using lightweight injection tokens for querying of these directives, instead of using the actual implementations as query predicate token. The retention was already an issue in View Engine too, but the issue became more noticable in Ivy where factories and definitions are directly attached to the classes.
The Angular Material tab group currently always retains `MatTabContent` and `MatTabLabel`. These are optional features of the tabs module and should only be retained when needed. To achieve this, we use lightweight tokens instead of the actual implementations when querying for a lazy content or label. This signifies an improvement of ~500b in the common case, and an increase (due to the lightweight tokens being provided) in the advanced case of ~140b. Related to #19576.
The select size test has slightly shifted already due to the recent Angular update. We are updating the golden here so that we can see the lightweight token effect in the followed commit for select.
The Angular Material always retains the `MatSelectTrigger` class. This is not ideal as this is an optional part of the select. The class itself is not large enough to contribute to any noticeable size impact, even with Ivy where factories/definitions are directly attached, but for the sake of completeness, and because custom triggers are assumed to be uncommon, a lightweight token is used for a small micro-optimization in the common case. Related to #19576.
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merges the
lightweight-token-optimizationsfeature branch intomaster.Provides and uses lightweight tokens for optimized treeshaking in components that were causing
unnecessary class retention (resulting in bundle size increases; especially with Ivy).
More details in #19576