generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathumami.plugin.ts
79 lines (72 loc) · 1.8 KB
/
umami.plugin.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
import type { Plugin, ResolvedConfig } from 'vite';
export interface UmamiPluginOptions {
/**
* The ID of the project Clarity provides to you.
*
* Can be found in the URL of your project.
*
* @example `k4vhy94oj3`
*/
websiteId?: string;
/**
* Host where the script is hosted
* @default "https://cloud.umami.is"
*/
host?: string;
/**
* Whether to inject the script in development mode.
*
* @default false
*/
enableInDevMode?: boolean;
/**
* Whether this is required when building for production
*/
requireInProduction?: boolean;
}
export function umamiPlugin(options: UmamiPluginOptions): Plugin {
let config: ResolvedConfig;
return {
name: 'umami-script',
enforce: 'pre',
configResolved(resolvedConfig) {
config = resolvedConfig;
},
transformIndexHtml() {
if (config.command === 'serve' && !options.enableInDevMode) {
return [];
}
// only fail if we are building in production mode and we need the value but it was not provided
if (
!options.websiteId &&
options.requireInProduction &&
config.mode == 'production'
) {
throw new Error(
'[umami-script] No website id provided. Please provide a website id.',
);
}
return [
{
tag: 'link',
attrs: {
rel: 'preconnect',
href: options.host,
},
},
{
tag: 'script',
attrs: {
defer: true,
// remove trailing slash from base
src: `${config.base.replace(/\/$/, '')}/script.js`,
'data-website-id': options.websiteId,
'data-host-url': options.host,
},
children: '',
injectTo: 'head',
},
];
},
};
}