refactor(ivy): do not generate providedIn: null (#34116)

We should only generate the `providedIn` property in injectable
defs if it has a non-null value. `null` does not communicate
any information to the runtime that isn't communicated already
by the absence of the property.

This should give us some modest code size savings.

PR Close #34116
This commit is contained in:
Kara Erickson 2019-11-27 16:25:47 -08:00 committed by Miško Hevery
parent 56f4e56094
commit 67eac733d2
4 changed files with 20 additions and 19 deletions

View File

@ -12,7 +12,7 @@
"master": { "master": {
"uncompressed": { "uncompressed": {
"runtime-es2015": 2987, "runtime-es2015": 2987,
"main-es2015": 462449, "main-es2015": 461697,
"polyfills-es2015": 52503 "polyfills-es2015": 52503
} }
} }

View File

@ -30,7 +30,7 @@
"master": { "master": {
"uncompressed": { "uncompressed": {
"runtime-es2015": 1485, "runtime-es2015": 1485,
"main-es2015": 137209, "main-es2015": 136594,
"polyfills-es2015": 37494 "polyfills-es2015": 37494
} }
} }
@ -39,7 +39,7 @@
"master": { "master": {
"uncompressed": { "uncompressed": {
"runtime-es2015": 2289, "runtime-es2015": 2289,
"main-es2015": 267389, "main-es2015": 266648,
"polyfills-es2015": 36808, "polyfills-es2015": 36808,
"5-es2015": 751 "5-es2015": 751
} }
@ -49,7 +49,7 @@
"master": { "master": {
"uncompressed": { "uncompressed": {
"runtime-es2015": 2289, "runtime-es2015": 2289,
"main-es2015": 226528, "main-es2015": 225787,
"polyfills-es2015": 36808, "polyfills-es2015": 36808,
"5-es2015": 779 "5-es2015": 779
} }

View File

@ -90,8 +90,7 @@ describe('compiler compliance: dependency injection', () => {
const def = ` const def = `
MyService.ɵprov = $r3$.ɵɵdefineInjectable({ MyService.ɵprov = $r3$.ɵɵdefineInjectable({
token: MyService, token: MyService,
factory: MyService.ɵfac, factory: MyService.ɵfac
providedIn: null
}); });
`; `;
@ -146,8 +145,7 @@ describe('compiler compliance: dependency injection', () => {
token: MyService, token: MyService,
factory: function() { factory: function() {
return alternateFactory(); return alternateFactory();
}, }
providedIn: null
}); });
`; `;
@ -186,8 +184,7 @@ describe('compiler compliance: dependency injection', () => {
r = (() => new MyAlternateFactory())($r3$.ɵɵinject(SomeDep)); r = (() => new MyAlternateFactory())($r3$.ɵɵinject(SomeDep));
} }
return r; return r;
}, }
providedIn: null
}); });
`; `;
@ -219,8 +216,7 @@ describe('compiler compliance: dependency injection', () => {
token: MyService, token: MyService,
factory: function(t) { factory: function(t) {
return MyAlternateService.ɵfac(t); return MyAlternateService.ɵfac(t);
}, }
providedIn: null
}); });
`; `;
@ -261,8 +257,7 @@ describe('compiler compliance: dependency injection', () => {
r = new MyAlternateService($r3$.ɵɵinject(SomeDep)); r = new MyAlternateService($r3$.ɵɵinject(SomeDep));
} }
return r; return r;
}, }
providedIn: null
}); });
`; `;
@ -344,14 +339,14 @@ describe('compiler compliance: dependency injection', () => {
const MyPipeDefs = ` const MyPipeDefs = `
MyPipe.ɵfac = function MyPipe_Factory(t) { return new (t || MyPipe)(i0.ɵɵdirectiveInject(Service)); }; MyPipe.ɵfac = function MyPipe_Factory(t) { return new (t || MyPipe)(i0.ɵɵdirectiveInject(Service)); };
MyPipe.ɵpipe = i0.ɵɵdefinePipe({ name: "myPipe", type: MyPipe, pure: true }); MyPipe.ɵpipe = i0.ɵɵdefinePipe({ name: "myPipe", type: MyPipe, pure: true });
MyPipe.ɵprov = i0.ɵɵdefineInjectable({ token: MyPipe, factory: MyPipe.ɵfac, providedIn: null }); MyPipe.ɵprov = i0.ɵɵdefineInjectable({ token: MyPipe, factory: MyPipe.ɵfac });
`; `;
// The prov definition must be last so MyOtherPipe.fac is defined // The prov definition must be last so MyOtherPipe.fac is defined
const MyOtherPipeDefs = ` const MyOtherPipeDefs = `
MyOtherPipe.ɵfac = function MyOtherPipe_Factory(t) { return new (t || MyOtherPipe)($r3$.ɵɵdirectiveInject(Service)); }; MyOtherPipe.ɵfac = function MyOtherPipe_Factory(t) { return new (t || MyOtherPipe)($r3$.ɵɵdirectiveInject(Service)); };
MyOtherPipe.ɵpipe = i0.ɵɵdefinePipe({ name: "myOtherPipe", type: MyOtherPipe, pure: true }); MyOtherPipe.ɵpipe = i0.ɵɵdefinePipe({ name: "myOtherPipe", type: MyOtherPipe, pure: true });
MyOtherPipe.ɵprov = i0.ɵɵdefineInjectable({ token: MyOtherPipe, factory: MyOtherPipe.ɵfac, providedIn: null }); MyOtherPipe.ɵprov = i0.ɵɵdefineInjectable({ token: MyOtherPipe, factory: MyOtherPipe.ɵfac });
`; `;
expectEmit(source, MyPipeDefs, 'Invalid pipe factory function'); expectEmit(source, MyPipeDefs, 'Invalid pipe factory function');

View File

@ -105,10 +105,16 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
} }
const token = meta.internalType; const token = meta.internalType;
const providedIn = meta.providedIn;
const expression = o.importExpr(Identifiers.ɵɵdefineInjectable).callFn([mapToMapExpression( const injectableProps: {[key: string]: o.Expression} = {token, factory: result.factory};
{token, factory: result.factory, providedIn})]);
// Only generate providedIn property if it has a non-null value
if ((meta.providedIn as o.LiteralExpr).value !== null) {
injectableProps.providedIn = meta.providedIn;
}
const expression =
o.importExpr(Identifiers.ɵɵdefineInjectable).callFn([mapToMapExpression(injectableProps)]);
const type = new o.ExpressionType(o.importExpr( const type = new o.ExpressionType(o.importExpr(
Identifiers.InjectableDef, [typeWithParameters(meta.type, meta.typeArgumentCount)])); Identifiers.InjectableDef, [typeWithParameters(meta.type, meta.typeArgumentCount)]));