2016-07-18 06:50:31 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-12-15 12:12:40 -05:00
|
|
|
import {NgModule, Type} from '@angular/core';
|
2016-07-18 06:50:31 -04:00
|
|
|
|
2016-11-18 18:17:44 -05:00
|
|
|
import {ListWrapper} from './facade/collection';
|
2017-01-04 16:59:43 -05:00
|
|
|
import {stringify} from './facade/lang';
|
2016-12-15 12:12:40 -05:00
|
|
|
import {CompilerInjectable} from './injectable';
|
2016-08-30 21:07:40 -04:00
|
|
|
import {ReflectorReader, reflector} from './private_import_core';
|
2016-07-18 06:50:31 -04:00
|
|
|
|
2016-09-12 22:14:17 -04:00
|
|
|
function _isNgModuleMetadata(obj: any): obj is NgModule {
|
|
|
|
return obj instanceof NgModule;
|
2016-07-18 06:50:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-12 22:14:17 -04:00
|
|
|
* Resolves types to {@link NgModule}.
|
2016-07-18 06:50:31 -04:00
|
|
|
*/
|
2016-12-15 12:12:40 -05:00
|
|
|
@CompilerInjectable()
|
2016-07-18 06:50:31 -04:00
|
|
|
export class NgModuleResolver {
|
|
|
|
constructor(private _reflector: ReflectorReader = reflector) {}
|
|
|
|
|
2016-11-10 17:07:30 -05:00
|
|
|
isNgModule(type: any) { return this._reflector.annotations(type).some(_isNgModuleMetadata); }
|
|
|
|
|
2016-09-12 22:14:17 -04:00
|
|
|
resolve(type: Type<any>, throwIfNotFound = true): NgModule {
|
2016-11-18 18:17:44 -05:00
|
|
|
const ngModuleMeta: NgModule =
|
|
|
|
ListWrapper.findLast(this._reflector.annotations(type), _isNgModuleMetadata);
|
2016-07-18 06:50:31 -04:00
|
|
|
|
2017-01-04 16:59:43 -05:00
|
|
|
if (ngModuleMeta) {
|
2016-07-18 06:50:31 -04:00
|
|
|
return ngModuleMeta;
|
|
|
|
} else {
|
|
|
|
if (throwIfNotFound) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`No NgModule metadata found for '${stringify(type)}'.`);
|
2016-07-18 06:50:31 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|