forked from angular/angular-cli
/
routes-extractor-worker.ts
73 lines (61 loc) · 1.95 KB
/
routes-extractor-worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @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 { workerData } from 'node:worker_threads';
import { loadEsmModule } from '../load-esm';
import type { ESMInMemoryFileLoaderWorkerData } from './esm-in-memory-loader/loader-hooks';
import { MainServerBundleExports } from './main-bundle-exports';
export interface RoutesExtractorWorkerData extends ESMInMemoryFileLoaderWorkerData {
document: string;
verbose: boolean;
}
export interface RoutersExtractorWorkerResult {
routes: string[];
warnings?: string[];
}
/**
* This is passed as workerData when setting up the worker via the `piscina` package.
*/
const { document, verbose } = workerData as RoutesExtractorWorkerData;
export default async function (): Promise<RoutersExtractorWorkerResult> {
const { default: bootstrapAppFnOrModule, extractRoutes } =
await loadEsmModule<MainServerBundleExports>('./main.server.mjs');
const skippedRedirects: string[] = [];
const skippedOthers: string[] = [];
const routes: string[] = [];
for await (const { route, success, redirect } of extractRoutes(
bootstrapAppFnOrModule,
document,
)) {
if (success) {
routes.push(route);
continue;
}
if (redirect) {
skippedRedirects.push(route);
} else {
skippedOthers.push(route);
}
}
if (!verbose) {
return { routes };
}
let warnings: string[] | undefined;
if (skippedOthers.length) {
(warnings ??= []).push(
'The following routes were skipped from prerendering because they contain routes with dynamic parameters:\n' +
skippedOthers.join('\n'),
);
}
if (skippedRedirects.length) {
(warnings ??= []).push(
'The following routes were skipped from prerendering because they contain redirects:\n',
skippedRedirects.join('\n'),
);
}
return { routes, warnings };
}