perf(forms): make `FormBuilder` class tree-shakable (#41126)

This commit makes the `FormBuilder` class tree-shakable by adding the `providedIn` property to its `@Injectable`
decorator. Now if the `FormBuilder` class is not referenced in application's code, it should not be included into
its production bundle.

PR Close #41126
This commit is contained in:
Andrew Kushnir 2021-03-08 17:51:26 -08:00 committed by Jessica Janiuk
parent 0093b3b19f
commit b93fb79839
2 changed files with 5 additions and 3 deletions

View File

@ -9,6 +9,7 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {AsyncValidatorFn, ValidatorFn} from './directives/validators'; import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
import {ReactiveFormsModule} from './form_providers';
import {AbstractControl, AbstractControlOptions, FormArray, FormControl, FormGroup, FormHooks} from './model'; import {AbstractControl, AbstractControlOptions, FormArray, FormControl, FormGroup, FormHooks} from './model';
function isAbstractControlOptions(options: AbstractControlOptions| function isAbstractControlOptions(options: AbstractControlOptions|
@ -30,7 +31,7 @@ function isAbstractControlOptions(options: AbstractControlOptions|
* *
* @publicApi * @publicApi
*/ */
@Injectable() @Injectable({providedIn: ReactiveFormsModule})
export class FormBuilder { export class FormBuilder {
/** /**
* @description * @description

View File

@ -10,7 +10,6 @@ import {ModuleWithProviders, NgModule} from '@angular/core';
import {InternalFormsSharedModule, NG_MODEL_WITH_FORM_CONTROL_WARNING, REACTIVE_DRIVEN_DIRECTIVES, TEMPLATE_DRIVEN_DIRECTIVES} from './directives'; import {InternalFormsSharedModule, NG_MODEL_WITH_FORM_CONTROL_WARNING, REACTIVE_DRIVEN_DIRECTIVES, TEMPLATE_DRIVEN_DIRECTIVES} from './directives';
import {RadioControlRegistry} from './directives/radio_control_value_accessor'; import {RadioControlRegistry} from './directives/radio_control_value_accessor';
import {FormBuilder} from './form_builder';
/** /**
* Exports the required providers and directives for template-driven forms, * Exports the required providers and directives for template-driven forms,
@ -40,7 +39,9 @@ export class FormsModule {
*/ */
@NgModule({ @NgModule({
declarations: [REACTIVE_DRIVEN_DIRECTIVES], declarations: [REACTIVE_DRIVEN_DIRECTIVES],
providers: [FormBuilder, RadioControlRegistry], // Note: FormBuilder is also provided in this module as a tree-shakable provider,
// see packages/forms/src/form_builder.ts.
providers: [RadioControlRegistry],
exports: [InternalFormsSharedModule, REACTIVE_DRIVEN_DIRECTIVES] exports: [InternalFormsSharedModule, REACTIVE_DRIVEN_DIRECTIVES]
}) })
export class ReactiveFormsModule { export class ReactiveFormsModule {