From c195d22f686d9d393c9395537ba5f1dbcfae34fa Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Fri, 21 Feb 2020 16:20:16 -0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20remove=20side=20effects=20from=20`?= =?UTF-8?q?=C9=B5=C9=B5getInheritedFactory()`=20(#35769)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ɵɵgetInheritedFactory()` is called from generated code for a component which extends another class. This function is detected by Closure to have a side effect and is not able to tree shake the component as a result. Marking it with `noSideEffects()` tells Closure it can remove this function under the relevant tree shaking conditions. PR Close #35769 --- integration/_payload-limits.json | 2 +- packages/core/src/render3/di.ts | 25 +++++++++++-------- .../cyclic_import/bundle.golden_symbols.json | 3 +++ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/integration/_payload-limits.json b/integration/_payload-limits.json index 8441e070c9..21cb9fbcb1 100644 --- a/integration/_payload-limits.json +++ b/integration/_payload-limits.json @@ -60,7 +60,7 @@ "uncompressed": { "bundle": "TODO(i): temporarily increase the payload size limit from 105779 - this is due to a closure issue related to ESM reexports that still needs to be investigated", "bundle": "TODO(i): we should define ngDevMode to false in Closure, but --define only works in the global scope.", - "bundle": 175498 + "bundle": 170618 } } } diff --git a/packages/core/src/render3/di.ts b/packages/core/src/render3/di.ts index 72dd5d85c8..c70505a2d1 100644 --- a/packages/core/src/render3/di.ts +++ b/packages/core/src/render3/di.ts @@ -14,6 +14,7 @@ import {getInjectorDef} from '../di/interface/defs'; import {InjectFlags} from '../di/interface/injector'; import {Type} from '../interface/type'; import {assertDefined, assertEqual} from '../util/assert'; +import {noSideEffects} from '../util/closure'; import {assertDirectiveDef} from './assert'; import {getFactoryDef} from './definition'; @@ -655,15 +656,17 @@ export function ɵɵgetFactoryOf(type: Type): FactoryFn|null { * @codeGenApi */ export function ɵɵgetInheritedFactory(type: Type): (type: Type) => T { - const proto = Object.getPrototypeOf(type.prototype).constructor as Type; - const factory = (proto as any)[NG_FACTORY_DEF] || ɵɵgetFactoryOf(proto); - if (factory !== null) { - return factory; - } else { - // There is no factory defined. Either this was improper usage of inheritance - // (no Angular decorator on the superclass) or there is no constructor at all - // in the inheritance chain. Since the two cases cannot be distinguished, the - // latter has to be assumed. - return (t) => new t(); - } + return noSideEffects(() => { + const proto = Object.getPrototypeOf(type.prototype).constructor as Type; + const factory = (proto as any)[NG_FACTORY_DEF] || ɵɵgetFactoryOf(proto); + if (factory !== null) { + return factory; + } else { + // There is no factory defined. Either this was improper usage of inheritance + // (no Angular decorator on the superclass) or there is no constructor at all + // in the inheritance chain. Since the two cases cannot be distinguished, the + // latter has to be assumed. + return (t) => new t(); + } + }); } diff --git a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json index facce6d3a7..7470bbe21e 100644 --- a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json +++ b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json @@ -167,6 +167,9 @@ { "name": "attachPatchData" }, + { + "name": "autoRegisterModuleById" + }, { "name": "baseResolveDirective" },