-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
66 lines (57 loc) · 1.64 KB
/
vite.config.ts
File metadata and controls
66 lines (57 loc) · 1.64 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 * as fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, URL } from 'node:url'
import { defineConfig, PluginOption } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import svgLoader from 'vite-svg-loader'
const current = fileURLToPath(import.meta.url);
const root = path.dirname(current);
function extractEnvVar(str: string): string[] {
return str.split('${').map(v => v.split('}')[0]).slice(1)
}
const RuntimeEnvPlugin: PluginOption = {
name: 'runtime-env-plugin',
apply: 'serve',
enforce: 'pre',
configureServer(server) {
return () => {
server.middlewares.use((req, res, next) => {
if (req.originalUrl != '/env.js') return next();
const configContent = fs.readFileSync(
path.resolve(root, './public/env.template.js'),
'utf-8',
);
let content = configContent;
extractEnvVar(configContent).forEach((v) => {
if (process.env?.[v] && process.env?.[v]?.length) {
content = content.replace('${' + v + '}', process.env[v]);
} else {
content = content.replace('${' + v + '}', '');
}
});
res.setHeader('Content-Type', 'application/javascript');
res.end(content);
});
};
},
};
// https://vitejs.dev/config/
export default defineConfig({
plugins: [RuntimeEnvPlugin, vue(), vueDevTools(), svgLoader()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
css: {
preprocessorOptions: {
scss: {
quietDeps: true
}
}
},
server: {
port: 4000
}
})