angular/packages/core/src/r3_symbols.ts
crisbeto 4e35e348af refactor(ivy): generate ngFactoryDef for injectables (#32433)
With #31953 we moved the factories for components, directives and pipes into a new field called `ngFactoryDef`, however I decided not to do it for injectables, because they needed some extra logic. These changes set up the `ngFactoryDef` for injectables as well.

For reference, the extra logic mentioned above is that for injectables we have two code paths:

1. For injectables that don't configure how they should be instantiated, we create a `factory` that proxies to `ngFactoryDef`:

```
// Source
@Injectable()
class Service {}

// Output
class Service {
  static ngInjectableDef = defineInjectable({
    factory: () => Service.ngFactoryFn(),
  });

  static ngFactoryFn: (t) => new (t || Service)();
}
```

2. For injectables that do configure how they're created, we keep the `ngFactoryDef` and generate the factory based on the metadata:

```
// Source
@Injectable({
  useValue: DEFAULT_IMPL,
})
class Service {}

// Output
export class Service {
  static ngInjectableDef = defineInjectable({
    factory: () => DEFAULT_IMPL,
  });

  static ngFactoryFn: (t) => new (t || Service)();
}
```

PR Close #32433
2019-10-02 13:04:26 -07:00

39 lines
1.6 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/*
* This file exists to support compilation of @angular/core in Ivy mode.
*
* When the Angular compiler processes a compilation unit, it normally writes imports to
* @angular/core. When compiling the core package itself this strategy isn't usable. Instead, the
* compiler writes imports to this file.
*
* Only a subset of such imports are supported - core is not allowed to declare components or pipes.
* A check in ngtsc's `R3SymbolsImportRewriter` validates this condition. The rewriter is only used
* when compiling @angular/core and is responsible for translating an external name (prefixed with
* ɵ) to the internal symbol name as exported below.
*
* The below symbols are used for @Injectable and @NgModule compilation.
*/
export {ɵɵinject} from './di/injector_compatibility';
export {ɵɵInjectableDef, ɵɵInjectorDef, ɵɵdefineInjectable, ɵɵdefineInjector} from './di/interface/defs';
export {NgModuleDef, ɵɵNgModuleDefWithMeta} from './metadata/ng_module';
export {ɵɵdefineNgModule} from './render3/definition';
export {ɵɵFactoryDef} from './render3/interfaces/definition';
export {setClassMetadata} from './render3/metadata';
export {NgModuleFactory} from './render3/ng_module_ref';
/**
* The existence of this constant (in this particular file) informs the Angular compiler that the
* current program is actually @angular/core, which needs to be compiled specially.
*/
export const ITS_JUST_ANGULAR = true;