Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
66 lines (58 loc) · 1.69 KB
/
Copy pathvite.config.ts
File metadata and controls
66 lines (58 loc) · 1.69 KB
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
import tailwindcss from '@tailwindcss/vite';
import { sveltekit } from '@sveltejs/kit/vite';
import type { Plugin } from 'vite';
import { loadEnv } from 'vite';
import { defineConfig } from 'vitest/config';
const DEVELOPMENT_SITE_URL = 'http://localhost:5173';
const TEST_SITE_URL = 'https://profilarr.com';
function resolveSiteUrl(mode: string): { configured: boolean; value: string } {
const environment = loadEnv(mode, process.cwd(), 'PUBLIC_');
const configuredValue = process.env.PUBLIC_SITE_URL ?? environment.PUBLIC_SITE_URL;
const value =
configuredValue ||
(mode === 'development'
? DEVELOPMENT_SITE_URL
: mode === 'test'
? TEST_SITE_URL
: TEST_SITE_URL);
let url: URL;
try {
url = new URL(value);
} catch {
throw new Error('PUBLIC_SITE_URL must be a valid absolute URL.');
}
if (
(url.protocol !== 'http:' && url.protocol !== 'https:') ||
url.username ||
url.password ||
url.pathname !== '/' ||
url.search ||
url.hash
) {
throw new Error('PUBLIC_SITE_URL must be an HTTP or HTTPS origin without a path or query.');
}
return { configured: Boolean(configuredValue), value: url.origin };
}
function requireProductionSiteUrl(configured: boolean): Plugin {
return {
name: 'require-production-site-url',
apply: 'build',
buildStart() {
if (!configured) {
throw new Error('PUBLIC_SITE_URL is required for production builds.');
}
}
};
}
export default defineConfig(({ mode }) => {
const siteUrl = resolveSiteUrl(mode);
return {
define: {
'import.meta.env.PUBLIC_SITE_URL': JSON.stringify(siteUrl.value)
},
plugins: [requireProductionSiteUrl(siteUrl.configured), tailwindcss(), sveltekit()],
test: {
include: ['tests/**/*.test.ts']
}
};
});