@lukoweb/apitogo 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/cli.js +168 -57
- package/dist/declarations/lib/ui/Command.d.ts +1 -1
- package/dist/declarations/lib/util/MdxComponents.d.ts +1 -1
- package/package.json +2 -1
- package/src/app/main.tsx +150 -150
- package/src/config/loader.ts +245 -245
- package/src/index.ts +33 -33
- package/src/lib/authentication/components/CallbackHandler.tsx +45 -45
- package/src/lib/authentication/components/SignIn.tsx +46 -46
- package/src/lib/authentication/components/SignUp.tsx +48 -48
- package/src/lib/authentication/providers/clerk.tsx +224 -224
- package/src/lib/authentication/ui/AuthCard.tsx +7 -7
- package/src/lib/authentication/ui/EmailLinkCallbackUi.tsx +129 -125
- package/src/lib/authentication/ui/EmailLinkSentUi.tsx +105 -101
- package/src/lib/authentication/ui/EmailLinkSignInUi.tsx +100 -96
- package/src/lib/authentication/ui/EmailVerificationUi.tsx +128 -124
- package/src/lib/authentication/ui/ZudokuAuthUi.tsx +620 -616
- package/src/lib/components/AiAssistantMenuItems.tsx +102 -102
- package/src/lib/components/Autocomplete.tsx +123 -123
- package/src/lib/components/DeveloperHint.tsx +39 -35
- package/src/lib/components/Header.tsx +260 -260
- package/src/lib/components/Layout.tsx +53 -53
- package/src/lib/components/Main.tsx +51 -51
- package/src/lib/components/Mermaid.tsx +74 -70
- package/src/lib/components/MobileTopNavigation.tsx +276 -276
- package/src/lib/components/MultiSelect.tsx +86 -82
- package/src/lib/components/TopNavigation.tsx +103 -103
- package/src/lib/components/index.ts +26 -26
- package/src/lib/components/navigation/NavigationCategory.tsx +157 -157
- package/src/lib/components/navigation/NavigationFilterInput.tsx +35 -35
- package/src/lib/components/navigation/NavigationItem.tsx +181 -177
- package/src/lib/core/RouteGuard.tsx +193 -193
- package/src/lib/errors/ErrorMessage.tsx +50 -46
- package/src/lib/plugins/api-catalog/Catalog.tsx +80 -80
- package/src/lib/plugins/api-keys/CreateApiKey.tsx +128 -124
- package/src/lib/plugins/api-keys/CreateApiKeyDialog.tsx +49 -49
- package/src/lib/plugins/api-keys/SettingsApiKeys.tsx +72 -72
- package/src/lib/plugins/api-keys/settings/ApiKeyItem.tsx +342 -342
- package/src/lib/plugins/api-keys/settings/RevealApiKey.tsx +127 -127
- package/src/lib/plugins/markdown/MdxPage.tsx +285 -285
- package/src/lib/plugins/openapi/ApiHeader.tsx +96 -96
- package/src/lib/plugins/openapi/CollapsibleCode.tsx +88 -88
- package/src/lib/plugins/openapi/DownloadSchemaButton.tsx +94 -94
- package/src/lib/plugins/openapi/GeneratedExampleSidecarBox.tsx +52 -52
- package/src/lib/plugins/openapi/OperationListItem.tsx +183 -183
- package/src/lib/plugins/openapi/RequestBodySidecarBox.tsx +63 -63
- package/src/lib/plugins/openapi/ResponsesSidecarBox.tsx +120 -117
- package/src/lib/plugins/openapi/SchemaInfo.tsx +344 -344
- package/src/lib/plugins/openapi/SchemaList.tsx +113 -113
- package/src/lib/plugins/openapi/Sidecar.tsx +312 -309
- package/src/lib/plugins/openapi/SidecarExamples.tsx +177 -174
- package/src/lib/plugins/openapi/components/EnumValues.tsx +58 -58
- package/src/lib/plugins/openapi/components/ResponseContent.tsx +117 -114
- package/src/lib/plugins/openapi/playground/BodyPanel.tsx +274 -271
- package/src/lib/plugins/openapi/playground/CollapsibleHeader.tsx +51 -51
- package/src/lib/plugins/openapi/playground/ExamplesDropdown.tsx +60 -60
- package/src/lib/plugins/openapi/playground/Headers.tsx +188 -181
- package/src/lib/plugins/openapi/playground/IdentityDialog.tsx +75 -75
- package/src/lib/plugins/openapi/playground/IdentitySelector.tsx +38 -38
- package/src/lib/plugins/openapi/playground/ParamsGrid.tsx +45 -45
- package/src/lib/plugins/openapi/playground/Playground.tsx +602 -599
- package/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx +56 -56
- package/src/lib/plugins/openapi/playground/QueryParams.tsx +151 -148
- package/src/lib/plugins/openapi/playground/RequestLoginDialog.tsx +70 -70
- package/src/lib/plugins/openapi/playground/request-panel/MultipartField.tsx +91 -91
- package/src/lib/plugins/openapi/playground/result-panel/AudioPlayer.tsx +50 -50
- package/src/lib/plugins/openapi/playground/result-panel/ResponseTab.tsx +355 -355
- package/src/lib/plugins/openapi/playground/result-panel/ResultPanel.tsx +106 -102
- package/src/lib/plugins/openapi/schema/SchemaPropertyItem.tsx +189 -184
- package/src/lib/plugins/openapi/schema/SchemaView.tsx +299 -299
- package/src/lib/plugins/search-pagefind/IndexingDialog.tsx +162 -162
- package/src/lib/plugins/search-pagefind/PagefindSearch.tsx +202 -202
- package/src/lib/plugins/search-pagefind/ResultList.tsx +122 -118
- package/src/lib/ui/ActionButton.tsx +21 -21
- package/src/lib/ui/Command.tsx +191 -191
- package/src/lib/util/MdxComponents.tsx +150 -150
- package/src/vite/config.ts +234 -231
- package/src/vite/dev-server.ts +291 -291
- package/src/vite/plugin-api-keys.ts +50 -50
- package/src/vite/plugin-api.ts +320 -318
- package/src/vite/plugin-auth.ts +38 -38
- package/src/vite/plugin-component.ts +45 -39
- package/src/vite/plugin-config.ts +75 -75
- package/src/vite/plugin-custom-pages.ts +36 -36
- package/src/vite/plugin-docs.ts +202 -202
- package/src/vite/plugin-markdown-export.ts +214 -214
- package/src/vite/plugin-mdx.ts +149 -149
- package/src/vite/plugin-navigation.ts +106 -106
- package/src/vite/plugin-search.ts +47 -47
- package/src/vite/plugin.ts +42 -42
- package/src/vite/prerender/prerender.ts +233 -233
package/src/vite/plugin.ts
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import tailwindcss from "@tailwindcss/vite";
|
|
2
|
-
import react from "@vitejs/plugin-react";
|
|
3
|
-
import type { PluginOption } from "vite";
|
|
4
|
-
import { vitePluginSsrCss } from "./css/plugin.js";
|
|
5
|
-
import viteApiKeysPlugin from "./plugin-api-keys.js";
|
|
6
|
-
import viteApiPlugin from "./plugin-api.js";
|
|
7
|
-
import viteAuthPlugin from "./plugin-auth.js";
|
|
8
|
-
import viteAliasPlugin from "./plugin-component.js";
|
|
9
|
-
import { viteConfigReloadPlugin } from "./plugin-config-reload.js";
|
|
10
|
-
import viteConfigPlugin from "./plugin-config.js";
|
|
11
|
-
import viteCustomPagesPlugin from "./plugin-custom-pages.js";
|
|
12
|
-
import { viteDocMetadataPlugin } from "./plugin-doc-metadata.js";
|
|
13
|
-
import viteDocsPlugin from "./plugin-docs.js";
|
|
14
|
-
import viteMarkdownExportPlugin from "./plugin-markdown-export.js";
|
|
15
|
-
import viteMdxPlugin from "./plugin-mdx.js";
|
|
16
|
-
import { viteNavigationPlugin } from "./plugin-navigation.js";
|
|
17
|
-
import { viteSearchPlugin } from "./plugin-search.js";
|
|
18
|
-
import { viteShikiRegisterPlugin } from "./plugin-shiki-register.js";
|
|
19
|
-
import { viteThemePlugin } from "./plugin-theme.js";
|
|
20
|
-
|
|
21
|
-
export default function vitePlugin(): PluginOption {
|
|
22
|
-
return [
|
|
23
|
-
viteShikiRegisterPlugin(),
|
|
24
|
-
viteConfigReloadPlugin(),
|
|
25
|
-
viteMdxPlugin(),
|
|
26
|
-
react({ include: /\.(mdx?|jsx?|tsx?)$/ }),
|
|
27
|
-
viteConfigPlugin(),
|
|
28
|
-
viteApiKeysPlugin(),
|
|
29
|
-
viteCustomPagesPlugin(),
|
|
30
|
-
viteAuthPlugin(),
|
|
31
|
-
viteDocsPlugin(),
|
|
32
|
-
viteDocMetadataPlugin(),
|
|
33
|
-
viteNavigationPlugin(),
|
|
34
|
-
viteApiPlugin(),
|
|
35
|
-
viteSearchPlugin(),
|
|
36
|
-
viteAliasPlugin(),
|
|
37
|
-
viteMarkdownExportPlugin(),
|
|
38
|
-
vitePluginSsrCss({ entries: ["@lukoweb/apitogo/app/entry.server.tsx"] }),
|
|
39
|
-
viteThemePlugin(),
|
|
40
|
-
tailwindcss(),
|
|
41
|
-
];
|
|
42
|
-
}
|
|
1
|
+
import tailwindcss from "@tailwindcss/vite";
|
|
2
|
+
import react from "@vitejs/plugin-react";
|
|
3
|
+
import type { PluginOption } from "vite";
|
|
4
|
+
import { vitePluginSsrCss } from "./css/plugin.js";
|
|
5
|
+
import viteApiKeysPlugin from "./plugin-api-keys.js";
|
|
6
|
+
import viteApiPlugin from "./plugin-api.js";
|
|
7
|
+
import viteAuthPlugin from "./plugin-auth.js";
|
|
8
|
+
import viteAliasPlugin from "./plugin-component.js";
|
|
9
|
+
import { viteConfigReloadPlugin } from "./plugin-config-reload.js";
|
|
10
|
+
import viteConfigPlugin from "./plugin-config.js";
|
|
11
|
+
import viteCustomPagesPlugin from "./plugin-custom-pages.js";
|
|
12
|
+
import { viteDocMetadataPlugin } from "./plugin-doc-metadata.js";
|
|
13
|
+
import viteDocsPlugin from "./plugin-docs.js";
|
|
14
|
+
import viteMarkdownExportPlugin from "./plugin-markdown-export.js";
|
|
15
|
+
import viteMdxPlugin from "./plugin-mdx.js";
|
|
16
|
+
import { viteNavigationPlugin } from "./plugin-navigation.js";
|
|
17
|
+
import { viteSearchPlugin } from "./plugin-search.js";
|
|
18
|
+
import { viteShikiRegisterPlugin } from "./plugin-shiki-register.js";
|
|
19
|
+
import { viteThemePlugin } from "./plugin-theme.js";
|
|
20
|
+
|
|
21
|
+
export default function vitePlugin(): PluginOption {
|
|
22
|
+
return [
|
|
23
|
+
viteShikiRegisterPlugin(),
|
|
24
|
+
viteConfigReloadPlugin(),
|
|
25
|
+
viteMdxPlugin(),
|
|
26
|
+
react({ include: /\.(mdx?|jsx?|tsx?)$/ }),
|
|
27
|
+
viteConfigPlugin(),
|
|
28
|
+
viteApiKeysPlugin(),
|
|
29
|
+
viteCustomPagesPlugin(),
|
|
30
|
+
viteAuthPlugin(),
|
|
31
|
+
viteDocsPlugin(),
|
|
32
|
+
viteDocMetadataPlugin(),
|
|
33
|
+
viteNavigationPlugin(),
|
|
34
|
+
viteApiPlugin(),
|
|
35
|
+
viteSearchPlugin(),
|
|
36
|
+
viteAliasPlugin(),
|
|
37
|
+
viteMarkdownExportPlugin(),
|
|
38
|
+
vitePluginSsrCss({ entries: ["@lukoweb/apitogo/app/entry.server.tsx"] }),
|
|
39
|
+
viteThemePlugin(),
|
|
40
|
+
tailwindcss(),
|
|
41
|
+
];
|
|
42
|
+
}
|
|
@@ -1,233 +1,233 @@
|
|
|
1
|
-
import { readFile, rm } from "node:fs/promises";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { pathToFileURL } from "node:url";
|
|
5
|
-
import { createIndex, type PagefindIndex } from "pagefind";
|
|
6
|
-
import colors from "picocolors";
|
|
7
|
-
import PiscinaImport from "piscina";
|
|
8
|
-
import type { getRoutesByConfig } from "../../app/main.js";
|
|
9
|
-
import { logger } from "../../cli/common/logger.js";
|
|
10
|
-
import { fileExists } from "../../config/file-exists.js";
|
|
11
|
-
import { getBuildConfig } from "../../config/validators/BuildSchema.js";
|
|
12
|
-
import type { ZudokuConfig } from "../../config/validators/ZudokuConfig.js";
|
|
13
|
-
import { runPluginTransformConfig } from "../../lib/core/transform-config.js";
|
|
14
|
-
import invariant from "../../lib/util/invariant.js";
|
|
15
|
-
import { joinUrl } from "../../lib/util/joinUrl.js";
|
|
16
|
-
import type { MarkdownFileInfo } from "../plugin-markdown-export.js";
|
|
17
|
-
import { isTTY, throttle, writeLine } from "../reporter.js";
|
|
18
|
-
import { generateSitemap } from "../sitemap.js";
|
|
19
|
-
import { routesToPaths, routesToRewrites } from "./utils.js";
|
|
20
|
-
import type { StaticWorkerData, WorkerData } from "./worker.js";
|
|
21
|
-
|
|
22
|
-
const Piscina = PiscinaImport as unknown as typeof PiscinaImport.default;
|
|
23
|
-
|
|
24
|
-
export type WorkerResult = {
|
|
25
|
-
outputPath: string;
|
|
26
|
-
html: string;
|
|
27
|
-
statusCode: number;
|
|
28
|
-
redirect?: { from: string; to: string };
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export const getRedirectUrls = (
|
|
32
|
-
workerResults: WorkerResult[],
|
|
33
|
-
basePath: string | undefined,
|
|
34
|
-
): Set<string> =>
|
|
35
|
-
new Set(
|
|
36
|
-
workerResults.flatMap((r) => {
|
|
37
|
-
if (!r.redirect) return [];
|
|
38
|
-
const from = r.redirect.from;
|
|
39
|
-
return [
|
|
40
|
-
basePath && from.startsWith(basePath)
|
|
41
|
-
? from.slice(basePath.length) || "/"
|
|
42
|
-
: from,
|
|
43
|
-
];
|
|
44
|
-
}),
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
export const prerender = async ({
|
|
48
|
-
html,
|
|
49
|
-
dir,
|
|
50
|
-
basePath = "",
|
|
51
|
-
serverConfigFilename,
|
|
52
|
-
writeRedirects = true,
|
|
53
|
-
}: {
|
|
54
|
-
html: string;
|
|
55
|
-
dir: string;
|
|
56
|
-
basePath?: string;
|
|
57
|
-
serverConfigFilename: string;
|
|
58
|
-
writeRedirects: boolean;
|
|
59
|
-
}) => {
|
|
60
|
-
const distDir = path.join(dir, "dist", basePath);
|
|
61
|
-
const serverConfigPath = pathToFileURL(
|
|
62
|
-
path.join(distDir, "server", serverConfigFilename),
|
|
63
|
-
).href;
|
|
64
|
-
const entryServerPath = pathToFileURL(
|
|
65
|
-
path.join(distDir, "server/entry.server.js"),
|
|
66
|
-
).href;
|
|
67
|
-
|
|
68
|
-
const rawConfig: ZudokuConfig = await import(serverConfigPath).then(
|
|
69
|
-
(m) => m.default,
|
|
70
|
-
);
|
|
71
|
-
const config = await runPluginTransformConfig(rawConfig);
|
|
72
|
-
|
|
73
|
-
const buildConfig = await getBuildConfig();
|
|
74
|
-
const module = await import(entryServerPath);
|
|
75
|
-
const getRoutes = module.getRoutesByConfig as typeof getRoutesByConfig;
|
|
76
|
-
|
|
77
|
-
const routes = getRoutes(config);
|
|
78
|
-
const paths = routesToPaths(routes);
|
|
79
|
-
const rewrites = routesToRewrites(routes);
|
|
80
|
-
|
|
81
|
-
// Add redirect source paths so they get prerendered as redirects
|
|
82
|
-
if (config.redirects) {
|
|
83
|
-
for (const r of config.redirects) {
|
|
84
|
-
paths.push(joinUrl(r.from));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const maxThreads =
|
|
88
|
-
buildConfig?.prerender?.workers ?? Math.floor(os.cpus().length * 0.8);
|
|
89
|
-
|
|
90
|
-
const start = performance.now();
|
|
91
|
-
const LOG_INTERVAL_MS = 30_000; // Log every 30 seconds
|
|
92
|
-
let lastLogTime = start;
|
|
93
|
-
|
|
94
|
-
const writeProgress = throttle(
|
|
95
|
-
(count: number, total: number, urlPath: string) => {
|
|
96
|
-
writeLine(`prerendering (${count}/${total}) ${colors.dim(urlPath)}`);
|
|
97
|
-
},
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
if (!isTTY()) {
|
|
101
|
-
logger.info(
|
|
102
|
-
colors.dim(
|
|
103
|
-
`prerendering ${paths.length} routes using ${maxThreads} workers...`,
|
|
104
|
-
),
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
let completedCount = 0;
|
|
109
|
-
let pagefindIndex: PagefindIndex | undefined;
|
|
110
|
-
|
|
111
|
-
if (config.search?.type === "pagefind") {
|
|
112
|
-
const { index, errors } = await createIndex();
|
|
113
|
-
invariant(
|
|
114
|
-
index,
|
|
115
|
-
`Failed to create pagefind index: ${JSON.stringify(errors)}`,
|
|
116
|
-
);
|
|
117
|
-
pagefindIndex = index;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const pool = new Piscina<WorkerData, WorkerResult>({
|
|
121
|
-
filename: new URL("./worker.js", import.meta.url).href,
|
|
122
|
-
idleTimeout: 5_000,
|
|
123
|
-
maxThreads,
|
|
124
|
-
workerData: {
|
|
125
|
-
template: html,
|
|
126
|
-
distDir,
|
|
127
|
-
serverConfigPath,
|
|
128
|
-
entryServerPath,
|
|
129
|
-
writeRedirects,
|
|
130
|
-
} satisfies StaticWorkerData,
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
const workerResults = await Promise.all(
|
|
134
|
-
paths.map(async (urlPath) => {
|
|
135
|
-
const result = await pool.run({ urlPath } satisfies WorkerData);
|
|
136
|
-
|
|
137
|
-
if (result.statusCode < 400) {
|
|
138
|
-
await pagefindIndex?.addHTMLFile({
|
|
139
|
-
url: urlPath,
|
|
140
|
-
content: result.html,
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
completedCount++;
|
|
145
|
-
|
|
146
|
-
if (isTTY()) {
|
|
147
|
-
writeProgress(completedCount, paths.length, urlPath);
|
|
148
|
-
} else {
|
|
149
|
-
const now = performance.now();
|
|
150
|
-
if (now - lastLogTime >= LOG_INTERVAL_MS) {
|
|
151
|
-
logger.info(
|
|
152
|
-
colors.blue(
|
|
153
|
-
`prerendered ${completedCount}/${paths.length} routes using ${maxThreads} workers`,
|
|
154
|
-
),
|
|
155
|
-
);
|
|
156
|
-
lastLogTime = now;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return result;
|
|
160
|
-
}),
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
const pagefindWriteResult = await pagefindIndex?.writeFiles({
|
|
164
|
-
outputPath: path.join(distDir, "pagefind"),
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
const seconds = ((performance.now() - start) / 1000).toFixed(1);
|
|
168
|
-
|
|
169
|
-
const message = `✓ finished prerendering ${paths.length} routes in ${seconds} seconds using ${maxThreads} workers`;
|
|
170
|
-
|
|
171
|
-
if (isTTY()) {
|
|
172
|
-
writeLine(colors.blue(`${message}\n`));
|
|
173
|
-
} else {
|
|
174
|
-
logger.info(colors.blue(message));
|
|
175
|
-
}
|
|
176
|
-
if (pagefindWriteResult?.outputPath) {
|
|
177
|
-
logger.info(
|
|
178
|
-
colors.blue(`✓ pagefind index built: ${pagefindWriteResult.outputPath}`),
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const redirectUrls = getRedirectUrls(workerResults, config.basePath);
|
|
183
|
-
|
|
184
|
-
await generateSitemap({
|
|
185
|
-
basePath: config.basePath,
|
|
186
|
-
outputUrls: paths,
|
|
187
|
-
config: config.sitemap,
|
|
188
|
-
baseOutputDir: distDir,
|
|
189
|
-
redirectUrls,
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
// Generate llms.txt files if markdown export is enabled
|
|
193
|
-
if (config.docs) {
|
|
194
|
-
const { DocsConfigSchema } =
|
|
195
|
-
await import("../../config/validators/ZudokuConfig.js");
|
|
196
|
-
const { generateLlmsTxtFiles } = await import("../llms.js");
|
|
197
|
-
|
|
198
|
-
const docsConfig = DocsConfigSchema.parse(config.docs);
|
|
199
|
-
const llmsConfig = docsConfig.llms ?? {};
|
|
200
|
-
|
|
201
|
-
const markdownInfoPath = path.join(
|
|
202
|
-
dir,
|
|
203
|
-
"node_modules/.apitogo/markdown-info.json",
|
|
204
|
-
);
|
|
205
|
-
let markdownFileInfos: MarkdownFileInfo[] = [];
|
|
206
|
-
|
|
207
|
-
if (await fileExists(markdownInfoPath)) {
|
|
208
|
-
const markdownInfoContent = await readFile(markdownInfoPath, "utf-8");
|
|
209
|
-
markdownFileInfos = JSON.parse(markdownInfoContent);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (llmsConfig.llmsTxt || llmsConfig.llmsTxtFull) {
|
|
213
|
-
await generateLlmsTxtFiles({
|
|
214
|
-
markdownFileInfos,
|
|
215
|
-
basePath: config.basePath,
|
|
216
|
-
outputUrls: paths,
|
|
217
|
-
baseOutputDir: distDir,
|
|
218
|
-
siteName: config.site?.title,
|
|
219
|
-
llmsTxt: llmsConfig.llmsTxt,
|
|
220
|
-
llmsTxtFull: llmsConfig.llmsTxtFull,
|
|
221
|
-
redirectUrls,
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (!docsConfig.publishMarkdown) {
|
|
226
|
-
await Promise.all(
|
|
227
|
-
markdownFileInfos.map((info) => rm(info.filePath).catch(() => {})),
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return { workerResults, rewrites };
|
|
233
|
-
};
|
|
1
|
+
import { readFile, rm } from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { createIndex, type PagefindIndex } from "pagefind";
|
|
6
|
+
import colors from "picocolors";
|
|
7
|
+
import PiscinaImport from "piscina";
|
|
8
|
+
import type { getRoutesByConfig } from "../../app/main.js";
|
|
9
|
+
import { logger } from "../../cli/common/logger.js";
|
|
10
|
+
import { fileExists } from "../../config/file-exists.js";
|
|
11
|
+
import { getBuildConfig } from "../../config/validators/BuildSchema.js";
|
|
12
|
+
import type { ZudokuConfig } from "../../config/validators/ZudokuConfig.js";
|
|
13
|
+
import { runPluginTransformConfig } from "../../lib/core/transform-config.js";
|
|
14
|
+
import invariant from "../../lib/util/invariant.js";
|
|
15
|
+
import { joinUrl } from "../../lib/util/joinUrl.js";
|
|
16
|
+
import type { MarkdownFileInfo } from "../plugin-markdown-export.js";
|
|
17
|
+
import { isTTY, throttle, writeLine } from "../reporter.js";
|
|
18
|
+
import { generateSitemap } from "../sitemap.js";
|
|
19
|
+
import { routesToPaths, routesToRewrites } from "./utils.js";
|
|
20
|
+
import type { StaticWorkerData, WorkerData } from "./worker.js";
|
|
21
|
+
|
|
22
|
+
const Piscina = PiscinaImport as unknown as typeof PiscinaImport.default;
|
|
23
|
+
|
|
24
|
+
export type WorkerResult = {
|
|
25
|
+
outputPath: string;
|
|
26
|
+
html: string;
|
|
27
|
+
statusCode: number;
|
|
28
|
+
redirect?: { from: string; to: string };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const getRedirectUrls = (
|
|
32
|
+
workerResults: WorkerResult[],
|
|
33
|
+
basePath: string | undefined,
|
|
34
|
+
): Set<string> =>
|
|
35
|
+
new Set(
|
|
36
|
+
workerResults.flatMap((r) => {
|
|
37
|
+
if (!r.redirect) return [];
|
|
38
|
+
const from = r.redirect.from;
|
|
39
|
+
return [
|
|
40
|
+
basePath && from.startsWith(basePath)
|
|
41
|
+
? from.slice(basePath.length) || "/"
|
|
42
|
+
: from,
|
|
43
|
+
];
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
export const prerender = async ({
|
|
48
|
+
html,
|
|
49
|
+
dir,
|
|
50
|
+
basePath = "",
|
|
51
|
+
serverConfigFilename,
|
|
52
|
+
writeRedirects = true,
|
|
53
|
+
}: {
|
|
54
|
+
html: string;
|
|
55
|
+
dir: string;
|
|
56
|
+
basePath?: string;
|
|
57
|
+
serverConfigFilename: string;
|
|
58
|
+
writeRedirects: boolean;
|
|
59
|
+
}) => {
|
|
60
|
+
const distDir = path.join(dir, "dist", basePath);
|
|
61
|
+
const serverConfigPath = pathToFileURL(
|
|
62
|
+
path.join(distDir, "server", serverConfigFilename),
|
|
63
|
+
).href;
|
|
64
|
+
const entryServerPath = pathToFileURL(
|
|
65
|
+
path.join(distDir, "server/entry.server.js"),
|
|
66
|
+
).href;
|
|
67
|
+
|
|
68
|
+
const rawConfig: ZudokuConfig = await import(serverConfigPath).then(
|
|
69
|
+
(m) => m.default,
|
|
70
|
+
);
|
|
71
|
+
const config = await runPluginTransformConfig(rawConfig);
|
|
72
|
+
|
|
73
|
+
const buildConfig = await getBuildConfig();
|
|
74
|
+
const module = await import(entryServerPath);
|
|
75
|
+
const getRoutes = module.getRoutesByConfig as typeof getRoutesByConfig;
|
|
76
|
+
|
|
77
|
+
const routes = getRoutes(config);
|
|
78
|
+
const paths = routesToPaths(routes);
|
|
79
|
+
const rewrites = routesToRewrites(routes);
|
|
80
|
+
|
|
81
|
+
// Add redirect source paths so they get prerendered as redirects
|
|
82
|
+
if (config.redirects) {
|
|
83
|
+
for (const r of config.redirects) {
|
|
84
|
+
paths.push(joinUrl(r.from));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const maxThreads =
|
|
88
|
+
buildConfig?.prerender?.workers ?? Math.floor(os.cpus().length * 0.8);
|
|
89
|
+
|
|
90
|
+
const start = performance.now();
|
|
91
|
+
const LOG_INTERVAL_MS = 30_000; // Log every 30 seconds
|
|
92
|
+
let lastLogTime = start;
|
|
93
|
+
|
|
94
|
+
const writeProgress = throttle(
|
|
95
|
+
(count: number, total: number, urlPath: string) => {
|
|
96
|
+
writeLine(`prerendering (${count}/${total}) ${colors.dim(urlPath)}`);
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
if (!isTTY()) {
|
|
101
|
+
logger.info(
|
|
102
|
+
colors.dim(
|
|
103
|
+
`prerendering ${paths.length} routes using ${maxThreads} workers...`,
|
|
104
|
+
),
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let completedCount = 0;
|
|
109
|
+
let pagefindIndex: PagefindIndex | undefined;
|
|
110
|
+
|
|
111
|
+
if (config.search?.type === "pagefind") {
|
|
112
|
+
const { index, errors } = await createIndex();
|
|
113
|
+
invariant(
|
|
114
|
+
index,
|
|
115
|
+
`Failed to create pagefind index: ${JSON.stringify(errors)}`,
|
|
116
|
+
);
|
|
117
|
+
pagefindIndex = index;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const pool = new Piscina<WorkerData, WorkerResult>({
|
|
121
|
+
filename: new URL("./worker.js", import.meta.url).href,
|
|
122
|
+
idleTimeout: 5_000,
|
|
123
|
+
maxThreads,
|
|
124
|
+
workerData: {
|
|
125
|
+
template: html,
|
|
126
|
+
distDir,
|
|
127
|
+
serverConfigPath,
|
|
128
|
+
entryServerPath,
|
|
129
|
+
writeRedirects,
|
|
130
|
+
} satisfies StaticWorkerData,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const workerResults = await Promise.all(
|
|
134
|
+
paths.map(async (urlPath) => {
|
|
135
|
+
const result = await pool.run({ urlPath } satisfies WorkerData);
|
|
136
|
+
|
|
137
|
+
if (result.statusCode < 400) {
|
|
138
|
+
await pagefindIndex?.addHTMLFile({
|
|
139
|
+
url: urlPath,
|
|
140
|
+
content: result.html,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
completedCount++;
|
|
145
|
+
|
|
146
|
+
if (isTTY()) {
|
|
147
|
+
writeProgress(completedCount, paths.length, urlPath);
|
|
148
|
+
} else {
|
|
149
|
+
const now = performance.now();
|
|
150
|
+
if (now - lastLogTime >= LOG_INTERVAL_MS) {
|
|
151
|
+
logger.info(
|
|
152
|
+
colors.blue(
|
|
153
|
+
`prerendered ${completedCount}/${paths.length} routes using ${maxThreads} workers`,
|
|
154
|
+
),
|
|
155
|
+
);
|
|
156
|
+
lastLogTime = now;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
}),
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
const pagefindWriteResult = await pagefindIndex?.writeFiles({
|
|
164
|
+
outputPath: path.join(distDir, "pagefind"),
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const seconds = ((performance.now() - start) / 1000).toFixed(1);
|
|
168
|
+
|
|
169
|
+
const message = `✓ finished prerendering ${paths.length} routes in ${seconds} seconds using ${maxThreads} workers`;
|
|
170
|
+
|
|
171
|
+
if (isTTY()) {
|
|
172
|
+
writeLine(colors.blue(`${message}\n`));
|
|
173
|
+
} else {
|
|
174
|
+
logger.info(colors.blue(message));
|
|
175
|
+
}
|
|
176
|
+
if (pagefindWriteResult?.outputPath) {
|
|
177
|
+
logger.info(
|
|
178
|
+
colors.blue(`✓ pagefind index built: ${pagefindWriteResult.outputPath}`),
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const redirectUrls = getRedirectUrls(workerResults, config.basePath);
|
|
183
|
+
|
|
184
|
+
await generateSitemap({
|
|
185
|
+
basePath: config.basePath,
|
|
186
|
+
outputUrls: paths,
|
|
187
|
+
config: config.sitemap,
|
|
188
|
+
baseOutputDir: distDir,
|
|
189
|
+
redirectUrls,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Generate llms.txt files if markdown export is enabled
|
|
193
|
+
if (config.docs) {
|
|
194
|
+
const { DocsConfigSchema } =
|
|
195
|
+
await import("../../config/validators/ZudokuConfig.js");
|
|
196
|
+
const { generateLlmsTxtFiles } = await import("../llms.js");
|
|
197
|
+
|
|
198
|
+
const docsConfig = DocsConfigSchema.parse(config.docs);
|
|
199
|
+
const llmsConfig = docsConfig.llms ?? {};
|
|
200
|
+
|
|
201
|
+
const markdownInfoPath = path.join(
|
|
202
|
+
dir,
|
|
203
|
+
"node_modules/.apitogo/markdown-info.json",
|
|
204
|
+
);
|
|
205
|
+
let markdownFileInfos: MarkdownFileInfo[] = [];
|
|
206
|
+
|
|
207
|
+
if (await fileExists(markdownInfoPath)) {
|
|
208
|
+
const markdownInfoContent = await readFile(markdownInfoPath, "utf-8");
|
|
209
|
+
markdownFileInfos = JSON.parse(markdownInfoContent);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (llmsConfig.llmsTxt || llmsConfig.llmsTxtFull) {
|
|
213
|
+
await generateLlmsTxtFiles({
|
|
214
|
+
markdownFileInfos,
|
|
215
|
+
basePath: config.basePath,
|
|
216
|
+
outputUrls: paths,
|
|
217
|
+
baseOutputDir: distDir,
|
|
218
|
+
siteName: config.site?.title,
|
|
219
|
+
llmsTxt: llmsConfig.llmsTxt,
|
|
220
|
+
llmsTxtFull: llmsConfig.llmsTxtFull,
|
|
221
|
+
redirectUrls,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (!docsConfig.publishMarkdown) {
|
|
226
|
+
await Promise.all(
|
|
227
|
+
markdownFileInfos.map((info) => rm(info.filePath).catch(() => {})),
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return { workerResults, rewrites };
|
|
233
|
+
};
|