fix(core): add `noSideEffects()` to `ɵɵdefineComponent()` (#35769)

This marks the function are "pure" and eligible to be tree shaken by Closure. Without this, initializing `ngDevMode` is considered a side effect which prevents this function from being tree shaken and also any component which calls it.

PR Close #35769
This commit is contained in:
Doug Parker 2020-02-21 11:20:45 -08:00 committed by atscott
parent dc6a7918e3
commit ba3612774f
1 changed files with 51 additions and 51 deletions

View File

@ -289,56 +289,56 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
*/ */
schemas?: SchemaMetadata[] | null; schemas?: SchemaMetadata[] | null;
}): never { }): never {
// Initialize ngDevMode. This must be the first statement in ɵɵdefineComponent. return noSideEffects(() => {
// See the `initNgDevMode` docstring for more information. // Initialize ngDevMode. This must be the first statement in ɵɵdefineComponent.
(typeof ngDevMode === 'undefined' || ngDevMode) && initNgDevMode(); // See the `initNgDevMode` docstring for more information.
(typeof ngDevMode === 'undefined' || ngDevMode) && initNgDevMode();
const type = componentDefinition.type; const type = componentDefinition.type;
const typePrototype = type.prototype; const typePrototype = type.prototype;
const declaredInputs: {[key: string]: string} = {} as any; const declaredInputs: {[key: string]: string} = {} as any;
const def: Mutable<ComponentDef<any>, keyof ComponentDef<any>> = { const def: Mutable<ComponentDef<any>, keyof ComponentDef<any>> = {
type: type, type: type,
providersResolver: null, providersResolver: null,
decls: componentDefinition.decls, decls: componentDefinition.decls,
vars: componentDefinition.vars, vars: componentDefinition.vars,
factory: null, factory: null,
template: componentDefinition.template || null !, template: componentDefinition.template || null !,
consts: componentDefinition.consts || null, consts: componentDefinition.consts || null,
ngContentSelectors: componentDefinition.ngContentSelectors, ngContentSelectors: componentDefinition.ngContentSelectors,
hostBindings: componentDefinition.hostBindings || null, hostBindings: componentDefinition.hostBindings || null,
hostVars: componentDefinition.hostVars || 0, hostVars: componentDefinition.hostVars || 0,
hostAttrs: componentDefinition.hostAttrs || null, hostAttrs: componentDefinition.hostAttrs || null,
contentQueries: componentDefinition.contentQueries || null, contentQueries: componentDefinition.contentQueries || null,
declaredInputs: declaredInputs, declaredInputs: declaredInputs,
inputs: null !, // assigned in noSideEffects inputs: null !, // assigned in noSideEffects
outputs: null !, // assigned in noSideEffects outputs: null !, // assigned in noSideEffects
exportAs: componentDefinition.exportAs || null, exportAs: componentDefinition.exportAs || null,
onChanges: null, onChanges: null,
onInit: typePrototype.ngOnInit || null, onInit: typePrototype.ngOnInit || null,
doCheck: typePrototype.ngDoCheck || null, doCheck: typePrototype.ngDoCheck || null,
afterContentInit: typePrototype.ngAfterContentInit || null, afterContentInit: typePrototype.ngAfterContentInit || null,
afterContentChecked: typePrototype.ngAfterContentChecked || null, afterContentChecked: typePrototype.ngAfterContentChecked || null,
afterViewInit: typePrototype.ngAfterViewInit || null, afterViewInit: typePrototype.ngAfterViewInit || null,
afterViewChecked: typePrototype.ngAfterViewChecked || null, afterViewChecked: typePrototype.ngAfterViewChecked || null,
onDestroy: typePrototype.ngOnDestroy || null, onDestroy: typePrototype.ngOnDestroy || null,
onPush: componentDefinition.changeDetection === ChangeDetectionStrategy.OnPush, onPush: componentDefinition.changeDetection === ChangeDetectionStrategy.OnPush,
directiveDefs: null !, // assigned in noSideEffects directiveDefs: null !, // assigned in noSideEffects
pipeDefs: null !, // assigned in noSideEffects pipeDefs: null !, // assigned in noSideEffects
selectors: componentDefinition.selectors || EMPTY_ARRAY, selectors: componentDefinition.selectors || EMPTY_ARRAY,
viewQuery: componentDefinition.viewQuery || null, viewQuery: componentDefinition.viewQuery || null,
features: componentDefinition.features as DirectiveDefFeature[] || null, features: componentDefinition.features as DirectiveDefFeature[] || null,
data: componentDefinition.data || {}, data: componentDefinition.data || {},
// TODO(misko): convert ViewEncapsulation into const enum so that it can be used directly in the // TODO(misko): convert ViewEncapsulation into const enum so that it can be used directly in
// next line. Also `None` should be 0 not 2. // the next line. Also `None` should be 0 not 2.
encapsulation: componentDefinition.encapsulation || ViewEncapsulation.Emulated, encapsulation: componentDefinition.encapsulation || ViewEncapsulation.Emulated,
id: 'c', id: 'c',
styles: componentDefinition.styles || EMPTY_ARRAY, styles: componentDefinition.styles || EMPTY_ARRAY,
_: null as never, _: null as never,
setInput: null, setInput: null,
schemas: componentDefinition.schemas || null, schemas: componentDefinition.schemas || null,
tView: null, tView: null,
}; };
def._ = noSideEffects(() => {
const directiveTypes = componentDefinition.directives !; const directiveTypes = componentDefinition.directives !;
const feature = componentDefinition.features; const feature = componentDefinition.features;
const pipeTypes = componentDefinition.pipes !; const pipeTypes = componentDefinition.pipes !;
@ -353,9 +353,9 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
def.pipeDefs = pipeTypes ? def.pipeDefs = pipeTypes ?
() => (typeof pipeTypes === 'function' ? pipeTypes() : pipeTypes).map(extractPipeDef) : () => (typeof pipeTypes === 'function' ? pipeTypes() : pipeTypes).map(extractPipeDef) :
null; null;
}) as never;
return def as never; return def as never;
});
} }
/** /**