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:

committed by
Miško Hevery

parent
02958c07f6
commit
755d2d572f
@ -68,7 +68,8 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
|
||||
} else if (useClassOnSelf) {
|
||||
result = compileFactoryFunction(factoryMeta);
|
||||
} else {
|
||||
result = delegateToFactory(meta.useClass);
|
||||
result = delegateToFactory(
|
||||
meta.type as o.WrappedNodeExpr<any>, meta.useClass as o.WrappedNodeExpr<any>);
|
||||
}
|
||||
} else if (meta.useFactory !== undefined) {
|
||||
if (meta.userDeps !== undefined) {
|
||||
@ -99,7 +100,8 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
|
||||
expression: o.importExpr(Identifiers.inject).callFn([meta.useExisting]),
|
||||
});
|
||||
} else {
|
||||
result = delegateToFactory(meta.internalType);
|
||||
result = delegateToFactory(
|
||||
meta.type as o.WrappedNodeExpr<any>, meta.internalType as o.WrappedNodeExpr<any>);
|
||||
}
|
||||
|
||||
const token = meta.internalType;
|
||||
@ -117,11 +119,15 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
|
||||
};
|
||||
}
|
||||
|
||||
function delegateToFactory(type: o.Expression) {
|
||||
function delegateToFactory(type: o.WrappedNodeExpr<any>, internalType: o.WrappedNodeExpr<any>) {
|
||||
return {
|
||||
statements: [],
|
||||
// () => type.ɵfac(t)
|
||||
factory: o.fn([new o.FnParam('t', o.DYNAMIC_TYPE)], [new o.ReturnStatement(type.callMethod(
|
||||
'ɵfac', [o.variable('t')]))])
|
||||
// If types are the same, we can generate `factory: type.ɵfac`
|
||||
// If types are different, we have to generate a wrapper function to ensure
|
||||
// the internal type has been resolved (`factory: function(t) { return type.ɵfac(t); }`)
|
||||
factory: type.node === internalType.node ?
|
||||
internalType.prop('ɵfac') :
|
||||
o.fn([new o.FnParam('t', o.DYNAMIC_TYPE)], [new o.ReturnStatement(internalType.callMethod(
|
||||
'ɵfac', [o.variable('t')]))])
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user