feat(ivy): generate ngInjectorDef for @NgModule in JIT mode (#24632)

This commit takes advantage of the @angular/compiler work for ngInjectorDef
in AOT mode in order to generate the same definition in JIT mode.

PR Close #24632
This commit is contained in:
Alex Rickabaugh
2018-06-19 11:40:29 -07:00
committed by Jason Aden
parent ae9418c7de
commit 89c442270a
6 changed files with 61 additions and 13 deletions

View File

@ -35,10 +35,3 @@ jasmine_node_test(
":ivy_node_lib",
],
)
ts_web_test_suite(
name = "ivy_web",
deps = [
":ivy_lib",
],
)

View File

@ -6,6 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/
import 'reflect-metadata';
import {InjectorDef, defineInjectable} from '@angular/core/src/di/defs';
import {Injectable} from '@angular/core/src/di/injectable';
import {inject, setCurrentInjector} from '@angular/core/src/di/injector';
import {ivyEnabled} from '@angular/core/src/ivy_switch';
@ -140,6 +143,31 @@ ivyEnabled && describe('render3 jit', () => {
expect(moduleDef.declarations[0]).toBe(Cmp);
});
it('compiles a module to an ngInjectorDef with the providers', () => {
class Token {
static ngInjectableDef = defineInjectable({
providedIn: 'root',
factory: () => 'default',
});
}
@NgModule({
providers: [{provide: Token, useValue: 'test'}],
})
class Module {
constructor(public token: Token) {}
}
const injectorDef: InjectorDef<Module> = (Module as any).ngInjectorDef;
const instance = injectorDef.factory();
// Since the instance was created outside of an injector using the module, the
// injection will use the default provider, not the provider from the module.
expect(instance.token).toBe('default');
expect(injectorDef.providers).toEqual([{provide: Token, useValue: 'test'}]);
});
it('patches a module onto the component', () => {
@Component({
template: 'foo',

View File

@ -14,6 +14,7 @@ import {angularCoreEnv} from '../../src/render3/jit/environment';
const INTERFACE_EXCEPTIONS = new Set<string>([
'ComponentDef',
'DirectiveDef',
'InjectorDef',
'NgModuleDef',
]);