-
Notifications
You must be signed in to change notification settings - Fork 7
/
vite.config.ts
107 lines (103 loc) · 2.97 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
import analog, { PrerenderContentFile } from '@analogjs/platform';
import angular from '@analogjs/vite-plugin-angular';
import { PostAttributes } from 'src/app/types';
/// <reference types="vitest" />
import { defineConfig } from 'vite';
function transFormContentDirRoute(file: PrerenderContentFile, base: string) {
const attributes = file.attributes as PostAttributes;
// do not include files marked as draft in frontmatter
if (attributes.draft) {
return false;
}
// use the slug from frontmatter if defined, otherwise use the files basename
const slug = attributes.slug || file.name;
return `/${base}/${slug}`;
}
function injectGtagScript(html: string) {
// Define the position to inject the script
const headEndTag = '</head>';
const insertPosition = html.indexOf(headEndTag);
// If </head> tag is found, insert the script right after it
if (insertPosition !== -1) {
return (
html.slice(0, insertPosition + headEndTag.length) +
`<script async src="https://www.googletagmanager.com/gtag/js?id=G-HY3CPEEH1Z"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-1');
</script>` +
html.slice(insertPosition + headEndTag.length)
);
} else {
// If </head> tag is not found, return the original HTML
return html;
}
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
publicDir: 'src/assets',
build: {
target: ['es2020'],
},
resolve: {
mainFields: ['module'],
},
plugins: [
angular({
inlineStylesExtension: 'scss',
}),
analog({
prerender: {
routes: [
'/api/rss.xml',
'/',
'/blog',
'/contact',
'/imprint',
'/projects',
'/recruitment',
{
contentDir: '/src/content/blog',
transform: (file: PrerenderContentFile) =>
transFormContentDirRoute(file, 'blog'),
},
{
contentDir: '/src/content/projects',
transform: (file: PrerenderContentFile) =>
transFormContentDirRoute(file, 'projects'),
},
{
contentDir: '/src/content/talks',
transform: (file: PrerenderContentFile) =>
transFormContentDirRoute(file, 'talks'),
},
],
postRenderingHooks: [
async (route) => {
if (route.route.endsWith('.xml')) {
return;
}
if (route.contents) {
route.contents = injectGtagScript(route.contents);
}
},
],
sitemap: {
host: 'https://k9n.dev/',
},
},
}),
],
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['src/test.ts'],
include: ['**/*.spec.ts'],
reporters: ['default'],
},
define: {
'import.meta.vitest': mode !== 'production',
},
}));