From ba3612774f1a95cc18e053df663d650417d5794c Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Fri, 21 Feb 2020 11:20:45 -0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20add=20`noSideEffects()`=20to=20`?= =?UTF-8?q?=C9=B5=C9=B5defineComponent()`=20(#35769)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- packages/core/src/render3/definition.ts | 102 ++++++++++++------------ 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/packages/core/src/render3/definition.ts b/packages/core/src/render3/definition.ts index 26ad0dbf0a..3b1215be9f 100644 --- a/packages/core/src/render3/definition.ts +++ b/packages/core/src/render3/definition.ts @@ -289,56 +289,56 @@ export function ɵɵdefineComponent(componentDefinition: { */ schemas?: SchemaMetadata[] | null; }): never { - // Initialize ngDevMode. This must be the first statement in ɵɵdefineComponent. - // See the `initNgDevMode` docstring for more information. - (typeof ngDevMode === 'undefined' || ngDevMode) && initNgDevMode(); + return noSideEffects(() => { + // Initialize ngDevMode. This must be the first statement in ɵɵdefineComponent. + // See the `initNgDevMode` docstring for more information. + (typeof ngDevMode === 'undefined' || ngDevMode) && initNgDevMode(); - const type = componentDefinition.type; - const typePrototype = type.prototype; - const declaredInputs: {[key: string]: string} = {} as any; - const def: Mutable, keyof ComponentDef> = { - type: type, - providersResolver: null, - decls: componentDefinition.decls, - vars: componentDefinition.vars, - factory: null, - template: componentDefinition.template || null !, - consts: componentDefinition.consts || null, - ngContentSelectors: componentDefinition.ngContentSelectors, - hostBindings: componentDefinition.hostBindings || null, - hostVars: componentDefinition.hostVars || 0, - hostAttrs: componentDefinition.hostAttrs || null, - contentQueries: componentDefinition.contentQueries || null, - declaredInputs: declaredInputs, - inputs: null !, // assigned in noSideEffects - outputs: null !, // assigned in noSideEffects - exportAs: componentDefinition.exportAs || null, - onChanges: null, - onInit: typePrototype.ngOnInit || null, - doCheck: typePrototype.ngDoCheck || null, - afterContentInit: typePrototype.ngAfterContentInit || null, - afterContentChecked: typePrototype.ngAfterContentChecked || null, - afterViewInit: typePrototype.ngAfterViewInit || null, - afterViewChecked: typePrototype.ngAfterViewChecked || null, - onDestroy: typePrototype.ngOnDestroy || null, - onPush: componentDefinition.changeDetection === ChangeDetectionStrategy.OnPush, - directiveDefs: null !, // assigned in noSideEffects - pipeDefs: null !, // assigned in noSideEffects - selectors: componentDefinition.selectors || EMPTY_ARRAY, - viewQuery: componentDefinition.viewQuery || null, - features: componentDefinition.features as DirectiveDefFeature[] || null, - data: componentDefinition.data || {}, - // TODO(misko): convert ViewEncapsulation into const enum so that it can be used directly in the - // next line. Also `None` should be 0 not 2. - encapsulation: componentDefinition.encapsulation || ViewEncapsulation.Emulated, - id: 'c', - styles: componentDefinition.styles || EMPTY_ARRAY, - _: null as never, - setInput: null, - schemas: componentDefinition.schemas || null, - tView: null, - }; - def._ = noSideEffects(() => { + const type = componentDefinition.type; + const typePrototype = type.prototype; + const declaredInputs: {[key: string]: string} = {} as any; + const def: Mutable, keyof ComponentDef> = { + type: type, + providersResolver: null, + decls: componentDefinition.decls, + vars: componentDefinition.vars, + factory: null, + template: componentDefinition.template || null !, + consts: componentDefinition.consts || null, + ngContentSelectors: componentDefinition.ngContentSelectors, + hostBindings: componentDefinition.hostBindings || null, + hostVars: componentDefinition.hostVars || 0, + hostAttrs: componentDefinition.hostAttrs || null, + contentQueries: componentDefinition.contentQueries || null, + declaredInputs: declaredInputs, + inputs: null !, // assigned in noSideEffects + outputs: null !, // assigned in noSideEffects + exportAs: componentDefinition.exportAs || null, + onChanges: null, + onInit: typePrototype.ngOnInit || null, + doCheck: typePrototype.ngDoCheck || null, + afterContentInit: typePrototype.ngAfterContentInit || null, + afterContentChecked: typePrototype.ngAfterContentChecked || null, + afterViewInit: typePrototype.ngAfterViewInit || null, + afterViewChecked: typePrototype.ngAfterViewChecked || null, + onDestroy: typePrototype.ngOnDestroy || null, + onPush: componentDefinition.changeDetection === ChangeDetectionStrategy.OnPush, + directiveDefs: null !, // assigned in noSideEffects + pipeDefs: null !, // assigned in noSideEffects + selectors: componentDefinition.selectors || EMPTY_ARRAY, + viewQuery: componentDefinition.viewQuery || null, + features: componentDefinition.features as DirectiveDefFeature[] || null, + data: componentDefinition.data || {}, + // TODO(misko): convert ViewEncapsulation into const enum so that it can be used directly in + // the next line. Also `None` should be 0 not 2. + encapsulation: componentDefinition.encapsulation || ViewEncapsulation.Emulated, + id: 'c', + styles: componentDefinition.styles || EMPTY_ARRAY, + _: null as never, + setInput: null, + schemas: componentDefinition.schemas || null, + tView: null, + }; const directiveTypes = componentDefinition.directives !; const feature = componentDefinition.features; const pipeTypes = componentDefinition.pipes !; @@ -353,9 +353,9 @@ export function ɵɵdefineComponent(componentDefinition: { def.pipeDefs = pipeTypes ? () => (typeof pipeTypes === 'function' ? pipeTypes() : pipeTypes).map(extractPipeDef) : null; - }) as never; - return def as never; + return def as never; + }); } /**