This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
68 lines (51 loc) · 1.88 KB
/
index.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
import _splitbee from '@splitbee/web';
import type { Plugin, ResolvedConfig } from 'vite';
export interface SplitbeeOptions {
/// Provide a custom API url to use instead of the default Splitbee one (Optional) [Default: 'https://hive.splitbee.io']
api_url?: string;
/// When using A/B testing, you can prevent the initial content flash by making the script blocking/synchronous (Optional) [Default: false]
async?: boolean;
/// Whether to include Splitbee analytics during local development (Optional) [Default: false]
dev?: boolean;
/// Whether to enable cookie-less mode (Optional) [Default: false]
disable_cookie?: boolean;
/// To use Splitbee on another subdomain you can provide a project token. This can be found in project settings (Optional)
token?: string;
/// URL to Splitbee script to load (Optional) [Default: 'https://cdn.splitbee.io/sb.js']
url?: string;
};
export const API_URL = 'https://hive.splitbee.io';
export const SCRIPT_URL = 'https://cdn.splitbee.io/sb.js';
export const Splitbee = (options: SplitbeeOptions = {}): Plugin => {
const {
api_url = API_URL,
async = true,
dev = false,
disable_cookie = false,
url = SCRIPT_URL,
} = options;
let viteConfig: ResolvedConfig | undefined;
return {
name: 'vite-plugin-splitbee',
enforce: 'post',
configResolved(config) {
viteConfig = config;
},
transformIndexHtml(html: string) {
if (!viteConfig?.isProduction && !dev) return html;
const script = document.createElement('script');
script.src = url;
script.async = async;
if (api_url) script.dataset.api = api_url;
if (disable_cookie) script.dataset.noCookie = '1';
if (options.token) script.dataset.token = options.token;
// Inject the Splitbee script at the end of the `<head>`
return html.replace(
'</head>',
`${script.outerHTML}</head>`,
);
},
}
};
export const splitbee = _splitbee;
export default Splitbee;