-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/build-angular): support standalone apps route di…
…scovery during prerendering This fixes an issue were routes could not be discovered automatically in a standalone application. This is a total overhaul of the route extraction process as instead of using `guess-parser` NPM package, we now use the Angular Router. This enables a number of exciting possibilities for the future which were not possible before. # How it works? The application is bootstrapped and through DI injection we get the injector and router config instance and recursively build the routes tree.
- Loading branch information
1 parent
5a204b8
commit 8f9a0d7
Showing
29 changed files
with
802 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/angular_devkit/build_angular/src/builders/prerender/routes-extractor-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import type { ApplicationRef, Type } from '@angular/core'; | ||
import assert from 'node:assert'; | ||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import { workerData } from 'node:worker_threads'; | ||
import type { extractRoutes } from '../../utils/routes-extractor/extractor'; | ||
|
||
export interface RoutesExtractorWorkerData { | ||
zonePackage: string; | ||
indexFile: string; | ||
outputPath: string; | ||
serverBundlePath: string; | ||
} | ||
|
||
interface ServerBundleExports { | ||
/** NgModule to render. */ | ||
AppServerModule?: Type<unknown>; | ||
|
||
/** Standalone application bootstrapping function. */ | ||
default?: (() => Promise<ApplicationRef>) | Type<unknown>; | ||
|
||
/** Method to extract routes from the router config. */ | ||
extractRoutes: typeof extractRoutes; | ||
} | ||
|
||
const { zonePackage, serverBundlePath, outputPath, indexFile } = | ||
workerData as RoutesExtractorWorkerData; | ||
|
||
async function extract(): Promise<string[]> { | ||
const { | ||
AppServerModule, | ||
extractRoutes, | ||
default: bootstrapAppFn, | ||
} = (await import(serverBundlePath)) as ServerBundleExports; | ||
|
||
const browserIndexInputPath = path.join(outputPath, indexFile); | ||
const document = await fs.promises.readFile(browserIndexInputPath, 'utf8'); | ||
|
||
const bootstrapAppFnOrModule = bootstrapAppFn || AppServerModule; | ||
assert( | ||
bootstrapAppFnOrModule, | ||
`Neither an AppServerModule nor a bootstrapping function was exported from: ${serverBundlePath}.`, | ||
); | ||
|
||
const routes: string[] = []; | ||
for await (const { route, success } of extractRoutes(bootstrapAppFnOrModule, document)) { | ||
if (success) { | ||
routes.push(route); | ||
} | ||
} | ||
|
||
return routes; | ||
} | ||
|
||
/** | ||
* Initializes the worker when it is first created by loading the Zone.js package | ||
* into the worker instance. | ||
* | ||
* @returns A promise resolving to the extract function of the worker. | ||
*/ | ||
async function initialize() { | ||
// Setup Zone.js | ||
await import(zonePackage); | ||
|
||
return extract; | ||
} | ||
|
||
/** | ||
* The default export will be the promise returned by the initialize function. | ||
* This is awaited by piscina prior to using the Worker. | ||
*/ | ||
export default initialize(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.