-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
vite.config.ts
100 lines (85 loc) · 2.46 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
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
import path from 'node:path'
import fs from 'node:fs/promises'
import type { Plugin, ProxyOptions } from 'vite'
import { defineConfig, loadEnv } from 'vite'
import Vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import BaseSSL from '@vitejs/plugin-basic-ssl'
import { simpleGit } from 'simple-git'
import { openapi2ts } from './plugins'
export default defineConfig(async ({ mode }) => {
const git = simpleGit()
const ENV = loadEnv(mode, '.') as ImportMetaEnv
// eslint-disable-next-line no-console
console.log('[ENV]', ENV)
const proxy: Record<string, string | ProxyOptions> = {
[ENV.VITE_API_BASE]: {
target: ENV.VITE_API_PROXY_TARGET,
changeOrigin: true,
rewrite: path => path.replace(new RegExp(`${ENV.VITE_API_BASE}`), ''),
},
}
const plugins: Plugin[] = [
Vue({
script: {
defineModel: true,
},
}),
AutoImport({
imports: ['vue', '@vueuse/core', 'vue-router'],
dts: './types/auto-imports.d.ts',
}),
openapi2ts([
{
schemaPath: `${ENV.VITE_API_PROXY_TARGET}/v3/api-docs`,
requestImportStatement: 'import { request } from \'@/utils\'',
serversPath: path.join('./src/api'),
apiPrefix: '',
projectName: 'api',
},
]),
]
if (ENV.VITE_HTTPS) {
plugins.push(BaseSSL({
name: 'test',
}))
}
const COMMIT_BRANCH = await git.revparse(['--abbrev-ref', 'HEAD'])
const COMMIT_HASH = await git.revparse(['--short', 'HEAD'])
const packageJSON = await fs.readFile('package.json', { encoding: 'utf8' })
const VERSION = JSON.parse(packageJSON).version as string
return {
define: {
// 关闭选项式 API 支持
'__VUE_OPTIONS_API__': false,
// 在生产环境中关闭 devtools 支持
'__VUE_PROD_DEVTOOLS__': false,
// 禁用生产版本中水合不匹配的详细警告以优化
'__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': false,
'import.meta.env.APP_VERSION': `'${VERSION}'`,
'import.meta.env.APP_BRANCH': `'${COMMIT_BRANCH}'`,
'import.meta.env.APP_COMMIT_HASH': `'${COMMIT_HASH}'`,
},
server: {
host: '0.0.0.0',
port: 9000,
cors: true,
proxy,
},
preview: {
host: '0.0.0.0',
port: 13101,
cors: true,
proxy,
},
resolve: {
alias: [
{
find: /^@\//,
replacement: `${path.resolve(__dirname, 'src')}/`,
},
],
},
plugins,
}
})