fix(compiler): add PURE annotation to getInheritedFactory calls (#38291)

Currently the `getInheritedFactory` function is implemented to allow
closure to remove the call if the base factory is unused.  However, this
method does not work with terser.  By adding the PURE annotation,
terser will also be able to remove the call when unused.

PR Close #38291
This commit is contained in:
Charles Lyding 2020-07-29 15:31:27 -04:00 committed by Alex Rickabaugh
parent 4c7f233fa8
commit 6f6102d8ad
4 changed files with 12 additions and 7 deletions

View File

@ -104,10 +104,10 @@ assertSucceeded "Expected 'ngcc' to log 'Compiling'."
# Did it generate a base factory call for synthesized constructors correctly?
grep "const ɵMatTable_BaseFactory = ɵngcc0.ɵɵgetInheritedFactory(MatTable);" node_modules/@angular/material/esm2015/table/table.js
grep "const ɵMatTable_BaseFactory = /\*@__PURE__\*/ ɵngcc0.ɵɵgetInheritedFactory(MatTable);" node_modules/@angular/material/esm2015/table/table.js
assertSucceeded "Expected 'ngcc' to generate a base factory for 'MatTable' in '@angular/material' (esm2015)."
grep "var ɵMatTable_BaseFactory = ɵngcc0.ɵɵgetInheritedFactory(MatTable);" node_modules/@angular/material/esm5/table/table.js
grep "var ɵMatTable_BaseFactory = /\*@__PURE__\*/ ɵngcc0.ɵɵgetInheritedFactory(MatTable);" node_modules/@angular/material/esm5/table/table.js
assertSucceeded "Expected 'ngcc' to generate a base factory for 'MatTable' in '@angular/material' (esm5)."

View File

@ -3822,7 +3822,8 @@ runInEachFileSystem(os => {
expect(jsContents)
.toContain('function Base_Factory(t) { return new (t || Base)(i0.ɵɵinject(Dep)); }');
expect(jsContents).toContain('var \u0275Child_BaseFactory = i0.ɵɵgetInheritedFactory(Child)');
expect(jsContents)
.toContain('var \u0275Child_BaseFactory = /*@__PURE__*/ i0.ɵɵgetInheritedFactory(Child)');
expect(jsContents)
.toContain('function Child_Factory(t) { return \u0275Child_BaseFactory(t || Child); }');
expect(jsContents)
@ -3849,7 +3850,8 @@ runInEachFileSystem(os => {
env.driveMain();
const jsContents = env.getContents('test.js');
expect(jsContents).toContain('var \u0275Dir_BaseFactory = i0.ɵɵgetInheritedFactory(Dir)');
expect(jsContents)
.toContain('var \u0275Dir_BaseFactory = /*@__PURE__*/ i0.ɵɵgetInheritedFactory(Dir)');
});
it('should wrap "directives" in component metadata in a closure when forward references are present',

View File

@ -176,8 +176,9 @@ export abstract class Expression {
return new InvokeMethodExpr(this, name, params, null, sourceSpan);
}
callFn(params: Expression[], sourceSpan?: ParseSourceSpan|null): InvokeFunctionExpr {
return new InvokeFunctionExpr(this, params, null, sourceSpan);
callFn(params: Expression[], sourceSpan?: ParseSourceSpan|null, pure?: boolean):
InvokeFunctionExpr {
return new InvokeFunctionExpr(this, params, null, sourceSpan, pure);
}
instantiate(params: Expression[], type?: Type|null, sourceSpan?: ParseSourceSpan|null):

View File

@ -212,7 +212,9 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3FactoryFn {
const baseFactory = o.variable(`ɵ${meta.name}_BaseFactory`);
const getInheritedFactory = o.importExpr(R3.getInheritedFactory);
const baseFactoryStmt =
baseFactory.set(getInheritedFactory.callFn([meta.internalType]))
baseFactory
.set(getInheritedFactory.callFn(
[meta.internalType], /* sourceSpan */ undefined, /* pure */ true))
.toDeclStmt(o.INFERRED_TYPE, [o.StmtModifier.Exported, o.StmtModifier.Final]);
statements.push(baseFactoryStmt);