From f9fdd0907c7e2e6c7ce3c7582a74317f1c090780 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 20 Sep 2023 09:42:46 +0000 Subject: [PATCH] =?UTF-8?q?refactor(@angular-devkit/build-angular):=20use?= =?UTF-8?q?=20`=C9=B5loadChildren`=20helper=20from=20router=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/angular/angular/pull/51818 --- .../tools/esbuild/application-code-bundle.ts | 1 - .../src/utils/routes-extractor/extractor.ts | 50 +++++++++++-------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts index aca2a64f65ee..8b2e80afdac3 100644 --- a/packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts @@ -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 = { diff --git a/packages/angular_devkit/build_angular/src/utils/routes-extractor/extractor.ts b/packages/angular_devkit/build_angular/src/utils/routes-extractor/extractor.ts index c0fce54544f2..20033b7c6399 100644 --- a/packages/angular_devkit/build_angular/src/utils/routes-extractor/extractor.ts +++ b/packages/angular_devkit/build_angular/src/utils/routes-extractor/extractor.ts @@ -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; @@ -27,9 +29,9 @@ interface RouterResult { async function* getRoutesFromRouterConfig( routes: Route[], - routerConfigLoader: RouterConfigLoader, - injector: Injector, - parent = '', + compiler: Compiler, + parentInjector: Injector, + parentRoute = '', ): AsyncIterableIterator { for (const route of routes) { const { path, redirectTo, loadChildren, children } = route; @@ -37,7 +39,7 @@ async function* getRoutesFromRouterConfig( continue; } - const currentRoutePath = buildRoutePath(parent, path); + const currentRoutePath = buildRoutePath(parentRoute, path); if (redirectTo !== undefined) { // TODO: handle `redirectTo`. @@ -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); + } } } } @@ -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(); }