6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { ApplicationRef , Injector , Type , createPlatformFactory , platformCore } from '@angular/core' ;
9
+ import {
10
+ ApplicationRef ,
11
+ Compiler ,
12
+ Injector ,
13
+ Type ,
14
+ createPlatformFactory ,
15
+ platformCore ,
16
+ } from '@angular/core' ;
10
17
import {
11
18
INITIAL_CONFIG ,
12
19
ɵINTERNAL_SERVER_PLATFORM_PROVIDERS as INTERNAL_SERVER_PLATFORM_PROVIDERS ,
13
20
} from '@angular/platform-server' ;
14
- import { Route , Router , ɵROUTER_PROVIDERS } from '@angular/router' ;
21
+ import { Route , Router , ɵloadChildren as loadChildrenHelper } from '@angular/router' ;
15
22
import { first } from 'rxjs/operators' ; // Import from `/operators` to support rxjs 6 which is still supported by the Framework.
16
23
17
- // TODO(alanagius): replace the below once `RouterConfigLoader` is privately exported from `@angular/router`.
18
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
- const RouterConfigLoader = ɵROUTER_PROVIDERS [ 5 ] as any ;
20
- type RouterConfigLoader = typeof RouterConfigLoader ;
21
-
22
24
interface RouterResult {
23
25
route : string ;
24
26
success : boolean ;
@@ -27,17 +29,17 @@ interface RouterResult {
27
29
28
30
async function * getRoutesFromRouterConfig (
29
31
routes : Route [ ] ,
30
- routerConfigLoader : RouterConfigLoader ,
31
- injector : Injector ,
32
- parent = '' ,
32
+ compiler : Compiler ,
33
+ parentInjector : Injector ,
34
+ parentRoute = '' ,
33
35
) : AsyncIterableIterator < RouterResult > {
34
36
for ( const route of routes ) {
35
37
const { path, redirectTo, loadChildren, children } = route ;
36
38
if ( path === undefined ) {
37
39
continue ;
38
40
}
39
41
40
- const currentRoutePath = buildRoutePath ( parent , path ) ;
42
+ const currentRoutePath = buildRoutePath ( parentRoute , path ) ;
41
43
42
44
if ( redirectTo !== undefined ) {
43
45
// TODO: handle `redirectTo`.
@@ -53,13 +55,21 @@ async function* getRoutesFromRouterConfig(
53
55
54
56
yield { route : currentRoutePath , success : true , redirect : false } ;
55
57
56
- if ( children ?. length || loadChildren ) {
57
- yield * getRoutesFromRouterConfig (
58
- children ?? ( await routerConfigLoader . loadChildren ( injector , route ) . toPromise ( ) ) . routes ,
59
- routerConfigLoader ,
60
- injector ,
61
- currentRoutePath ,
62
- ) ;
58
+ if ( children ?. length ) {
59
+ yield * getRoutesFromRouterConfig ( children , compiler , parentInjector , currentRoutePath ) ;
60
+ }
61
+
62
+ if ( loadChildren ) {
63
+ const loadedChildRoutes = await loadChildrenHelper (
64
+ route ,
65
+ compiler ,
66
+ parentInjector ,
67
+ ) . toPromise ( ) ;
68
+
69
+ if ( loadedChildRoutes ) {
70
+ const { routes : childRoutes , injector = parentInjector } = loadedChildRoutes ;
71
+ yield * getRoutesFromRouterConfig ( childRoutes , compiler , injector , currentRoutePath ) ;
72
+ }
63
73
}
64
74
}
65
75
}
@@ -92,10 +102,10 @@ export async function* extractRoutes(
92
102
93
103
const injector = applicationRef . injector ;
94
104
const router = injector . get ( Router ) ;
95
- const routerConfigLoader = injector . get ( RouterConfigLoader ) ;
105
+ const compiler = injector . get ( Compiler ) ;
96
106
97
107
// Extract all the routes from the config.
98
- yield * getRoutesFromRouterConfig ( router . config , routerConfigLoader , injector ) ;
108
+ yield * getRoutesFromRouterConfig ( router . config , compiler , injector ) ;
99
109
} finally {
100
110
platformRef . destroy ( ) ;
101
111
}
0 commit comments