-
Notifications
You must be signed in to change notification settings - Fork 7
/
vite.config.ts
133 lines (129 loc) · 3.55 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
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({
content: {
highlighter: 'shiki',
shikiOptions: {
highlight: {
theme: 'github-dark',
},
highlighter: {
langs: [
'json',
'ts',
'tsx',
'js',
'jsx',
'html',
'css',
'angular-html',
'angular-ts',
'bash',
'yaml',
'asciidoc',
'mermaid',
],
themes: ['github-dark', 'github-light'],
},
},
},
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-setup.ts'],
include: ['**/*.spec.ts'],
reporters: ['default'],
},
define: {
'import.meta.vitest': mode !== 'production',
},
}));