-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
140 lines (129 loc) · 3.57 KB
/
vite.config.ts
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
133
134
135
136
137
138
139
140
import { join } from "node:path";
import vue from "@vitejs/plugin-vue";
import { defineConfig, loadEnv, type Plugin } from "vite";
import { imagetools } from "vite-imagetools";
import { metadata } from "./config/metadata.config";
import { createAnalyticsScript } from "./src/app/matomo-analytics";
const env = loadEnv(process.env.NODE_ENV!, process.cwd());
const canonicalUrl = env.VITE_APP_BASE_URL;
const title = [metadata.shortTitle, metadata.title].join(" - ");
/**
* Plugin to add `<title>` and `<meta>` to `index.html`.
*/
function meta(): Plugin {
return {
name: "meta",
transformIndexHtml(html) {
return {
html: html.replace(/<html lang="(.*?)">/, `<html lang="${metadata.locale}">`),
tags: [
{ tag: "title", children: title, injectTo: "head" },
{
tag: "link",
attrs: { href: "/favicon.ico", rel: "icon", sizes: "any" },
injectTo: "head",
},
{
tag: "link",
attrs: { href: "/apple-touch-icon.png", rel: "apple-touch-icon" },
injectTo: "head",
},
{ tag: "link", attrs: { href: "/app.webmanifest", rel: "manifest" }, injectTo: "head" },
{
tag: "meta",
attrs: { name: "description", content: metadata.description },
injectTo: "head",
},
{ tag: "link", attrs: { rel: "canonical", href: canonicalUrl }, injectTo: "head" },
{ tag: "meta", attrs: { property: "og:type", content: "website" }, injectTo: "head" },
{ tag: "meta", attrs: { property: "og:url", content: canonicalUrl }, injectTo: "head" },
{ tag: "meta", attrs: { property: "og:title", content: title }, injectTo: "head" },
{
tag: "meta",
attrs: { property: "og:description", content: metadata.description },
injectTo: "head",
},
{
tag: "meta",
attrs: { property: "og:image", content: "/image.webp" },
injectTo: "head",
},
{
tag: "meta",
attrs: { property: "og:locale", content: metadata.locale },
injectTo: "head",
},
],
};
},
};
}
/**
* Add preload `<link>` for database chunk.
*/
function preloadDatabase(): Plugin {
return {
name: "preloadDatabase",
transformIndexHtml(html, ctx) {
/** Only add in production build. */
if (ctx.bundle == null) return;
const key = Object.keys(ctx.bundle).find((key) => {
return key.startsWith("assets/db.");
});
if (key == null) return undefined;
return [{ tag: "link", attrs: { rel: "modulepreload", href: "/" + key }, injectTo: "head" }];
},
};
}
/**
* Add Matomo analytics script.
*/
function matomoAnalytics(): Plugin | undefined {
const baseUrl = env.VITE_APP_MATOMO_BASE_URL;
const id = env.VITE_APP_MATOMO_ID;
if (baseUrl == null || id == null) return;
return {
name: "matomoAnalytics",
transformIndexHtml(html, ctx) {
/** Only add in production build. */
if (ctx.bundle == null) return;
return [
{
tag: "script",
attrs: { defer: true },
children: createAnalyticsScript(baseUrl, id),
injectTo: "body",
},
];
},
};
}
export default defineConfig({
build: {
rollupOptions: {
output: {
manualChunks(id) {
/** Could probably be further optimized by putting all idea.quotes in a separete file and chunk. */
if (id.includes("/src/db/")) {
return "db";
}
return undefined;
},
},
},
},
define: {
__VUE_OPTIONS_API__: false,
},
plugins: [vue(), imagetools(), meta(), preloadDatabase(), matomoAnalytics()],
resolve: {
/** Consider using `vite-tsconfig-paths` plugin. */
alias: {
"@": join(process.cwd(), "src"),
"~": process.cwd(),
},
},
server: {
port: 3000,
},
});