diff --git a/packages/core/src/linker/compiler.ts b/packages/core/src/linker/compiler.ts index d9e5acec94..86446a9eae 100644 --- a/packages/core/src/linker/compiler.ts +++ b/packages/core/src/linker/compiler.ts @@ -78,6 +78,11 @@ export class Compiler { * Clears the cache for the given component/ngModule. */ clearCacheFor(type: Type) {} + + /** + * Returns the id for a given NgModule, if one is defined and known to the compiler. + */ + getModuleId(moduleType: Type): string|undefined { return undefined; } } /** diff --git a/packages/platform-browser-dynamic/src/compiler_factory.ts b/packages/platform-browser-dynamic/src/compiler_factory.ts index 1e71000025..dd7e887f05 100644 --- a/packages/platform-browser-dynamic/src/compiler_factory.ts +++ b/packages/platform-browser-dynamic/src/compiler_factory.ts @@ -78,6 +78,10 @@ export class CompilerImpl implements Compiler { } clearCache(): void { this._delegate.clearCache(); } clearCacheFor(type: Type) { this._delegate.clearCacheFor(type); } + getModuleId(moduleType: Type): string|undefined { + const meta = this._metadataResolver.getNgModuleMetadata(moduleType); + return meta && meta.id || undefined; + } } /** diff --git a/packages/platform-browser-dynamic/test/testing_public_browser_spec.ts b/packages/platform-browser-dynamic/test/testing_public_browser_spec.ts index 668a552eb9..f219a92ffe 100644 --- a/packages/platform-browser-dynamic/test/testing_public_browser_spec.ts +++ b/packages/platform-browser-dynamic/test/testing_public_browser_spec.ts @@ -7,7 +7,7 @@ */ import {ResourceLoader} from '@angular/compiler'; -import {Component} from '@angular/core'; +import {Compiler, Component, NgModule} from '@angular/core'; import {TestBed, async, fakeAsync, inject, tick} from '@angular/core/testing'; import {ResourceLoaderImpl} from '../src/resource_loader/resource_loader_impl'; @@ -77,6 +77,22 @@ class BadTemplateUrl { }); }); + describe('Compiler', () => { + it('should return NgModule id when asked', () => { + @NgModule({ + id: 'test-module', + }) + class TestModule { + } + + TestBed.configureTestingModule({ + imports: [TestModule], + }); + const compiler = TestBed.get(Compiler) as Compiler; + expect(compiler.getModuleId(TestModule)).toBe('test-module'); + }); + }); + describe('errors', () => { let originalJasmineIt: any; diff --git a/packages/platform-browser-dynamic/testing/src/compiler_factory.ts b/packages/platform-browser-dynamic/testing/src/compiler_factory.ts index b2dda8b0f9..02d139f25c 100644 --- a/packages/platform-browser-dynamic/testing/src/compiler_factory.ts +++ b/packages/platform-browser-dynamic/testing/src/compiler_factory.ts @@ -99,4 +99,8 @@ export class TestingCompilerImpl implements TestingCompiler { clearCacheFor(type: Type) { this._compiler.clearCacheFor(type); } getComponentFromError(error: Error) { return (error as any)[ERROR_COMPONENT_TYPE] || null; } + + getModuleId(moduleType: Type): string|undefined { + return this._moduleResolver.resolve(moduleType, true).id; + } } diff --git a/tools/public_api_guard/core/core.d.ts b/tools/public_api_guard/core/core.d.ts index d65ed33a3c..7c6a03a6e3 100644 --- a/tools/public_api_guard/core/core.d.ts +++ b/tools/public_api_guard/core/core.d.ts @@ -86,6 +86,7 @@ export declare class Compiler { compileModuleAndAllComponentsSync(moduleType: Type): ModuleWithComponentFactories; compileModuleAsync(moduleType: Type): Promise>; compileModuleSync(moduleType: Type): NgModuleFactory; + getModuleId(moduleType: Type): string | undefined; } /** @experimental */