This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
117 lines (112 loc) · 4.06 KB
/
vite.config.ts
File metadata and controls
117 lines (112 loc) · 4.06 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
import { fileURLToPath } from 'url';
import { defineConfig, loadEnv, UserConfig } from 'vite';
import minifyHtml from 'vite-plugin-html-minifier';
import removeConsole from 'vite-plugin-remove-console';
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const config: UserConfig = {
root: 'src',
base: './',
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
plugins: [
minifyHtml(),
mode === 'production'
? removeConsole({
includes: ['log', 'debug', 'info', 'warn', 'error', 'group', 'groupEnd'],
})
: null,
{
name: 'version-file',
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url === '/version.txt') {
res.setHeader('Content-Type', 'text/plain');
res.end('[Dev]');
return;
}
next();
});
},
generateBundle() {
this.emitFile({
type: 'asset',
fileName: 'version.txt',
source: env.VITE_APP_VERSION ?? '[Unknown]',
});
},
},
{
name: 'ts-extension',
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url && /^(?!\/@fs\/).+\.[tj]s(\?.*)?$/.test(req.url)) {
try {
const result = await server.transformRequest(
req.url.replace(/\.js(\?.*?)?$/, '.ts$1'),
);
if (result) {
res.setHeader('Content-Type', 'application/javascript');
res.setHeader('Access-Control-Allow-Origin', '*');
res.end(result.code);
return;
}
} catch (e) {
console.error(e);
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end(e instanceof Error ? e.message : String(e));
return;
}
}
next();
});
},
},
],
build: {
outDir: '../dist',
emptyOutDir: true,
rollupOptions: {
input: {
main: 'src/main.ts',
tracker: 'src/tracker.html',
},
output: {
entryFileNames: (chunkInfo) => {
if (chunkInfo.name === 'main') {
return 'main.js';
}
return 'assets/[name]-[hash].js';
},
},
},
},
server: {
open: '/dev.html',
host: 'localhost',
port: 5174,
strictPort: true,
},
};
config.define = {};
for (const [key, value] of Object.entries(env)) {
if (!key.startsWith('VITE_')) continue;
config.define[`import.meta.env.${key}`] = JSON.stringify(value);
}
try {
const apiHost = new URL(env.VITE_API_HOST);
if (config.server) {
config.server.proxy = {
[apiHost.pathname]: {
target: apiHost.origin,
changeOrigin: true,
},
};
}
} catch {}
return config;
});