1
1
import {
2
- Injectable ,
3
2
Compiler ,
3
+ Injectable ,
4
4
NgModuleFactory ,
5
5
NgModuleFactoryLoader ,
6
- ModuleWithComponentFactories ,
7
6
SystemJsNgModuleLoader ,
7
+ Type ,
8
8
} from "@angular/core" ;
9
-
10
9
import { path , knownFolders } from "tns-core-modules/file-system" ;
11
10
12
11
const SEPARATOR = "#" ;
@@ -15,63 +14,50 @@ const SEPARATOR = "#";
15
14
export class NSModuleFactoryLoader implements NgModuleFactoryLoader {
16
15
private offlineMode : boolean ;
17
16
18
- constructor ( private compiler : Compiler , private ngModuleLoader : SystemJsNgModuleLoader ) {
17
+ constructor (
18
+ private compiler : Compiler ,
19
+ private ngModuleLoader : SystemJsNgModuleLoader ,
20
+ ) {
19
21
this . offlineMode = compiler instanceof Compiler ;
20
22
}
21
23
22
24
load ( path : string ) : Promise < NgModuleFactory < any > > {
23
- if ( this . offlineMode ) {
24
- return this . ngModuleLoader . load ( path ) ;
25
- } else {
26
- return this . loadAndCompile ( path ) ;
27
- }
25
+ return this . offlineMode ?
26
+ this . ngModuleLoader . load ( path ) :
27
+ this . loadAndCompile ( path ) ;
28
28
}
29
29
30
- /**
31
- * When needing the module with component factories
32
- * Example: lazy loading on demand via user actions instead of routing
33
- * Provides access to components in the lazy loaded module right away
34
- * @param path module path
35
- */
36
- public loadAndCompileComponents ( path : string ) : Promise < any > {
37
- return this . loadAndCompile ( path , true ) ;
30
+ private loadAndCompile ( path : string ) : Promise < NgModuleFactory < any > > {
31
+ const module = requireModule ( path ) ;
32
+ return Promise . resolve ( this . compiler . compileModuleAsync ( module ) ) ;
38
33
}
34
+ }
39
35
40
- private loadAndCompile ( path : string , includeComponents ?: boolean ) :
41
- Promise < NgModuleFactory < any > | ModuleWithComponentFactories < any > > {
42
- let { modulePath, exportName} = splitPath ( path ) ;
43
-
44
- let loadedModule = global . require ( modulePath ) [ exportName ] ;
45
- checkNotEmpty ( loadedModule , modulePath , exportName ) ;
36
+ function requireModule ( path : string ) : Type < any > {
37
+ const { modulePath, exportName} = splitPath ( path ) ;
46
38
47
- if ( includeComponents ) {
48
- return Promise . resolve ( this . compiler . compileModuleAndAllComponentsAsync ( loadedModule ) ) ;
49
- } else {
50
- return Promise . resolve ( this . compiler . compileModuleAsync ( loadedModule ) ) ;
51
- }
52
- }
39
+ const loadedModule = global . require ( modulePath ) [ exportName ] ;
40
+ checkNotEmpty ( loadedModule , modulePath , exportName ) ;
53
41
42
+ return loadedModule ;
54
43
}
55
44
56
45
function splitPath ( path : string ) : { modulePath : string , exportName : string } {
57
- let [ modulePath , exportName ] = path . split ( SEPARATOR ) ;
58
- modulePath = getAbsolutePath ( modulePath ) ;
59
-
60
- if ( typeof exportName === "undefined" ) {
61
- exportName = "default" ;
62
- }
46
+ const [ relativeModulePath , exportName = "default" ] = path . split ( SEPARATOR ) ;
47
+ const absoluteModulePath = getAbsolutePath ( relativeModulePath ) ;
63
48
64
- return { modulePath, exportName} ;
49
+ return { modulePath : absoluteModulePath , exportName} ;
65
50
}
66
51
67
52
function getAbsolutePath ( relativePath : string ) {
68
- return path . normalize ( path . join ( knownFolders . currentApp ( ) . path , relativePath ) ) ;
53
+ const projectPath = knownFolders . currentApp ( ) . path ;
54
+ const absolutePath = path . join ( projectPath , relativePath ) ;
55
+
56
+ return path . normalize ( absolutePath ) ;
69
57
}
70
58
71
- function checkNotEmpty ( value : any , modulePath : string , exportName : string ) : any {
59
+ function checkNotEmpty ( value : any , modulePath : string , exportName : string ) : void {
72
60
if ( ! value ) {
73
61
throw new Error ( `Cannot find '${ exportName } ' in '${ modulePath } '` ) ;
74
62
}
75
-
76
- return value ;
77
63
}
0 commit comments