66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- export const ivyEnabled = false ;
10- export const R3_COMPILE_COMPONENT : ( ( type : any , meta : any ) => void ) | null = null ;
11- export const R3_COMPILE_DIRECTIVE : ( ( type : any , meta : any ) => void ) | null = null ;
12- export const R3_COMPILE_INJECTABLE : ( ( type : any , meta : any ) => void ) | null = null ;
13- export const R3_COMPILE_NGMODULE : ( ( type : any , meta : any ) => void ) | null = null ;
14- export const R3_COMPILE_PIPE : ( ( type : any , meta : any ) => void ) | null = null ;
9+ import { InjectableType , InjectorType , defineInjectable , defineInjector } from './di/defs' ;
10+ import { InjectableProvider } from './di/injectable' ;
11+ import { inject , injectArgs } from './di/injector' ;
12+ import { ClassSansProvider , ConstructorSansProvider , ExistingSansProvider , FactorySansProvider , StaticClassSansProvider , ValueProvider , ValueSansProvider } from './di/provider' ;
13+ import * as ivyOn from './ivy_switch_on' ;
14+ import { NgModule } from './metadata' ;
15+ import { ReflectionCapabilities } from './reflection/reflection_capabilities' ;
16+ import { Type } from './type' ;
17+ import { getClosureSafeProperty } from './util/property' ;
18+
19+ function noop ( ) { }
20+
21+ export interface DirectiveCompiler { ( type : any , meta : any ) : void ; }
22+
23+ const R3_COMPILE_COMPONENT__POST_NGCC__ = ivyOn . R3_COMPILE_COMPONENT ;
24+ const R3_COMPILE_DIRECTIVE__POST_NGCC__ = ivyOn . R3_COMPILE_DIRECTIVE ;
25+ const R3_COMPILE_INJECTABLE__POST_NGCC__ = ivyOn . R3_COMPILE_INJECTABLE ;
26+ const R3_COMPILE_NGMODULE__POST_NGCC__ = ivyOn . R3_COMPILE_NGMODULE ;
27+ const R3_COMPILE_PIPE__POST_NGCC__ = ivyOn . R3_COMPILE_PIPE ;
28+ const ivyEnable__POST_NGCC__ = ivyOn . ivyEnabled ;
29+
30+ const compileComponentQueue : any [ ] = [ ] ;
31+ const compileDirectiveQueue : any [ ] = [ ] ;
32+ const compileInjectableQueue : any [ ] = [ ] ;
33+ const compileNgModuleQueue : any [ ] = [ ] ;
34+ const compilePipeQueue : any [ ] = [ ] ;
35+
36+ const R3_COMPILE_COMPONENT__PRE_NGCC__ : DirectiveCompiler = noop ;
37+ const R3_COMPILE_DIRECTIVE__PRE_NGCC__ : DirectiveCompiler = noop ;
38+ const R3_COMPILE_INJECTABLE__PRE_NGCC__ : DirectiveCompiler = preR3InjectableCompile ;
39+ const R3_COMPILE_NGMODULE__PRE_NGCC__ : DirectiveCompiler = preR3NgModuleCompile ;
40+ const R3_COMPILE_PIPE__PRE_NGCC__ : DirectiveCompiler = noop ;
41+ const ivyEnable__PRE_NGCC__ = false ;
42+
43+ export const ivyEnabled = ivyEnable__PRE_NGCC__ ;
44+ export let R3_COMPILE_COMPONENT : DirectiveCompiler = R3_COMPILE_COMPONENT__PRE_NGCC__ ;
45+ export let R3_COMPILE_DIRECTIVE : DirectiveCompiler = R3_COMPILE_DIRECTIVE__PRE_NGCC__ ;
46+ export let R3_COMPILE_INJECTABLE : DirectiveCompiler = R3_COMPILE_INJECTABLE__PRE_NGCC__ ;
47+ export let R3_COMPILE_NGMODULE : DirectiveCompiler = R3_COMPILE_NGMODULE__PRE_NGCC__ ;
48+ export let R3_COMPILE_PIPE : DirectiveCompiler = R3_COMPILE_PIPE__PRE_NGCC__ ;
49+
50+
51+ ////////////////////////////////////////////////////////////
52+ // Glue code which should be removed after Ivy is default //
53+ ////////////////////////////////////////////////////////////
54+
55+ function preR3NgModuleCompile ( moduleType : InjectorType < any > , metadata : NgModule ) : void {
56+ let imports = ( metadata && metadata . imports ) || [ ] ;
57+ if ( metadata && metadata . exports ) {
58+ imports = [ ...imports , metadata . exports ] ;
59+ }
60+
61+ moduleType . ngInjectorDef = defineInjector ( {
62+ factory : convertInjectableProviderToFactory ( moduleType , { useClass : moduleType } ) ,
63+ providers : metadata && metadata . providers ,
64+ imports : imports ,
65+ } ) ;
66+ }
67+
68+ const GET_PROPERTY_NAME = { } as any ;
69+ const USE_VALUE = getClosureSafeProperty < ValueProvider > (
70+ { provide : String , useValue : GET_PROPERTY_NAME } , GET_PROPERTY_NAME ) ;
71+ const EMPTY_ARRAY : any [ ] = [ ] ;
72+
73+ function convertInjectableProviderToFactory ( type : Type < any > , provider ?: InjectableProvider ) : ( ) =>
74+ any {
75+ if ( ! provider ) {
76+ const reflectionCapabilities = new ReflectionCapabilities ( ) ;
77+ const deps = reflectionCapabilities . parameters ( type ) ;
78+ // TODO - convert to flags.
79+ return ( ) => new type ( ...injectArgs ( deps as any [ ] ) ) ;
80+ }
81+
82+ if ( USE_VALUE in provider ) {
83+ const valueProvider = ( provider as ValueSansProvider ) ;
84+ return ( ) => valueProvider . useValue ;
85+ } else if ( ( provider as ExistingSansProvider ) . useExisting ) {
86+ const existingProvider = ( provider as ExistingSansProvider ) ;
87+ return ( ) => inject ( existingProvider . useExisting ) ;
88+ } else if ( ( provider as FactorySansProvider ) . useFactory ) {
89+ const factoryProvider = ( provider as FactorySansProvider ) ;
90+ return ( ) => factoryProvider . useFactory ( ...injectArgs ( factoryProvider . deps || EMPTY_ARRAY ) ) ;
91+ } else if ( ( provider as StaticClassSansProvider | ClassSansProvider ) . useClass ) {
92+ const classProvider = ( provider as StaticClassSansProvider | ClassSansProvider ) ;
93+ let deps = ( provider as StaticClassSansProvider ) . deps ;
94+ if ( ! deps ) {
95+ const reflectionCapabilities = new ReflectionCapabilities ( ) ;
96+ deps = reflectionCapabilities . parameters ( type ) ;
97+ }
98+ return ( ) => new classProvider . useClass ( ...injectArgs ( deps ) ) ;
99+ } else {
100+ let deps = ( provider as ConstructorSansProvider ) . deps ;
101+ if ( ! deps ) {
102+ const reflectionCapabilities = new ReflectionCapabilities ( ) ;
103+ deps = reflectionCapabilities . parameters ( type ) ;
104+ }
105+ return ( ) => new type ( ...injectArgs ( deps ! ) ) ;
106+ }
107+ }
108+
109+ /**
110+ * Supports @Injectable() in JIT mode for Render2.
111+ */
112+ function preR3InjectableCompile (
113+ injectableType : InjectableType < any > ,
114+ options : { providedIn ?: Type < any > | 'root' | null } & InjectableProvider ) : void {
115+ if ( options && options . providedIn !== undefined && injectableType . ngInjectableDef === undefined ) {
116+ injectableType . ngInjectableDef = defineInjectable ( {
117+ providedIn : options . providedIn ,
118+ factory : convertInjectableProviderToFactory ( injectableType , options ) ,
119+ } ) ;
120+ }
121+ }
0 commit comments