-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
45 lines (42 loc) · 1.15 KB
/
Copy pathvite.config.ts
File metadata and controls
45 lines (42 loc) · 1.15 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
import react from "@vitejs/plugin-react-swc";
import path from "path";
import { defineConfig } from "vite";
const repo = process.env.GITHUB_REPOSITORY?.split("/")[1];
const base =
process.env.GITHUB_ACTIONS === "true" && repo
? repo.endsWith(".github.io")
? "/"
: `/${repo}/`
: "/";
const gaMeasurementId = process.env.VITE_GA_MEASUREMENT_ID?.trim();
export default defineConfig(() => ({
base,
server: {
host: "::",
port: 8080,
},
plugins: [
react(),
{
name: "inject-google-analytics",
transformIndexHtml(html) {
if (!gaMeasurementId) return html;
const snippet = `
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=${gaMeasurementId}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "${gaMeasurementId}", { send_page_view: false });
</script>`;
return html.replace("</head>", `${snippet}\n </head>`);
},
},
],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
}));