Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
89 lines (81 loc) · 2.99 KB
/
Copy pathvite.config.js
File metadata and controls
89 lines (81 loc) · 2.99 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
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import fs from "fs";
import { componentTagger } from "lovable-tagger";
/**
* Two build-time HTML steps, in one plugin so their order is guaranteed:
*
* 1. Flatten the homepage request waterfall. The "/" route's chunks (the Index
* page, the Navbar, and their static imports) are otherwise discovered only
* after the entry bundle executes, costing a round-trip before first paint.
* Emitting modulepreload links lets the browser fetch them alongside the
* entry. Chunks reached only through a dynamic import (three.js, Firebase)
* are deliberately not preloaded.
*
* 2. Copy the finished shell to 404.html. GitHub Pages serves that file for
* unknown paths, so deep links (/technology, /contact, ...) boot the router
* instead of showing the GitHub 404 page.
*
* The chunk graph is captured in writeBundle but the HTML is rewritten in
* closeBundle: Vite writes index.html itself during the write phase, and
* editing it earlier races that write.
*/
const htmlBuildSteps = () => {
let preloadFiles = [];
return {
name: "zeliontech-html-build-steps",
apply: "build",
writeBundle(options, bundle) {
const chunks = Object.values(bundle).filter((c) => c.type === "chunk");
const byFile = new Map(chunks.map((c) => [c.fileName, c]));
// Match on moduleIds, not facadeModuleId: when Rollup merges these
// modules into a shared chunk the chunk has no single facade and
// facadeModuleId is null.
const HOME_ROOTS = /(pages[\\/]Index|components[\\/]Navbar)\.jsx$/;
const isHomeRoot = (c) => (c.moduleIds || []).some((id) => HOME_ROOTS.test(id));
const seen = new Set();
const visit = (file) => {
if (seen.has(file)) return;
seen.add(file);
const chunk = byFile.get(file);
if (chunk) chunk.imports.forEach(visit);
};
chunks.filter(isHomeRoot).forEach((c) => visit(c.fileName));
preloadFiles = [...seen];
},
closeBundle() {
const indexPath = path.resolve(__dirname, "dist/index.html");
if (!fs.existsSync(indexPath)) return;
if (preloadFiles.length) {
const html = fs.readFileSync(indexPath, "utf8");
const links = preloadFiles
.filter((f) => !html.includes(f))
.map((f) => ` <link rel="modulepreload" crossorigin href="/${f}" />`)
.join("\n");
if (links) {
fs.writeFileSync(indexPath, html.replace("</head>", `${links}\n </head>`));
}
}
fs.copyFileSync(indexPath, path.resolve(__dirname, "dist/404.html"));
},
};
};
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
server: {
host: "::",
port: 8080,
hmr: {
overlay: false,
},
},
plugins: [react(), mode === "development" && componentTagger(), htmlBuildSteps()].filter(
Boolean
),
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
}));