-
Notifications
You must be signed in to change notification settings - Fork 18
/
vite.config.ts
43 lines (41 loc) · 1.38 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
import react from '@vitejs/plugin-react';
import normalizeUrl from "normalize-url";
import { defineConfig } from 'vite';
const QUOTES_UNWRAP_REGEX: RegExp = new RegExp(/^"(.*)"$/);
export const generateBaseURL = (userUrl: string | undefined): URL => {
let cleanUserUrl = userUrl.trim();
if(QUOTES_UNWRAP_REGEX.test(cleanUserUrl)) {
const results = cleanUserUrl.match(QUOTES_UNWRAP_REGEX);
cleanUserUrl = results[1];
}
const base = normalizeUrl(cleanUserUrl, {removeSingleSlash: true});
const u = new URL(base);
if(u.port === '') {
if(u.protocol === 'https:') {
u.port = '443';
} else if(userUrl.includes(`${u.hostname}:80`)) {
u.port = '80';
}
}
return u;
}
export default defineConfig(() => {
let baseUrlStr = '/';
if(process.env.BASE_URL !== undefined && process.env.BASE_URL !== '') {
const baseUrl = generateBaseURL(process.env.BASE_URL);
if(baseUrl.pathname !== '/') {
baseUrlStr = baseUrl.toString();
}
}
console.debug(`[VITE] BASE_URL ENV: ${process.env.BASE_URL} | Base Url String: ${baseUrlStr}`);
return {
base: baseUrlStr,
plugins: [react()],
build: {
sourcemap: true
},
define: {
"__USE_HASH_ROUTER__": JSON.stringify((process.env.USE_HASH_ROUTER ?? false))
}
};
});