Skip to content

Commit e88a9c6

Browse files
AndrewKushnirthePunderWoman
authored andcommitted
perf(forms): make RadioControlRegistry class tree-shakable (angular#41126)
This commit makes the `RadioControlRegistry` class tree-shakable by adding the `providedIn` property to its `@Injectable` decorator. Now if the radio buttons are not used in the app (thus no `RadioControlValueAccessor` directive is initialized), the `RadioControlRegistry` should not be included into application's prod bundle. PR Close angular#41126
1 parent b93fb79 commit e88a9c6

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

goldens/public-api/forms/forms.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ export declare class RadioControlValueAccessor extends ɵangular_packages_forms_
477477
onChange: () => void;
478478
onTouched: () => void;
479479
value: any;
480-
constructor(_renderer: Renderer2, _elementRef: ElementRef, _registry: ɵangular_packages_forms_forms_o, _injector: Injector);
480+
constructor(_renderer: Renderer2, _elementRef: ElementRef, _registry: ɵangular_packages_forms_forms_p, _injector: Injector);
481481
fireUncheck(value: any): void;
482482
ngOnDestroy(): void;
483483
ngOnInit(): void;

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@
450450
"name": "R3ViewContainerRef"
451451
},
452452
{
453-
"name": "RadioControlRegistry"
453+
"name": "RadioControlRegistryModule"
454454
},
455455
{
456456
"name": "ReactiveFormsComponent"

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@
444444
"name": "REQUIRED_VALIDATOR"
445445
},
446446
{
447-
"name": "RadioControlRegistry"
447+
"name": "RadioControlRegistryModule"
448448
},
449449
{
450450
"name": "RecordViewTuple"

packages/forms/src/directives.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {NgModel} from './directives/ng_model';
1616
import {NgModelGroup} from './directives/ng_model_group';
1717
import {NgNoValidate} from './directives/ng_no_validate_directive';
1818
import {NumberValueAccessor} from './directives/number_value_accessor';
19-
import {RadioControlValueAccessor} from './directives/radio_control_value_accessor';
19+
import {RadioControlRegistryModule, RadioControlValueAccessor} from './directives/radio_control_value_accessor';
2020
import {RangeValueAccessor} from './directives/range_value_accessor';
2121
import {FormControlDirective} from './directives/reactive_directives/form_control_directive';
2222
import {FormControlName} from './directives/reactive_directives/form_control_name';
@@ -77,6 +77,7 @@ export const REACTIVE_DRIVEN_DIRECTIVES: Type<any>[] =
7777
*/
7878
@NgModule({
7979
declarations: SHARED_FORM_DIRECTIVES,
80+
imports: [RadioControlRegistryModule],
8081
exports: SHARED_FORM_DIRECTIVES,
8182
})
8283
export class ɵInternalFormsSharedModule {

packages/forms/src/directives/radio_control_value_accessor.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, ElementRef, forwardRef, Injectable, Injector, Input, OnDestroy, OnInit, Renderer2} from '@angular/core';
9+
import {Directive, ElementRef, forwardRef, Injectable, Injector, Input, NgModule, OnDestroy, OnInit, Renderer2} from '@angular/core';
1010

1111
import {BuiltInControlValueAccessor, ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
1212
import {NgControl} from './ng_control';
@@ -24,11 +24,21 @@ function throwNameError() {
2424
`);
2525
}
2626

27+
/**
28+
* Internal-only NgModule that works as a host for the `RadioControlRegistry` tree-shakable
29+
* provider. Note: the `InternalFormsSharedModule` can not be used here directly, since it's
30+
* declared *after* the `RadioControlRegistry` class and the `providedIn` doesn't support
31+
* `forwardRef` logic.
32+
*/
33+
@NgModule()
34+
export class RadioControlRegistryModule {
35+
}
36+
2737
/**
2838
* @description
2939
* Class used by Angular to track radio buttons. For internal use only.
3040
*/
31-
@Injectable()
41+
@Injectable({providedIn: RadioControlRegistryModule})
3242
export class RadioControlRegistry {
3343
private _accessors: any[] = [];
3444

packages/forms/src/form_providers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99
import {ModuleWithProviders, NgModule} from '@angular/core';
1010

1111
import {InternalFormsSharedModule, NG_MODEL_WITH_FORM_CONTROL_WARNING, REACTIVE_DRIVEN_DIRECTIVES, TEMPLATE_DRIVEN_DIRECTIVES} from './directives';
12-
import {RadioControlRegistry} from './directives/radio_control_value_accessor';
1312

1413
/**
1514
* Exports the required providers and directives for template-driven forms,
1615
* making them available for import by NgModules that import this module.
1716
*
17+
* Providers associated with this module:
18+
* * `RadioControlRegistry`
19+
*
1820
* @see [Forms Overview](/guide/forms-overview)
1921
* @see [Template-driven Forms Guide](/guide/forms)
2022
*
2123
* @publicApi
2224
*/
2325
@NgModule({
2426
declarations: TEMPLATE_DRIVEN_DIRECTIVES,
25-
providers: [RadioControlRegistry],
2627
exports: [InternalFormsSharedModule, TEMPLATE_DRIVEN_DIRECTIVES]
2728
})
2829
export class FormsModule {
@@ -32,16 +33,17 @@ export class FormsModule {
3233
* Exports the required infrastructure and directives for reactive forms,
3334
* making them available for import by NgModules that import this module.
3435
*
36+
* Providers associated with this module:
37+
* * `FormBuilder`
38+
* * `RadioControlRegistry`
39+
*
3540
* @see [Forms Overview](guide/forms-overview)
3641
* @see [Reactive Forms Guide](guide/reactive-forms)
3742
*
3843
* @publicApi
3944
*/
4045
@NgModule({
4146
declarations: [REACTIVE_DRIVEN_DIRECTIVES],
42-
// Note: FormBuilder is also provided in this module as a tree-shakable provider,
43-
// see packages/forms/src/form_builder.ts.
44-
providers: [RadioControlRegistry],
4547
exports: [InternalFormsSharedModule, REACTIVE_DRIVEN_DIRECTIVES]
4648
})
4749
export class ReactiveFormsModule {

0 commit comments

Comments
 (0)