-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
72 lines (58 loc) · 2.32 KB
/
vite.config.ts
File metadata and controls
72 lines (58 loc) · 2.32 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
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import fs from "fs";
const extractFromResume = (content: string, key: string) => {
const regex = new RegExp(`${key}:\\s*["'\`](.*?)["'\`]`);
const match = content.match(regex);
return match ? match[1] : null;
};
const getIndefiniteArticle = (word: string) => {
const w = word.trim().toLowerCase();
if (/^u(ni|ser|x|i)/.test(w)) return 'a';
if (/^h(our|onest|onor)/.test(w)) return 'an';
if (/^[FHLMNRSX][A-Z0-9]*$/.test(word) && !/^[AEIOU]/.test(w)) return 'an';
return /^[aeiou]/.test(w) ? 'an' : 'a';
};
export default defineConfig(({ mode }) => {
const resumePath = path.resolve(__dirname, "./src/data/resume.tsx");
const resumeContent = fs.existsSync(resumePath) ? fs.readFileSync(resumePath, "utf-8") : "";
const name = extractFromResume(resumeContent, "name") || "Portfolio";
const role = extractFromResume(resumeContent, "role") || "Web Developer";
const article = getIndefiniteArticle(role);
const metaDescription = `Professional portfolio of ${name}, ${article} ${role}. View my projects, skills, and contact information.`;
return {
server: {
host: "::",
port: 8080,
},
plugins: [
react(),
{
name: "html-meta-injector",
transformIndexHtml(html: string) {
let newHtml = html.replace(
/<title>(.*?)<\/title>/,
`<title>${name} - ${role}</title>`
);
const metaTag = `<meta name="description" content="${metaDescription}" />`;
if (newHtml.includes('<meta name="description"')) {
newHtml = newHtml.replace(
/<meta name="description" content=".*?" \/>/,
metaTag
);
} else {
newHtml = newHtml.replace("</head>", ` ${metaTag}\n </head>`);
}
return newHtml;
},
},
].filter(Boolean),
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
};
});