-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
132 lines (123 loc) · 4.52 KB
/
Copy pathvite.config.ts
File metadata and controls
132 lines (123 loc) · 4.52 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
import react from "@vitejs/plugin-react";
import { defineConfig, type Plugin } from "vite";
import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { jsonDocumentSourceAliases } from "../../config/json-document-source-aliases.ts";
import siteRoutes from "./src/site-routes.json";
function rootLlmsTxt(): Plugin {
const path = fileURLToPath(new URL("../../llms.txt", import.meta.url));
return {
name: "root-llms-txt",
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url?.split("?")[0] !== "/llms.txt") return next();
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end(readFileSync(path, "utf8"));
});
},
generateBundle() {
this.emitFile({
type: "asset",
fileName: "llms.txt",
source: readFileSync(path, "utf8"),
});
},
};
}
function productionSiteAssets(): Plugin {
const siteUrl = (process.env.SITE_URL ?? "https://developer-1px.github.io/json-document").replace(/\/$/, "");
const routePages = siteRoutes;
return {
name: "production-site-assets",
writeBundle(options) {
if (options.dir) {
const indexPath = join(options.dir, "index.html");
const indexHtml = readFileSync(indexPath, "utf8");
copyFileSync(indexPath, join(options.dir, "404.html"));
for (const route of routePages) {
if (route.path === "/") continue;
const routeDir = join(options.dir, route.path.slice(1));
mkdirSync(routeDir, { recursive: true });
writeFileSync(join(routeDir, "index.html"), routeHtml(indexHtml, route));
}
}
},
generateBundle(_options, bundle) {
const index = bundle["index.html"];
if (index?.type === "asset" && typeof index.source === "string") {
this.emitFile({
type: "asset",
fileName: "404.html",
source: index.source,
});
}
this.emitFile({
type: "asset",
fileName: "robots.txt",
source: [
"User-agent: *",
"Allow: /",
"",
`Sitemap: ${siteUrl}/sitemap.xml`,
"",
].join("\n"),
});
this.emitFile({
type: "asset",
fileName: "sitemap.xml",
source: [
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
...routePages.map((route) => {
const loc = route.path === "/" ? `${siteUrl}/` : `${siteUrl}${route.path}`;
return ` <url><loc>${loc}</loc></url>`;
}),
"</urlset>",
"",
].join("\n"),
});
},
};
function routeHtml(indexHtml: string, route: { path: string; title: string; description: string }): string {
const url = `${siteUrl}${route.path}`;
return indexHtml
.replace(/<title>.*?<\/title>/, `<title>${route.title}</title>`)
.replace(/(<meta\s+name="description"\s+content=")[^"]*(")/, `$1${route.description}$2`)
.replace(/(<meta property="og:title" content=")[^"]*(" \/>)/, `$1${route.title}$2`)
.replace(/(<meta\s+property="og:description"\s+content=")[^"]*(")/, `$1${route.description}$2`)
.replace(/(<meta property="og:url" content=")[^"]*(" \/>)/, `$1${url}$2`)
.replace(/(<meta name="twitter:title" content=")[^"]*(" \/>)/, `$1${route.title}$2`)
.replace(/(<meta\s+name="twitter:description"\s+content=")[^"]*(")/, `$1${route.description}$2`)
.replace(/(<link rel="canonical" href=")[^"]*(" \/>)/, `$1${url}$2`);
}
}
export default defineConfig({
base: process.env.SITE_BASE ?? "/",
plugins: [react(), rootLlmsTxt(), productionSiteAssets()],
resolve: {
alias: jsonDocumentSourceAliases({ officialExtensions: true }),
dedupe: ["react", "react-dom"],
},
server: {
host: "127.0.0.1",
port: 5183,
strictPort: true,
watch: { usePolling: true, interval: 300 },
},
build: {
outDir: "dist",
emptyOutDir: true,
chunkSizeWarningLimit: 1100,
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes("/node_modules/react/") || id.includes("/node_modules/react-dom/")) return "react";
if (id.includes("/packages/json-document/src/")) return "@interactive-os/json-document";
if (id.includes("/apps/outliner/src/")) return "playground-outliner";
if (id.includes("/apps/mobile-cms/src/")) return "playground-mobile-cms";
},
},
},
},
});