summaryrefslogtreecommitdiff
path: root/packages/integrations/vercel/src/lib/speed-insights.ts
blob: 8e36395360eb0e3cb56733eb8b8c4017f1c8865e (plain) (blame)
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
export type VercelSpeedInsightsConfig = {
	enabled: boolean;
};

export function getSpeedInsightsViteConfig(enabled?: boolean) {
	if (enabled) {
		return {
			define: exposeEnv(['VERCEL_ANALYTICS_ID']),
		};
	}

	return {};
}

/**
 * While Vercel adds the `PUBLIC_` prefix for their `VERCEL_` env vars by default, some env vars
 * like `VERCEL_ANALYTICS_ID` aren't, so handle them here so that it works correctly in runtime.
 */
export function exposeEnv(envs: string[]): Record<string, unknown> {
	const mapped: Record<string, unknown> = {};

	envs
		.filter((env) => process.env[env])
		.forEach((env) => {
			mapped[`import.meta.env.PUBLIC_${env}`] = JSON.stringify(process.env[env]);
		});

	return mapped;
}