Skip to content

Commit f9fdd09

Browse files
committed
refactor(@angular-devkit/build-angular): use ɵloadChildren helper from router package
This commit updates the routes extractor to use the newly exported private `ɵloadChildren` method from the router to executes a `route.loadChildren` callback and return an array of child routes. See: angular/angular#51818
1 parent e308747 commit f9fdd09

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts

-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ export function createServerCodeBundleOptions(
110110
);
111111

112112
const mainServerNamespace = 'angular:main-server';
113-
const routeExtractorNamespace = 'angular:prerender-route-extractor';
114113
const ssrEntryNamespace = 'angular:ssr-entry';
115114

116115
const entryPoints: Record<string, string> = {

packages/angular_devkit/build_angular/src/utils/routes-extractor/extractor.ts

+30-20
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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';
1017
import {
1118
INITIAL_CONFIG,
1219
ɵINTERNAL_SERVER_PLATFORM_PROVIDERS as INTERNAL_SERVER_PLATFORM_PROVIDERS,
1320
} from '@angular/platform-server';
14-
import { Route, Router, ɵROUTER_PROVIDERS } from '@angular/router';
21+
import { Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
1522
import { first } from 'rxjs/operators'; // Import from `/operators` to support rxjs 6 which is still supported by the Framework.
1623

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-
2224
interface RouterResult {
2325
route: string;
2426
success: boolean;
@@ -27,17 +29,17 @@ interface RouterResult {
2729

2830
async function* getRoutesFromRouterConfig(
2931
routes: Route[],
30-
routerConfigLoader: RouterConfigLoader,
31-
injector: Injector,
32-
parent = '',
32+
compiler: Compiler,
33+
parentInjector: Injector,
34+
parentRoute = '',
3335
): AsyncIterableIterator<RouterResult> {
3436
for (const route of routes) {
3537
const { path, redirectTo, loadChildren, children } = route;
3638
if (path === undefined) {
3739
continue;
3840
}
3941

40-
const currentRoutePath = buildRoutePath(parent, path);
42+
const currentRoutePath = buildRoutePath(parentRoute, path);
4143

4244
if (redirectTo !== undefined) {
4345
// TODO: handle `redirectTo`.
@@ -53,13 +55,21 @@ async function* getRoutesFromRouterConfig(
5355

5456
yield { route: currentRoutePath, success: true, redirect: false };
5557

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+
}
6373
}
6474
}
6575
}
@@ -92,10 +102,10 @@ export async function* extractRoutes(
92102

93103
const injector = applicationRef.injector;
94104
const router = injector.get(Router);
95-
const routerConfigLoader = injector.get(RouterConfigLoader);
105+
const compiler = injector.get(Compiler);
96106

97107
// Extract all the routes from the config.
98-
yield* getRoutesFromRouterConfig(router.config, routerConfigLoader, injector);
108+
yield* getRoutesFromRouterConfig(router.config, compiler, injector);
99109
} finally {
100110
platformRef.destroy();
101111
}

0 commit comments

Comments
 (0)