-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
68 lines (60 loc) · 2.22 KB
/
Copy pathvite.config.ts
File metadata and controls
68 lines (60 loc) · 2.22 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
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import react from "@vitejs/plugin-react";
import { defineConfig, loadEnv, type Plugin } from "vite";
import { getSitemapStaticPaths } from "./src/lib/sitemapPaths";
const dirname = path.dirname(fileURLToPath(import.meta.url));
function escapeXml(s: string) {
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
function seoArtifactsPlugin(siteUrl: string): Plugin {
return {
name: "seo-artifacts",
closeBundle() {
const outDir = path.resolve(dirname, "dist");
fs.mkdirSync(outDir, { recursive: true });
const base = siteUrl.trim().replace(/\/$/, "");
const robotsLines = ["User-agent: *", "Allow: /", ""];
if (base && /^https?:\/\//i.test(base)) {
robotsLines.push(`Sitemap: ${base}/sitemap.xml`, "");
const paths = getSitemapStaticPaths();
const lastmod = new Date().toISOString().slice(0, 10);
const body = paths
.map((p) => {
const loc = `${base}${p}`;
const pri =
p === "/"
? "1.0"
: p === "/learn"
? "0.95"
: p === "/blog"
? "0.85"
: p.startsWith("/learn/")
? "0.75"
: "0.7";
return `<url><loc>${escapeXml(loc)}</loc><lastmod>${lastmod}</lastmod><changefreq>weekly</changefreq><priority>${pri}</priority></url>`;
})
.join("\n");
const xml = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${body}\n</urlset>\n`;
fs.writeFileSync(path.join(outDir, "sitemap.xml"), xml, "utf8");
}
fs.writeFileSync(path.join(outDir, "robots.txt"), robotsLines.join("\n"), "utf8");
},
};
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, dirname, "");
const siteUrl = env.VITE_SITE_URL ?? "";
return {
plugins: [react(), seoArtifactsPlugin(siteUrl)],
optimizeDeps: {
exclude: ["pyodide"],
},
resolve: {
alias: {
"@": path.resolve(dirname, "./src"),
},
},
};
});