forked from OpenShock/Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
117 lines (94 loc) · 3.75 KB
/
vite.config.ts
File metadata and controls
117 lines (94 loc) · 3.75 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/// <reference types="vitest/config" />
import { sveltekit } from '@sveltejs/kit/vite';
import tailwindcss from '@tailwindcss/vite';
import dns from 'dns/promises';
import { env } from 'process';
import license from 'rollup-plugin-license';
import { type PluginOption, defineConfig, loadEnv } from 'vite';
import devtoolsJson from 'vite-plugin-devtools-json';
import mkcert from 'vite-plugin-mkcert';
function printColor(message: string, colorCode: string) {
console.log(`\u001b[1;${colorCode}m${message}\u001b[0m`);
}
const printError = (msg: string) => printColor(msg, '31');
const printInfo = (msg: string) => printColor(msg, '34');
const isTruthy = (value?: string) => value === 'true' || value === '1';
async function ensureFqdnRedirect(expectedHost: string, fqdn: string) {
// Do a DNS lookup to ensure the FQDN resolves to localhost
try {
const { address } = await dns.lookup(fqdn);
if (address === expectedHost) return;
} catch {
// Ignore errors
}
printError(`Please ensure that ${fqdn} resolves to ${expectedHost} in your hosts file\n`);
console.log('macOS / Linux:');
printInfo(` echo "${expectedHost} ${fqdn}" | sudo tee -a /etc/hosts\n`);
console.log('Windows (PowerShell as Administrator):');
printInfo(
` Add-Content -Path "C:\\Windows\\System32\\drivers\\etc\\hosts" -Value "${expectedHost} ${fqdn}"\n`
);
printError('Then restart your development server');
process.exit(1);
}
function getPlugins(useLocalRedirect: boolean): PluginOption[] {
const plugins: PluginOption = [];
if (useLocalRedirect) {
plugins.push(mkcert());
}
plugins.push(sveltekit());
plugins.push(tailwindcss());
plugins.push(devtoolsJson());
plugins.push(
license({
thirdParty: {
includePrivate: true,
includeSelf: true,
multipleVersions: true,
output: {
file: './.svelte-kit/output/client/LICENSES.txt', // TODO: This seems like a hack, check if theres a better way...
},
},
}) as PluginOption
); // TODO: Figure out why typescript thinks this is incompatible ("as PluginOption" is mandatory for svelte check to succeed)
return plugins;
}
async function getServerConfig(mode: string, useLocalRedirect: boolean) {
const vars = { ...env, ...loadEnv(mode, process.cwd(), ['PUBLIC_']) };
if (!vars.PUBLIC_SITE_URL) {
printError('PUBLIC_SITE_URL must be set in your environment');
process.exit(1);
}
if (!useLocalRedirect) return undefined;
const domain = new URL(vars.PUBLIC_SITE_URL).hostname;
if (domain === 'localhost') {
return { host: 'localhost', port: 8080, proxy: {} };
}
let host = domain;
if (!domain.startsWith('local.')) {
host = `local.${domain}`;
}
// Ensure we have the host entry redirecting to localhost
await ensureFqdnRedirect('127.0.0.1', host);
return { host, port: 443, proxy: {} };
}
export default defineConfig(async ({ command, mode, isPreview }) => {
const isLocalServe = command === 'serve' || isPreview === true;
const isProduction = mode === 'production' && (isTruthy(env.DOCKER) || isTruthy(env.CF_PAGES));
// If we are running locally, ensure that local.{PUBLIC_SITE_URL} resolves to localhost, and then use mkcert to generate a certificate
const useLocalRedirect = isLocalServe && !isProduction && !isTruthy(env.CI);
return defineConfig({
build: {
target: 'es2022',
},
plugins: getPlugins(useLocalRedirect),
server: await getServerConfig(mode, useLocalRedirect),
test: { include: ['src/**/*.{test,spec}.{js,ts}'] },
esbuild: {
legalComments: 'none',
banner: '/*! For licenses information, see LICENSES.txt */',
drop: mode === 'production' ? ['debugger'] : [],
pure: mode === 'production' ? ['console.log', 'console.debug', 'console.trace'] : [],
},
});
});