refactor(ivy): remove unnecessary fac wrapper (#34076)

For injectables, we currently generate a factory function in the
injectable def (prov) that delegates to the factory function in
the factory def (fac). It looks something like this:

```
factory: function(t) { return Svc.fac(t); }
```

The extra wrapper function is unnecessary since the args for
the factory functions are the same. This commit changes the
compiler to generate this instead:

```
factory: Svc.fac
```

Because we are generating less code for each injectable, we
should see some modest code size savings. AIO's main bundle
is about 1 KB smaller.

PR Close #34076
This commit is contained in:
Kara Erickson
2019-11-27 15:52:34 -08:00
committed by Miško Hevery
parent 02958c07f6
commit 755d2d572f
7 changed files with 68 additions and 24 deletions

View File

@ -90,9 +90,7 @@ describe('compiler compliance: dependency injection', () => {
const def = `
MyService.ɵprov = $r3$.ɵɵdefineInjectable({
token: MyService,
factory: function(t) {
return MyService.ɵfac(t);
},
factory: MyService.ɵfac,
providedIn: null
});
`;
@ -342,16 +340,22 @@ describe('compiler compliance: dependency injection', () => {
const result = compile(files, angularFiles);
const source = result.source;
const MyPipeFactory = `
MyPipe.ɵfac = function MyPipe_Factory(t) { return new (t || MyPipe)($r3$.ɵɵdirectiveInject(Service)); };
// The prov definition must be last so MyPipe.fac is defined
const MyPipeDefs = `
MyPipe.ɵfac = function MyPipe_Factory(t) { return new (t || MyPipe)(i0.ɵɵdirectiveInject(Service)); };
MyPipe.ɵpipe = i0.ɵɵdefinePipe({ name: "myPipe", type: MyPipe, pure: true });
MyPipe.ɵprov = i0.ɵɵdefineInjectable({ token: MyPipe, factory: MyPipe.ɵfac, providedIn: null });
`;
const MyOtherPipeFactory = `
// The prov definition must be last so MyOtherPipe.fac is defined
const MyOtherPipeDefs = `
MyOtherPipe.ɵfac = function MyOtherPipe_Factory(t) { return new (t || MyOtherPipe)($r3$.ɵɵdirectiveInject(Service)); };
MyOtherPipe.ɵpipe = i0.ɵɵdefinePipe({ name: "myOtherPipe", type: MyOtherPipe, pure: true });
MyOtherPipe.ɵprov = i0.ɵɵdefineInjectable({ token: MyOtherPipe, factory: MyOtherPipe.ɵfac, providedIn: null });
`;
expectEmit(source, MyPipeFactory, 'Invalid pipe factory function');
expectEmit(source, MyOtherPipeFactory, 'Invalid pipe factory function');
expectEmit(source, MyPipeDefs, 'Invalid pipe factory function');
expectEmit(source, MyOtherPipeDefs, 'Invalid pipe factory function');
expect(source.match(/MyPipe\.ɵfac =/g) !.length).toBe(1);
expect(source.match(/MyOtherPipe\.ɵfac =/g) !.length).toBe(1);
});