Skip to content

Commit

Permalink
refactor(@angular-devkit/build-angular): use ɵloadChildren helper f…
Browse files Browse the repository at this point in the history
…rom 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
  • Loading branch information
alan-agius4 committed Sep 21, 2023
1 parent e308747 commit f9fdd09
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export function createServerCodeBundleOptions(
);

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

const entryPoints: Record<string, string> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
* found in the LICENSE file at https://angular.io/license
*/

import { ApplicationRef, Injector, Type, createPlatformFactory, platformCore } from '@angular/core';
import {
ApplicationRef,
Compiler,
Injector,
Type,
createPlatformFactory,
platformCore,
} from '@angular/core';
import {
INITIAL_CONFIG,
ɵINTERNAL_SERVER_PLATFORM_PROVIDERS as INTERNAL_SERVER_PLATFORM_PROVIDERS,
} from '@angular/platform-server';
import { Route, Router, ɵROUTER_PROVIDERS } from '@angular/router';
import { Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
import { first } from 'rxjs/operators'; // Import from `/operators` to support rxjs 6 which is still supported by the Framework.

// TODO(alanagius): replace the below once `RouterConfigLoader` is privately exported from `@angular/router`.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const RouterConfigLoader = ɵROUTER_PROVIDERS[5] as any;
type RouterConfigLoader = typeof RouterConfigLoader;

interface RouterResult {
route: string;
success: boolean;
Expand All @@ -27,17 +29,17 @@ interface RouterResult {

async function* getRoutesFromRouterConfig(
routes: Route[],
routerConfigLoader: RouterConfigLoader,
injector: Injector,
parent = '',
compiler: Compiler,
parentInjector: Injector,
parentRoute = '',
): AsyncIterableIterator<RouterResult> {
for (const route of routes) {
const { path, redirectTo, loadChildren, children } = route;
if (path === undefined) {
continue;
}

const currentRoutePath = buildRoutePath(parent, path);
const currentRoutePath = buildRoutePath(parentRoute, path);

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

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

if (children?.length || loadChildren) {
yield* getRoutesFromRouterConfig(
children ?? (await routerConfigLoader.loadChildren(injector, route).toPromise()).routes,
routerConfigLoader,
injector,
currentRoutePath,
);
if (children?.length) {
yield* getRoutesFromRouterConfig(children, compiler, parentInjector, currentRoutePath);
}

if (loadChildren) {
const loadedChildRoutes = await loadChildrenHelper(
route,
compiler,
parentInjector,
).toPromise();

if (loadedChildRoutes) {
const { routes: childRoutes, injector = parentInjector } = loadedChildRoutes;
yield* getRoutesFromRouterConfig(childRoutes, compiler, injector, currentRoutePath);
}
}
}
}
Expand Down Expand Up @@ -92,10 +102,10 @@ export async function* extractRoutes(

const injector = applicationRef.injector;
const router = injector.get(Router);
const routerConfigLoader = injector.get(RouterConfigLoader);
const compiler = injector.get(Compiler);

// Extract all the routes from the config.
yield* getRoutesFromRouterConfig(router.config, routerConfigLoader, injector);
yield* getRoutesFromRouterConfig(router.config, compiler, injector);
} finally {
platformRef.destroy();
}
Expand Down

0 comments on commit f9fdd09

Please sign in to comment.