This repository was archived by the owner on Jun 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
170 lines (144 loc) · 5.11 KB
/
Copy pathvite.config.ts
File metadata and controls
170 lines (144 loc) · 5.11 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { readFile } from "node:fs/promises";
import path from "node:path";
import type { Plugin, ViteDevServer } from "vite";
import { defineConfig } from "vitest/config";
import { buildPresentationManifest } from "./src/content/load-topics.ts";
import { renderPresentationManifestModule } from "./src/content/presentation-manifest-module.ts";
import { copyTopicAssets, isTopicAssetFile, resolveTopicAssetPublicPath } from "./src/content/topic-assets.ts";
import { SITE_DOCUMENT_TITLE_DEFAULT } from "./src/presentation/site-brand.ts";
const repositoryName = process.env.GITHUB_REPOSITORY?.split("/")[1] ?? "knowledge-sharing";
const PRESENTATION_MANIFEST_ID = "virtual:presentation-manifest";
const RESOLVED_PRESENTATION_MANIFEST_ID = `\0${PRESENTATION_MANIFEST_ID}`;
function injectSiteDocumentTitlePlugin(): Plugin {
return {
name: "inject-site-document-title",
transformIndexHtml(html: string) {
return html.replace(/<title>[\s\S]*?<\/title>/i, `<title>${SITE_DOCUMENT_TITLE_DEFAULT}</title>`);
}
};
}
function contentTypeForAsset(filePath: string): string {
const extension = path.extname(filePath).toLowerCase();
switch (extension) {
case ".png":
return "image/png";
case ".jpg":
case ".jpeg":
return "image/jpeg";
case ".gif":
return "image/gif";
case ".svg":
return "image/svg+xml";
case ".webp":
return "image/webp";
case ".avif":
return "image/avif";
default:
return "application/octet-stream";
}
}
function markdownManifestReloadPlugin(command: "build" | "serve"): Plugin {
const repoRoot = process.cwd();
const topicsDirectory = path.join(repoRoot, "topics");
let rebuildQueue = Promise.resolve();
let buildOutputDirectory = path.join(repoRoot, "dist");
let cachedManifestModule: string | null = null;
const isTopicMarkdownFile = (filePath: string): boolean => {
const relativePath = path.relative(topicsDirectory, filePath);
return !relativePath.startsWith("..") && !path.isAbsolute(relativePath) && relativePath.endsWith(".md");
};
const refreshManifestModule = async () => {
const manifest = await buildPresentationManifest(topicsDirectory);
cachedManifestModule = renderPresentationManifestModule(manifest);
};
const invalidateManifestModule = (server: ViteDevServer) => {
const module = server.moduleGraph.getModuleById(RESOLVED_PRESENTATION_MANIFEST_ID);
if (module) {
server.moduleGraph.invalidateModule(module);
}
};
const queueRebuild = (server: ViteDevServer) => {
rebuildQueue = rebuildQueue.then(async () => {
try {
await refreshManifestModule();
invalidateManifestModule(server);
server.ws.send({ type: "full-reload" });
} catch (error) {
const details = error instanceof Error ? error.message : String(error);
server.config.logger.error(`Failed to rebuild presentation manifest from Markdown changes.\n${details}`, { error });
}
});
};
return {
name: "markdown-manifest-reload",
resolveId(id) {
if (id === PRESENTATION_MANIFEST_ID) {
return RESOLVED_PRESENTATION_MANIFEST_ID;
}
return null;
},
async load(id) {
if (id !== RESOLVED_PRESENTATION_MANIFEST_ID) {
return null;
}
if (!cachedManifestModule) {
await refreshManifestModule();
}
return cachedManifestModule;
},
configResolved(config) {
buildOutputDirectory = path.resolve(repoRoot, config.build.outDir);
},
async buildStart() {
await refreshManifestModule();
},
async closeBundle() {
if (command === "build") {
await copyTopicAssets(topicsDirectory, buildOutputDirectory);
}
},
configureServer(server) {
server.watcher.add(topicsDirectory);
server.middlewares.use(async (req, res, next) => {
const requestPath = req.url ? decodeURIComponent(req.url.split("?", 1)[0]) : "";
const assetFile = resolveTopicAssetPublicPath(requestPath, topicsDirectory);
if (!assetFile) {
next();
return;
}
try {
const assetContent = await readFile(assetFile);
res.statusCode = 200;
res.setHeader("Content-Type", contentTypeForAsset(assetFile));
if (req.method === "HEAD") {
res.end();
return;
}
res.end(assetContent);
} catch {
next();
}
});
const handleFileEvent = (filePath: string) => {
if (isTopicMarkdownFile(filePath)) {
queueRebuild(server);
return;
}
if (isTopicAssetFile(filePath, topicsDirectory)) {
server.ws.send({ type: "full-reload" });
}
};
server.watcher.on("add", handleFileEvent);
server.watcher.on("change", handleFileEvent);
server.watcher.on("unlink", handleFileEvent);
}
};
}
export default defineConfig(({ command }) => ({
base: command === "build" ? `/${repositoryName}/` : "/",
plugins: process.env.VITEST ? [] : [markdownManifestReloadPlugin(command), injectSiteDocumentTitlePlugin()],
test: {
environment: "jsdom",
globals: true
}
}));