11import {
2+ CompilerHost ,
23 CompilerOptions ,
4+ Program ,
35 SourceFile ,
46 resolveModuleName
57} from 'typescript' ;
@@ -10,10 +12,10 @@ import {
1012 resolve
1113} from 'path' ;
1214
13- import { CompilerVmHost } from './compiler-vm' ;
1415import { CompilerException } from '../../exception' ;
15- import { Project } from '../../application' ;
16+ import { ApplicationModuleDescriptor , Project } from '../../application' ;
1617import { Publisher } from '../../publisher' ;
18+ import { StaticAnalyzer } from '../static' ;
1719
1820export type CompiledSource = { filename : string , source : string } ;
1921
@@ -47,20 +49,38 @@ export class CompilationEmit {
4749 }
4850 }
4951
50- rootModule ( compilerHost : CompilerVmHost , basePath : string , options : CompilerOptions ) : [ string , string ] {
51- const containingFile = join ( basePath , 'index.ts' ) ;
52+ applicationModule ( program : Program ) : ApplicationModuleDescriptor {
53+ const parser = new StaticAnalyzer ( program . getSourceFiles ( ) ) ;
5254
53- const rootModule = sourceToNgFactory ( this . project . rootModule . source ) ;
55+ return parser . getBootstrapModule ( ) ;
56+ }
57+
58+ rootModule ( program : Program , compilerHost : CompilerHost , options : CompilerOptions ) : [ string , string ] {
59+ const containingFile = join ( this . project . basePath , 'index.ts' ) ;
60+
61+ const applicationModule =
62+ this . project . applicationModule
63+ ? this . project . applicationModule
64+ : this . applicationModule ( program ) ;
65+
66+ if ( applicationModule == null ||
67+ applicationModule . source == null ||
68+ applicationModule . symbol == null ) {
69+ throw new CompilerException ( `Cannot find application root @NgModule, please provide in Project structure` ) ;
70+ }
71+
72+ const rootModule = sourceToNgFactory ( applicationModule . source ) ;
5473
5574 const resolved = resolveModuleName ( rootModule , containingFile , options , compilerHost ) ;
5675
57- if ( resolved == null || resolved . resolvedModule == null ) {
76+ if ( resolved == null ||
77+ resolved . resolvedModule == null ) {
5878 throw new CompilerException ( `Cannot resolve root module: ${ rootModule } ` ) ;
5979 }
6080
6181 const moduleFile = this . getModuleFromSourceFile ( resolved . resolvedModule . resolvedFileName ) ;
6282
63- const symbol = symbolToNgFactory ( this . project . rootModule . symbol ) ;
83+ const symbol = symbolToNgFactory ( applicationModule . symbol ) ;
6484
6585 return [ moduleFile , symbol ] ;
6686 }
@@ -94,7 +114,7 @@ const executable = (filename: string) => /\.js$/.test(filename);
94114
95115const sourceToNgFactory = ( source : string ) : string => {
96116 if ( / \. n g f a c t o r y ( \. ( t s | j s ) ) ? $ / . test ( source ) === false ) {
97- return `${ source } .ngfactory` ;
117+ return `${ source . replace ( / \. ( j s | t s ) $ / , String ( ) ) } .ngfactory` ;
98118 }
99119 return source ;
100120} ;
0 commit comments