Skip to content

Commit 930accf

Browse files
Mihail GoloborodovMihail Goloborodov
Mihail Goloborodov
authored and
Mihail Goloborodov
committed
fix: remove deprecated NgModuleFactory, Compiler, ComponentFactoryResolver
1 parent 7a8513b commit 930accf

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

src/directives/uiView.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import {
22
Component,
3-
ComponentFactory,
4-
ComponentFactoryResolver,
3+
ComponentMirror,
54
ComponentRef,
65
Inject,
76
Injector,
87
Input,
98
OnDestroy,
109
OnInit,
11-
ReflectiveInjector,
10+
reflectComponentType,
1211
ViewChild,
1312
ViewContainerRef,
1413
} from '@angular/core';
@@ -57,7 +56,7 @@ interface InputMapping {
5756
*
5857
* @internal
5958
*/
60-
const ng2ComponentInputs = (factory: ComponentFactory<any>): InputMapping[] => {
59+
const ng2ComponentInputs = (factory: ComponentMirror<any>): InputMapping[] => {
6160
return factory.inputs.map((input) => ({ prop: input.propName, token: input.templateName }));
6261
};
6362

@@ -291,12 +290,19 @@ export class UIView implements OnInit, OnDestroy {
291290

292291
// Create the component
293292
const moduleInjector = context.getResolvable(NATIVE_INJECTOR_TOKEN).data;
294-
const compFactoryResolver = moduleInjector.get(ComponentFactoryResolver);
295-
const compFactory = compFactoryResolver.resolveComponentFactory(componentClass);
296-
this._componentRef = this._componentTarget.createComponent(compFactory, undefined, componentInjector);
293+
294+
this._componentRef = this._componentTarget.createComponent(componentClass, {
295+
injector: componentInjector,
296+
environmentInjector: moduleInjector
297+
});
297298

298299
// Wire resolves to @Input()s
299-
this._applyInputBindings(compFactory, this._componentRef.instance, context, componentClass);
300+
this._applyInputBindings(
301+
reflectComponentType(componentClass),
302+
this._componentRef.instance,
303+
context,
304+
componentClass
305+
);
300306
}
301307

302308
/**
@@ -332,7 +338,7 @@ export class UIView implements OnInit, OnDestroy {
332338
* Finds component inputs which match resolves (by name) and sets the input value
333339
* to the resolve data.
334340
*/
335-
private _applyInputBindings(factory: ComponentFactory<any>, component: any, context: ResolveContext, componentClass) {
341+
private _applyInputBindings(factory: ComponentMirror<any>, component: any, context: ResolveContext, componentClass) {
336342
const bindings = this._uiViewData.config.viewDecl['bindings'] || {};
337343
const explicitBoundProps = Object.keys(bindings);
338344

src/lazyLoad/lazyLoadNgModule.ts

+6-33
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { NgModuleRef, Injector, NgModuleFactory, Type, Compiler } from '@angular/core';
1+
import { NgModuleRef, Injector, Type, createNgModule } from '@angular/core';
22
import {
33
Transition,
44
LazyLoadResult,
55
UIRouter,
66
Resolvable,
77
NATIVE_INJECTOR_TOKEN,
8-
isString,
98
unnestR,
109
inArray,
1110
StateObject,
@@ -72,42 +71,16 @@ export function loadNgModule(
7271
): (transition: Transition, stateObject: StateDeclaration) => Promise<LazyLoadResult> {
7372
return (transition: Transition, stateObject: StateDeclaration) => {
7473
const ng2Injector = transition.injector().get(NATIVE_INJECTOR_TOKEN);
75-
76-
const createModule = (factory: NgModuleFactory<any>) => factory.create(ng2Injector);
77-
74+
const unwrapEsModuleDefault = x => (x && x.__esModule && x['default'] ? x['default'] : x);
7875
const applyModule = (moduleRef: NgModuleRef<any>) => applyNgModule(transition, moduleRef, ng2Injector, stateObject);
7976

80-
return loadModuleFactory(moduleToLoad, ng2Injector).then(createModule).then(applyModule);
77+
return Promise.resolve(moduleToLoad())
78+
.then(unwrapEsModuleDefault)
79+
.then((ngModule: Type<any>) => createNgModule(ngModule, ng2Injector))
80+
.then(applyModule);
8181
};
8282
}
8383

84-
/**
85-
* Returns the module factory that can be used to instantiate a module
86-
*
87-
* For a Type<any> or Promise<Type<any>> this:
88-
* - Compiles the component type (if not running with AOT)
89-
* - Returns the NgModuleFactory resulting from compilation (or direct loading if using AOT) as a Promise
90-
*
91-
* @internal
92-
*/
93-
export function loadModuleFactory(
94-
moduleToLoad: ModuleTypeCallback,
95-
ng2Injector: Injector
96-
): Promise<NgModuleFactory<any>> {
97-
const compiler: Compiler = ng2Injector.get(Compiler);
98-
99-
const unwrapEsModuleDefault = (x) => (x && x.__esModule && x['default'] ? x['default'] : x);
100-
101-
return Promise.resolve(moduleToLoad())
102-
.then(unwrapEsModuleDefault)
103-
.then((t: NgModuleFactory<any> | Type<any>) => {
104-
if (t instanceof NgModuleFactory) {
105-
return t;
106-
}
107-
return compiler.compileModuleAsync(t);
108-
});
109-
}
110-
11184
/**
11285
* Apply the UI-Router Modules found in the lazy loaded module.
11386
*

0 commit comments

Comments
 (0)